KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > services > ActiveShellSourceProvider


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.services;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
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 /**
32  * A provider of notifications for when the active shell changes.
33  *
34  * @since 3.1
35  */

36 public final class ActiveShellSourceProvider extends AbstractSourceProvider {
37
38     /**
39      * The names of the sources supported by this source provider.
40      */

41     private static final String JavaDoc[] PROVIDED_SOURCE_NAMES = new String JavaDoc[] {
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     /**
48      * The display on which this provider is working.
49      */

50     private final Display display;
51
52     /**
53      * The last shell seen as active by this provider. This value may be
54      * <code>null</code> if the last call to
55      * <code>Display.getActiveShell()</code> returned <code>null</code>.
56      */

57     private Shell lastActiveShell = null;
58
59     /**
60      * The last workbench window shell seen as active by this provider. This
61      * value may be <code>null</code> if the last call to
62      * <code>workbench.getActiveWorkbenchWindow()</code> returned
63      * <code>null</code>.
64      */

65     private Shell lastActiveWorkbenchWindowShell = null;
66     
67     /**
68      * The last workbench window seen as active by this provider. This value may
69      * be null if the last call to
70      * <code>workbench.getActiveWorkbenchWindow()</code> returned
71      * <code>null</code>.
72      *
73      * @since 3.3
74      */

75     private WorkbenchWindow lastActiveWorkbenchWindow = null;
76
77     /**
78      * The result of the last visibility check on the coolbar of the last active
79      * workbench window.
80      *
81      * @since 3.3
82      */

83     private Boolean JavaDoc lastCoolbarVisibility = Boolean.FALSE;
84     
85     /**
86      * The result of the last visibility check on the perspective bar of the
87      * last active workbench window.
88      *
89      * @since 3.3
90      */

91     private Boolean JavaDoc lastPerspectiveBarVisibility = Boolean.FALSE;
92     
93     /**
94      * The listener to individual window properties.
95      *
96      * @since 3.3
97      */

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 JavaDoc newValue = event.getNewValue();
104                 if (newValue == null || !(newValue instanceof Boolean JavaDoc))
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 JavaDoc) newValue;
112                 }
113             }
114             else if (WorkbenchWindow.PROP_PERSPECTIVEBAR_VISIBLE.equals(event
115                     .getProperty())) {
116                 Object JavaDoc newValue = event.getNewValue();
117                 if (newValue == null || !(newValue instanceof Boolean JavaDoc))
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 JavaDoc) newValue;
125                 }
126             }
127         }
128         
129     };
130     
131     /**
132      * The listener to shell activations on the display.
133      */

134     private final Listener listener = new Listener() {
135         /**
136          * Notifies all listeners that the source has changed.
137          */

138         public final void handleEvent(final Event event) {
139             if (!(event.widget instanceof Shell)) {
140                 if (DEBUG) {
141                     logDebuggingInfo("ASSP: passOnEvent: " + event.widget); //$NON-NLS-1$
142
}
143                 return;
144             }
145             
146             if (DEBUG) {
147                 logDebuggingInfo("\tASSP:lastActiveShell: " + lastActiveShell); //$NON-NLS-1$
148
logDebuggingInfo("\tASSP:lastActiveWorkbenchWindowShell" + lastActiveWorkbenchWindowShell); //$NON-NLS-1$
149
}
150
151             final Map JavaDoc 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             // dont update the coolbar/perspective bar visibility unless we're
160
// processing a workbench window change
161
final Boolean JavaDoc newCoolbarVisibility = newActiveWorkbenchWindow == null ? lastCoolbarVisibility
162                     : (newActiveWorkbenchWindow.getCoolBarVisible() ? Boolean.TRUE
163                             : Boolean.FALSE);
164             final Boolean JavaDoc newPerspectiveBarVisibility = newActiveWorkbenchWindow == null ? lastPerspectiveBarVisibility
165                     : (newActiveWorkbenchWindow.getPerspectiveBarVisible() ? Boolean.TRUE
166                             : Boolean.FALSE);
167
168             // Figure out which variables have changed.
169
final boolean shellChanged = newActiveShell != lastActiveShell;
170             final boolean windowChanged = newActiveWorkbenchWindowShell != lastActiveWorkbenchWindowShell;
171             final boolean coolbarChanged = newCoolbarVisibility != lastCoolbarVisibility;
172             final boolean perspectiveBarChanged = newPerspectiveBarVisibility != lastPerspectiveBarVisibility;
173             // Fire an event for those sources that have changed.
174
if (shellChanged && windowChanged) {
175                 final Map JavaDoc sourceValuesByName = new HashMap JavaDoc(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 " //$NON-NLS-1$
198
+ newActiveShell);
199                     logDebuggingInfo("Active workbench window changed to " //$NON-NLS-1$
200
+ newActiveWorkbenchWindow);
201                     logDebuggingInfo("Active workbench window shell changed to " //$NON-NLS-1$
202
+ newActiveWorkbenchWindowShell);
203                     logDebuggingInfo("Active workbench window coolbar visibility " //$NON-NLS-1$
204
+ newCoolbarVisibility);
205                     logDebuggingInfo("Active workbench window perspective bar visibility " //$NON-NLS-1$
206
+ 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 " //$NON-NLS-1$
215
+ newActiveShell);
216                 }
217                 fireSourceChanged(ISources.ACTIVE_SHELL,
218                         ISources.ACTIVE_SHELL_NAME, newActiveShell);
219
220             } else if (windowChanged) {
221                 final Map JavaDoc sourceValuesByName = new HashMap JavaDoc(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 " //$NON-NLS-1$
243
+ newActiveWorkbenchWindow);
244                     logDebuggingInfo("Active workbench window shell changed to " //$NON-NLS-1$
245
+ newActiveWorkbenchWindowShell);
246                     logDebuggingInfo("Active workbench window coolbar visibility " //$NON-NLS-1$
247
+ newCoolbarVisibility);
248                     logDebuggingInfo("Active workbench window perspective bar visibility " //$NON-NLS-1$
249
+ newPerspectiveBarVisibility);
250                 }
251
252                 fireSourceChanged(sourceFlags,
253                         sourceValuesByName);
254                 hookListener(lastActiveWorkbenchWindow, newActiveWorkbenchWindow);
255             }
256
257             // Update the member variables.
258
lastActiveShell = newActiveShell;
259             lastActiveWorkbenchWindowShell = newActiveWorkbenchWindowShell;
260             lastActiveWorkbenchWindow = newActiveWorkbenchWindow;
261             lastCoolbarVisibility = newCoolbarVisibility;
262             lastPerspectiveBarVisibility = newPerspectiveBarVisibility;
263         }
264     };
265
266     /**
267      * The workbench on which to work; never <code>null</code>.
268      */

269     private final Workbench workbench;
270
271     /**
272      * Constructs a new instance of <code>ShellSourceProvider</code>.
273      *
274      * @param workbench
275      * The workbench on which to monitor shell activations; must not
276      * be <code>null</code>.
277      */

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 JavaDoc getCurrentState() {
293         final Map JavaDoc currentState = new HashMap JavaDoc(4);
294
295         final Shell newActiveShell = display.getActiveShell();
296         currentState.put(ISources.ACTIVE_SHELL_NAME, newActiveShell);
297
298         /*
299          * We will fallback to the workbench window, but only if a dialog is not
300          * open.
301          */

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 JavaDoc[] 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