]>
Commit | Line | Data |
---|---|---|
1b2798f6 EK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2000, 2011 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; | |
14 | import java.util.ArrayList; | |
15 | import java.util.Arrays; | |
16 | import java.util.Collections; | |
17 | import java.util.Iterator; | |
18 | import java.util.List; | |
19 | ||
20 | import org.eclipse.core.runtime.CoreException; | |
21 | import org.eclipse.core.runtime.IAdaptable; | |
22 | import org.eclipse.core.runtime.IProgressMonitor; | |
23 | import org.eclipse.core.runtime.IStatus; | |
24 | import org.eclipse.core.runtime.OperationCanceledException; | |
25 | import org.eclipse.core.runtime.Status; | |
26 | import org.eclipse.core.runtime.SubProgressMonitor; | |
27 | ||
28 | import org.eclipse.core.resources.IProject; | |
29 | import org.eclipse.core.resources.IResource; | |
30 | import org.eclipse.core.resources.IWorkspaceRoot; | |
31 | import org.eclipse.core.resources.IWorkspaceRunnable; | |
32 | import org.eclipse.core.resources.ResourcesPlugin; | |
33 | ||
34 | import org.eclipse.jface.viewers.IStructuredSelection; | |
35 | import org.eclipse.jface.window.IShellProvider; | |
36 | ||
37 | import org.eclipse.ui.IWorkbenchSite; | |
38 | import org.eclipse.ui.IWorkingSet; | |
39 | import org.eclipse.ui.PlatformUI; | |
40 | ||
41 | import org.eclipse.jdt.core.IJavaElement; | |
42 | import org.eclipse.jdt.core.IJavaModel; | |
43 | import org.eclipse.jdt.core.IPackageFragmentRoot; | |
44 | import org.eclipse.jdt.core.JavaCore; | |
45 | import org.eclipse.jdt.core.JavaModelException; | |
46 | ||
47 | import org.eclipse.jdt.ui.JavaUI; | |
48 | ||
49 | import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; | |
50 | import org.eclipse.jdt.internal.ui.JavaPluginImages; | |
51 | import org.eclipse.jdt.internal.ui.actions.ActionMessages; | |
52 | import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter; | |
53 | import org.eclipse.jdt.internal.ui.packageview.PackageFragmentRootContainer; | |
54 | ||
55 | /** | |
56 | * Action for refreshing the workspace from the local file system for | |
57 | * the selected resources and all of their descendants. This action | |
58 | * also considers external Jars managed by the Java Model. | |
59 | * <p> | |
60 | * Action is applicable to selections containing resources and Java | |
61 | * elements down to compilation units. | |
62 | * | |
63 | * <p> | |
64 | * This class may be instantiated; it is not intended to be subclassed. | |
65 | * </p> | |
66 | * | |
67 | * @since 2.0 | |
68 | * | |
69 | * @noextend This class is not intended to be subclassed by clients. | |
70 | */ | |
71 | public class RefreshAction extends SelectionDispatchAction { | |
72 | ||
73 | /** | |
74 | * As the JDT RefreshAction is already API, we have to wrap the workbench action. | |
75 | */ | |
76 | private static class WrappedWorkbenchRefreshAction extends org.eclipse.ui.actions.RefreshAction { | |
77 | ||
78 | public WrappedWorkbenchRefreshAction(IShellProvider provider) { | |
79 | super(provider); | |
80 | } | |
81 | ||
82 | @Override | |
83 | protected List<IResource> getSelectedResources() { | |
84 | List<IResource> selectedResources= super.getSelectedResources(); | |
85 | if (!getStructuredSelection().isEmpty() && selectedResources.size() == 1 && selectedResources.get(0) instanceof IWorkspaceRoot) { | |
86 | selectedResources= Collections.emptyList(); // Refresh action refreshes root when it can't find any resources in selection | |
87 | } | |
88 | ||
89 | ArrayList<IResource> allResources= new ArrayList<IResource>(selectedResources); | |
90 | addWorkingSetResources(allResources); | |
91 | return allResources; | |
92 | } | |
93 | ||
94 | private void addWorkingSetResources(List<IResource> selectedResources) { | |
95 | Object[] elements= getStructuredSelection().toArray(); | |
96 | for (int i= 0; i < elements.length; i++) { | |
97 | Object curr= elements[i]; | |
98 | if (curr instanceof IWorkingSet) { | |
99 | IAdaptable[] members= ((IWorkingSet) curr).getElements(); | |
100 | for (int k= 0; k < members.length; k++) { | |
101 | IResource adapted= (IResource) members[k].getAdapter(IResource.class); | |
102 | if (adapted != null) { | |
103 | selectedResources.add(adapted); | |
104 | } | |
105 | } | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | public void run(IProgressMonitor monitor) throws CoreException, OperationCanceledException { | |
111 | try { | |
112 | final IStatus[] errorStatus= new IStatus[] { Status.OK_STATUS }; | |
113 | createOperation(errorStatus).run(monitor); | |
114 | if (errorStatus[0].matches(IStatus.ERROR)) { | |
115 | throw new CoreException(errorStatus[0]); | |
116 | } | |
117 | } catch (InvocationTargetException e) { | |
118 | Throwable targetException= e.getTargetException(); | |
119 | if (targetException instanceof CoreException) | |
120 | throw (CoreException) targetException; | |
121 | throw new CoreException(new Status(IStatus.ERROR, JavaUI.ID_PLUGIN, ActionMessages.RefreshAction_error_workbenchaction_message, targetException)); | |
122 | } catch (InterruptedException e) { | |
123 | throw new OperationCanceledException(); | |
124 | } | |
125 | } | |
126 | } | |
127 | ||
128 | /** | |
129 | * Creates a new <code>RefreshAction</code>. The action requires | |
130 | * that the selection provided by the site's selection provider is of type <code> | |
131 | * org.eclipse.jface.viewers.IStructuredSelection</code>. | |
132 | * | |
133 | * @param site the site providing context information for this action | |
134 | */ | |
135 | public RefreshAction(IWorkbenchSite site) { | |
136 | super(site); | |
137 | setText(ActionMessages.RefreshAction_label); | |
138 | setToolTipText(ActionMessages.RefreshAction_toolTip); | |
139 | JavaPluginImages.setLocalImageDescriptors(this, "refresh.gif");//$NON-NLS-1$ | |
140 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REFRESH_ACTION); | |
141 | } | |
142 | ||
143 | /* (non-Javadoc) | |
144 | * Method declared in SelectionDispatchAction | |
145 | */ | |
146 | @Override | |
147 | public void selectionChanged(IStructuredSelection selection) { | |
148 | setEnabled(checkEnabled(selection)); | |
149 | } | |
150 | ||
151 | private boolean checkEnabled(IStructuredSelection selection) { | |
152 | if (selection.isEmpty()) | |
153 | return true; | |
154 | for (Iterator<?> iter= selection.iterator(); iter.hasNext();) { | |
155 | Object element= iter.next(); | |
156 | if (element instanceof IWorkingSet) { | |
157 | // don't inspect working sets any deeper. | |
158 | } else if (element instanceof IPackageFragmentRoot) { | |
159 | // on internal folders/JARs we do a normal refresh, and Java archive refresh on external | |
160 | } else if (element instanceof PackageFragmentRootContainer) { | |
161 | // too expensive to look at children. assume we can refresh | |
162 | } else if (element instanceof IAdaptable) { // test for IAdaptable last (types before are IAdaptable as well) | |
163 | IResource resource= (IResource)((IAdaptable)element).getAdapter(IResource.class); | |
164 | if (resource == null) | |
165 | return false; | |
166 | if (resource.getType() == IResource.PROJECT && !((IProject)resource).isOpen()) | |
167 | return false; | |
168 | } else { | |
169 | return false; | |
170 | } | |
171 | } | |
172 | return true; | |
173 | } | |
174 | ||
175 | /* (non-Javadoc) | |
176 | * Method declared in SelectionDispatchAction | |
177 | */ | |
178 | @Override | |
179 | public void run(final IStructuredSelection selection) { | |
180 | IWorkspaceRunnable operation= new IWorkspaceRunnable() { | |
181 | public void run(IProgressMonitor monitor) throws CoreException { | |
182 | performRefresh(selection, monitor); | |
183 | } | |
184 | }; | |
185 | new WorkbenchRunnableAdapter(operation).runAsUserJob(ActionMessages.RefreshAction_refresh_operation_label, null); | |
186 | } | |
187 | ||
188 | private void performRefresh(IStructuredSelection selection, IProgressMonitor monitor) throws CoreException, OperationCanceledException { | |
189 | monitor.beginTask(ActionMessages.RefreshAction_progressMessage, 2); | |
190 | ||
191 | WrappedWorkbenchRefreshAction workbenchAction= new WrappedWorkbenchRefreshAction(getSite()); | |
192 | workbenchAction.selectionChanged(selection); | |
193 | workbenchAction.run(new SubProgressMonitor(monitor, 1)); | |
194 | refreshJavaElements(selection, new SubProgressMonitor(monitor, 1)); | |
195 | } | |
196 | ||
197 | private void refreshJavaElements(IStructuredSelection selection, IProgressMonitor monitor) throws JavaModelException { | |
198 | Object[] selectedElements= selection.toArray(); | |
199 | ArrayList<IJavaElement> javaElements= new ArrayList<IJavaElement>(); | |
200 | for (int i= 0; i < selectedElements.length; i++) { | |
201 | Object curr= selectedElements[i]; | |
202 | if (curr instanceof IPackageFragmentRoot) { | |
203 | javaElements.add((IPackageFragmentRoot) curr); | |
204 | } else if (curr instanceof PackageFragmentRootContainer) { | |
205 | javaElements.addAll(Arrays.asList(((PackageFragmentRootContainer) curr).getPackageFragmentRoots())); | |
206 | } else if (curr instanceof IWorkingSet) { | |
207 | IAdaptable[] members= ((IWorkingSet) curr).getElements(); | |
208 | for (int k= 0; k < members.length; k++) { | |
209 | IJavaElement adapted= (IJavaElement)members[k].getAdapter(IJavaElement.class); | |
210 | if (adapted instanceof IPackageFragmentRoot) { | |
211 | javaElements.add(adapted); | |
212 | } | |
213 | } | |
214 | } | |
215 | } | |
216 | if (!javaElements.isEmpty()) { | |
217 | IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()); | |
218 | model.refreshExternalArchives(javaElements.toArray(new IJavaElement[javaElements.size()]), monitor); | |
219 | } | |
220 | } | |
221 | } |