1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.swt.widgets.Shell; 14 15 import org.eclipse.jface.action.IAction; 16 import org.eclipse.jface.action.IMenuManager; 17 import org.eclipse.jface.viewers.ISelection; 18 import org.eclipse.jface.viewers.ISelectionProvider; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 21 import org.eclipse.core.resources.IncrementalProjectBuilder; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 24 import org.eclipse.ui.IActionBars; 25 import org.eclipse.ui.IViewPart; 26 import org.eclipse.ui.IWorkbenchSite; 27 import org.eclipse.ui.actions.ActionFactory; 28 import org.eclipse.ui.actions.ActionGroup; 29 import org.eclipse.ui.actions.BuildAction; 30 import org.eclipse.ui.ide.IDEActionFactory; 31 32 import org.eclipse.jdt.core.IJavaProject; 33 34 import org.eclipse.jdt.internal.ui.actions.ActionMessages; 35 36 import org.eclipse.jdt.ui.IContextMenuConstants; 37 38 48 public class BuildActionGroup extends ActionGroup { 49 50 private IWorkbenchSite fSite; 51 52 private BuildAction fBuildAction; 53 private RefreshAction fRefreshAction; 54 55 62 public BuildActionGroup(IViewPart part) { 63 fSite= part.getSite(); 64 Shell shell= fSite.getShell(); 65 ISelectionProvider provider= fSite.getSelectionProvider(); 66 67 fBuildAction= new BuildAction(shell, IncrementalProjectBuilder.INCREMENTAL_BUILD); 68 fBuildAction.setText(ActionMessages.BuildAction_label); 69 fBuildAction.setActionDefinitionId("org.eclipse.ui.project.buildProject"); 71 fRefreshAction= new RefreshAction(fSite); 72 fRefreshAction.setActionDefinitionId("org.eclipse.ui.file.refresh"); 74 provider.addSelectionChangedListener(fBuildAction); 75 provider.addSelectionChangedListener(fRefreshAction); 76 } 77 78 84 public IAction getRefreshAction() { 85 return fRefreshAction; 86 } 87 88 91 public void fillActionBars(IActionBars actionBar) { 92 super.fillActionBars(actionBar); 93 setGlobalActionHandlers(actionBar); 94 } 95 96 99 public void fillContextMenu(IMenuManager menu) { 100 ISelection selection= getContext().getSelection(); 101 if (!ResourcesPlugin.getWorkspace().isAutoBuilding() && isBuildTarget(selection)) { 102 appendToGroup(menu, fBuildAction); 103 } 104 appendToGroup(menu, fRefreshAction); 105 super.fillContextMenu(menu); 106 } 107 108 111 public void dispose() { 112 ISelectionProvider provider= fSite.getSelectionProvider(); 113 provider.removeSelectionChangedListener(fBuildAction); 114 provider.removeSelectionChangedListener(fRefreshAction); 115 super.dispose(); 116 } 117 118 private void setGlobalActionHandlers(IActionBars actionBar) { 119 actionBar.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT.getId(), fBuildAction); 120 actionBar.setGlobalActionHandler(ActionFactory.REFRESH.getId(), fRefreshAction); 121 } 122 123 private void appendToGroup(IMenuManager menu, IAction action) { 124 if (action.isEnabled()) 125 menu.appendToGroup(IContextMenuConstants.GROUP_BUILD, action); 126 } 127 128 private boolean isBuildTarget(ISelection s) { 129 if (!(s instanceof IStructuredSelection)) 130 return false; 131 IStructuredSelection selection= (IStructuredSelection)s; 132 if (selection.size() != 1) 133 return false; 134 return selection.getFirstElement() instanceof IJavaProject; 135 } 136 } 137 | Popular Tags |