001: /*******************************************************************************
002: * Copyright (c) 2000, 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: * Sebastian Davids <sdavids@gmx.de> - Images for menu items (27481)
011: *******************************************************************************/package org.eclipse.ui.views.navigator;
012:
013: import java.util.Iterator;
014:
015: import org.eclipse.core.resources.ICommand;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IncrementalProjectBuilder;
018: import org.eclipse.core.resources.ResourcesPlugin;
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IAdaptable;
021: import org.eclipse.jface.action.IMenuManager;
022: import org.eclipse.jface.viewers.IStructuredSelection;
023: import org.eclipse.swt.SWT;
024: import org.eclipse.swt.events.KeyEvent;
025: import org.eclipse.swt.widgets.Shell;
026: import org.eclipse.ui.IActionBars;
027: import org.eclipse.ui.actions.ActionFactory;
028: import org.eclipse.ui.actions.BuildAction;
029: import org.eclipse.ui.actions.CloseResourceAction;
030: import org.eclipse.ui.actions.CloseUnrelatedProjectsAction;
031: import org.eclipse.ui.actions.OpenResourceAction;
032: import org.eclipse.ui.actions.RefreshAction;
033: import org.eclipse.ui.ide.IDEActionFactory;
034:
035: /**
036: * This is the action group for workspace actions such as Build, Refresh Local,
037: * and Open/Close Project.
038: */
039: public class WorkspaceActionGroup extends ResourceNavigatorActionGroup {
040:
041: private BuildAction buildAction;
042:
043: private OpenResourceAction openProjectAction;
044:
045: private CloseResourceAction closeProjectAction;
046:
047: private CloseUnrelatedProjectsAction closeUnrelatedProjectsAction;
048:
049: private RefreshAction refreshAction;
050:
051: public WorkspaceActionGroup(IResourceNavigator navigator) {
052: super (navigator);
053: }
054:
055: public void fillActionBars(IActionBars actionBars) {
056: actionBars.setGlobalActionHandler(
057: ActionFactory.REFRESH.getId(), refreshAction);
058: actionBars.setGlobalActionHandler(
059: IDEActionFactory.BUILD_PROJECT.getId(), buildAction);
060: actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT
061: .getId(), openProjectAction);
062: actionBars.setGlobalActionHandler(
063: IDEActionFactory.CLOSE_PROJECT.getId(),
064: closeProjectAction);
065: actionBars.setGlobalActionHandler(
066: IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(),
067: closeUnrelatedProjectsAction);
068: }
069:
070: /**
071: * Adds the build, open project, close project and refresh resource
072: * actions to the context menu.
073: * <p>
074: * The following conditions apply:
075: * build-only projects selected, auto build disabled, at least one
076: * builder present
077: * open project-only projects selected, at least one closed project
078: * close project-only projects selected, at least one open project
079: * refresh-no closed project selected
080: * </p>
081: * <p>
082: * Both the open project and close project action may be on the menu
083: * at the same time.
084: * </p>
085: * <p>
086: * No disabled action should be on the context menu.
087: * </p>
088: *
089: * @param menu context menu to add actions to
090: */
091: public void fillContextMenu(IMenuManager menu) {
092: IStructuredSelection selection = (IStructuredSelection) getContext()
093: .getSelection();
094: boolean isProjectSelection = true;
095: boolean hasOpenProjects = false;
096: boolean hasClosedProjects = false;
097: boolean hasBuilder = true; // false if any project is closed or does not have builder
098: Iterator resources = selection.iterator();
099:
100: while (resources.hasNext()
101: && (!hasOpenProjects || !hasClosedProjects
102: || hasBuilder || isProjectSelection)) {
103: Object next = resources.next();
104: IProject project = null;
105:
106: if (next instanceof IProject) {
107: project = (IProject) next;
108: } else if (next instanceof IAdaptable) {
109: project = (IProject) ((IAdaptable) next)
110: .getAdapter(IProject.class);
111: }
112:
113: if (project == null) {
114: isProjectSelection = false;
115: continue;
116: }
117: if (project.isOpen()) {
118: hasOpenProjects = true;
119: if (hasBuilder && !hasBuilder(project)) {
120: hasBuilder = false;
121: }
122: } else {
123: hasClosedProjects = true;
124: hasBuilder = false;
125: }
126: }
127: if (!selection.isEmpty() && isProjectSelection
128: && !ResourcesPlugin.getWorkspace().isAutoBuilding()
129: && hasBuilder) {
130: // Allow manual incremental build only if auto build is off.
131: buildAction.selectionChanged(selection);
132: menu.add(buildAction);
133: }
134: if (!hasClosedProjects) {
135: refreshAction.selectionChanged(selection);
136: menu.add(refreshAction);
137: }
138: if (isProjectSelection) {
139: if (hasClosedProjects) {
140: openProjectAction.selectionChanged(selection);
141: menu.add(openProjectAction);
142: }
143: if (hasOpenProjects) {
144: closeProjectAction.selectionChanged(selection);
145: menu.add(closeProjectAction);
146: closeUnrelatedProjectsAction
147: .selectionChanged(selection);
148: menu.add(closeUnrelatedProjectsAction);
149: }
150: }
151: }
152:
153: /**
154: * Handles a key pressed event by invoking the appropriate action.
155: */
156: public void handleKeyPressed(KeyEvent event) {
157: if (event.keyCode == SWT.F5 && event.stateMask == 0) {
158: if (refreshAction.isEnabled()) {
159: refreshAction.refreshAll();
160: }
161:
162: // Swallow the event
163: event.doit = false;
164: }
165: }
166:
167: /**
168: * Returns whether there are builders configured on the given project.
169: *
170: * @return <code>true</code> if it has builders,
171: * <code>false</code> if not, or if this could not be determined
172: */
173: boolean hasBuilder(IProject project) {
174: try {
175: ICommand[] commands = project.getDescription()
176: .getBuildSpec();
177: if (commands.length > 0) {
178: return true;
179: }
180: } catch (CoreException e) {
181: // Cannot determine if project has builders. Project is closed
182: // or does not exist. Fall through to return false.
183: }
184: return false;
185: }
186:
187: protected void makeActions() {
188: Shell shell = navigator.getSite().getShell();
189: openProjectAction = new OpenResourceAction(shell);
190: closeProjectAction = new CloseResourceAction(shell);
191: closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(
192: shell);
193: refreshAction = new RefreshAction(shell);
194: refreshAction
195: .setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));//$NON-NLS-1$
196: refreshAction
197: .setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));//$NON-NLS-1$
198: buildAction = new BuildAction(shell,
199: IncrementalProjectBuilder.INCREMENTAL_BUILD);
200: }
201:
202: public void updateActionBars() {
203: IStructuredSelection selection = (IStructuredSelection) getContext()
204: .getSelection();
205: refreshAction.selectionChanged(selection);
206: buildAction.selectionChanged(selection);
207: openProjectAction.selectionChanged(selection);
208: closeUnrelatedProjectsAction.selectionChanged(selection);
209: closeProjectAction.selectionChanged(selection);
210: }
211: }
|