KickJava   Java API By Example, From Geeks To Geeks.

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


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.Map JavaDoc;
15 import java.util.TreeMap JavaDoc;
16
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.ui.AbstractSourceProvider;
19 import org.eclipse.ui.INullSelectionListener;
20 import org.eclipse.ui.ISelectionService;
21 import org.eclipse.ui.ISources;
22 import org.eclipse.ui.IWindowListener;
23 import org.eclipse.ui.IWorkbench;
24 import org.eclipse.ui.IWorkbenchPart;
25 import org.eclipse.ui.IWorkbenchWindow;
26
27 /**
28  * <p>
29  * This listens to changes to the current selection, and propagates them through
30  * the <code>ISourceProvider</code> framework (a common language in which
31  * events are communicated to expression-based services).
32  * </p>
33  * <p>
34  * This class is not intended for use outside of the
35  * <code>org.eclipse.ui.workbench</code> plug-in.
36  * </p>
37  *
38  * @since 3.2
39  */

40 public final class CurrentSelectionSourceProvider extends
41         AbstractSourceProvider implements INullSelectionListener {
42
43     /**
44      * The names of the sources supported by this source provider.
45      */

46     private static final String JavaDoc[] PROVIDED_SOURCE_NAMES = new String JavaDoc[] { ISources.ACTIVE_CURRENT_SELECTION_NAME };
47
48     /**
49      * Monitors changes to the active workbench window, and swaps the selection
50      * listener to the active workbench window.
51      */

52     private final IWindowListener windowListener = new IWindowListener() {
53         public final void windowActivated(final IWorkbenchWindow window) {
54             swapListeners(window, false);
55         }
56
57         public final void windowClosed(final IWorkbenchWindow window) {
58             swapListeners(window, true);
59         }
60
61         public final void windowDeactivated(final IWorkbenchWindow window) {
62             swapListeners(window, true);
63         }
64
65         public final void windowOpened(final IWorkbenchWindow window) {
66             swapListeners(window, false);
67         }
68     };
69
70     /**
71      * The workbench on which this source provider is acting. This value is
72      * never <code>null</code>.
73      */

74     private final IWorkbench workbench;
75
76     /**
77      * Constructs a new instance of <code>CurrentSelectionSourceProvider</code>.
78      *
79      * @param workbench
80      * The workbench on which this source provider should act; this
81      * value must not be <code>null</code>.
82      */

83     public CurrentSelectionSourceProvider(final IWorkbench workbench) {
84         this.workbench = workbench;
85         workbench.addWindowListener(windowListener);
86     }
87
88     public final void dispose() {
89         workbench.removeWindowListener(windowListener);
90     }
91
92     public final Map JavaDoc getCurrentState() {
93         final Map JavaDoc currentState = new TreeMap JavaDoc();
94         final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
95         if (window != null) {
96             final ISelectionService service = window.getSelectionService();
97             final ISelection selection = service.getSelection();
98             currentState.put(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
99         } else {
100             currentState.put(ISources.ACTIVE_CURRENT_SELECTION_NAME, null);
101         }
102         return currentState;
103     }
104
105     public final String JavaDoc[] getProvidedSourceNames() {
106         return PROVIDED_SOURCE_NAMES;
107     }
108
109     public final void selectionChanged(final IWorkbenchPart part,
110             final ISelection selection) {
111         if (DEBUG) {
112             logDebuggingInfo("Selection changed to " + selection); //$NON-NLS-1$
113
}
114
115         fireSourceChanged(ISources.ACTIVE_CURRENT_SELECTION,
116                 ISources.ACTIVE_CURRENT_SELECTION_NAME, selection);
117     }
118
119     /**
120      * Swaps the selection listener. This either adds or removes a selection
121      * listener from the given window's selection service.
122      *
123      * @param window
124      * The workbench window to which the listener should be added or
125      * from which the listener should be removed; must not be
126      * <code>null</code>.
127      * @param remove
128      * Whether the selection listener should be removed; otherwise,
129      * it should be added.
130      */

131     private final void swapListeners(final IWorkbenchWindow window,
132             final boolean remove) {
133         final ISelectionService selectionService = window.getSelectionService();
134         if (remove) {
135             window.getSelectionService().removeSelectionListener(
136                     CurrentSelectionSourceProvider.this);
137             selectionChanged(null, null);
138         } else {
139             window.getSelectionService().addSelectionListener(
140                     CurrentSelectionSourceProvider.this);
141             selectionChanged(null, selectionService.getSelection());
142         }
143     }
144 }
145
Popular Tags