KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005 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;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
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 /**
29  * A provider of notifications for when the active shell changes.
30  *
31  * @since 3.1
32  */

33 public final class ActiveShellSourceProvider extends AbstractSourceProvider {
34
35     /**
36      * The display on which this provider is working.
37      */

38     private final Display display;
39
40     /**
41      * The last shell seen as active by this provider. This value may be
42      * <code>null</code> if the last call to
43      * <code>Display.getActiveShell()</code> returned <code>null</code>.
44      */

45     private Shell lastActiveShell = null;
46
47     /**
48      * The last workbench window shell seen as active by this provider. This
49      * value may be <code>null</code> if the last call to
50      * <code>workbench.getActiveWorkbenchWindow()</code> returned
51      * <code>null</code>.
52      */

53     private Shell lastActiveWorkbenchWindow = null;
54
55     /**
56      * The listener to shell activations on the display.
57      */

58     private final Listener listener = new Listener() {
59         /**
60          * Notifies all listeners that the source has changed.
61          */

62         public final void handleEvent(final Event event) {
63             if (!(event.widget instanceof Shell)) {
64                 return;
65             }
66             
67             final Map JavaDoc 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             // Figure out which variables have changed.
74
final boolean shellChanged = newActiveShell != lastActiveShell;
75             final boolean windowChanged = newActiveWorkbenchWindowShell != lastActiveWorkbenchWindow;
76
77             // Fire an event for those sources that have changed.
78
if (shellChanged && windowChanged) {
79                 final Map JavaDoc sourceValuesByName = new HashMap JavaDoc(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             // Update the member variables.
96
lastActiveShell = newActiveShell;
97             lastActiveWorkbenchWindow = newActiveWorkbenchWindowShell;
98         }
99     };
100
101     /**
102      * The workbench on which to work; never <code>null</code>.
103      */

104     private final IWorkbench workbench;
105
106     /**
107      * Constructs a new instance of <code>ShellSourceProvider</code>.
108      *
109      * @param workbench
110      * The workbench on which to monitor shell activations; must not
111      * be <code>null</code>.
112      */

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 JavaDoc getCurrentState() {
124         final Map JavaDoc currentState = new HashMap JavaDoc(4);
125
126         final Shell newActiveShell = display.getActiveShell();
127         currentState.put(ISources.ACTIVE_SHELL_NAME, newActiveShell);
128
129         /*
130          * We will fallback to the workbench window, but only if a dialog is not
131          * open.
132          */

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