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.Map;
013: import java.util.TreeMap;
014:
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.ui.AbstractSourceProvider;
017: import org.eclipse.ui.INullSelectionListener;
018: import org.eclipse.ui.ISelectionService;
019: import org.eclipse.ui.ISources;
020: import org.eclipse.ui.IWindowListener;
021: import org.eclipse.ui.IWorkbench;
022: import org.eclipse.ui.IWorkbenchPart;
023: import org.eclipse.ui.IWorkbenchWindow;
024:
025: /**
026: * <p>
027: * This listens to changes to the current selection, and propagates them through
028: * the <code>ISourceProvider</code> framework (a common language in which
029: * events are communicated to expression-based services).
030: * </p>
031: * <p>
032: * This class is not intended for use outside of the
033: * <code>org.eclipse.ui.workbench</code> plug-in.
034: * </p>
035: *
036: * @since 3.2
037: */
038: public final class CurrentSelectionSourceProvider extends
039: AbstractSourceProvider implements INullSelectionListener {
040:
041: /**
042: * The names of the sources supported by this source provider.
043: */
044: private static final String[] PROVIDED_SOURCE_NAMES = new String[] { ISources.ACTIVE_CURRENT_SELECTION_NAME };
045:
046: /**
047: * Monitors changes to the active workbench window, and swaps the selection
048: * listener to the active workbench window.
049: */
050: private final IWindowListener windowListener = new IWindowListener() {
051: public final void windowActivated(final IWorkbenchWindow window) {
052: swapListeners(window, false);
053: }
054:
055: public final void windowClosed(final IWorkbenchWindow window) {
056: swapListeners(window, true);
057: }
058:
059: public final void windowDeactivated(
060: final IWorkbenchWindow window) {
061: swapListeners(window, true);
062: }
063:
064: public final void windowOpened(final IWorkbenchWindow window) {
065: swapListeners(window, false);
066: }
067: };
068:
069: /**
070: * The workbench on which this source provider is acting. This value is
071: * never <code>null</code>.
072: */
073: private final IWorkbench workbench;
074:
075: /**
076: * Constructs a new instance of <code>CurrentSelectionSourceProvider</code>.
077: *
078: * @param workbench
079: * The workbench on which this source provider should act; this
080: * value must not be <code>null</code>.
081: */
082: public CurrentSelectionSourceProvider(final IWorkbench workbench) {
083: this .workbench = workbench;
084: workbench.addWindowListener(windowListener);
085: }
086:
087: public final void dispose() {
088: workbench.removeWindowListener(windowListener);
089: }
090:
091: public final Map getCurrentState() {
092: final Map currentState = new TreeMap();
093: final IWorkbenchWindow window = workbench
094: .getActiveWorkbenchWindow();
095: if (window != null) {
096: final ISelectionService service = window
097: .getSelectionService();
098: final ISelection selection = service.getSelection();
099: currentState.put(ISources.ACTIVE_CURRENT_SELECTION_NAME,
100: selection);
101: } else {
102: currentState.put(ISources.ACTIVE_CURRENT_SELECTION_NAME,
103: null);
104: }
105: return currentState;
106: }
107:
108: public final String[] getProvidedSourceNames() {
109: return PROVIDED_SOURCE_NAMES;
110: }
111:
112: public final void selectionChanged(final IWorkbenchPart part,
113: final ISelection selection) {
114: if (DEBUG) {
115: logDebuggingInfo("Selection changed to " + selection); //$NON-NLS-1$
116: }
117:
118: fireSourceChanged(ISources.ACTIVE_CURRENT_SELECTION,
119: ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
120: }
121:
122: /**
123: * Swaps the selection listener. This either adds or removes a selection
124: * listener from the given window's selection service.
125: *
126: * @param window
127: * The workbench window to which the listener should be added or
128: * from which the listener should be removed; must not be
129: * <code>null</code>.
130: * @param remove
131: * Whether the selection listener should be removed; otherwise,
132: * it should be added.
133: */
134: private final void swapListeners(final IWorkbenchWindow window,
135: final boolean remove) {
136: final ISelectionService selectionService = window
137: .getSelectionService();
138: if (remove) {
139: window.getSelectionService().removeSelectionListener(
140: CurrentSelectionSourceProvider.this);
141: selectionChanged(null, null);
142: } else {
143: window.getSelectionService().addSelectionListener(
144: CurrentSelectionSourceProvider.this);
145: selectionChanged(null, selectionService.getSelection());
146: }
147: }
148: }
|