KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.mapping.ResourceMapping;
21 import org.eclipse.core.resources.mapping.ResourceMappingContext;
22 import org.eclipse.core.resources.mapping.ResourceTraversal;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IAdaptable;
25 import org.eclipse.core.runtime.IAdapterManager;
26 import org.eclipse.core.runtime.NullProgressMonitor;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
29
30 /**
31  * The abstract superclass for resource-based actions that listen to selection
32  * change events. This implementation tracks the current selection (see
33  * <code>getStructuredSelection</code>) and provides a convenient place to
34  * monitor selection changes that could affect the availability of the action.
35  * <p>
36  * Subclasses must implement the following <code>IAction</code> method:
37  * <ul>
38  * <li><code>run</code> - to do the action's work</li>
39  * </ul>
40  * </p>
41  * <p>
42  * Subclasses may extend the <code>updateSelection</code> method to update the
43  * action determine its availability based on the current selection.
44  * </p>
45  * <p>
46  * The object instantiating the subclass is responsible for registering the
47  * instance with a selection provider. Alternatively, the object can notify the
48  * subclass instance directly of a selection change using the methods:
49  * <ul>
50  * <li><code>selectionChanged(IStructuredSelection)</code> - passing the
51  * selection</li>
52  * <li><code>selectionChanged(ISelectionChangedEvent)</code> - passing the
53  * selection change event</li>
54  * </ul>
55  * </p>
56  */

