1 12 package org.eclipse.ui.views.navigator; 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.viewers.IStructuredSelection; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.KeyEvent; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.ui.IActionBars; 28 import org.eclipse.ui.actions.ActionFactory; 29 import org.eclipse.ui.actions.BuildAction; 30 import org.eclipse.ui.actions.CloseResourceAction; 31 import org.eclipse.ui.actions.CloseUnrelatedProjectsAction; 32 import org.eclipse.ui.actions.OpenResourceAction; 33 import org.eclipse.ui.actions.RefreshAction; 34 import org.eclipse.ui.ide.IDEActionFactory; 35 36 40 public class WorkspaceActionGroup extends ResourceNavigatorActionGroup { 41 42 private BuildAction buildAction; 43 44 private OpenResourceAction openProjectAction; 45 46 private CloseResourceAction closeProjectAction; 47 48 private CloseUnrelatedProjectsAction closeUnrelatedProjectsAction; 49 50 private RefreshAction refreshAction; 51 52 public WorkspaceActionGroup(IResourceNavigator navigator) { 53 super(navigator); 54 } 55 56 public void fillActionBars(IActionBars actionBars) { 57 actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), 58 refreshAction); 59 actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT 60 .getId(), buildAction); 61 actionBars.setGlobalActionHandler( 62 IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction); 63 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT 64 .getId(), closeProjectAction); 65 actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS 66 .getId(), closeUnrelatedProjectsAction); 67 } 68 69 90 public void fillContextMenu(IMenuManager menu) { 91 IStructuredSelection selection = (IStructuredSelection) getContext() 92 .getSelection(); 93 boolean isProjectSelection = true; 94 boolean hasOpenProjects = false; 95 boolean hasClosedProjects = false; 96 boolean hasBuilder = true; Iterator resources = selection.iterator(); 98 99 while (resources.hasNext() 100 && (!hasOpenProjects || !hasClosedProjects || hasBuilder || isProjectSelection)) { 101 Object next = resources.next(); 102 IProject project = null; 103 104 if (next instanceof IProject) { 105 project = (IProject) next; 106 } else if (next instanceof IAdaptable) { 107 project = (IProject) ((IAdaptable) next) 108 .getAdapter(IProject.class); 109 } 110 111 if (project == null) { 112 isProjectSelection = false; 113 continue; 114 } 115 if (project.isOpen()) { 116 hasOpenProjects = true; 117 if (hasBuilder && !hasBuilder(project)) { 118 hasBuilder = false; 119 } 120 } else { 121 hasClosedProjects = true; 122 hasBuilder = false; 123 } 124 } 125 if (!selection.isEmpty() && isProjectSelection 126 && !ResourcesPlugin.getWorkspace().isAutoBuilding() 127 && hasBuilder) { 128 buildAction.selectionChanged(selection); 130 menu.add(buildAction); 131 } 132 if (!hasClosedProjects) { 133 refreshAction.selectionChanged(selection); 134 menu.add(refreshAction); 135 } 136 if (isProjectSelection) { 137 if (hasClosedProjects) { 138 openProjectAction.selectionChanged(selection); 139 menu.add(openProjectAction); 140 } 141 if (hasOpenProjects) { 142 closeProjectAction.selectionChanged(selection); 143 menu.add(closeProjectAction); 144 closeUnrelatedProjectsAction.selectionChanged(selection); 145 menu.add(closeUnrelatedProjectsAction); 146 } 147 } 148 } 149 150 153 public void handleKeyPressed(KeyEvent event) { 154 if (event.keyCode == SWT.F5 && event.stateMask == 0) { 155 if (refreshAction.isEnabled()) { 156 refreshAction.refreshAll(); 157 } 158 159 event.doit = false; 161 } 162 } 163 164 170 boolean hasBuilder(IProject project) { 171 try { 172 ICommand[] commands = project.getDescription().getBuildSpec(); 173 if (commands.length > 0) { 174 return true; 175 } 176 } catch (CoreException e) { 177 } 180 return false; 181 } 182 183 protected void makeActions() { 184 Shell shell = navigator.getSite().getShell(); 185 openProjectAction = new OpenResourceAction(shell); 186 closeProjectAction = new CloseResourceAction(shell); 187 closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(shell); 188 refreshAction = new RefreshAction(shell); 189 refreshAction 190 .setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif")); refreshAction 192 .setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif")); buildAction = new BuildAction(shell, 194 IncrementalProjectBuilder.INCREMENTAL_BUILD); 195 } 196 197 public void updateActionBars() { 198 IStructuredSelection selection = (IStructuredSelection) getContext() 199 .getSelection(); 200 refreshAction.selectionChanged(selection); 201 buildAction.selectionChanged(selection); 202 openProjectAction.selectionChanged(selection); 203 closeUnrelatedProjectsAction.selectionChanged(selection); 204 closeProjectAction.selectionChanged(selection); 205 } 206 } 207 | Popular Tags |