KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > 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.internal.ide.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.actions.CloseResourceAction;
26 import org.eclipse.ui.ide.IDEActionFactory;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
28 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
29 import org.eclipse.ui.internal.ide.misc.DisjointSet;
30
31 /**
32  * This action closes all projects that are unrelated to the selected projects. A
33  * project is unrelated if it is not directly or transitively referenced by one
34  * of the selected projects, and does not directly or transitively reference
35  * one of the selected projects.
36  * <p>
37  * This class may be instantiated; it is not intended to be subclassed.
38  * </p>
39  *
40  * @see IDEActionFactory#CLOSE_UNRELATED_PROJECTS
41  * @since 3.2
42  */

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

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

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

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

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

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

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

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

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