KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 package org.eclipse.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.IResourceChangeEvent;
20 import org.eclipse.core.resources.IResourceDelta;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.ide.IDEActionFactory;
26 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
27 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
28 import org.eclipse.ui.internal.ide.misc.DisjointSet;
29
30 /**
31  * This action closes all projects that are unrelated to the selected projects. A
32  * project is unrelated if it is not directly or transitively referenced by one
33  * of the selected projects, and does not directly or transitively reference
34  * one of the selected projects.
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p>
38  *
39  * @see IDEActionFactory#CLOSE_UNRELATED_PROJECTS
40  * @since 3.3
41  */

42 public class CloseUnrelatedProjectsAction extends CloseResourceAction {
43     /**
44      * The id of this action.
45      */

46     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".CloseUnrelatedProjectsAction"; //$NON-NLS-1$
47

48     private final List JavaDoc projectsToClose = new ArrayList JavaDoc();
49
50     private boolean selectionDirty = true;
51
52     private List JavaDoc oldSelection = Collections.EMPTY_LIST;
53
54     /**
55      * Builds the connected component set for the input projects.
56      * The result is a DisjointSet where all related projects belong
57      * to the same set.
58      */

59     private static DisjointSet buildConnectedComponents(IProject[] projects) {
60         //initially each vertex is in a set by itself
61
DisjointSet set = new DisjointSet();
62         for (int i = 0; i < projects.length; i++) {
63             set.makeSet(projects[i]);
64         }
65         for (int i = 0; i < projects.length; i++) {
66             try {
67                 IProject[] references = projects[i].getReferencedProjects();
68                 //each reference represents an edge in the project reference
69
//digraph from projects[i] -> references[j]
70
for (int j = 0; j < references.length; j++) {
71                     Object JavaDoc setOne = set.findSet(projects[i]);
72                     //note that referenced projects may not exist in the workspace
73
Object JavaDoc setTwo = set.findSet(references[j]);
74                     //these two projects are related, so join their sets
75
if (setOne != null && setTwo != null && setOne != setTwo)
76                         set.union(setOne, setTwo);
77                 }
78             } catch (CoreException e) {
79                 //assume inaccessible projects have no references
80
}
81         }
82         return set;
83     }
84
85     /**
86      * Creates this action.
87      *
88      * @param shell
89      * The shell to use for parenting any dialogs created by this
90      * action.
91      */

92     public CloseUnrelatedProjectsAction(Shell shell) {
93         super(shell, IDEWorkbenchMessages.CloseUnrelatedProjectsAction_text);
94         setId(ID);
95         setToolTipText(IDEWorkbenchMessages.CloseUnrelatedProjectsAction_toolTip);
96         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IIDEHelpContextIds.CLOSE_UNRELATED_PROJECTS_ACTION);
97     }
98
99     /*
100      * (non-Javadoc)
101      *
102      * @see org.eclipse.ui.actions.SelectionListenerAction#clearCache()
103      */

104     protected void clearCache() {
105         super.clearCache();
106         oldSelection = Collections.EMPTY_LIST;
107         selectionDirty = true;
108     }
109
110     /**
111      * Computes the related projects of the selection.
112      */

113     private void computeRelated(List JavaDoc selection) {
114         //build the connected component set for all projects in the workspace
115
DisjointSet set = buildConnectedComponents(ResourcesPlugin.getWorkspace().getRoot().getProjects());
116         //remove the connected components that the selected projects are in
117
for (Iterator JavaDoc it = selection.iterator(); it.hasNext();)
118             set.removeSet(it.next());
119         //the remainder of the projects in the disjoint set are unrelated to the selection
120
projectsToClose.clear();
121         set.toList(projectsToClose);
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.eclipse.ui.actions.SelectionListenerAction#getSelectedResources()
128      */

129     protected List JavaDoc getSelectedResources() {
130         if (selectionDirty) {
131             List JavaDoc newSelection = super.getSelectedResources();
132             if (!oldSelection.equals(newSelection)) {
133                 oldSelection = newSelection;
134                 computeRelated(newSelection);
135             }
136             selectionDirty = false;
137         }
138         return projectsToClose;
139     }
140
141     /**
142      * Handles a resource changed event by updating the enablement
143      * when projects change.
144      * <p>
145      * This method overrides the super-type implementation to update
146      * the selection when the open state or description of any project changes.
147      */

148     public void resourceChanged(IResourceChangeEvent event) {
149         // don't bother looking at delta if selection not applicable
150
if (selectionIsOfType(IResource.PROJECT)) {
151             IResourceDelta delta = event.getDelta();
152             if (delta != null) {
153                 IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
154                 for (int i = 0; i < projDeltas.length; ++i) {
155                     IResourceDelta projDelta = projDeltas[i];
156                     //changing either the description or the open state can affect enablement
157
if ((projDelta.getFlags() & (IResourceDelta.OPEN | IResourceDelta.DESCRIPTION)) != 0) {
158                         selectionChanged(getStructuredSelection());
159                         return;
160                     }
161                 }
162             }
163         }
164     }
165 }
166
Popular Tags