001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.actions;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.filesystem.IFileInfo;
017: import org.eclipse.core.resources.IProject;
018: import org.eclipse.core.resources.IResource;
019: import org.eclipse.core.resources.IResourceRuleFactory;
020: import org.eclipse.core.resources.IWorkspaceRoot;
021: import org.eclipse.core.resources.ResourcesPlugin;
022: import org.eclipse.core.runtime.CoreException;
023: import org.eclipse.core.runtime.IProgressMonitor;
024: import org.eclipse.core.runtime.jobs.ISchedulingRule;
025: import org.eclipse.core.runtime.jobs.MultiRule;
026: import org.eclipse.jface.dialogs.IDialogConstants;
027: import org.eclipse.jface.dialogs.MessageDialog;
028: import org.eclipse.jface.viewers.IStructuredSelection;
029: import org.eclipse.jface.viewers.StructuredSelection;
030: import org.eclipse.osgi.util.NLS;
031: import org.eclipse.swt.SWT;
032: import org.eclipse.swt.events.KeyEvent;
033: import org.eclipse.swt.widgets.Shell;
034: import org.eclipse.ui.PlatformUI;
035: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
036: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
037: import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;
038:
039: /**
040: * Standard action for refreshing the workspace from the local file system for
041: * the selected resources and all of their descendents.
042: * <p>
043: * This class may be instantiated; it is not intended to be subclassed.
044: * </p>
045: */
046: public class RefreshAction extends WorkspaceAction {
047:
048: /**
049: * The id of this action.
050: */
051: public static final String ID = PlatformUI.PLUGIN_ID
052: + ".RefreshAction";//$NON-NLS-1$
053:
054: /**
055: * Creates a new action.
056: *
057: * @param shell
058: * the shell for any dialogs
059: */
060: public RefreshAction(Shell shell) {
061: super (shell, IDEWorkbenchMessages.RefreshAction_text);
062: setToolTipText(IDEWorkbenchMessages.RefreshAction_toolTip);
063: setId(ID);
064: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
065: IIDEHelpContextIds.REFRESH_ACTION);
066: }
067:
068: /**
069: * Checks whether the given project's location has been deleted. If so,
070: * prompts the user with whether to delete the project or not.
071: */
072: void checkLocationDeleted(IProject project) throws CoreException {
073: if (!project.exists()) {
074: return;
075: }
076: IFileInfo location = IDEResourceInfoUtils.getFileInfo(project
077: .getLocationURI());
078: if (!location.exists()) {
079: String message = NLS
080: .bind(
081: IDEWorkbenchMessages.RefreshAction_locationDeletedMessage,
082: project.getName(), location.toString());
083:
084: final MessageDialog dialog = new MessageDialog(getShell(),
085: IDEWorkbenchMessages.RefreshAction_dialogTitle, // dialog
086: // title
087: null, // use default window icon
088: message, MessageDialog.QUESTION, new String[] {
089: IDialogConstants.YES_LABEL,
090: IDialogConstants.NO_LABEL }, 0); // yes is the
091: // default
092:
093: // Must prompt user in UI thread (we're in the operation thread
094: // here).
095: getShell().getDisplay().syncExec(new Runnable() {
096: public void run() {
097: dialog.open();
098: }
099: });
100:
101: // Do the deletion back in the operation thread
102: if (dialog.getReturnCode() == 0) { // yes was chosen
103: project.delete(true, true, null);
104: }
105: }
106: }
107:
108: /*
109: * (non-Javadoc) Method declared on WorkspaceAction.
110: */
111: protected String getOperationMessage() {
112: return IDEWorkbenchMessages.RefreshAction_progressMessage;
113: }
114:
115: /*
116: * (non-Javadoc) Method declared on WorkspaceAction.
117: */
118: protected String getProblemsMessage() {
119: return IDEWorkbenchMessages.RefreshAction_problemMessage;
120: }
121:
122: /*
123: * (non-Javadoc) Method declared on WorkspaceAction.
124: */
125: protected String getProblemsTitle() {
126: return IDEWorkbenchMessages.RefreshAction_problemTitle;
127: }
128:
129: /**
130: * Returns a list containing the workspace root if the selection would
131: * otherwise be empty.
132: */
133: protected List getSelectedResources() {
134: List resources = super .getSelectedResources();
135: if (resources.isEmpty()) {
136: resources = new ArrayList();
137: resources.add(ResourcesPlugin.getWorkspace().getRoot());
138: }
139: return resources;
140: }
141:
142: /*
143: * (non-Javadoc) Method declared on WorkspaceAction.
144: */
145: protected void invokeOperation(IResource resource,
146: IProgressMonitor monitor) throws CoreException {
147: // Check if project's location has been deleted,
148: // as per 1G83UCE: ITPUI:WINNT - Refresh from local doesn't detect new
149: // or deleted projects
150: // and also for bug report #18283
151: if (resource.getType() == IResource.PROJECT) {
152: checkLocationDeleted((IProject) resource);
153: } else if (resource.getType() == IResource.ROOT) {
154: IProject[] projects = ((IWorkspaceRoot) resource)
155: .getProjects();
156: for (int i = 0; i < projects.length; i++) {
157: checkLocationDeleted(projects[i]);
158: }
159: }
160: resource.refreshLocal(IResource.DEPTH_INFINITE, monitor);
161: }
162:
163: /**
164: * The <code>RefreshAction</code> implementation of this
165: * <code>SelectionListenerAction</code> method ensures that this action is
166: * enabled if the selection is empty, but is disabled if any of the selected
167: * elements are not resources.
168: */
169: protected boolean updateSelection(IStructuredSelection s) {
170: return (super .updateSelection(s) || s.isEmpty())
171: && getSelectedNonResources().size() == 0;
172: }
173:
174: /**
175: * Handle the key release.
176: *
177: * @param event
178: * the event
179: */
180: public void handleKeyReleased(KeyEvent event) {
181:
182: if (event.keyCode == SWT.F5 && event.stateMask == 0) {
183: refreshAll();
184: }
185: }
186:
187: /**
188: * Refreshes the entire workspace.
189: */
190: public void refreshAll() {
191: IStructuredSelection currentSelection = getStructuredSelection();
192: selectionChanged(StructuredSelection.EMPTY);
193: run();
194: selectionChanged(currentSelection);
195: }
196:
197: /*
198: * (non-Javadoc) Method declared on IAction; overrides method on
199: * WorkspaceAction.
200: */
201: public void run() {
202: ISchedulingRule rule = null;
203: IResourceRuleFactory factory = ResourcesPlugin.getWorkspace()
204: .getRuleFactory();
205: Iterator resources = getSelectedResources().iterator();
206: while (resources.hasNext()) {
207: rule = MultiRule.combine(rule, factory
208: .refreshRule((IResource) resources.next()));
209: }
210: runInBackground(rule);
211: }
212: }
|