KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.net.URI JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18
19 import org.eclipse.core.filesystem.EFS;
20 import org.eclipse.core.filesystem.IFileStore;
21
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.core.resources.IWorkspaceRoot;
30 import org.eclipse.core.resources.IWorkspaceRunnable;
31 import org.eclipse.core.resources.ResourcesPlugin;
32
33 import org.eclipse.jface.dialogs.MessageDialog;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35
36 import org.eclipse.ui.IWorkbenchSite;
37 import org.eclipse.ui.IWorkingSet;
38 import org.eclipse.ui.PlatformUI;
39
40 import org.eclipse.jdt.core.IJavaElement;
41 import org.eclipse.jdt.core.IJavaModel;
42 import org.eclipse.jdt.core.JavaCore;
43
44 import org.eclipse.jdt.internal.corext.util.Messages;
45 import org.eclipse.jdt.internal.corext.util.Resources;
46
47 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
48 import org.eclipse.jdt.internal.ui.JavaPluginImages;
49 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
50 import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
51 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
52
53 /**
54  * Action for refreshing the workspace from the local file system for
55  * the selected resources and all of their descendants. This action
56  * also considers external Jars managed by the Java Model.
57  * <p>
58  * Action is applicable to selections containing resources and Java
59  * elements down to compilation units.
60  *
61  * <p>
62  * This class may be instantiated; it is not intended to be subclassed.
63  * </p>
64  *
65  * @since 2.0
66  */

67 public class RefreshAction extends SelectionDispatchAction {
68
69     /**
70      * Creates a new <code>RefreshAction</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 RefreshAction(IWorkbenchSite site) {
77         super(site);
78         setText(ActionMessages.RefreshAction_label);
79         setToolTipText(ActionMessages.RefreshAction_toolTip);
80         JavaPluginImages.setLocalImageDescriptors(this, "refresh_nav.gif");//$NON-NLS-1$
81
PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REFRESH_ACTION);
82     }
83     
84     /* (non-Javadoc)
85      * Method declared in SelectionDispatchAction
86      */

87     public void selectionChanged(IStructuredSelection selection) {
88         setEnabled(checkEnabled(selection));
89     }
90
91     private boolean checkEnabled(IStructuredSelection selection) {
92         if (selection.isEmpty())
93             return true;
94         for (Iterator JavaDoc iter= selection.iterator(); iter.hasNext();) {
95             Object JavaDoc element= iter.next();
96             if (element instanceof IWorkingSet) {
97                 // don't inspect working sets any deeper.
98
} else if (element instanceof IAdaptable) {
99                 IResource resource= (IResource)((IAdaptable)element).getAdapter(IResource.class);
100                 if (resource == null)
101                     return false;
102                 if (resource.getType() == IResource.PROJECT && !((IProject)resource).isOpen())
103                     return false;
104             } else {
105                 return false;
106             }
107         }
108         return true;
109     }
110
111     /* (non-Javadoc)
112      * Method declared in SelectionDispatchAction
113      */

114     public void run(IStructuredSelection selection) {
115         final IResource[] resources= getResources(selection);
116         IWorkspaceRunnable operation= new IWorkspaceRunnable() {
117             public void run(IProgressMonitor monitor) throws CoreException {
118                 monitor.beginTask(ActionMessages.RefreshAction_progressMessage, resources.length * 2);
119                 monitor.subTask(""); //$NON-NLS-1$
120
List JavaDoc javaElements= new ArrayList JavaDoc(5);
121                 for (int r= 0; r < resources.length; r++) {
122                     IResource resource= resources[r];
123                     if (resource.getType() == IResource.PROJECT) {
124                         checkLocationDeleted((IProject) resource);
125                     } else if (resource.getType() == IResource.ROOT) {
126                         IProject[] projects = ((IWorkspaceRoot)resource).getProjects();
127                         for (int p = 0; p < projects.length; p++) {
128                             checkLocationDeleted(projects[p]);
129                         }
130                     }
131                     resource.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 1));
132                     IJavaElement jElement= JavaCore.create(resource);
133                     if (jElement != null && jElement.exists())
134                         javaElements.add(jElement);
135                 }
136                 IJavaModel model= JavaCore.create(ResourcesPlugin.getWorkspace().getRoot());
137                 model.refreshExternalArchives(
138                     (IJavaElement[]) javaElements.toArray(new IJavaElement[javaElements.size()]),
139                     new SubProgressMonitor(monitor, resources.length));
140             }
141         };
142         
143         try {
144             PlatformUI.getWorkbench().getProgressService().run(true, true, new WorkbenchRunnableAdapter(operation));
145         } catch (InvocationTargetException JavaDoc e) {
146             ExceptionHandler.handle(e, getShell(),
147                 ActionMessages.RefreshAction_error_title,
148                 ActionMessages.RefreshAction_error_message);
149         } catch (InterruptedException JavaDoc e) {
150             // canceled
151
}
152     }
153     
154     private IResource[] getResources(IStructuredSelection selection) {
155         if (selection.isEmpty()) {
156             return new IResource[] {ResourcesPlugin.getWorkspace().getRoot()};
157         }
158         
159         List JavaDoc result= new ArrayList JavaDoc(selection.size());
160         getResources(result, selection.toArray());
161         
162         for (Iterator JavaDoc iter= result.iterator(); iter.hasNext();) {
163             IResource resource= (IResource) iter.next();
164             if (isDescendent(result, resource))
165                 iter.remove();
166         }
167         
168         return (IResource[]) result.toArray(new IResource[result.size()]);
169     }
170     
171     private void getResources(List JavaDoc result, Object JavaDoc[] elements) {
172         for (int i= 0; i < elements.length; i++) {
173             Object JavaDoc element= elements[i];
174             // Must check working set before IAdaptable since WorkingSet
175
// implements IAdaptable
176
if (element instanceof IWorkingSet) {
177                 getResources(result, ((IWorkingSet)element).getElements());
178             } else if (element instanceof IAdaptable) {
179                 IResource resource= (IResource)((IAdaptable)element).getAdapter(IResource.class);
180                 if (resource == null)
181                     continue;
182                 if (resource.getType() != IResource.PROJECT ||
183                         (resource.getType() == IResource.PROJECT && ((IProject)resource).isOpen())) {
184                     result.add(resource);
185                 }
186             }
187         }
188     }
189     
190     private boolean isDescendent(List JavaDoc candidates, IResource element) {
191         IResource parent= element.getParent();
192         while (parent != null) {
193             if (candidates.contains(parent))
194                 return true;
195             parent= parent.getParent();
196         }
197         return false;
198     }
199     
200     private void checkLocationDeleted(IProject project) throws CoreException {
201         if (!project.exists())
202             return;
203         URI JavaDoc location= project.getLocationURI();
204         if (location == null)
205             return;
206         IFileStore store= EFS.getStore(location);
207         if (!store.fetchInfo().exists()) {
208             final String JavaDoc message = Messages.format(
209                 ActionMessages.RefreshAction_locationDeleted_message,
210                 new Object JavaDoc[] { project.getName(), Resources.getLocationString(project) });
211             final boolean[] result= new boolean[1];
212             // Must prompt user in UI thread (we're in the operation thread here).
213
getShell().getDisplay().syncExec(new Runnable JavaDoc() {
214                 public void run() {
215                     result[0]= MessageDialog.openQuestion(getShell(),
216                         ActionMessages.RefreshAction_locationDeleted_title,
217                         message);
218                 }
219             });
220             if (result[0]) {
221                 project.delete(true, true, null);
222             }
223         }
224     }
225 }
Popular Tags