1 11 package org.eclipse.jdt.ui.actions; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URI ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 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 67 public class RefreshAction extends SelectionDispatchAction { 68 69 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"); PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.REFRESH_ACTION); 82 } 83 84 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 iter= selection.iterator(); iter.hasNext();) { 95 Object element= iter.next(); 96 if (element instanceof IWorkingSet) { 97 } 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 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(""); List javaElements= new ArrayList (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 e) { 146 ExceptionHandler.handle(e, getShell(), 147 ActionMessages.RefreshAction_error_title, 148 ActionMessages.RefreshAction_error_message); 149 } catch (InterruptedException e) { 150 } 152 } 153 154 private IResource[] getResources(IStructuredSelection selection) { 155 if (selection.isEmpty()) { 156 return new IResource[] {ResourcesPlugin.getWorkspace().getRoot()}; 157 } 158 159 List result= new ArrayList (selection.size()); 160 getResources(result, selection.toArray()); 161 162 for (Iterator 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 result, Object [] elements) { 172 for (int i= 0; i < elements.length; i++) { 173 Object element= elements[i]; 174 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 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 location= project.getLocationURI(); 204 if (location == null) 205 return; 206 IFileStore store= EFS.getStore(location); 207 if (!store.fetchInfo().exists()) { 208 final String message = Messages.format( 209 ActionMessages.RefreshAction_locationDeleted_message, 210 new Object [] { project.getName(), Resources.getLocationString(project) }); 211 final boolean[] result= new boolean[1]; 212 getShell().getDisplay().syncExec(new Runnable () { 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 |