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
00022
00023
00024 public class WindowTitle extends BaseWindowTitle {
00025 public enum MethodFlag { WINDOWTITLE, VIEWTITLE, EDITORTITLE};
00026
00027
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
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
00062
00063
00064
00065
00066 public void refreshAllTitles() {
00067
00068 final IWorkbenchWindow[] wkBenchWins = PlatformUI.getWorkbench().getWorkbenchWindows();
00069 for (IWorkbenchWindow wkBenchWin: wkBenchWins) {
00070
00071 if (ViewerWindowManager.getWindowNumber(wkBenchWin) == -1) {
00072 continue;
00073 }
00074
00075 final String title = getWindowTitle(wkBenchWin);
00076 wkBenchWin.getShell().setText(title);
00077
00078 refreshViewTitles(wkBenchWin);
00079
00080 refreshEditorTitles(wkBenchWin);
00081 }
00082 return;
00083 }
00084
00085
00086
00087
00088
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
00101
00102
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
00115
00116
00117
00118
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
00125 if (windowTitle != null) {
00126 return windowTitle;
00127 }
00128 }
00129 }
00130
00131 return super.getWindowTitle(window);
00132 }
00133
00134
00135
00136
00137
00138
00139
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
00147 if ( viewTitle != null) {
00148 return viewTitle;
00149 }
00150 }
00151 }
00152
00153 return super.setTitle(window, view);
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 public String setEditorTitle(IWorkbenchWindow window, IEditorPart editorPart) {
00165 if (runnable != null) {
00166 if ( this.runExtension(runnable, MethodFlag.EDITORTITLE, window, editorPart) ) {
00167 String editorTitle = runnable.getTitle();
00168
00169 if ( editorTitle != null) {
00170 return editorTitle;
00171 }
00172 }
00173 }
00174
00175 return super.setEditorTitle(window, editorPart);
00176 }
00177
00178
00179
00180
00181
00182
00183
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 }