KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ui.actions;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
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 /**
41  * Standard action for refreshing the workspace from the local file system for
42  * the selected resources and all of their descendents.
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  */

47 public class RefreshAction extends WorkspaceAction {
48
49     /**
50      * The id of this action.
51      */

52     public static final String JavaDoc ID = PlatformUI.PLUGIN_ID + ".RefreshAction";//$NON-NLS-1$
53

54     /**
55      * Creates a new action.
56      *
57      * @param shell
58      * the shell for any dialogs
59      */

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     /**
69      * Checks whether the given project's location has been deleted. If so,
70      * prompts the user with whether to delete the project or not.
71      */

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 JavaDoc message = NLS.bind(
80                     IDEWorkbenchMessages.RefreshAction_locationDeletedMessage,
81                     project.getName(), location.toString());
82
83             final MessageDialog dialog = new MessageDialog(getShell(),
84                     IDEWorkbenchMessages.RefreshAction_dialogTitle, // dialog
85
// title
86
null, // use default window icon
87
message, MessageDialog.QUESTION, new String JavaDoc[] {
88                             IDialogConstants.YES_LABEL,
89                             IDialogConstants.NO_LABEL }, 0); // yes is the
90
// default
91

92             // Must prompt user in UI thread (we're in the operation thread
93
// here).
94
getShell().getDisplay().syncExec(new Runnable JavaDoc() {
95                 public void run() {
96                     dialog.open();
97                 }
98             });
99
100             // Do the deletion back in the operation thread
101
if (dialog.getReturnCode() == 0) { // yes was chosen
102
project.delete(true, true, null);
103             }
104         }
105     }
106
107     /*
108      * (non-Javadoc) Method declared on WorkspaceAction.
109      */

110     protected String JavaDoc getOperationMessage() {
111         return IDEWorkbenchMessages.RefreshAction_progressMessage;
112     }
113
114     /*
115      * (non-Javadoc) Method declared on WorkspaceAction.
116      */

117     protected String JavaDoc getProblemsMessage() {
118         return IDEWorkbenchMessages.RefreshAction_problemMessage;
119     }
120
121     /*
122      * (non-Javadoc) Method declared on WorkspaceAction.
123      */

124     protected String JavaDoc getProblemsTitle() {
125         return IDEWorkbenchMessages.RefreshAction_problemTitle;
126     }
127
128     /**
129      * Returns a list containing the workspace root if the selection would
130      * otherwise be empty.
131      */

132     protected List JavaDoc getSelectedResources() {
133         List JavaDoc resources = super.getSelectedResources();
134         if (resources.isEmpty()) {
135             resources = new ArrayList JavaDoc();
136             resources.add(ResourcesPlugin.getWorkspace().getRoot());
137         }
138         return resources;
139     }
140
141     /*
142      * (non-Javadoc) Method declared on WorkspaceAction.
143      */

144     protected void invokeOperation(IResource resource, IProgressMonitor monitor)
145             throws CoreException {
146         // Check if project's location has been deleted,
147
// as per 1G83UCE: ITPUI:WINNT - Refresh from local doesn't detect new
148
// or deleted projects
149
// and also for bug report #18283
150
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     /**
162      * The <code>RefreshAction</code> implementation of this
163      * <code>SelectionListenerAction</code> method ensures that this action is
164      * enabled if the selection is empty, but is disabled if any of the selected
165      * elements are not resources.
166      */

167     protected boolean updateSelection(IStructuredSelection s) {
168         return (super.updateSelection(s) || s.isEmpty())
169                 && getSelectedNonResources().size() == 0;
170     }
171
172     /**
173      * Handle the key release.
174      *
175      * @param event
176      * the event
177      */

178     public void handleKeyReleased(KeyEvent event) {
179
180         if (event.keyCode == SWT.F5 && event.stateMask == 0) {
181             refreshAll();
182         }
183     }
184
185     /**
186      * Refreshes the entire workspace.
187      */

188     public void refreshAll() {
189         IStructuredSelection currentSelection = getStructuredSelection();
190         selectionChanged(StructuredSelection.EMPTY);
191         run();
192         selectionChanged(currentSelection);
193     }
194
195     /*
196      * (non-Javadoc) Method declared on IAction; overrides method on
197      * WorkspaceAction.
198      */

199     public void run() {
200         ISchedulingRule rule = null;
201         IResourceRuleFactory factory = ResourcesPlugin.getWorkspace()
202                 .getRuleFactory();
203         Iterator JavaDoc 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