WindowTitle.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.util;
00002 
00003 import org.eclipse.core.runtime.CoreException;
00004 import org.eclipse.core.runtime.IConfigurationElement;
00005 import org.eclipse.core.runtime.ISafeRunnable;
00006 import org.eclipse.core.runtime.Platform;
00007 import org.eclipse.core.runtime.SafeRunner;
00008 import org.eclipse.ui.IEditorPart;
00009 import org.eclipse.ui.IEditorReference;
00010 import org.eclipse.ui.IViewPart;
00011 import org.eclipse.ui.IViewReference;
00012 import org.eclipse.ui.IWorkbenchWindow;
00013 import org.eclipse.ui.PlatformUI;
00014 
00015 import edu.rice.cs.hpc.viewer.util.IWindowTitle;
00016 import edu.rice.cs.hpc.viewer.util.BaseWindowTitle;
00017 import edu.rice.cs.hpc.viewer.window.ViewerWindowManager;
00018 
00019 /***
00020  *  
00021  * class to handle titles for windows, views, and editors
00022  *
00023  */
00024 public class WindowTitle extends BaseWindowTitle {
00025     public enum MethodFlag { WINDOWTITLE, VIEWTITLE, EDITORTITLE};
00026 
00027     // The ID of the extension point
00028     private static final String IWINDOWTITLE_ID = "edu.rice.cs.hpc.viewer.util.windowTitle";
00029 
00030     private IWindowTitle extWindowTitle[] = null;
00031     private ExtensionSafeRunnable runnable = null;
00032 
00033     public WindowTitle() {
00034         super();
00035         // find all the extensions and save a list so we do not have to do this every time we want to call them.
00036         IConfigurationElement[] configs = Platform.getExtensionRegistry().getConfigurationElementsFor(IWINDOWTITLE_ID);
00037         if (configs != null && configs.length>0) {
00038             
00039             extWindowTitle = new IWindowTitle[configs.length];
00040             int i = 0;
00041             
00042             for (IConfigurationElement e: configs)
00043             {
00044                 try {
00045                     final Object o = e.createExecutableExtension("class");
00046                     if (o instanceof IWindowTitle) {
00047                         extWindowTitle[i] = ((IWindowTitle)o);
00048                         i++;
00049                         
00050                         if (runnable == null)
00051                             runnable = new ExtensionSafeRunnable();
00052                     }
00053                 } catch (CoreException e1) {
00054                     e1.printStackTrace();
00055                 }
00056             }
00057         }       
00058     }
00059 
00060     /***
00061      * Reset all window, view and editor titles
00062      * 
00063      * @param window
00064      * @param experiment: current database
00065      */
00066     public void refreshAllTitles() {
00067 
00068         final IWorkbenchWindow[] wkBenchWins = PlatformUI.getWorkbench().getWorkbenchWindows();
00069         for (IWorkbenchWindow wkBenchWin: wkBenchWins) {
00070             // if there are no databases open in this window, it may have been disposed so we can not update titles
00071             if (ViewerWindowManager.getWindowNumber(wkBenchWin) == -1) {
00072                 continue;
00073             }
00074             // refresh this window title
00075             final String title = getWindowTitle(wkBenchWin);
00076             wkBenchWin.getShell().setText(title);
00077             // refresh the view titles in this window
00078             refreshViewTitles(wkBenchWin);
00079             // refresh the editor titles in this window
00080             refreshEditorTitles(wkBenchWin);
00081         }
00082         return;
00083     }
00084     
00085 
00086     /****
00087      * Reset all view titles
00088      * @param window
00089      */
00090     public void refreshViewTitles(IWorkbenchWindow window) {
00091         final IViewReference viewRefs[] = window.getActivePage().getViewReferences();
00092         for (IViewReference viewRef: viewRefs) {
00093             final IViewPart view = viewRef.getView(false);
00094             setTitle(window, view);
00095         }
00096         return;
00097     }
00098 
00099     /***
00100      * reset all editor titles
00101      * 
00102      * @param window
00103      */
00104     public void refreshEditorTitles(IWorkbenchWindow window) {
00105         final IEditorReference editors[] = window.getActivePage().getEditorReferences();
00106         for (IEditorReference editor: editors) {
00107             final IEditorPart editorPart = editor.getEditor(false);
00108             setEditorTitle(window, editorPart);
00109         }
00110         return;
00111     }
00112 
00113     /***
00114      * Get the title of a window
00115      * 
00116      * @param window
00117      * @param experiment
00118      * @return
00119      */
00120     public String getWindowTitle(final IWorkbenchWindow window) {
00121         if (runnable != null) {
00122             if ( this.runExtension(runnable, MethodFlag.WINDOWTITLE, window, null)) {
00123                 String windowTitle = runnable.getTitle();
00124                 // if the extension got the window title, just return it
00125                 if (windowTitle != null) {
00126                     return windowTitle;
00127                 }
00128             }
00129         }
00130         // either there was no extension or the extension did not handle window titles, let the super method try and set it
00131         return super.getWindowTitle(window);
00132     }
00133 
00134     /***
00135      * Set the title of a view
00136      * 
00137      * @param window
00138      * @param view
00139      * @returns - the suggested title of the view
00140      */
00141     public String setTitle(IWorkbenchWindow window, IViewPart view) { 
00142         if (runnable != null) {
00143             if ( this.runExtension(runnable, MethodFlag.VIEWTITLE, window, view) ) {
00144 
00145                 String viewTitle = runnable.getTitle();
00146                 // if the extension set the view title, just return that we are done
00147                 if ( viewTitle != null) {
00148                         return viewTitle;
00149                 }
00150             }
00151         }
00152         // either there was no extension or the extension did not handle this kind of view, let the super method try and set it
00153         return super.setTitle(window, view);
00154     }
00155 
00156     /***
00157      * Set the title of an Editor
00158      * 
00159      * @param window
00160      * @param experiment
00161      * @param sTitle
00162      * @returns - non-null if the title was set, null otherwise
00163      */
00164     public String setEditorTitle(IWorkbenchWindow window, IEditorPart editorPart) { //, Experiment experiment, String sTitle) {
00165         if (runnable != null) {
00166             if ( this.runExtension(runnable, MethodFlag.EDITORTITLE, window, editorPart) ) {
00167                 String editorTitle = runnable.getTitle();
00168                 // if the extension set the editor title, just return that we are done
00169                 if ( editorTitle != null) {
00170                     return editorTitle;
00171                 }
00172             }
00173         }
00174         // either there was no extension or the extension did not handle this kind of editor, let the super method try and set it
00175         return super.setEditorTitle(window, editorPart);
00176     }
00177 
00178     /***
00179      * run all registered extensions of window title
00180      * 
00181      * @param run
00182      * @param element
00183      * @param mf
00184      */
00185     private boolean runExtension( ISafeRunnable run, MethodFlag mf, final IWorkbenchWindow window, Object object ) {
00186         
00187         boolean isCalled = false;
00188         
00189         for (IWindowTitle ext: this.extWindowTitle) {
00190             if (ext != null) {
00191                 runnable.setInfo(ext, mf, window, object);
00192                 SafeRunner.run(run);
00193                 isCalled = true;
00194             }
00195         }
00196         return isCalled;
00197     }
00198 
00199     static class ExtensionSafeRunnable implements ISafeRunnable {
00200         private IWindowTitle windowTitle;
00201         private MethodFlag mf;
00202         private IWorkbenchWindow window;
00203         private Object object;
00204         private String strResult;
00205         
00206         public void setInfo(IWindowTitle _windowTitle, MethodFlag _mf, final IWorkbenchWindow _window, Object _object) {
00207             windowTitle = _windowTitle;
00208             mf = _mf;
00209             window = _window;
00210             object = _object;
00211         }
00212 
00213         public void handleException(Throwable exception) {
00214             System.out.println("Exception in window title extension.");
00215         }
00216 
00217         public void run() throws Exception {
00218             if (mf == MethodFlag.WINDOWTITLE)
00219             {
00220                 strResult = windowTitle.getWindowTitle(window);
00221                 return;
00222             }
00223             if (mf == MethodFlag.VIEWTITLE)
00224             {
00225                 strResult = windowTitle.setTitle(window, (IViewPart)object);
00226                 return;
00227             }
00228             if (mf == MethodFlag.EDITORTITLE)
00229             {
00230                 strResult = windowTitle.setEditorTitle(window, (IEditorPart)object);
00231                 return;
00232             }
00233             return;
00234         }
00235         
00236         public String getTitle() {
00237             return strResult;
00238         }
00239     }
00240 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1