57 public abstract class SelectionListenerAction extends
58         BaseSelectionListenerAction {
59     /**
60      * Empty list that is immutable.
61      */

62     private static final List JavaDoc EMPTY_LIST = Arrays.asList(new Object JavaDoc[0]);
63
64     /**
65      * Indicates whether the selection has changes since <code>resources</code>
66      * and <code>nonResources</code> were computed.
67      */

68     private boolean selectionDirty = true;
69
70     /**
71      * The list of resource elements in the current selection (element type:
72      * <code>IResource</code>); meaningful only when
73      * <code>selectionDirty == false</code>.
74      */

75     private List JavaDoc resources;
76
77     /**
78      * The list of non-resource elements in the current selection (element type:
79      * <code>Object</code>); meaningful only when
80      * <code>selectionDirty == false</code>.
81      */

82     private List JavaDoc nonResources;
83
84     /**
85      * Creates a new action with the given text.
86      *
87      * @param text
88      * the string used as the text for the action, or
89      * <code>null</code> if there is no text
90      */

91     protected SelectionListenerAction(String JavaDoc text) {
92         super(text);
93     }
94
95     /**
96      * The <code>SelectionListenerAction</code> implementation of this
97      * <code>BaseSelectionListenerAction</code> method clears the cached
98      * resources and non-resources.
99      */

100     protected void clearCache() {
101         selectionDirty = true;
102         // clear out the lists in case computeResources does not get called
103
// immediately
104
resources = null;
105         nonResources = null;
106     }
107
108     /**
109      * Extracts <code>IResource</code>s from the current selection and adds
110      * them to the resources list, and the rest into the non-resources list.
111      */

112     private final void computeResources() {
113         resources = null;
114         nonResources = null;
115
116         for (Iterator JavaDoc e = getStructuredSelection().iterator(); e.hasNext();) {
117             Object JavaDoc next = e.next();
118             if (next instanceof IResource) {
119                 if (resources == null) {
120                     // assume selection contains mostly resources most times
121
resources = new ArrayList JavaDoc(getStructuredSelection().size());
122                 }
123                 resources.add(next);
124                 continue;
125             } else if (next instanceof IAdaptable) {
126                 Object JavaDoc resource = ((IAdaptable) next)
127                         .getAdapter(IResource.class);
128                 if (resource != null) {
129                     if (resources == null) {
130                         // assume selection contains mostly resources most times
131
resources = new ArrayList JavaDoc(getStructuredSelection()
132                                 .size());
133                     }
134                     resources.add(resource);
135                     continue;
136                 }
137             } else {
138
139                 boolean resourcesFoundForThisSelection = false;
140
141                 IAdapterManager adapterManager = Platform.getAdapterManager();
142                 ResourceMapping mapping = (ResourceMapping) adapterManager
143                         .getAdapter(next, ResourceMapping.class);
144
145                 if (mapping != null) {
146
147                     ResourceTraversal[] traversals = null;
148                     try {
149                         traversals = mapping.getTraversals(
150                                 ResourceMappingContext.LOCAL_CONTEXT,
151                                 new NullProgressMonitor());
152                     } catch (CoreException exception) {
153                         IDEWorkbenchPlugin.log(exception.getLocalizedMessage(),
154                                 exception.getStatus());
155                     }
156
157                     if (traversals != null) {
158
159                         for (int i = 0; i < traversals.length; i++) {
160
161                             IResource[] traversalResources = traversals[i]
162                                     .getResources();
163
164                             if (traversalResources != null) {
165
166                                 resourcesFoundForThisSelection = true;
167
168                                 if (resources == null) {
169                                     resources = new ArrayList JavaDoc(
170                                             getStructuredSelection().size());
171                                 }
172
173                                 for (int j = 0; j < traversalResources.length; j++) {
174                                     resources.add(traversalResources[j]);
175                                 }// for
176

177                             }// if
178

179                         }// for
180

181                     }// if
182

183                 }// if
184

185                 if (resourcesFoundForThisSelection) {
186                     continue;
187                 }
188             }
189
190             if (nonResources == null) {
191                 // assume selection contains mostly resources most times
192
nonResources = new ArrayList JavaDoc(1);
193             }
194             nonResources.add(next);
195         }
196     }
197
198     /**
199      * Returns the elements in the current selection that are not
200      * <code>IResource</code>s.
201      *
202      * @return list of elements (element type: <code>Object</code>)
203      */

204     protected List JavaDoc getSelectedNonResources() {
205         // recompute if selection has changed.
206
if (selectionDirty) {
207             computeResources();
208             selectionDirty = false;
209         }
210
211         if (nonResources == null) {
212             return EMPTY_LIST;
213         }
214         
215         return nonResources;
216     }
217
218     /**
219      * Returns the elements in the current selection that are
220      * <code>IResource</code>s.
221      *
222      * @return list of resource elements (element type: <code>IResource</code>)
223      */

224     protected List JavaDoc getSelectedResources() {
225         // recompute if selection has changed.
226
if (selectionDirty) {
227             computeResources();
228             selectionDirty = false;
229         }
230
231         if (resources == null) {
232             return EMPTY_LIST;
233         }
234         return resources;
235     }
236
237     /**
238      * Returns whether the type of the given resource is among those in the
239      * given resource type mask.
240      *
241      * @param resource
242      * the resource
243      * @param resourceMask
244      * a bitwise OR of resource types: <code>IResource</code>.{<code>FILE</code>,
245      * <code>FOLDER</code>, <code>PROJECT</code>,
246      * <code>ROOT</code>}
247      * @return <code>true</code> if the resource type matches, and
248      * <code>false</code> otherwise
249      * @see IResource
250      */

251     protected boolean resourceIsType(IResource resource, int resourceMask) {
252         return (resource.getType() & resourceMask) != 0;
253     }
254
255     /**
256      * Returns whether the current selection consists entirely of resources
257      * whose types are among those in the given resource type mask.
258      *
259      * @param resourceMask
260      * a bitwise OR of resource types: <code>IResource</code>.{<code>FILE</code>,
261      * <code>FOLDER</code>, <code>PROJECT</code>,
262      * <code>ROOT</code>}
263      * @return <code>true</code> if all resources in the current selection are
264      * of the specified types or if the current selection is empty, and
265      * <code>false</code> if some elements are resources of a
266      * different type or not resources
267      * @see IResource
268      */

269     protected boolean selectionIsOfType(int resourceMask) {
270         if (getSelectedNonResources().size() > 0) {
271             return false;
272         }
273
274         for (Iterator JavaDoc e = getSelectedResources().iterator(); e.hasNext();) {
275             IResource next = (IResource) e.next();
276             if (!resourceIsType(next, resourceMask)) {
277                 return false;
278             }
279         }
280         return true;
281     }
282
283 }
284
Popular Tags