KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > actions > OpenResourceAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IResourceChangeEvent;
19 import org.eclipse.core.resources.IResourceChangeListener;
20 import org.eclipse.core.resources.IResourceDelta;
21 import org.eclipse.core.resources.IResourceRuleFactory;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.resources.WorkspaceJob;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.OperationCanceledException;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.core.runtime.SubProgressMonitor;
30 import org.eclipse.core.runtime.jobs.ISchedulingRule;
31 import org.eclipse.core.runtime.jobs.Job;
32 import org.eclipse.core.runtime.jobs.MultiRule;
33 import org.eclipse.jface.dialogs.IDialogConstants;
34 import org.eclipse.jface.dialogs.MessageDialogWithToggle;
35 import org.eclipse.jface.preference.IPreferenceStore;
36 import org.eclipse.jface.viewers.IStructuredSelection;
37 import org.eclipse.jface.window.Window;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.internal.ide.IDEInternalPreferences;
41 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
42 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
43 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
44
45 /**
46  * Standard action for opening the currently selected project(s).
47  * <p>
48  * Note that there is a different action for opening an editor on file
49  * resources: <code>OpenFileAction</code>.
50  * </p>
51  * <p>
52  * This class may be instantiated; it is not intended to be subclassed.
53  * </p>
54  */

55 public class OpenResourceAction extends WorkspaceAction implements IResourceChangeListener {
56
57     /**
58      * The id of this action.
59      */

60     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".OpenResourceAction"; //$NON-NLS-1$
61

62     /**
63      * Creates a new action.
64      *
65      * @param shell
66      * the shell for any dialogs
67      */

68     public OpenResourceAction(Shell shell) {
69         super(shell, IDEWorkbenchMessages.OpenResourceAction_text);
70         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IIDEHelpContextIds.OPEN_RESOURCE_ACTION);
71         setToolTipText(IDEWorkbenchMessages.OpenResourceAction_toolTip);
72         setId(ID);
73     }
74
75     /**
76      * Returns the total number of closed projects in the workspace.
77      */

78     private int countClosedProjects() {
79         int count = 0;
80         IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
81         for (int i = 0; i < projects.length; i++) {
82             if (!projects[i].isOpen()) {
83                 count++;
84             }
85         }
86         return count;
87     }
88
89     /*
90      * (non-Javadoc) Method declared on WorkspaceAction.
91      */

92     protected String JavaDoc getOperationMessage() {
93         return IDEWorkbenchMessages.OpenResourceAction_operationMessage;
94     }
95
96     /*
97      * (non-Javadoc) Method declared on WorkspaceAction.
98      */

99     protected String JavaDoc getProblemsMessage() {
100         return IDEWorkbenchMessages.OpenResourceAction_problemMessage;
101     }
102
103     /*
104      * (non-Javadoc) Method declared on WorkspaceAction.
105      */

106     protected String JavaDoc getProblemsTitle() {
107         return IDEWorkbenchMessages.OpenResourceAction_dialogTitle;
108     }
109
110     /**
111      * Returns whether there are closed projects in the workspace that are
112      * not part of the current selection.
113      */

114     private boolean hasOtherClosedProjects() {
115         //count the closed projects in the selection
116
int closedInSelection = 0;
117         Iterator JavaDoc resources = getSelectedResources().iterator();
118         while (resources.hasNext()) {
119             IProject project = (IProject) resources.next();
120             if (!project.isOpen())
121                 closedInSelection++;
122         }
123         //there are other closed projects if the selection does
124
//not contain all closed projects in the workspace
125
return closedInSelection < countClosedProjects();
126     }
127
128     protected void invokeOperation(IResource resource, IProgressMonitor monitor) throws CoreException {
129         ((IProject) resource).open(monitor);
130     }
131
132     /**
133      * Returns the preference for whether to open required projects when opening
134      * a project. Consults the preference and prompts the user if necessary.
135      *
136      * @return <code>true</code> if referenced projects should be opened, and
137      * <code>false</code> otherwise.
138      */

