1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IAdaptable; 18 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.IWorkspace; 21 import org.eclipse.core.resources.ResourcesPlugin; 22 23 import org.eclipse.swt.widgets.Shell; 24 25 import org.eclipse.jface.action.IMenuManager; 26 import org.eclipse.jface.viewers.ISelection; 27 import org.eclipse.jface.viewers.ISelectionChangedListener; 28 import org.eclipse.jface.viewers.ISelectionProvider; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.SelectionChangedEvent; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 33 import org.eclipse.ui.IActionBars; 34 import org.eclipse.ui.IViewPart; 35 import org.eclipse.ui.IWorkbenchSite; 36 import org.eclipse.ui.IWorkingSet; 37 import org.eclipse.ui.actions.ActionGroup; 38 import org.eclipse.ui.actions.CloseResourceAction; 39 import org.eclipse.ui.actions.CloseUnrelatedProjectsAction; 40 41 import org.eclipse.ui.ide.IDEActionFactory; 42 43 import org.eclipse.jdt.core.IJavaProject; 44 45 import org.eclipse.jdt.ui.IContextMenuConstants; 46 47 56 public class ProjectActionGroup extends ActionGroup { 57 58 private IWorkbenchSite fSite; 59 60 private OpenProjectAction fOpenAction; 61 private CloseResourceAction fCloseAction; 62 private CloseResourceAction fCloseUnrelatedAction; 63 64 private ISelectionChangedListener fSelectionChangedListener; 65 66 73 public ProjectActionGroup(IViewPart part) { 74 fSite = part.getSite(); 75 Shell shell= fSite.getShell(); 76 ISelectionProvider provider= fSite.getSelectionProvider(); 77 ISelection selection= provider.getSelection(); 78 79 fCloseAction= new CloseResourceAction(shell); 80 fCloseAction.setActionDefinitionId("org.eclipse.ui.project.closeProject"); 82 fCloseUnrelatedAction= new CloseUnrelatedProjectsAction(shell); 83 fCloseUnrelatedAction.setActionDefinitionId("org.eclipse.ui.project.closeUnrelatedProjects"); 85 fOpenAction= new OpenProjectAction(fSite); 86 fOpenAction.setActionDefinitionId("org.eclipse.ui.project.openProject"); if (selection instanceof IStructuredSelection) { 88 IStructuredSelection s= (IStructuredSelection)selection; 89 fOpenAction.selectionChanged(s); 90 fCloseAction.selectionChanged(s); 91 fCloseUnrelatedAction.selectionChanged(s); 92 } 93 94 fSelectionChangedListener= new ISelectionChangedListener() { 95 public void selectionChanged(SelectionChangedEvent event) { 96 ISelection s= event.getSelection(); 97 if (s instanceof IStructuredSelection) { 98 performSelectionChanged((IStructuredSelection) s); 99 } 100 } 101 }; 102 provider.addSelectionChangedListener(fSelectionChangedListener); 103 104 IWorkspace workspace= ResourcesPlugin.getWorkspace(); 105 workspace.addResourceChangeListener(fOpenAction); 106 workspace.addResourceChangeListener(fCloseAction); 107 workspace.addResourceChangeListener(fCloseUnrelatedAction); 108 } 109 110 protected void performSelectionChanged(IStructuredSelection structuredSelection) { 111 Object [] array= structuredSelection.toArray(); 112 ArrayList openProjects= new ArrayList (); 113 int selectionStatus= evaluateSelection(array, openProjects); 114 StructuredSelection sel= new StructuredSelection(openProjects); 115 116 fOpenAction.setEnabled(selectionStatus == CLOSED_PROJECTS_SELECTED || (selectionStatus == 0 && hasClosedProjectsInWorkspace())); 117 fCloseAction.selectionChanged(sel); 118 fCloseUnrelatedAction.selectionChanged(sel); 119 } 120 121 private int CLOSED_PROJECTS_SELECTED= 1; 122 private int NON_PROJECT_SELECTED= 2; 123 124 private int evaluateSelection(Object [] array, List allOpenProjects) { 125 int status= 0; 126 for (int i= 0; i < array.length; i++) { 127 Object curr= array[i]; 128 if (curr instanceof IJavaProject) { 129 curr= ((IJavaProject) curr).getProject(); 130 } 131 if (curr instanceof IProject) { 132 IProject project= (IProject) curr; 133 if (project.isOpen()) { 134 allOpenProjects.add(project); 135 } else { 136 status |= CLOSED_PROJECTS_SELECTED; 137 } 138 } else { 139 if (curr instanceof IWorkingSet) { 140 int res= evaluateSelection(((IWorkingSet) curr).getElements(), allOpenProjects); 141 status |= res; 142 } else { 143 status |= NON_PROJECT_SELECTED; 144 } 145 } 146 } 147 return status; 148 } 149 150 private boolean hasClosedProjectsInWorkspace() { 151 IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects(); 152 for (int i = 0; i < projects.length; i++) { 153 if (!projects[i].isOpen()) 154 return true; 155 } 156 return false; 157 } 158 159 162 public void fillActionBars(IActionBars actionBars) { 163 super.fillActionBars(actionBars); 164 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), fCloseAction); 165 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(), fCloseUnrelatedAction); 166 actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), fOpenAction); 167 } 168 169 172 public void fillContextMenu(IMenuManager menu) { 173 super.fillContextMenu(menu); 174 if (fOpenAction.isEnabled()) 175 menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fOpenAction); 176 if (fCloseAction.isEnabled()) 177 menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseAction); 178 if (fCloseUnrelatedAction.isEnabled() && areOnlyProjectsSelected(fCloseUnrelatedAction.getStructuredSelection())) 179 menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, fCloseUnrelatedAction); 180 } 181 182 189 public OpenProjectAction getOpenProjectAction() { 190 return fOpenAction; 191 } 192 193 private boolean areOnlyProjectsSelected(IStructuredSelection selection) { 194 if (selection.isEmpty()) 195 return false; 196 197 Iterator iter= selection.iterator(); 198 while (iter.hasNext()) { 199 Object obj= iter.next(); 200 if (obj instanceof IAdaptable) { 201 if (((IAdaptable)obj).getAdapter(IProject.class) == null) 202 return false; 203 } 204 } 205 return true; 206 } 207 208 211 public void dispose() { 212 ISelectionProvider provider= fSite.getSelectionProvider(); 213 provider.removeSelectionChangedListener(fSelectionChangedListener); 214 215 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 216 workspace.removeResourceChangeListener(fOpenAction); 217 workspace.removeResourceChangeListener(fCloseAction); 218 workspace.removeResourceChangeListener(fCloseUnrelatedAction); 219 super.dispose(); 220 } 221 } 222 | Popular Tags |