KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.ui.AbstractSourceProvider;
18 import org.eclipse.ui.IEditorPart;
19 import org.eclipse.ui.IEditorSite;
20 import org.eclipse.ui.IPartListener;
21 import org.eclipse.ui.ISources;
22 import org.eclipse.ui.IWindowListener;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPage;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.IWorkbenchPartSite;
27 import org.eclipse.ui.IWorkbenchWindow;
28 import org.eclipse.ui.internal.util.Util;
29
30 /**
31  * Provides notifications when the active part changes.
32  *
33  * @since 3.1
34  */

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

40     private static final String JavaDoc[] PROVIDED_SOURCE_NAMES = new String JavaDoc[] {
41             ISources.ACTIVE_EDITOR_ID_NAME, ISources.ACTIVE_EDITOR_NAME,
42             ISources.ACTIVE_PART_ID_NAME, ISources.ACTIVE_PART_NAME,
43             ISources.ACTIVE_SITE_NAME };
44
45     /**
46      * The last active editor part seen as active by this provider. This value
47      * may be <code>null</code> if there is no currently active editor.
48      */

49     private IEditorPart lastActiveEditor = null;
50
51     /**
52      * The last active editor id seen as active by this provider. This value may
53      * be <code>null</code> if there is no currently active editor.
54      */

55     private String JavaDoc lastActiveEditorId = null;
56
57     /**
58      * The last active part seen as active by this provider. This value may be
59      * <code>null</code> if there is no currently active part.
60      */

61     private IWorkbenchPart lastActivePart = null;
62
63     /**
64      * The last active part id seen as active by this provider. This value may
65      * be <code>null</code> if there is no currently active part.
66      */

67     private String JavaDoc lastActivePartId = null;
68
69     /**
70      * The last active part site seen by this provider. This value may be
71      * <code>null</code> if there is no currently active site.
72      */

73     private IWorkbenchPartSite lastActivePartSite = null;
74
75     private final IPartListener partListener = new IPartListener() {
76
77         public final void partActivated(final IWorkbenchPart part) {
78             checkActivePart();
79         }
80
81         public final void partBroughtToTop(final IWorkbenchPart part) {
82             checkActivePart();
83         }
84
85         public final void partClosed(final IWorkbenchPart part) {
86             checkActivePart();
87         }
88
89         public final void partDeactivated(final IWorkbenchPart part) {
90             checkActivePart();
91         }
92
93         public final void partOpened(final IWorkbenchPart part) {
94             checkActivePart();
95         }
96
97     };
98
99     private final IWindowListener windowListener = new IWindowListener() {
100
101         public final void windowActivated(final IWorkbenchWindow window) {
102             checkActivePart();
103         }
104
105         public final void windowClosed(final IWorkbenchWindow window) {
106             if (window != null) {
107                 window.getPartService().removePartListener(partListener);
108             }
109             checkActivePart();
110         }
111
112         public final void windowDeactivated(final IWorkbenchWindow window) {
113             checkActivePart();
114         }
115
116         public final void windowOpened(final IWorkbenchWindow window) {
117             if (window != null) {
118                 window.getPartService().addPartListener(partListener);
119             }
120             checkActivePart();
121         }
122
123     };
124
125     /**
126      * The workbench on which this source provider will act.
127      */

128     private final IWorkbench workbench;
129
130     /**
131      * Constructs a new instance of <code>ShellSourceProvider</code>.
132      *
133      * @param workbench
134      * The workbench on which to monitor shell activations; must not
135      * be <code>null</code>.
136      */

