KickJava   Java API By Example, From Geeks To Geeks.

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


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

33 public class ActivePartSourceProvider extends AbstractSourceProvider {
34
35     /**
36      * The last active part id seen as active by this provider. This value may
37      * be <code>null</code> if there is no currently active part.
38      */

39     private String JavaDoc lastActivePartId = null;
40
41     /**
42      * The last active part site seen by this provider. This value may be
43      * <code>null</code> if there is no currently active site.
44      */

45     private IWorkbenchPartSite lastActivePartSite = null;
46
47     private final IPartListener partListener = new IPartListener() {
48
49         public final void partActivated(final IWorkbenchPart part) {
50             checkActivePart();
51         }
52
53         public final void partBroughtToTop(final IWorkbenchPart part) {
54             checkActivePart();
55         }
56
57         public final void partClosed(final IWorkbenchPart part) {
58             checkActivePart();
59         }
60
61         public final void partDeactivated(final IWorkbenchPart part) {
62             checkActivePart();
63         }
64
65         public final void partOpened(final IWorkbenchPart part) {
66             checkActivePart();
67         }
68
69     };
70
71     private final IWindowListener windowListener = new IWindowListener() {
72
73         public final void windowActivated(final IWorkbenchWindow window) {
74             if (window != null) {
75                 window.getPartService().addPartListener(partListener);
76             }
77             checkActivePart();
78         }
79
80         public final void windowClosed(final IWorkbenchWindow window) {
81             if (window != null) {
82                 window.getPartService().removePartListener(partListener);
83             }
84             checkActivePart();
85         }
86
87         public final void windowDeactivated(final IWorkbenchWindow window) {
88             if (window != null) {
89                 window.getPartService().removePartListener(partListener);
90             }
91             checkActivePart();
92         }
93
94         public final void windowOpened(final IWorkbenchWindow window) {
95             if (window != null) {
96                 window.getPartService().addPartListener(partListener);
97             }
98             checkActivePart();
99         }
100
101     };
102
103     /**
104      * The workbench on which this source provider will act.
105      */

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

115     public ActivePartSourceProvider(final IWorkbench workbench) {
116         this.workbench = workbench;
117         workbench.addWindowListener(windowListener);
118     }
119
120     private final void checkActivePart() {
121         final Map JavaDoc currentState = getCurrentState();
122         int sources = 0;
123         final Object JavaDoc newActivePartId = currentState
124                 .get(ISources.ACTIVE_PART_NAME);
125         if (!Util.equals(newActivePartId, lastActivePartId)) {
126             sources |= ISources.ACTIVE_PART;
127             lastActivePartId = (String JavaDoc) newActivePartId;
128         }
129         final Object JavaDoc newActivePartSite = currentState
130                 .get(ISources.ACTIVE_SITE_NAME);
131         if (!Util.equals(newActivePartSite, lastActivePartSite)) {
132             sources |= ISources.ACTIVE_SITE;
133             lastActivePartSite = (IWorkbenchPartSite) newActivePartSite;
134         }
135
136         if (sources != 0) {
137             fireSourceChanged(sources, currentState);
138         }
139     }
140
141     public final void dispose() {
142         workbench.removeWindowListener(windowListener);
143     }
144
145     public final Map JavaDoc getCurrentState() {
146         final Map JavaDoc currentState = new HashMap JavaDoc(4);
147         currentState.put(ISources.ACTIVE_SITE_NAME, null);
148         currentState.put(ISources.ACTIVE_PART_NAME, null);
149
150         final IWorkbenchWindow activeWorkbenchWindow = workbench
151                 .getActiveWorkbenchWindow();
152         if (activeWorkbenchWindow != null) {
153             final IWorkbenchPage activeWorkbenchPage = activeWorkbenchWindow
154                     .getActivePage();
155             if (activeWorkbenchPage != null) {
156                 final IWorkbenchPart activeWorkbenchPart = activeWorkbenchPage
157                         .getActivePart();
158                 if (activeWorkbenchPart != null) {
159                     final IWorkbenchPartSite activeWorkbenchPartSite = activeWorkbenchPart
160                             .getSite();
161                     currentState.put(ISources.ACTIVE_SITE_NAME,
162                             activeWorkbenchPartSite);
163                     if (activeWorkbenchPartSite != null) {
164                         final String JavaDoc newActivePartId = activeWorkbenchPartSite
165                                 .getId();
166                         currentState.put(ISources.ACTIVE_PART_NAME,
167                                 newActivePartId);
168                     }
169                 }
170             }
171         }
172
173         return currentState;
174     }
175
176 }
177
Popular Tags