001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.jdt.ui.actions;
011:
012: import java.lang.reflect.InvocationTargetException;
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IAdaptable;
018: import org.eclipse.core.runtime.IProgressMonitor;
019: import org.eclipse.core.runtime.IStatus;
020: import org.eclipse.core.runtime.MultiStatus;
021: import org.eclipse.core.runtime.SubProgressMonitor;
022:
023: import org.eclipse.core.resources.IProject;
024: import org.eclipse.core.resources.IResourceChangeEvent;
025: import org.eclipse.core.resources.IResourceChangeListener;
026: import org.eclipse.core.resources.IResourceDelta;
027: import org.eclipse.core.resources.IWorkspaceRunnable;
028: import org.eclipse.core.resources.ResourcesPlugin;
029:
030: import org.eclipse.jface.viewers.ArrayContentProvider;
031: import org.eclipse.jface.viewers.ISelection;
032: import org.eclipse.jface.viewers.IStructuredSelection;
033: import org.eclipse.jface.viewers.StructuredSelection;
034: import org.eclipse.jface.window.Window;
035:
036: import org.eclipse.ui.IWorkbenchSite;
037: import org.eclipse.ui.IWorkingSet;
038: import org.eclipse.ui.PlatformUI;
039: import org.eclipse.ui.actions.OpenResourceAction;
040: import org.eclipse.ui.dialogs.ListSelectionDialog;
041:
042: import org.eclipse.jdt.ui.JavaElementLabelProvider;
043:
044: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
045: import org.eclipse.jdt.internal.ui.JavaPlugin;
046: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
047: import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
048: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
049:
050: /**
051: * Action to open a closed project. Action either opens the closed projects
052: * provided by the structured selection or presents a dialog from which the
053: * user can select the projects to be opened.
054: *
055: * <p>
056: * This class may be instantiated; it is not intended to be subclassed.
057: * </p>
058: *
059: * @since 2.0
060: */
061: public class OpenProjectAction extends SelectionDispatchAction
062: implements IResourceChangeListener {
063:
064: private int CLOSED_PROJECTS_SELECTED = 1;
065: private int OTHER_ELEMENTS_SELECTED = 2;
066:
067: private OpenResourceAction fWorkbenchAction;
068:
069: /**
070: * Creates a new <code>OpenProjectAction</code>. The action requires
071: * that the selection provided by the site's selection provider is of type <code>
072: * org.eclipse.jface.viewers.IStructuredSelection</code>.
073: *
074: * @param site the site providing context information for this action
075: */
076: public OpenProjectAction(IWorkbenchSite site) {
077: super (site);
078: fWorkbenchAction = new OpenResourceAction(site.getShell());
079: setText(fWorkbenchAction.getText());
080: setToolTipText(fWorkbenchAction.getToolTipText());
081: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
082: IJavaHelpContextIds.OPEN_PROJECT_ACTION);
083: setEnabled(hasClosedProjectsInWorkspace());
084: }
085:
086: /*
087: * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
088: */
089: public void resourceChanged(IResourceChangeEvent event) {
090: IResourceDelta delta = event.getDelta();
091: if (delta != null) {
092: IResourceDelta[] projDeltas = delta
093: .getAffectedChildren(IResourceDelta.CHANGED);
094: for (int i = 0; i < projDeltas.length; ++i) {
095: IResourceDelta projDelta = projDeltas[i];
096: if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) {
097: setEnabled(hasClosedProjectsInWorkspace());
098: return;
099: }
100: }
101: }
102: }
103:
104: //---- normal selection -------------------------------------
105:
106: /* (non-Javadoc)
107: * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
108: */
109: public void selectionChanged(ISelection selection) {
110: }
111:
112: /* (non-Javadoc)
113: * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.ISelection)
114: */
115: public void run(ISelection selection) {
116: internalRun(null);
117: }
118:
119: private int evaluateSelection(IStructuredSelection selection,
120: List allClosedProjects) {
121: Object[] array = selection.toArray();
122: int selectionStatus = 0;
123: for (int i = 0; i < array.length; i++) {
124: Object curr = array[i];
125: if (isClosedProject(curr)) {
126: if (allClosedProjects != null)
127: allClosedProjects.add(curr);
128: selectionStatus |= CLOSED_PROJECTS_SELECTED;
129: } else {
130: if (curr instanceof IWorkingSet) {
131: IAdaptable[] elements = ((IWorkingSet) curr)
132: .getElements();
133: for (int k = 0; k < elements.length; k++) {
134: Object elem = elements[k];
135: if (isClosedProject(elem)) {
136: if (allClosedProjects != null)
137: allClosedProjects.add(elem);
138: selectionStatus |= CLOSED_PROJECTS_SELECTED;
139: }
140: }
141: }
142: selectionStatus |= OTHER_ELEMENTS_SELECTED;
143: }
144:
145: }
146: return selectionStatus;
147: }
148:
149: private static boolean isClosedProject(Object element) {
150: // assume all closed project are rendered as IProject
151: return element instanceof IProject
152: && !((IProject) element).isOpen();
153: }
154:
155: //---- structured selection ---------------------------------------
156:
157: /* (non-Javadoc)
158: * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
159: */
160: public void run(IStructuredSelection selection) {
161: ArrayList allClosedProjects = new ArrayList();
162: int selectionStatus = evaluateSelection(selection,
163: allClosedProjects);
164: if (selectionStatus == CLOSED_PROJECTS_SELECTED) { // only closed projects selected
165: fWorkbenchAction.selectionChanged(new StructuredSelection(
166: allClosedProjects));
167: fWorkbenchAction.run();
168: } else {
169: internalRun(allClosedProjects);
170: }
171: }
172:
173: private void internalRun(List initialSelection) {
174: ListSelectionDialog dialog = new ListSelectionDialog(
175: getShell(), getClosedProjectsInWorkspace(),
176: new ArrayContentProvider(),
177: new JavaElementLabelProvider(),
178: ActionMessages.OpenProjectAction_dialog_message);
179: dialog.setTitle(ActionMessages.OpenProjectAction_dialog_title);
180: if (initialSelection != null && !initialSelection.isEmpty()) {
181: dialog.setInitialElementSelections(initialSelection);
182: }
183: int result = dialog.open();
184: if (result != Window.OK)
185: return;
186: final Object[] projects = dialog.getResult();
187: IWorkspaceRunnable runnable = createRunnable(projects);
188: try {
189: PlatformUI.getWorkbench().getProgressService().run(true,
190: true, new WorkbenchRunnableAdapter(runnable));
191: } catch (InvocationTargetException e) {
192: ExceptionHandler.handle(e, getShell(),
193: ActionMessages.OpenProjectAction_dialog_title,
194: ActionMessages.OpenProjectAction_error_message);
195: } catch (InterruptedException e) {
196: // user cancelled
197: }
198: }
199:
200: private IWorkspaceRunnable createRunnable(final Object[] projects) {
201: return new IWorkspaceRunnable() {
202: public void run(IProgressMonitor monitor)
203: throws CoreException {
204: monitor.beginTask("", projects.length); //$NON-NLS-1$
205: MultiStatus errorStatus = null;
206: for (int i = 0; i < projects.length; i++) {
207: IProject project = (IProject) projects[i];
208: try {
209: project
210: .open(new SubProgressMonitor(monitor, 1));
211: } catch (CoreException e) {
212: if (errorStatus == null)
213: errorStatus = new MultiStatus(
214: JavaPlugin.getPluginId(),
215: IStatus.ERROR,
216: ActionMessages.OpenProjectAction_error_message,
217: null);
218: errorStatus.add(e.getStatus());
219: }
220: }
221: monitor.done();
222: if (errorStatus != null)
223: throw new CoreException(errorStatus);
224: }
225: };
226: }
227:
228: private Object[] getClosedProjectsInWorkspace() {
229: IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
230: .getProjects();
231: List result = new ArrayList(5);
232: for (int i = 0; i < projects.length; i++) {
233: IProject project = projects[i];
234: if (!project.isOpen())
235: result.add(project);
236: }
237: return result.toArray();
238: }
239:
240: private boolean hasClosedProjectsInWorkspace() {
241: IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
242: .getProjects();
243: for (int i = 0; i < projects.length; i++) {
244: if (!projects[i].isOpen())
245: return true;
246: }
247: return false;
248: }
249: }
|