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