1 11 12 package org.eclipse.ui.internal; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.widgets.Display; 19 import org.eclipse.swt.widgets.Event; 20 import org.eclipse.swt.widgets.Listener; 21 import org.eclipse.swt.widgets.Shell; 22 import org.eclipse.ui.AbstractSourceProvider; 23 import org.eclipse.ui.ISources; 24 import org.eclipse.ui.IWorkbench; 25 import org.eclipse.ui.IWorkbenchWindow; 26 import org.eclipse.ui.contexts.IContextService; 27 28 33 public final class ActiveShellSourceProvider extends AbstractSourceProvider { 34 35 38 private final Display display; 39 40 45 private Shell lastActiveShell = null; 46 47 53 private Shell lastActiveWorkbenchWindow = null; 54 55 58 private final Listener listener = new Listener() { 59 62 public final void handleEvent(final Event event) { 63 if (!(event.widget instanceof Shell)) { 64 return; 65 } 66 67 final Map currentState = getCurrentState(); 68 final Shell newActiveShell = (Shell) currentState 69 .get(ISources.ACTIVE_SHELL_NAME); 70 final Shell newActiveWorkbenchWindowShell = (Shell) currentState 71 .get(ISources.ACTIVE_WORKBENCH_WINDOW_NAME); 72 73 final boolean shellChanged = newActiveShell != lastActiveShell; 75 final boolean windowChanged = newActiveWorkbenchWindowShell != lastActiveWorkbenchWindow; 76 77 if (shellChanged && windowChanged) { 79 final Map sourceValuesByName = new HashMap (4); 80 sourceValuesByName.put(ISources.ACTIVE_SHELL_NAME, 81 newActiveShell); 82 sourceValuesByName.put(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 83 newActiveWorkbenchWindowShell); 84 fireSourceChanged(ISources.ACTIVE_SHELL 85 | ISources.ACTIVE_WORKBENCH_WINDOW, sourceValuesByName); 86 } else if (shellChanged) { 87 fireSourceChanged(ISources.ACTIVE_SHELL, 88 ISources.ACTIVE_SHELL_NAME, newActiveShell); 89 } else if (windowChanged) { 90 fireSourceChanged(ISources.ACTIVE_WORKBENCH_WINDOW, 91 ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 92 newActiveWorkbenchWindowShell); 93 } 94 95 lastActiveShell = newActiveShell; 97 lastActiveWorkbenchWindow = newActiveWorkbenchWindowShell; 98 } 99 }; 100 101 104 private final IWorkbench workbench; 105 106 113 public ActiveShellSourceProvider(final IWorkbench workbench) { 114 this.workbench = workbench; 115 this.display = workbench.getDisplay(); 116 this.display.addFilter(SWT.Activate, listener); 117 } 118 119 public final void dispose() { 120 display.removeFilter(SWT.Activate, listener); 121 } 122 123 public final Map getCurrentState() { 124 final Map currentState = new HashMap (4); 125 126 final Shell newActiveShell = display.getActiveShell(); 127 currentState.put(ISources.ACTIVE_SHELL_NAME, newActiveShell); 128 129 133 final IContextService contextService = (IContextService) workbench 134 .getAdapter(IContextService.class); 135 final int shellType = contextService.getShellType(newActiveShell); 136 if (shellType != IContextService.TYPE_DIALOG) { 137 final IWorkbenchWindow newActiveWorkbenchWindow = workbench 138 .getActiveWorkbenchWindow(); 139 final Shell newActiveWorkbenchWindowShell; 140 if (newActiveWorkbenchWindow == null) { 141 newActiveWorkbenchWindowShell = null; 142 } else { 143 newActiveWorkbenchWindowShell = newActiveWorkbenchWindow 144 .getShell(); 145 } 146 currentState.put(ISources.ACTIVE_WORKBENCH_WINDOW_NAME, 147 newActiveWorkbenchWindowShell); 148 } 149 150 return currentState; 151 } 152 } 153 | Popular Tags |