1 11 12 package org.eclipse.ui.internal.services; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.jface.util.IPropertyChangeListener; 18 import org.eclipse.jface.util.PropertyChangeEvent; 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.widgets.Display; 21 import org.eclipse.swt.widgets.Event; 22 import org.eclipse.swt.widgets.Listener; 23 import org.eclipse.swt.widgets.Shell; 24 import org.eclipse.ui.AbstractSourceProvider; 25 import org.eclipse.ui.ISources; 26 import org.eclipse.ui.IWorkbenchWindow; 27 import org.eclipse.ui.contexts.IContextService; 28 import org.eclipse.ui.internal.Workbench; 29 import org.eclipse.ui.internal.WorkbenchWindow; 30 31 36 public final class ActiveShellSourceProvider extends AbstractSourceProvider { 37 38 41 private static final String [] PROVIDED_SOURCE_NAMES = new String [] { 42 ISources.ACTIVE_SHELL_NAME, ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 43 ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME, 44 ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME, 45 ISources.ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME }; 46 47 50 private final Display display; 51 52 57 private Shell lastActiveShell = null; 58 59 65 private Shell lastActiveWorkbenchWindowShell = null; 66 67 75 private WorkbenchWindow lastActiveWorkbenchWindow = null; 76 77 83 private Boolean lastCoolbarVisibility = Boolean.FALSE; 84 85 91 private Boolean lastPerspectiveBarVisibility = Boolean.FALSE; 92 93 98 private final IPropertyChangeListener propertyListener = new IPropertyChangeListener() { 99 100 public void propertyChange(PropertyChangeEvent event) { 101 if (WorkbenchWindow.PROP_COOLBAR_VISIBLE 102 .equals(event.getProperty())) { 103 Object newValue = event.getNewValue(); 104 if (newValue == null || !(newValue instanceof Boolean )) 105 return; 106 if (!lastCoolbarVisibility.equals(newValue)) { 107 fireSourceChanged( 108 ISources.ACTIVE_WORKBENCH_WINDOW_SUBORDINATE, 109 ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME, 110 newValue); 111 lastCoolbarVisibility = (Boolean ) newValue; 112 } 113 } 114 else if (WorkbenchWindow.PROP_PERSPECTIVEBAR_VISIBLE.equals(event 115 .getProperty())) { 116 Object newValue = event.getNewValue(); 117 if (newValue == null || !(newValue instanceof Boolean )) 118 return; 119 if (!lastPerspectiveBarVisibility.equals(newValue)) { 120 fireSourceChanged( 121 ISources.ACTIVE_WORKBENCH_WINDOW_SUBORDINATE, 122 ISources.ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME, 123 newValue); 124 lastPerspectiveBarVisibility = (Boolean ) newValue; 125 } 126 } 127 } 128 129 }; 130 131 134 private final Listener listener = new Listener() { 135 138 public final void handleEvent(final Event event) { 139 if (!(event.widget instanceof Shell)) { 140 if (DEBUG) { 141 logDebuggingInfo("ASSP: passOnEvent: " + event.widget); } 143 return; 144 } 145 146 if (DEBUG) { 147 logDebuggingInfo("\tASSP:lastActiveShell: " + lastActiveShell); logDebuggingInfo("\tASSP:lastActiveWorkbenchWindowShell" + lastActiveWorkbenchWindowShell); } 150 151 final Map currentState = getCurrentState(); 152 final Shell newActiveShell = (Shell) currentState 153 .get(ISources.ACTIVE_SHELL_NAME); 154 final WorkbenchWindow newActiveWorkbenchWindow = (WorkbenchWindow) currentState 155 .get(ISources.ACTIVE_WORKBENCH_WINDOW_NAME); 156 final Shell newActiveWorkbenchWindowShell = (Shell) currentState 157 .get(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME); 158 159 final Boolean newCoolbarVisibility = newActiveWorkbenchWindow == null ? lastCoolbarVisibility 162 : (newActiveWorkbenchWindow.getCoolBarVisible() ? Boolean.TRUE 163 : Boolean.FALSE); 164 final Boolean newPerspectiveBarVisibility = newActiveWorkbenchWindow == null ? lastPerspectiveBarVisibility 165 : (newActiveWorkbenchWindow.getPerspectiveBarVisible() ? Boolean.TRUE 166 : Boolean.FALSE); 167 168 final boolean shellChanged = newActiveShell != lastActiveShell; 170 final boolean windowChanged = newActiveWorkbenchWindowShell != lastActiveWorkbenchWindowShell; 171 final boolean coolbarChanged = newCoolbarVisibility != lastCoolbarVisibility; 172 final boolean perspectiveBarChanged = newPerspectiveBarVisibility != lastPerspectiveBarVisibility; 173 if (shellChanged && windowChanged) { 175 final Map sourceValuesByName = new HashMap (5); 176 sourceValuesByName.put(ISources.ACTIVE_SHELL_NAME, 177 newActiveShell); 178 sourceValuesByName.put(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 179 newActiveWorkbenchWindow); 180 sourceValuesByName.put( 181 ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME, 182 newActiveWorkbenchWindowShell); 183 int sourceFlags = ISources.ACTIVE_SHELL | ISources.ACTIVE_WORKBENCH_WINDOW; 184 185 if (coolbarChanged) { 186 sourceValuesByName.put(ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME, 187 newCoolbarVisibility); 188 sourceFlags |= ISources.ACTIVE_WORKBENCH_WINDOW_SUBORDINATE; 189 } 190 if (perspectiveBarChanged) { 191 sourceValuesByName.put(ISources.ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME, 192 newPerspectiveBarVisibility); 193 sourceFlags |= ISources.ACTIVE_WORKBENCH_WINDOW_SUBORDINATE; 194 } 195 196 if (DEBUG) { 197 logDebuggingInfo("Active shell changed to " + newActiveShell); 199 logDebuggingInfo("Active workbench window changed to " + newActiveWorkbenchWindow); 201 logDebuggingInfo("Active workbench window shell changed to " + newActiveWorkbenchWindowShell); 203 logDebuggingInfo("Active workbench window coolbar visibility " + newCoolbarVisibility); 205 logDebuggingInfo("Active workbench window perspective bar visibility " + newPerspectiveBarVisibility); 207 } 208 209 fireSourceChanged(sourceFlags, sourceValuesByName); 210 hookListener(lastActiveWorkbenchWindow, newActiveWorkbenchWindow); 211 212 } else if (shellChanged) { 213 if (DEBUG) { 214 logDebuggingInfo("Active shell changed to " + newActiveShell); 216 } 217 fireSourceChanged(ISources.ACTIVE_SHELL, 218 ISources.ACTIVE_SHELL_NAME, newActiveShell); 219 220 } else if (windowChanged) { 221 final Map sourceValuesByName = new HashMap (4); 222 sourceValuesByName.put(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 223 newActiveWorkbenchWindow); 224 sourceValuesByName.put( 225 ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME, 226 newActiveWorkbenchWindowShell); 227 228 int sourceFlags = ISources.ACTIVE_SHELL | ISources.ACTIVE_WORKBENCH_WINDOW; 229 230 if (coolbarChanged) { 231 sourceValuesByName.put(ISources.ACTIVE_WORKBENCH_WINDOW_IS_COOLBAR_VISIBLE_NAME, 232 newCoolbarVisibility); 233 sourceFlags |= ISources.ACTIVE_WORKBENCH_WINDOW_SUBORDINATE; 234 } 235 if (perspectiveBarChanged) { 236 sourceValuesByName.put(ISources.ACTIVE_WORKBENCH_WINDOW_IS_PERSPECTIVEBAR_VISIBLE_NAME, 237 newPerspectiveBarVisibility); 238 sourceFlags |= ISources.ACTIVE_WORKBENCH_WINDOW_SUBORDINATE; 239 } 240 241 if (DEBUG) { 242 logDebuggingInfo("Active workbench window changed to " + newActiveWorkbenchWindow); 244 logDebuggingInfo("Active workbench window shell changed to " + newActiveWorkbenchWindowShell); 246 logDebuggingInfo("Active workbench window coolbar visibility " + newCoolbarVisibility); 248 logDebuggingInfo("Active workbench window perspective bar visibility " + newPerspectiveBarVisibility); 250 } 251 252 fireSourceChanged(sourceFlags, 253 sourceValuesByName); 254 hookListener(lastActiveWorkbenchWindow, newActiveWorkbenchWindow); 255 } 256 257 lastActiveShell = newActiveShell; 259 lastActiveWorkbenchWindowShell = newActiveWorkbenchWindowShell; 260 lastActiveWorkbenchWindow = newActiveWorkbenchWindow; 261 lastCoolbarVisibility = newCoolbarVisibility; 262 lastPerspectiveBarVisibility = newPerspectiveBarVisibility; 263 } 264 }; 265 266 269 private final Workbench workbench; 270 271 278 public ActiveShellSourceProvider(final Workbench workbench) { 279 this.workbench = workbench; 280 this.display = workbench.getDisplay(); 281 this.display.addFilter(SWT.Activate, listener); 282 } 283 284 public final void dispose() { 285 display.removeFilter(SWT.Activate, listener); 286 hookListener(lastActiveWorkbenchWindow, null); 287 lastActiveWorkbenchWindow = null; 288 lastActiveWorkbenchWindowShell = null; 289 lastActiveShell = null; 290 } 291 292 public final Map getCurrentState() { 293 final Map currentState = new HashMap (4); 294 295 final Shell newActiveShell = display.getActiveShell(); 296 currentState.put(ISources.ACTIVE_SHELL_NAME, newActiveShell); 297 298 302 final IContextService contextService = (IContextService) workbench 303 .getService(IContextService.class); 304 final int shellType = contextService.getShellType(newActiveShell); 305 if (shellType != IContextService.TYPE_DIALOG) { 306 final IWorkbenchWindow newActiveWorkbenchWindow = workbench 307 .getActiveWorkbenchWindow(); 308 final Shell newActiveWorkbenchWindowShell; 309 if (newActiveWorkbenchWindow == null) { 310 newActiveWorkbenchWindowShell = null; 311 } else { 312 newActiveWorkbenchWindowShell = newActiveWorkbenchWindow 313 .getShell(); 314 } 315 currentState.put(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 316 newActiveWorkbenchWindow); 317 currentState.put(ISources.ACTIVE_WORKBENCH_WINDOW_SHELL_NAME, 318 newActiveWorkbenchWindowShell); 319 } 320 321 return currentState; 322 } 323 324 public final String [] getProvidedSourceNames() { 325 return PROVIDED_SOURCE_NAMES; 326 } 327 328 private void hookListener(WorkbenchWindow lastActiveWorkbenchWindow, 329 WorkbenchWindow newActiveWorkbenchWindow) { 330 if (lastActiveWorkbenchWindow != null) 331 lastActiveWorkbenchWindow.removePropertyChangeListener(propertyListener); 332 333 if (newActiveWorkbenchWindow != null) 334 newActiveWorkbenchWindow.addPropertyChangeListener(propertyListener); 335 } 336 } 337 | Popular Tags |