1 11 package org.eclipse.ui.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.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 42 public class CloseUnrelatedProjectsAction extends CloseResourceAction { 43 46 public static final String ID = PlatformUI.PLUGIN_ID + ".CloseUnrelatedProjectsAction"; 48 private final List projectsToClose = new ArrayList (); 49 50 private boolean selectionDirty = true; 51 52 private List oldSelection = Collections.EMPTY_LIST; 53 54 59 private static DisjointSet buildConnectedComponents(IProject[] projects) { 60 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 for (int j = 0; j < references.length; j++) { 71 Object setOne = set.findSet(projects[i]); 72 Object setTwo = set.findSet(references[j]); 74 if (setOne != null && setTwo != null && setOne != setTwo) 76 set.union(setOne, setTwo); 77 } 78 } catch (CoreException e) { 79 } 81 } 82 return set; 83 } 84 85 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 104 protected void clearCache() { 105 super.clearCache(); 106 oldSelection = Collections.EMPTY_LIST; 107 selectionDirty = true; 108 } 109 110 113 private void computeRelated(List selection) { 114 DisjointSet set = buildConnectedComponents(ResourcesPlugin.getWorkspace().getRoot().getProjects()); 116 for (Iterator it = selection.iterator(); it.hasNext();) 118 set.removeSet(it.next()); 119 projectsToClose.clear(); 121 set.toList(projectsToClose); 122 } 123 124 129 protected List getSelectedResources() { 130 if (selectionDirty) { 131 List 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 148 public void resourceChanged(IResourceChangeEvent event) { 149 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 if ((projDelta.getFlags() & (IResourceDelta.OPEN | IResourceDelta.DESCRIPTION)) != 0) { 158 selectionChanged(getStructuredSelection()); 159 return; 160 } 161 } 162 } 163 } 164 } 165 } 166 | Popular Tags |