KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > OpenProjectAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.ui.actions;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.MultiStatus;
22 import org.eclipse.core.runtime.SubProgressMonitor;
23
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResourceChangeEvent;
26 import org.eclipse.core.resources.IResourceChangeListener;
27 import org.eclipse.core.resources.IResourceDelta;
28 import org.eclipse.core.resources.IWorkspaceRunnable;
29 import org.eclipse.core.resources.ResourcesPlugin;
30
31 import org.eclipse.jface.viewers.ArrayContentProvider;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.IStructuredSelection;
34 import org.eclipse.jface.viewers.StructuredSelection;
35 import org.eclipse.jface.window.Window;
36
37 import org.eclipse.ui.IWorkbenchSite;
38 import org.eclipse.ui.IWorkingSet;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.actions.OpenResourceAction;
41 import org.eclipse.ui.dialogs.ListSelectionDialog;
42
43 import org.eclipse.jdt.ui.JavaElementLabelProvider;
44
45 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
46 import org.eclipse.jdt.internal.ui.JavaPlugin;
47 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
48 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
49 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
50
51 /**
52  * Action to open a closed project. Action either opens the closed projects
53  * provided by the structured selection or presents a dialog from which the
54  * user can select the projects to be opened.
55  *
56  * <p>
57  * This class may be instantiated; it is not intended to be subclassed.
58  * </p>
59  *
60  * @since 2.0
61  */

62 public class OpenProjectAction extends SelectionDispatchAction implements IResourceChangeListener {
63     
64     private int CLOSED_PROJECTS_SELECTED= 1;
65     private int OTHER_ELEMENTS_SELECTED= 2;
66     
67     private OpenResourceAction fWorkbenchAction;
68
69     /**
70      * Creates a new <code>OpenProjectAction</code>. The action requires
71      * that the selection provided by the site's selection provider is of type <code>
72      * org.eclipse.jface.viewers.IStructuredSelection</code>.
73      *
74      * @param site the site providing context information for this action
75      */

76     public OpenProjectAction(IWorkbenchSite site) {
77         super(site);
78         fWorkbenchAction= new OpenResourceAction(site.getShell());
79         setText(fWorkbenchAction.getText());
80         setToolTipText(fWorkbenchAction.getToolTipText());
81         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_PROJECT_ACTION);
82         setEnabled(hasClosedProjectsInWorkspace());
83     }
84     
85     /*
86      * @see IResourceChangeListener#resourceChanged(IResourceChangeEvent)
87      */

88     public void resourceChanged(IResourceChangeEvent event) {
89         IResourceDelta delta = event.getDelta();
90         if (delta != null) {
91             IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
92             for (int i = 0; i < projDeltas.length; ++i) {
93                 IResourceDelta projDelta = projDeltas[i];
94                 if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) {
95                     setEnabled(hasClosedProjectsInWorkspace());
96                     return;
97                 }
98             }
99         }
100     }
101     
102     //---- normal selection -------------------------------------
103

104     /* (non-Javadoc)
105      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jface.viewers.ISelection)
106      */

107     public void selectionChanged(ISelection selection) {
108     }
109     
110     /* (non-Javadoc)
111      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.ISelection)
112      */

113     public void run(ISelection selection) {
114         internalRun(null);
115     }
116     
117     private int evaluateSelection(IStructuredSelection selection, List JavaDoc allClosedProjects) {
118         Object JavaDoc[] array= selection.toArray();
119         int selectionStatus = 0;
120         for (int i= 0; i < array.length; i++) {
121             Object JavaDoc curr= array[i];
122             if (isClosedProject(curr)) {
123                 if (allClosedProjects != null)
124                     allClosedProjects.add(curr);
125                 selectionStatus |= CLOSED_PROJECTS_SELECTED;
126             } else {
127                 if (curr instanceof IWorkingSet) {
128                     IAdaptable[] elements= ((IWorkingSet) curr).getElements();
129                     for (int k= 0; k < elements.length; k++) {
130                         Object JavaDoc elem= elements[k];
131                         if (isClosedProject(elem)) {
132                             if (allClosedProjects != null)
133                                 allClosedProjects.add(elem);
134                             selectionStatus |= CLOSED_PROJECTS_SELECTED;
135                         }
136                     }
137                 }
138                 selectionStatus |= OTHER_ELEMENTS_SELECTED;
139             }
140
141         }
142         return selectionStatus;
143     }
144     
145     private static boolean isClosedProject(Object JavaDoc element) {
146         // assume all closed project are rendered as IProject
147
return element instanceof IProject && !((IProject) element).isOpen();
148     }
149     
150     
151     //---- structured selection ---------------------------------------
152

