KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > BaseSelectionListenerAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.actions;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.SelectionChangedEvent;
19 import org.eclipse.jface.viewers.StructuredSelection;
20 import org.eclipse.swt.widgets.Event;
21
22 /**
23  * The abstract superclass for actions that listen to selection change events.
24  * This implementation tracks the current selection (see
25  * <code>getStructuredSelection</code>) and provides a convenient place to
26  * monitor selection changes that could affect the availability of the action.
27  * <p>
28  * Subclasses must implement the following <code>IAction</code> method:
29  * <ul>
30  * <li><code>run</code> - to do the action's work</li>
31  * </ul>
32  * </p>
33  * <p>
34  * Subclasses may extend the <code>updateSelection</code> method to update
35  * the action determine its availability based on the current selection.
36  * </p>
37  * <p>
38  * The object instantiating the subclass is responsible for registering
39  * the instance with a selection provider. Alternatively, the object can
40  * notify the subclass instance directly of a selection change using the
41  * methods:
42  * <ul>
43  * <li><code>selectionChanged(IStructuredSelection)</code> - passing the selection</li>
44  * <li><code>selectionChanged(ISelectionChangedEvent)</code> - passing the selection change event</li>
45  * </ul>
46  * </p>
47  * @since 3.0
48  */

49 public abstract class BaseSelectionListenerAction extends Action implements
50         ISelectionChangedListener {
51     /**
52      * The current selection.
53      */

54     private IStructuredSelection selection = new StructuredSelection();
55
56     /**
57      * Running flag: <code>true</code> iff the action is running.
58      */

59     private boolean running = false;
60
61     /**
62      * The deferred selection. Any selection change that occurs
63      * while the action is running is held here until the run is complete.
64      */

65     private IStructuredSelection deferredSelection = null;
66
67     /**
68      * Creates a new action with the given text.
69      *
70      * @param text the string used as the text for the action,
71      * or <code>null</code> if there is no text
72      */

73     protected BaseSelectionListenerAction(String JavaDoc text) {
74         super(text);
75     }
76
77     /**
78      * Clears any cached state associated with the selection.
79      * Called when the selection changes.
80      * <p>
81      * The <code>BaseSelectionListenerAction</code> implementation of this method
82      * does nothing. Subclasses may override.
83      * </p>
84      */

85     protected void clearCache() {
86         // do nothing
87
}
88
89     /**
90      * Returns the current structured selection in the workbench, or an empty
91      * selection if nothing is selected or if selection does not include
92      * objects (for example, raw text).
93      *
94      * @return the current structured selection in the workbench
95      */

96     public IStructuredSelection getStructuredSelection() {
97         return selection;
98     }
99
100     /**
101      * Notifies this action that the given structured selection has changed.
102      * <p>
103      * The <code>BaseSelectionListenerAction</code> implementation of this method
104      * records the given selection for future reference and calls
105      * <code>updateSelection</code>, updating the enable state of this action
106      * based on the outcome. Subclasses should override <code>updateSelection</code>
107      * to react to selection changes.
108      * </p>
109      *
110      * @param selection the new selection
111      */

112     public final void selectionChanged(IStructuredSelection selection) {
113         // Ignore any incoming selection change while the action is running,
114
// otherwise the action can have unpredictable results, including lost
115
// data, if it operates on a different selection than what it initially
116
// validated.
117
// See Bug 60606 [Navigator] (data loss) Navigator deletes/moves the wrong file
118
if (running) {
119             deferredSelection = selection;
120             return;
121         }
122         this.selection = selection;
123         clearCache();
124         setEnabled(updateSelection(selection));
125     }
126
127     /**
128      * The <code>BaseSelectionListenerAction</code> implementation of this
129      * <code>ISelectionChangedListener</code> method calls
130      * <code>selectionChanged(IStructuredSelection)</code> assuming the selection is
131      * a structured one. Subclasses should override the <code>updateSelection</code>
132      * method to react to selection changes.
133      */

134     public final void selectionChanged(SelectionChangedEvent event) {
135         ISelection selection = event.getSelection();
136         if (selection instanceof IStructuredSelection) {
137             selectionChanged((IStructuredSelection) selection);
138         } else {
139             selectionChanged(StructuredSelection.EMPTY);
140         }
141     }
142
143     /**
144      * Updates this action in response to the given selection.
145      * <p>
146      * The <code>BaseSelectionListenerAction</code> implementation of this method
147      * returns <code>true</code>. Subclasses may extend to react to selection
148      * changes; however, if the super method returns <code>false</code>, the
149      * overriding method must also return <code>false</code>.
150      * </p>
151      *
152      * @param selection the new selection
153      * @return <code>true</code> if the action should be enabled for this selection,
154      * and <code>false</code> otherwise
155      */

156     protected boolean updateSelection(IStructuredSelection selection) {
157         return true;
158     }
159
160     /* (non-Javadoc)
161      * @see org.eclipse.jface.action.Action#runWithEvent(org.eclipse.swt.widgets.Event)
162      */

163     public void runWithEvent(Event event) {
164         // Set the running flag during the run so that selection changes are deferred.
165
// See selectionChanged(IStructuredSelection) for more details.
166
running = true;
167         try {
168             run();
169         } finally {
170             running = false;
171             IStructuredSelection s = deferredSelection;
172             deferredSelection = null;
173             if (s != null) {
174                 selectionChanged(s);
175             }
176         }
177     }
178 }
179
Popular Tags