Removed
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2006, 2009 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 |
|
12 |
package org.eclipse.ui.internal.navigator.resources.actions; |
13 |
|
14 |
import java.lang.reflect.InvocationTargetException; |
15 |
import java.util.Iterator; |
16 |
|
17 |
import org.eclipse.osgi.util.NLS; |
18 |
|
19 |
import org.eclipse.swt.widgets.Shell; |
20 |
|
21 |
import org.eclipse.core.runtime.CoreException; |
22 |
import org.eclipse.core.runtime.IAdaptable; |
23 |
import org.eclipse.core.runtime.IProgressMonitor; |
24 |
import org.eclipse.core.runtime.IStatus; |
25 |
import org.eclipse.core.runtime.Status; |
26 |
import org.eclipse.core.runtime.jobs.ISchedulingRule; |
27 |
|
28 |
import org.eclipse.core.resources.ICommand; |
29 |
import org.eclipse.core.resources.IProject; |
30 |
import org.eclipse.core.resources.IncrementalProjectBuilder; |
31 |
import org.eclipse.core.resources.ResourcesPlugin; |
32 |
import org.eclipse.core.resources.WorkspaceJob; |
33 |
|
34 |
import org.eclipse.jface.action.IMenuManager; |
35 |
import org.eclipse.jface.resource.ImageDescriptor; |
36 |
import org.eclipse.jface.viewers.IStructuredSelection; |
37 |
import org.eclipse.jface.viewers.StructuredViewer; |
38 |
import org.eclipse.jface.window.IShellProvider; |
39 |
|
40 |
import org.eclipse.ui.IActionBars; |
41 |
import org.eclipse.ui.IWorkbenchCommandConstants; |
42 |
import org.eclipse.ui.actions.ActionFactory; |
43 |
import org.eclipse.ui.actions.BuildAction; |
44 |
import org.eclipse.ui.actions.CloseResourceAction; |
45 |
import org.eclipse.ui.actions.CloseUnrelatedProjectsAction; |
46 |
import org.eclipse.ui.actions.OpenResourceAction; |
47 |
import org.eclipse.ui.actions.RefreshAction; |
48 |
import org.eclipse.ui.actions.WorkspaceModifyOperation; |
49 |
import org.eclipse.ui.ide.IDEActionFactory; |
50 |
import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; |
51 |
import org.eclipse.ui.internal.navigator.NavigatorPlugin; |
52 |
import org.eclipse.ui.internal.navigator.resources.plugin.WorkbenchNavigatorMessages; |
53 |
import org.eclipse.ui.navigator.CommonActionProvider; |
54 |
import org.eclipse.ui.navigator.ICommonActionExtensionSite; |
55 |
import org.eclipse.ui.navigator.ICommonMenuConstants; |
56 |
|
57 |
/** |
58 |
* @since 3.2 |
59 |
* |
60 |
*/ |
61 |
public class ResourceMgmtActionProvider extends CommonActionProvider { |
62 |
|
63 |
private BuildAction buildAction; |
64 |
|
65 |
private OpenResourceAction openProjectAction; |
66 |
|
67 |
private CloseResourceAction closeProjectAction; |
68 |
|
69 |
private CloseUnrelatedProjectsAction closeUnrelatedProjectsAction; |
70 |
|
71 |
private RefreshAction refreshAction; |
72 |
|
73 |
private Shell shell; |
74 |
|
75 |
/* |
76 |
* (non-Javadoc) |
77 |
* |
78 |
* @see |
79 |
* org.eclipse.ui.navigator.CommonActionProvider#init(org.eclipse.ui.navigator |
80 |
* .ICommonActionExtensionSite) |
81 |
*/ |
82 |
public void init(ICommonActionExtensionSite aSite) { |
83 |
super.init(aSite); |
84 |
shell = aSite.getViewSite().getShell(); |
85 |
makeActions(); |
86 |
} |
87 |
|
88 |
public void fillActionBars(IActionBars actionBars) { |
89 |
actionBars.setGlobalActionHandler(ActionFactory.REFRESH.getId(), refreshAction); |
90 |
actionBars.setGlobalActionHandler(IDEActionFactory.BUILD_PROJECT.getId(), buildAction); |
91 |
actionBars.setGlobalActionHandler(IDEActionFactory.OPEN_PROJECT.getId(), openProjectAction); |
92 |
actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_PROJECT.getId(), closeProjectAction); |
93 |
actionBars.setGlobalActionHandler(IDEActionFactory.CLOSE_UNRELATED_PROJECTS.getId(), closeUnrelatedProjectsAction); |
94 |
updateActionBars(); |
95 |
} |
96 |
|
97 |
/** |
98 |
* Adds the build, open project, close project and refresh resource actions |
99 |
* to the context menu. |
100 |
* <p> |
101 |
* The following conditions apply: build-only projects selected, auto build |
102 |
* disabled, at least one builder present open project-only projects |
103 |
* selected, at least one closed project close project-only projects |
104 |
* selected, at least one open project refresh-no closed project selected |
105 |
* </p> |
106 |
* <p> |
107 |
* Both the open project and close project action may be on the menu at the |
108 |
* same time. |
109 |
* </p> |
110 |
* <p> |
111 |
* No disabled action should be on the context menu. |
112 |
* </p> |
113 |
* |
114 |
* @param menu |
115 |
* context menu to add actions to |
116 |
*/ |
117 |
public void fillContextMenu(IMenuManager menu) { |
118 |
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection(); |
119 |
boolean isProjectSelection = true; |
120 |
boolean hasOpenProjects = false; |
121 |
boolean hasClosedProjects = false; |
122 |
boolean hasBuilder = true; // false if any project is closed or does not |
123 |
// have builder |
124 |
Iterator resources = selection.iterator(); |
125 |
|
126 |
while (resources.hasNext() && (!hasOpenProjects || !hasClosedProjects || hasBuilder || isProjectSelection)) { |
127 |
Object next = resources.next(); |
128 |
IProject project = null; |
129 |
|
130 |
if (next instanceof IProject) { |
131 |
project = (IProject) next; |
132 |
} else if (next instanceof IAdaptable) { |
133 |
project = (IProject) ((IAdaptable) next).getAdapter(IProject.class); |
134 |
} |
135 |
|
136 |
if (project == null) { |
137 |
isProjectSelection = false; |
138 |
continue; |
139 |
} |
140 |
if (project.isOpen()) { |
141 |
hasOpenProjects = true; |
142 |
if (hasBuilder && !hasBuilder(project)) { |
143 |
hasBuilder = false; |
144 |
} |
145 |
} else { |
146 |
hasClosedProjects = true; |
147 |
hasBuilder = false; |
148 |
} |
149 |
} |
150 |
if (!selection.isEmpty() && isProjectSelection && !ResourcesPlugin.getWorkspace().isAutoBuilding() && hasBuilder) { |
151 |
// Allow manual incremental build only if auto build is off. |
152 |
buildAction.selectionChanged(selection); |
153 |
menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, buildAction); |
154 |
} |
155 |
if (!hasClosedProjects) { |
156 |
refreshAction.selectionChanged(selection); |
157 |
menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, refreshAction); |
158 |
} |
159 |
if (isProjectSelection) { |
160 |
if (hasClosedProjects) { |
161 |
openProjectAction.selectionChanged(selection); |
162 |
menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, openProjectAction); |
163 |
} |
164 |
if (hasOpenProjects) { |
165 |
closeProjectAction.selectionChanged(selection); |
166 |
menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, closeProjectAction); |
167 |
closeUnrelatedProjectsAction.selectionChanged(selection); |
168 |
menu.appendToGroup(ICommonMenuConstants.GROUP_BUILD, closeUnrelatedProjectsAction); |
169 |
} |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* Returns whether there are builders configured on the given project. |
175 |
* |
176 |
* @return <code>true</code> if it has builders, <code>false</code> if not, |
177 |
* or if this could not be determined |
178 |
*/ |
179 |
boolean hasBuilder(IProject project) { |
180 |
try { |
181 |
ICommand[] commands = project.getDescription().getBuildSpec(); |
182 |
if (commands.length > 0) { |
183 |
return true; |
184 |
} |
185 |
} catch (CoreException e) { |
186 |
// Cannot determine if project has builders. Project is closed |
187 |
// or does not exist. Fall through to return false. |
188 |
} |
189 |
return false; |
190 |
} |
191 |
|
192 |
protected void makeActions() { |
193 |
IShellProvider sp = new IShellProvider() { |
194 |
public Shell getShell() { |
195 |
return shell; |
196 |
} |
197 |
}; |
198 |
|
199 |
openProjectAction = new OpenResourceAction(sp); |
200 |
|
201 |
closeProjectAction = new CloseResourceAction(sp); |
202 |
|
203 |
closeUnrelatedProjectsAction = new CloseUnrelatedProjectsAction(sp); |
204 |
|
205 |
refreshAction = new RefreshAction(sp) { |
206 |
public void run() { |
207 |
final IStatus[] errorStatus = new IStatus[1]; |
208 |
errorStatus[0] = Status.OK_STATUS; |
209 |
final WorkspaceModifyOperation op = (WorkspaceModifyOperation) createOperation(errorStatus); |
210 |
WorkspaceJob job = new WorkspaceJob("refresh") { //$NON-NLS-1$ |
211 |
|
212 |
public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { |
213 |
try { |
214 |
op.run(monitor); |
215 |
if (shell != null && !shell.isDisposed()) { |
216 |
shell.getDisplay().asyncExec(new Runnable() { |
217 |
public void run() { |
218 |
StructuredViewer viewer = getActionSite().getStructuredViewer(); |
219 |
if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed()) { |
220 |
viewer.refresh(); |
221 |
} |
222 |
} |
223 |
}); |
224 |
} |
225 |
} catch (InvocationTargetException e) { |
226 |
String msg = NLS.bind(WorkbenchNavigatorMessages.ResourceMgmtActionProvider_logTitle, getClass().getName(), e.getTargetException()); |
227 |
throw new CoreException(new Status(IStatus.ERROR, NavigatorPlugin.PLUGIN_ID, IStatus.ERROR, msg, e.getTargetException())); |
228 |
} catch (InterruptedException e) { |
229 |
return Status.CANCEL_STATUS; |
230 |
} |
231 |
return errorStatus[0]; |
232 |
} |
233 |
|
234 |
}; |
235 |
ISchedulingRule rule = op.getRule(); |
236 |
if (rule != null) { |
237 |
job.setRule(rule); |
238 |
} |
239 |
job.setUser(true); |
240 |
job.schedule(); |
241 |
} |
242 |
}; |
243 |
refreshAction.setDisabledImageDescriptor(getImageDescriptor("dlcl16/refresh_nav.gif"));//$NON-NLS-1$ |
244 |
refreshAction.setImageDescriptor(getImageDescriptor("elcl16/refresh_nav.gif"));//$NON-NLS-1$ |
245 |
refreshAction.setActionDefinitionId(IWorkbenchCommandConstants.FILE_REFRESH); |
246 |
|
247 |
buildAction = new BuildAction(sp, IncrementalProjectBuilder.INCREMENTAL_BUILD); |
248 |
buildAction.setActionDefinitionId(IWorkbenchCommandConstants.PROJECT_BUILD_PROJECT); |
249 |
} |
250 |
|
251 |
/** |
252 |
* Returns the image descriptor with the given relative path. |
253 |
*/ |
254 |
protected ImageDescriptor getImageDescriptor(String relativePath) { |
255 |
return IDEWorkbenchPlugin.getIDEImageDescriptor(relativePath); |
256 |
|
257 |
} |
258 |
|
259 |
public void updateActionBars() { |
260 |
IStructuredSelection selection = (IStructuredSelection) getContext().getSelection(); |
261 |
refreshAction.selectionChanged(selection); |
262 |
buildAction.selectionChanged(selection); |
263 |
openProjectAction.selectionChanged(selection); |
264 |
closeUnrelatedProjectsAction.selectionChanged(selection); |
265 |
closeProjectAction.selectionChanged(selection); |
266 |
} |
267 |
|
268 |
} |