137     public ActivePartSourceProvider(final IWorkbench workbench) {
138         this.workbench = workbench;
139         workbench.addWindowListener(windowListener);
140     }
141
142     private final void checkActivePart() {
143         final Map JavaDoc currentState = getCurrentState();
144         int sources = 0;
145
146         // Figure out what was changed.
147
final Object JavaDoc newActivePart = currentState
148                 .get(ISources.ACTIVE_PART_NAME);
149         if (!Util.equals(newActivePart, lastActivePart)) {
150             sources |= ISources.ACTIVE_PART;
151             lastActivePart = (IWorkbenchPart) newActivePart;
152         }
153         final Object JavaDoc newActivePartId = currentState
154                 .get(ISources.ACTIVE_PART_ID_NAME);
155         if (!Util.equals(newActivePartId, lastActivePartId)) {
156             sources |= ISources.ACTIVE_PART_ID;
157             lastActivePartId = (String JavaDoc) newActivePartId;
158         }
159         final Object JavaDoc newActivePartSite = currentState
160                 .get(ISources.ACTIVE_SITE_NAME);
161         if (!Util.equals(newActivePartSite, lastActivePartSite)) {
162             sources |= ISources.ACTIVE_SITE;
163             lastActivePartSite = (IWorkbenchPartSite) newActivePartSite;
164         }
165         final Object JavaDoc newActiveEditor = currentState
166                 .get(ISources.ACTIVE_EDITOR_NAME);
167         if (!Util.equals(newActiveEditor, lastActiveEditor)) {
168             sources |= ISources.ACTIVE_EDITOR;
169             lastActiveEditor = (IEditorPart) newActiveEditor;
170         }
171         final Object JavaDoc newActiveEditorId = currentState
172                 .get(ISources.ACTIVE_EDITOR_ID_NAME);
173         if (!Util.equals(newActiveEditorId, lastActiveEditorId)) {
174             sources |= ISources.ACTIVE_EDITOR_ID;
175             lastActiveEditorId = (String JavaDoc) newActiveEditorId;
176         }
177
178         // Fire the event, if something has changed.
179
if (sources != 0) {
180             if (DEBUG) {
181                 if ((sources & ISources.ACTIVE_PART) != 0) {
182                     logDebuggingInfo("Active part changed to " //$NON-NLS-1$
183
+ lastActivePart);
184                 }
185                 if ((sources & ISources.ACTIVE_PART_ID) != 0) {
186                     logDebuggingInfo("Active part id changed to " //$NON-NLS-1$
187
+ lastActivePartId);
188                 }
189                 if ((sources & ISources.ACTIVE_SITE) != 0) {
190                     logDebuggingInfo("Active site changed to " //$NON-NLS-1$
191
+ lastActivePartSite);
192                 }
193                 if ((sources & ISources.ACTIVE_EDITOR) != 0) {
194                     logDebuggingInfo("Active editor changed to " //$NON-NLS-1$
195
+ lastActiveEditor);
196                 }
197                 if ((sources & ISources.ACTIVE_EDITOR_ID) != 0) {
198                     logDebuggingInfo("Active editor id changed to " //$NON-NLS-1$
199
+ lastActiveEditorId);
200                 }
201             }
202             fireSourceChanged(sources, currentState);
203         }
204     }
205
206     public final void dispose() {
207         workbench.removeWindowListener(windowListener);
208     }
209
210     public final Map JavaDoc getCurrentState() {
211         final Map JavaDoc currentState = new HashMap JavaDoc(7);
212         currentState.put(ISources.ACTIVE_SITE_NAME, null);
213         currentState.put(ISources.ACTIVE_PART_NAME, null);
214         currentState.put(ISources.ACTIVE_PART_ID_NAME, null);
215         currentState.put(ISources.ACTIVE_EDITOR_NAME, null);
216         currentState.put(ISources.ACTIVE_EDITOR_ID_NAME, null);
217
218         final IWorkbenchWindow activeWorkbenchWindow = workbench
219                 .getActiveWorkbenchWindow();
220         if (activeWorkbenchWindow != null) {
221             final IWorkbenchPage activeWorkbenchPage = activeWorkbenchWindow
222                     .getActivePage();
223             if (activeWorkbenchPage != null) {
224                 // Check the active workbench part.
225
final IWorkbenchPart newActivePart = activeWorkbenchPage
226                         .getActivePart();
227                 currentState.put(ISources.ACTIVE_PART_NAME, newActivePart);
228                 if (newActivePart != null) {
229                     final IWorkbenchPartSite activeWorkbenchPartSite = newActivePart
230                             .getSite();
231                     currentState.put(ISources.ACTIVE_SITE_NAME,
232                             activeWorkbenchPartSite);
233                     if (activeWorkbenchPartSite != null) {
234                         final String JavaDoc newActivePartId = activeWorkbenchPartSite
235                                 .getId();
236                         currentState.put(ISources.ACTIVE_PART_ID_NAME,
237                                 newActivePartId);
238                     }
239                 }
240
241                 // Check the active editor part.
242
final IEditorPart newActiveEditor = activeWorkbenchPage
243                         .getActiveEditor();
244                 currentState.put(ISources.ACTIVE_EDITOR_NAME, newActiveEditor);
245                 if (newActiveEditor != null) {
246                     final IEditorSite activeEditorSite = newActiveEditor
247                             .getEditorSite();
248                     if (activeEditorSite != null) {
249                         final String JavaDoc newActiveEditorId = activeEditorSite
250                                 .getId();
251                         currentState.put(ISources.ACTIVE_EDITOR_ID_NAME,
252                                 newActiveEditorId);
253                     }
254                 }
255             }
256         }
257
258         return currentState;
259     }
260
261     public final String JavaDoc[] getProvidedSourceNames() {
262         return PROVIDED_SOURCE_NAMES;
263     }
264
265 }
266
Popular Tags