153     /* (non-Javadoc)
154      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#run(org.eclipse.jface.viewers.IStructuredSelection)
155      */

156     public void run(IStructuredSelection selection) {
157         ArrayList JavaDoc allClosedProjects= new ArrayList JavaDoc();
158         int selectionStatus= evaluateSelection(selection, allClosedProjects);
159         if (selectionStatus == CLOSED_PROJECTS_SELECTED) { // only closed projects selected
160
fWorkbenchAction.selectionChanged(new StructuredSelection(allClosedProjects));
161             fWorkbenchAction.run();
162         } else {
163             internalRun(allClosedProjects);
164         }
165     }
166     
167     private void internalRun(List JavaDoc initialSelection) {
168         ListSelectionDialog dialog= new ListSelectionDialog(getShell(), getClosedProjectsInWorkspace(), new ArrayContentProvider(), new JavaElementLabelProvider(), ActionMessages.OpenProjectAction_dialog_message);
169         dialog.setTitle(ActionMessages.OpenProjectAction_dialog_title);
170         if (initialSelection != null && !initialSelection.isEmpty()) {
171             dialog.setInitialElementSelections(initialSelection);
172         }
173         int result= dialog.open();
174         if (result != Window.OK)
175             return;
176         final Object JavaDoc[] projects= dialog.getResult();
177         IWorkspaceRunnable runnable= createRunnable(projects);
178         try {
179             PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(runnable));
180         } catch (InvocationTargetException JavaDoc e) {
181             ExceptionHandler.handle(e, getShell(), ActionMessages.OpenProjectAction_dialog_title, ActionMessages.OpenProjectAction_error_message);
182         } catch (InterruptedException JavaDoc e) {
183             // user cancelled
184
}
185     }
186     
187     private IWorkspaceRunnable createRunnable(final Object JavaDoc[] projects) {
188         return new IWorkspaceRunnable() {
189             public void run(IProgressMonitor monitor) throws CoreException {
190                 monitor.beginTask("", projects.length); //$NON-NLS-1$
191
MultiStatus errorStatus= null;
192                 for (int i = 0; i < projects.length; i++) {
193                     IProject project= (IProject)projects[i];
194                     try {
195                         project.open(new SubProgressMonitor(monitor, 1));
196                     } catch (CoreException e) {
197                         if (errorStatus == null)
198                             errorStatus = new MultiStatus(JavaPlugin.getPluginId(), IStatus.ERROR, ActionMessages.OpenProjectAction_error_message, null);
199                         errorStatus.add(e.getStatus());
200                     }
201                 }
202                 monitor.done();
203                 if (errorStatus != null)
204                     throw new CoreException(errorStatus);
205             }
206         };
207     }
208     
209     private Object JavaDoc[] getClosedProjectsInWorkspace() {
210         IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
211         List JavaDoc result= new ArrayList JavaDoc(5);
212         for (int i = 0; i < projects.length; i++) {
213             IProject project= projects[i];
214             if (!project.isOpen())
215                 result.add(project);
216         }
217         return result.toArray();
218     }
219     
220     private boolean hasClosedProjectsInWorkspace() {
221         IProject[] projects= ResourcesPlugin.getWorkspace().getRoot().getProjects();
222         for (int i = 0; i < projects.length; i++) {
223             if (!projects[i].isOpen())
224                 return true;
225         }
226         return false;
227     }
228 }
229
Popular Tags