139     private boolean promptToOpenWithReferences() {
140         IPreferenceStore store = IDEWorkbenchPlugin.getDefault().getPreferenceStore();
141         String JavaDoc key = IDEInternalPreferences.OPEN_REQUIRED_PROJECTS;
142         String JavaDoc value = store.getString(key);
143         if (MessageDialogWithToggle.ALWAYS.equals(value)) {
144             return true;
145         }
146         if (MessageDialogWithToggle.NEVER.equals(value)) {
147             return false;
148         }
149         String JavaDoc message = IDEWorkbenchMessages.OpenResourceAction_openRequiredProjects;
150         MessageDialogWithToggle dialog = MessageDialogWithToggle.openYesNoCancelQuestion(getShell(), IDEWorkbenchMessages.Question, message, null, false, store, key);
151         int result = dialog.getReturnCode();
152         if (result == Window.CANCEL) {
153             throw new OperationCanceledException();
154         }
155         return dialog.getReturnCode() == IDialogConstants.YES_ID;
156     }
157
158     /**
159      * Handles a resource changed event by updating the enablement if one of the
160      * selected projects is opened or closed.
161      */

162     public void resourceChanged(IResourceChangeEvent event) {
163         // Warning: code duplicated in CloseResourceAction
164
List JavaDoc sel = getSelectedResources();
165         // don't bother looking at delta if selection not applicable
166
if (selectionIsOfType(IResource.PROJECT)) {
167             IResourceDelta delta = event.getDelta();
168             if (delta != null) {
169                 IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
170                 for (int i = 0; i < projDeltas.length; ++i) {
171                     IResourceDelta projDelta = projDeltas[i];
172                     if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) {
173                         if (sel.contains(projDelta.getResource())) {
174                             selectionChanged(getStructuredSelection());
175                             return;
176                         }
177                     }
178                 }
179             }
180         }
181     }
182
183     /*
184      * (non-Javadoc) Method declared on IAction; overrides method on
185      * WorkspaceAction.
186      */

187     public void run() {
188         try {
189             if (hasOtherClosedProjects() && promptToOpenWithReferences()) {
190                 runOpenWithReferences();
191             }
192             ISchedulingRule rule = null;
193             // be conservative and include all projects in the selection - projects
194
// can change state between now and when the job starts
195
IResourceRuleFactory factory = ResourcesPlugin.getWorkspace().getRuleFactory();
196             Iterator JavaDoc resources = getSelectedResources().iterator();
197             while (resources.hasNext()) {
198                 IProject project = (IProject) resources.next();
199                 rule = MultiRule.combine(rule, factory.modifyRule(project));
200             }
201             runInBackground(rule);
202         } catch (OperationCanceledException e) {
203             //just return when canceled
204
}
205     }
206
207     /**
208      * Opens the selected projects, and all related projects, in the background.
209      */

210     private void runOpenWithReferences() {
211         final List JavaDoc resources = new ArrayList JavaDoc(getActionResources());
212         Job job = new WorkspaceJob(removeMnemonics(getText())) {
213
214             /**
215              * Opens a project along with all projects it references
216              */

217             private void doOpenWithReferences(IProject project, IProgressMonitor monitor) throws CoreException {
218                 if (!project.exists() || project.isOpen()) {
219                     return;
220                 }
221                 project.open(new SubProgressMonitor(monitor, 1000));
222                 IProject[] references = project.getReferencedProjects();
223                 for (int i = 0; i < references.length; i++) {
224                     doOpenWithReferences(references[i], monitor);
225                 }
226             }
227
228             public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
229                 try {
230                     // at most we can only open all projects currently closed
231
monitor.beginTask("", countClosedProjects() * 1000); //$NON-NLS-1$
232
monitor.setTaskName(getOperationMessage());
233                     for (Iterator JavaDoc it = resources.iterator(); it.hasNext();) {
234                         doOpenWithReferences((IProject) it.next(), monitor);
235                     }
236                 } finally {
237                     monitor.done();
238                 }
239                 return Status.OK_STATUS;
240             }
241         };
242         job.setRule(ResourcesPlugin.getWorkspace().getRoot());
243         job.setUser(true);
244         job.schedule();
245     }
246
247     /*
248      * (non-Javadoc) Method declared on WorkspaceAction.
249      */

250     protected boolean shouldPerformResourcePruning() {
251         return false;
252     }
253
254     /**
255      * The <code>OpenResourceAction</code> implementation of this
256      * <code>SelectionListenerAction</code> method ensures that this action is
257      * enabled only if one of the selections is a closed project.
258      */

259     protected boolean updateSelection(IStructuredSelection s) {
260         // don't call super since we want to enable if closed project is
261
// selected.
262

263         if (!selectionIsOfType(IResource.PROJECT)) {
264             return false;
265         }
266
267         Iterator JavaDoc resources = getSelectedResources().iterator();
268         while (resources.hasNext()) {
269             IProject currentResource = (IProject) resources.next();
270             if (!currentResource.isOpen()) {
271                 return true;
272             }
273         }
274         return false;
275     }
276 }
277
Popular Tags