1 11 package org.eclipse.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.filesystem.IFileInfo; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.IResourceRuleFactory; 21 import org.eclipse.core.resources.IWorkspaceRoot; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.runtime.CoreException; 24 import org.eclipse.core.runtime.IProgressMonitor; 25 import org.eclipse.core.runtime.jobs.ISchedulingRule; 26 import org.eclipse.core.runtime.jobs.MultiRule; 27 import org.eclipse.jface.dialogs.IDialogConstants; 28 import org.eclipse.jface.dialogs.MessageDialog; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.StructuredSelection; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.events.KeyEvent; 34 import org.eclipse.swt.widgets.Shell; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 37 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 38 import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils; 39 40 47 public class RefreshAction extends WorkspaceAction { 48 49 52 public static final String ID = PlatformUI.PLUGIN_ID + ".RefreshAction"; 54 60 public RefreshAction(Shell shell) { 61 super(shell, IDEWorkbenchMessages.RefreshAction_text); 62 setToolTipText(IDEWorkbenchMessages.RefreshAction_toolTip); 63 setId(ID); 64 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, 65 IIDEHelpContextIds.REFRESH_ACTION); 66 } 67 68 72 void checkLocationDeleted(IProject project) throws CoreException { 73 if (!project.exists()) { 74 return; 75 } 76 IFileInfo location = IDEResourceInfoUtils.getFileInfo(project 77 .getLocationURI()); 78 if (!location.exists()) { 79 String message = NLS.bind( 80 IDEWorkbenchMessages.RefreshAction_locationDeletedMessage, 81 project.getName(), location.toString()); 82 83 final MessageDialog dialog = new MessageDialog(getShell(), 84 IDEWorkbenchMessages.RefreshAction_dialogTitle, null, message, MessageDialog.QUESTION, new String [] { 88 IDialogConstants.YES_LABEL, 89 IDialogConstants.NO_LABEL }, 0); 92 getShell().getDisplay().syncExec(new Runnable () { 95 public void run() { 96 dialog.open(); 97 } 98 }); 99 100 if (dialog.getReturnCode() == 0) { project.delete(true, true, null); 103 } 104 } 105 } 106 107 110 protected String getOperationMessage() { 111 return IDEWorkbenchMessages.RefreshAction_progressMessage; 112 } 113 114 117 protected String getProblemsMessage() { 118 return IDEWorkbenchMessages.RefreshAction_problemMessage; 119 } 120 121 124 protected String getProblemsTitle() { 125 return IDEWorkbenchMessages.RefreshAction_problemTitle; 126 } 127 128 132 protected List getSelectedResources() { 133 List resources = super.getSelectedResources(); 134 if (resources.isEmpty()) { 135 resources = new ArrayList (); 136 resources.add(ResourcesPlugin.getWorkspace().getRoot()); 137 } 138 return resources; 139 } 140 141 144 protected void invokeOperation(IResource resource, IProgressMonitor monitor) 145 throws CoreException { 146 if (resource.getType() == IResource.PROJECT) { 151 checkLocationDeleted((IProject) resource); 152 } else if (resource.getType() == IResource.ROOT) { 153 IProject[] projects = ((IWorkspaceRoot) resource).getProjects(); 154 for (int i = 0; i < projects.length; i++) { 155 checkLocationDeleted(projects[i]); 156 } 157 } 158 resource.refreshLocal(IResource.DEPTH_INFINITE, monitor); 159 } 160 161 167 protected boolean updateSelection(IStructuredSelection s) { 168 return (super.updateSelection(s) || s.isEmpty()) 169 && getSelectedNonResources().size() == 0; 170 } 171 172 178 public void handleKeyReleased(KeyEvent event) { 179 180 if (event.keyCode == SWT.F5 && event.stateMask == 0) { 181 refreshAll(); 182 } 183 } 184 185 188 public void refreshAll() { 189 IStructuredSelection currentSelection = getStructuredSelection(); 190 selectionChanged(StructuredSelection.EMPTY); 191 run(); 192 selectionChanged(currentSelection); 193 } 194 195 199 public void run() { 200 ISchedulingRule rule = null; 201 IResourceRuleFactory factory = ResourcesPlugin.getWorkspace() 202 .getRuleFactory(); 203 Iterator resources = getSelectedResources().iterator(); 204 while (resources.hasNext()) { 205 rule = MultiRule.combine(rule, factory 206 .refreshRule((IResource) resources.next())); 207 } 208 runInBackground(rule); 209 } 210 } 211 | Popular Tags |