1 11 12 package org.eclipse.ui.internal.navigator.resources.actions; 13 14 import java.util.Iterator ; 15 16 import org.eclipse.core.resources.ICommand; 17 import org.eclipse.core.resources.IProject; 18 import org.eclipse.core.resources.IncrementalProjectBuilder; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IAdaptable; 22 import org.eclipse.jface.action.IMenuManager; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.swt.widgets.Shell; 26 import org.eclipse.ui.IActionBars; 27 import org.eclipse.ui.actions.ActionFactory; 28 import org.eclipse.ui.actions.BuildAction; 29 import org.eclipse.ui.actions.CloseResourceAction; 30 import org.eclipse.ui.actions.CloseUnrelatedProjectsAction; 31 import org.eclipse.ui.actions.OpenResourceAction; 32 import org.eclipse.ui.actions.RefreshAction; 33 import org.eclipse.ui.ide.IDEActionFactory; 34 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 35 import org.eclipse.ui.navigator.CommonActionProvider; 36 import org.eclipse.ui.navigator.ICommonActionExtensionSite; 37 import org.eclipse.ui.navigator.ICommonMenuConstants; 38 39 43 public class ResourceMgmtActionProvider extends CommonActionProvider { 44 45 private BuildAction buildAction; 46 47 private OpenResourceAction openProjectAction; 48 49 private CloseResourceAction closeProjectAction; 50 51 private CloseUnrelatedProjectsAction closeUnrelatedProjectsAction; 52 53 private RefreshAction refreshAction; 54 55 private Shell shell; 56 57 60 public void init(ICommonActionExtensionSite aSite) { 61 super.init(aSite); 62 shell = aSite.getViewSite().getShell(); 63 makeActions(); 64 } 65 66 public void fillActionBars(IActionBars actionBars) { 67 actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), 68 refreshAction); 69 actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT 70 .getId(), buildAction); 71 actionBars.setGlobalActionHandler( 72 IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction); 73 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT 74 .getId(), closeProjectAction); 75 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS 76 .getId(), closeUnrelatedProjectsAction); 77 updateActionBars(); 78 } 79 80 81 102 public void fillContextMenu(IMenuManager menu) { 103 IStructuredSelection selection = (IStructuredSelection) getContext() 104 .getSelection(); 105 boolean isProjectSelection = true; 106 boolean hasOpenProjects = false; 107 boolean hasClosedProjects = false; 108 boolean hasBuilder = true; Iterator resources = selection.iterator(); 110 111 while (resources.hasNext() 112 && (!hasOpenProjects || !hasClosedProjects || hasBuilder || isProjectSelection)) { 113 Object next = resources.next(); 114 IProject project = null; 115 116 if (next instanceof IProject) { 117 project = (IProject) next; 118 } else if (next instanceof IAdaptable) { 119 project = (IProject) ((IAdaptable) next) 120 .getAdapter(IProject.class); 121 } 122 123 if (project == null) { 124 isProjectSelection = false; 125 continue; 126 } 127 if (project.isOpen()) { 128 hasOpenProjects = true; 129 if (hasBuilder && !hasBuilder(project)) { 130 hasBuilder = false; 131 } 132 } else { 133 hasClosedProjects = true; 134 hasBuilder = false; 135 } 136 } 137 if (!selection.isEmpty() && isProjectSelection 138 && !ResourcesPlugin.getWorkspace().isAutoBuilding() 139 && hasBuilder) { 140 buildAction.selectionChanged(selection); 142 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, buildAction); 143 } 144 if (!hasClosedProjects) { 145 refreshAction.selectionChanged(selection); 146 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, refreshAction); 147 } 148 if (isProjectSelection) { 149 if (hasClosedProjects) { 150 openProjectAction.selectionChanged(selection); 151 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, openProjectAction); 152 } 153 if (hasOpenProjects) { 154 closeProjectAction.selectionChanged(selection); 155 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, closeProjectAction); 156 closeUnrelatedProjectsAction.selectionChanged(selection); 157 menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, closeUnrelatedProjectsAction); 158 } 159 } 160 } 161 162 163 169 boolean hasBuilder(IProject project) { 170 try { 171 ICommand[] commands = project.getDescription().getBuildSpec(); 172 if (commands.length > 0) { 173 return true; 174 } 175 } catch (CoreException e) { 176 } 179 return false; 180 } 181 182 protected void makeActions() { 183 openProjectAction = new OpenResourceAction(shell); 185 186 closeProjectAction = new CloseResourceAction(shell); 187 188 closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(shell); 189 190 refreshAction = new RefreshAction(shell); 191 refreshAction 192 .setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif")); refreshAction 194 .setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif")); refreshAction.setActionDefinitionId("org.eclipse.ui.file.refresh"); 197 buildAction = new BuildAction(shell, 198 IncrementalProjectBuilder.INCREMENTAL_BUILD); 199 buildAction.setActionDefinitionId("org.eclipse.ui.project.buildProject"); } 201 202 205 protected ImageDescriptor getImageDescriptor(String relativePath) { 206 return IDEWorkbenchPlugin.getIDEImageDescriptor(relativePath); 207 208 } 209 public void updateActionBars() { 210 IStructuredSelection selection = (IStructuredSelection) getContext() 211 .getSelection(); 212 refreshAction.selectionChanged(selection); 213 buildAction.selectionChanged(selection); 214 openProjectAction.selectionChanged(selection); 215 closeUnrelatedProjectsAction.selectionChanged(selection); 216 closeProjectAction.selectionChanged(selection); 217 } 218 219 } 220 | Popular Tags |