EditorManager.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.editor;
00002 
00003 import java.io.FileNotFoundException;
00004 
00005 import org.eclipse.core.filesystem.IFileStore;
00006 import org.eclipse.core.resources.IMarker;
00007 import org.eclipse.core.resources.IResource;
00008 import org.eclipse.ui.IEditorInput;
00009 import org.eclipse.ui.IEditorPart;
00010 import org.eclipse.ui.IWorkbenchPage;
00011 import org.eclipse.ui.PartInitException;
00012 import org.eclipse.ui.IWorkbenchSite;
00013 import org.eclipse.ui.IWorkbenchWindow;
00014 import org.eclipse.ui.ide.FileStoreEditorInput;
00015 import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
00016 import org.eclipse.ui.editors.text.EditorsUI;
00017 
00018 import edu.rice.cs.hpc.data.experiment.Experiment;
00019 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00020 import edu.rice.cs.hpc.data.experiment.source.FileSystemSourceFile;
00021 import edu.rice.cs.hpc.viewer.util.Utilities;
00022 import edu.rice.cs.hpc.viewer.util.WindowTitle;
00023 
00029 public class EditorManager extends BaseEditorManager{
00030     private IWorkbenchWindow windowCurrent;
00031 
00032 
00037     public EditorManager(IWorkbenchWindow window) {
00038         String sLine = AbstractDecoratedTextEditorPreferenceConstants.EDITOR_LINE_NUMBER_RULER;
00039         EditorsUI.getPreferenceStore().setValue(sLine, true);
00040         this.windowCurrent = window;
00041     }
00042     
00043     public EditorManager(IWorkbenchSite site) {
00044         this(site.getWorkbenchWindow());
00045     }
00046     
00051     public void displayFileEditor(Scope scope) 
00052     throws FileNotFoundException
00053     {
00054         // get the complete file name
00055         if(Utilities.isFileReadable(scope)) {
00056             // lets get the database number being used for this file
00057             Experiment experiment = (Experiment) scope.getExperiment();
00058             
00059             String sLongName;
00060             FileSystemSourceFile newFile = ((FileSystemSourceFile)scope.getSourceFile());
00061             sLongName = newFile.getCompleteFilename();
00062             int iLine = scope.getFirstLineNumber();
00063             openFileEditor( sLongName, newFile.getName(), iLine, experiment );
00064         }
00065     }
00066     
00072     public void openFileEditor(String sFilename, Experiment experiment) 
00073     throws FileNotFoundException
00074     {
00075         java.io.File objInfo = new java.io.File(sFilename);
00076         if(objInfo.exists())
00077             this.openFileEditor(sFilename, objInfo.getName(), 1, experiment);
00078         else
00079             // Laks: 12.1.2008: return the filename in case the file is not found
00080             throw new FileNotFoundException(sFilename);
00081     }
00082     
00089     private void openFileEditor(String sLongFilename, String sFilename, int iLineNumber, Experiment experiment)
00090         throws FileNotFoundException
00091     {
00092         // get the complete path of the file
00093         org.eclipse.core.filesystem.IFileStore objFile = 
00094             org.eclipse.core.filesystem.EFS.getLocalFileSystem().getStore(new 
00095                     org.eclipse.core.runtime.Path(sLongFilename).removeLastSegments(1));
00096         // get the active page for the editor
00097         org.eclipse.ui.IWorkbenchPage wbPage = this.windowCurrent.getActivePage();
00098         if(wbPage != null ){
00099             objFile=objFile.getChild(sFilename);
00100             if(!objFile.fetchInfo().exists()) {
00101                 throw new FileNotFoundException(sFilename+": File not found.");
00102             }
00103             try {
00104                 openEditorOnFileStore(wbPage, objFile, experiment);
00105                 this.setEditorMarker(wbPage, iLineNumber);
00106             } catch (PartInitException e) {
00107                 // TODO Auto-generated catch block
00108                 e.printStackTrace();
00109             }
00110         }
00111     }
00112 
00118     private void setEditorMarker(org.eclipse.ui.IWorkbenchPage wbPage, int iLineNumber) {
00119            //IFile file;
00120            try{
00121                IResource resource = org.eclipse.core.resources.ResourcesPlugin.getWorkspace().getRoot();
00122                IMarker marker=resource.createMarker(IMarker.MARKER); 
00123                marker.setAttribute(IMarker.LINE_NUMBER, iLineNumber+1);
00124                IEditorPart editor = wbPage.getActiveEditor();
00125                if (editor != null)
00126                    org.eclipse.ui.ide.IDE.gotoMarker(wbPage.getActiveEditor(), marker);
00127                
00128            } catch (org.eclipse.core.runtime.CoreException e) {
00129                e.printStackTrace();
00130            }
00131 
00132     }
00133 
00134     //-------========================= TAKEN FROM IDE ===============
00143     private static IEditorPart openEditorOnFileStore(IWorkbenchPage page, IFileStore fileStore, Experiment experiment) throws PartInitException {
00144 
00145         boolean needNewPartition = BaseEditorManager.splitBegin(page, experiment);
00146         
00147         IEditorInput input = getEditorInput(fileStore);
00148         //String editorId = getEditorId(fileStore);
00149         // forbid eclipse to use an external editor
00150         String editorId = edu.rice.cs.hpc.viewer.editor.SourceCodeEditor.ID;
00151         // open the editor on the file
00152         IEditorPart iep = page.openEditor(input, editorId);
00153         // if we want a database number prefix, add it to the editor title
00154         if (iep instanceof SourceCodeEditor) {
00155             SourceCodeEditor sce = (SourceCodeEditor)iep;
00156             sce.setExperiment(experiment);
00157             WindowTitle wt = new WindowTitle();
00158             wt.setEditorTitle(page.getWorkbenchWindow(), iep); //, experiment, sce.getEditorPartName());
00159 
00160                 // database numbers start with 0 but titles start with 1
00161                 //sce.setPartNamePrefix((dbNum+1) + "-");
00162         }
00163 
00164         BaseEditorManager.splitEnd(needNewPartition, iep);
00165         
00166         return iep;
00167     }
00168 
00169 
00180     private static IEditorInput getEditorInput(IFileStore fileStore) {
00181         return new FileStoreEditorInput(fileStore);
00182     }
00183 
00184     //-------========================= END TAKEN FROM IDE ===============
00185 
00186 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1