1 11 package org.eclipse.ui.internal.ide.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Collections ; 15 import java.util.Iterator ; 16 import java.util.List ; 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 43 public class CloseUnrelatedProjectsAction extends CloseResourceAction { 44 47 public static final String ID = PlatformUI.PLUGIN_ID + ".CloseUnrelatedProjectsAction"; 49 private final List projectsToClose = new ArrayList (); 50 51 private boolean selectionDirty = true; 52 53 private List oldSelection = Collections.EMPTY_LIST; 54 55 60 static DisjointSet buildConnectedComponents(IProject[] projects) { 61 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 for (int j = 0; j < references.length; j++) { 72 Object setOne = set.findSet(projects[i]); 73 Object setTwo = set.findSet(references[j]); 75 if (setOne != null && setTwo != null && setOne != setTwo) 77 set.union(setOne, setTwo); 78 } 79 } catch (CoreException e) { 80 } 82 } 83 return set; 84 } 85 86 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 105 protected void clearCache() { 106 super.clearCache(); 107 oldSelection = Collections.EMPTY_LIST; 108 selectionDirty = true; 109 } 110 111 114 private void computeRelated(List selection) { 115 DisjointSet set = buildConnectedComponents(ResourcesPlugin.getWorkspace().getRoot().getProjects()); 117 for (Iterator it = selection.iterator(); it.hasNext();) 119 set.removeSet(it.next()); 120 projectsToClose.clear(); 122 set.toList(projectsToClose); 123 } 124 125 130 protected List getSelectedResources() { 131 if (selectionDirty) { 132 List 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 149 public void resourceChanged(IResourceChangeEvent event) { 150 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 if ((projDelta.getFlags() & (IResourceDelta.OPEN | IResourceDelta.DESCRIPTION)) != 0) { 159 selectionChanged(getStructuredSelection()); 160 return; 161 } 162 } 163 } 164 } 165 } 166 } | Popular Tags |