001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.services;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.ui.AbstractSourceProvider;
016: import org.eclipse.ui.IEditorPart;
017: import org.eclipse.ui.IEditorSite;
018: import org.eclipse.ui.IPartListener;
019: import org.eclipse.ui.ISources;
020: import org.eclipse.ui.IWindowListener;
021: import org.eclipse.ui.IWorkbench;
022: import org.eclipse.ui.IWorkbenchPage;
023: import org.eclipse.ui.IWorkbenchPart;
024: import org.eclipse.ui.IWorkbenchPartSite;
025: import org.eclipse.ui.IWorkbenchWindow;
026: import org.eclipse.ui.internal.util.Util;
027:
028: /**
029: * Provides notifications when the active part changes.
030: *
031: * @since 3.1
032: */
033: public class ActivePartSourceProvider extends AbstractSourceProvider {
034:
035: /**
036: * The names of the sources supported by this source provider.
037: */
038: private static final String[] PROVIDED_SOURCE_NAMES = new String[] {
039: ISources.ACTIVE_EDITOR_ID_NAME,
040: ISources.ACTIVE_EDITOR_NAME, ISources.ACTIVE_PART_ID_NAME,
041: ISources.ACTIVE_PART_NAME, ISources.ACTIVE_SITE_NAME };
042:
043: /**
044: * The last active editor part seen as active by this provider. This value
045: * may be <code>null</code> if there is no currently active editor.
046: */
047: private IEditorPart lastActiveEditor = null;
048:
049: /**
050: * The last active editor id seen as active by this provider. This value may
051: * be <code>null</code> if there is no currently active editor.
052: */
053: private String lastActiveEditorId = null;
054:
055: /**
056: * The last active part seen as active by this provider. This value may be
057: * <code>null</code> if there is no currently active part.
058: */
059: private IWorkbenchPart lastActivePart = null;
060:
061: /**
062: * The last active part id seen as active by this provider. This value may
063: * be <code>null</code> if there is no currently active part.
064: */
065: private String lastActivePartId = null;
066:
067: /**
068: * The last active part site seen by this provider. This value may be
069: * <code>null</code> if there is no currently active site.
070: */
071: private IWorkbenchPartSite lastActivePartSite = null;
072:
073: private final IPartListener partListener = new IPartListener() {
074:
075: public final void partActivated(final IWorkbenchPart part) {
076: checkActivePart();
077: }
078:
079: public final void partBroughtToTop(final IWorkbenchPart part) {
080: checkActivePart();
081: }
082:
083: public final void partClosed(final IWorkbenchPart part) {
084: checkActivePart();
085: }
086:
087: public final void partDeactivated(final IWorkbenchPart part) {
088: checkActivePart();
089: }
090:
091: public final void partOpened(final IWorkbenchPart part) {
092: checkActivePart();
093: }
094:
095: };
096:
097: private final IWindowListener windowListener = new IWindowListener() {
098:
099: public final void windowActivated(final IWorkbenchWindow window) {
100: checkActivePart();
101: }
102:
103: public final void windowClosed(final IWorkbenchWindow window) {
104: if (window != null) {
105: window.getPartService()
106: .removePartListener(partListener);
107: }
108: checkActivePart();
109: }
110:
111: public final void windowDeactivated(
112: 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 currentState = getCurrentState();
144: int sources = 0;
145:
146: // Figure out what was changed.
147: final Object 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 newActivePartId = currentState
154: .get(ISources.ACTIVE_PART_ID_NAME);
155: if (!Util.equals(newActivePartId, lastActivePartId)) {
156: sources |= ISources.ACTIVE_PART_ID;
157: lastActivePartId = (String) newActivePartId;
158: }
159: final Object 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 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 newActiveEditorId = currentState
172: .get(ISources.ACTIVE_EDITOR_ID_NAME);
173: if (!Util.equals(newActiveEditorId, lastActiveEditorId)) {
174: sources |= ISources.ACTIVE_EDITOR_ID;
175: lastActiveEditorId = (String) 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 getCurrentState() {
211: final Map currentState = new HashMap(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,
228: newActivePart);
229: if (newActivePart != null) {
230: final IWorkbenchPartSite activeWorkbenchPartSite = newActivePart
231: .getSite();
232: currentState.put(ISources.ACTIVE_SITE_NAME,
233: activeWorkbenchPartSite);
234: if (activeWorkbenchPartSite != null) {
235: final String newActivePartId = activeWorkbenchPartSite
236: .getId();
237: currentState.put(ISources.ACTIVE_PART_ID_NAME,
238: newActivePartId);
239: }
240: }
241:
242: // Check the active editor part.
243: final IEditorPart newActiveEditor = activeWorkbenchPage
244: .getActiveEditor();
245: currentState.put(ISources.ACTIVE_EDITOR_NAME,
246: newActiveEditor);
247: if (newActiveEditor != null) {
248: final IEditorSite activeEditorSite = newActiveEditor
249: .getEditorSite();
250: if (activeEditorSite != null) {
251: final String newActiveEditorId = activeEditorSite
252: .getId();
253: currentState.put(
254: ISources.ACTIVE_EDITOR_ID_NAME,
255: newActiveEditorId);
256: }
257: }
258: }
259: }
260:
261: return currentState;
262: }
263:
264: public final String[] getProvidedSourceNames() {
265: return PROVIDED_SOURCE_NAMES;
266: }
267:
268: }
|