001: /*******************************************************************************
002: * Copyright (c) 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.navigator.resources.actions;
011:
012: import java.util.Iterator;
013:
014: import org.eclipse.core.resources.ICommand;
015: import org.eclipse.core.resources.IProject;
016: import org.eclipse.core.resources.IncrementalProjectBuilder;
017: import org.eclipse.core.resources.ResourcesPlugin;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IAdaptable;
020: import org.eclipse.jface.action.IMenuManager;
021: import org.eclipse.jface.resource.ImageDescriptor;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.swt.widgets.Shell;
024: import org.eclipse.ui.IActionBars;
025: import org.eclipse.ui.actions.ActionFactory;
026: import org.eclipse.ui.actions.BuildAction;
027: import org.eclipse.ui.actions.CloseResourceAction;
028: import org.eclipse.ui.actions.CloseUnrelatedProjectsAction;
029: import org.eclipse.ui.actions.OpenResourceAction;
030: import org.eclipse.ui.actions.RefreshAction;
031: import org.eclipse.ui.ide.IDEActionFactory;
032: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
033: import org.eclipse.ui.navigator.CommonActionProvider;
034: import org.eclipse.ui.navigator.ICommonActionExtensionSite;
035: import org.eclipse.ui.navigator.ICommonMenuConstants;
036:
037: /**
038: * @since 3.2
039: *
040: */
041: public class ResourceMgmtActionProvider extends CommonActionProvider {
042:
043: private BuildAction buildAction;
044:
045: private OpenResourceAction openProjectAction;
046:
047: private CloseResourceAction closeProjectAction;
048:
049: private CloseUnrelatedProjectsAction closeUnrelatedProjectsAction;
050:
051: private RefreshAction refreshAction;
052:
053: private Shell shell;
054:
055: /* (non-Javadoc)
056: * @see org.eclipse.ui.navigator.CommonActionProvider#init(org.eclipse.ui.navigator.ICommonActionExtensionSite)
057: */
058: public void init(ICommonActionExtensionSite aSite) {
059: super .init(aSite);
060: shell = aSite.getViewSite().getShell();
061: makeActions();
062: }
063:
064: public void fillActionBars(IActionBars actionBars) {
065: actionBars.setGlobalActionHandler(
066: ActionFactory.REFRESH.getId(), refreshAction);
067: actionBars.setGlobalActionHandler(
068: IDEActionFactory.BUILD_PROJECT.getId(), buildAction);
069: actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT
070: .getId(), openProjectAction);
071: actionBars.setGlobalActionHandler(
072: IDEActionFactory.CLOSE_PROJECT.getId(),
073: closeProjectAction);
074: actionBars.setGlobalActionHandler(
075: IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(),
076: closeUnrelatedProjectsAction);
077: updateActionBars();
078: }
079:
080: /**
081: * Adds the build, open project, close project and refresh resource
082: * actions to the context menu.
083: * <p>
084: * The following conditions apply:
085: * build-only projects selected, auto build disabled, at least one
086: * builder present
087: * open project-only projects selected, at least one closed project
088: * close project-only projects selected, at least one open project
089: * refresh-no closed project selected
090: * </p>
091: * <p>
092: * Both the open project and close project action may be on the menu
093: * at the same time.
094: * </p>
095: * <p>
096: * No disabled action should be on the context menu.
097: * </p>
098: *
099: * @param menu context menu to add actions to
100: */
101: public void fillContextMenu(IMenuManager menu) {
102: IStructuredSelection selection = (IStructuredSelection) getContext()
103: .getSelection();
104: boolean isProjectSelection = true;
105: boolean hasOpenProjects = false;
106: boolean hasClosedProjects = false;
107: boolean hasBuilder = true; // false if any project is closed or does not have builder
108: Iterator resources = selection.iterator();
109:
110: while (resources.hasNext()
111: && (!hasOpenProjects || !hasClosedProjects
112: || 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: // Allow manual incremental build only if auto build is off.
141: buildAction.selectionChanged(selection);
142: menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD,
143: buildAction);
144: }
145: if (!hasClosedProjects) {
146: refreshAction.selectionChanged(selection);
147: menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD,
148: refreshAction);
149: }
150: if (isProjectSelection) {
151: if (hasClosedProjects) {
152: openProjectAction.selectionChanged(selection);
153: menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD,
154: openProjectAction);
155: }
156: if (hasOpenProjects) {
157: closeProjectAction.selectionChanged(selection);
158: menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD,
159: closeProjectAction);
160: closeUnrelatedProjectsAction
161: .selectionChanged(selection);
162: menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD,
163: closeUnrelatedProjectsAction);
164: }
165: }
166: }
167:
168: /**
169: * Returns whether there are builders configured on the given project.
170: *
171: * @return <code>true</code> if it has builders,
172: * <code>false</code> if not, or if this could not be determined
173: */
174: boolean hasBuilder(IProject project) {
175: try {
176: ICommand[] commands = project.getDescription()
177: .getBuildSpec();
178: if (commands.length > 0) {
179: return true;
180: }
181: } catch (CoreException e) {
182: // Cannot determine if project has builders. Project is closed
183: // or does not exist. Fall through to return false.
184: }
185: return false;
186: }
187:
188: protected void makeActions() {
189: //Shell shell = navigator.getSite().getShell();
190: openProjectAction = new OpenResourceAction(shell);
191:
192: closeProjectAction = new CloseResourceAction(shell);
193:
194: closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(
195: shell);
196:
197: refreshAction = new RefreshAction(shell);
198: refreshAction
199: .setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));//$NON-NLS-1$
200: refreshAction
201: .setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));//$NON-NLS-1$
202: refreshAction
203: .setActionDefinitionId("org.eclipse.ui.file.refresh"); //$NON-NLS-1$
204:
205: buildAction = new BuildAction(shell,
206: IncrementalProjectBuilder.INCREMENTAL_BUILD);
207: buildAction
208: .setActionDefinitionId("org.eclipse.ui.project.buildProject"); //$NON-NLS-1$
209: }
210:
211: /**
212: * Returns the image descriptor with the given relative path.
213: */
214: protected ImageDescriptor getImageDescriptor(String relativePath) {
215: return IDEWorkbenchPlugin.getIDEImageDescriptor(relativePath);
216:
217: }
218:
219: public void updateActionBars() {
220: IStructuredSelection selection = (IStructuredSelection) getContext()
221: .getSelection();
222: refreshAction.selectionChanged(selection);
223: buildAction.selectionChanged(selection);
224: openProjectAction.selectionChanged(selection);
225: closeUnrelatedProjectsAction.selectionChanged(selection);
226: closeProjectAction.selectionChanged(selection);
227: }
228:
229: }
|