1 11 package org.eclipse.ui.actions; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IResourceChangeEvent; 19 import org.eclipse.core.resources.IResourceChangeListener; 20 import org.eclipse.core.resources.IResourceDelta; 21 import org.eclipse.core.resources.IResourceRuleFactory; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.resources.WorkspaceJob; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IProgressMonitor; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.OperationCanceledException; 28 import org.eclipse.core.runtime.Status; 29 import org.eclipse.core.runtime.SubProgressMonitor; 30 import org.eclipse.core.runtime.jobs.ISchedulingRule; 31 import org.eclipse.core.runtime.jobs.Job; 32 import org.eclipse.core.runtime.jobs.MultiRule; 33 import org.eclipse.jface.dialogs.IDialogConstants; 34 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 35 import org.eclipse.jface.preference.IPreferenceStore; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 import org.eclipse.jface.window.Window; 38 import org.eclipse.swt.widgets.Shell; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.internal.ide.IDEInternalPreferences; 41 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 42 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 43 import org.eclipse.ui.internal.ide.IIDEHelpContextIds; 44 45 55 public class OpenResourceAction extends WorkspaceAction implements IResourceChangeListener { 56 57 60 public static final String ID = PlatformUI.PLUGIN_ID + ".OpenResourceAction"; 62 68 public OpenResourceAction(Shell shell) { 69 super(shell, IDEWorkbenchMessages.OpenResourceAction_text); 70 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IIDEHelpContextIds.OPEN_RESOURCE_ACTION); 71 setToolTipText(IDEWorkbenchMessages.OpenResourceAction_toolTip); 72 setId(ID); 73 } 74 75 78 private int countClosedProjects() { 79 int count = 0; 80 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 81 for (int i = 0; i < projects.length; i++) { 82 if (!projects[i].isOpen()) { 83 count++; 84 } 85 } 86 return count; 87 } 88 89 92 protected String getOperationMessage() { 93 return IDEWorkbenchMessages.OpenResourceAction_operationMessage; 94 } 95 96 99 protected String getProblemsMessage() { 100 return IDEWorkbenchMessages.OpenResourceAction_problemMessage; 101 } 102 103 106 protected String getProblemsTitle() { 107 return IDEWorkbenchMessages.OpenResourceAction_dialogTitle; 108 } 109 110 114 private boolean hasOtherClosedProjects() { 115 int closedInSelection = 0; 117 Iterator resources = getSelectedResources().iterator(); 118 while (resources.hasNext()) { 119 IProject project = (IProject) resources.next(); 120 if (!project.isOpen()) 121 closedInSelection++; 122 } 123 return closedInSelection < countClosedProjects(); 126 } 127 128 protected void invokeOperation(IResource resource, IProgressMonitor monitor) throws CoreException { 129 ((IProject) resource).open(monitor); 130 } 131 132 139 private boolean promptToOpenWithReferences() { 140 IPreferenceStore store = IDEWorkbenchPlugin.getDefault().getPreferenceStore(); 141 String key = IDEInternalPreferences.OPEN_REQUIRED_PROJECTS; 142 String value = store.getString(key); 143 if (MessageDialogWithToggle.ALWAYS.equals(value)) { 144 return true; 145 } 146 if (MessageDialogWithToggle.NEVER.equals(value)) { 147 return false; 148 } 149 String message = IDEWorkbenchMessages.OpenResourceAction_openRequiredProjects; 150 MessageDialogWithToggle dialog = MessageDialogWithToggle.openYesNoCancelQuestion(getShell(), IDEWorkbenchMessages.Question, message, null, false, store, key); 151 int result = dialog.getReturnCode(); 152 if (result == Window.CANCEL) { 153 throw new OperationCanceledException(); 154 } 155 return dialog.getReturnCode() == IDialogConstants.YES_ID; 156 } 157 158 162 public void resourceChanged(IResourceChangeEvent event) { 163 List sel = getSelectedResources(); 165 if (selectionIsOfType(IResource.PROJECT)) { 167 IResourceDelta delta = event.getDelta(); 168 if (delta != null) { 169 IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED); 170 for (int i = 0; i < projDeltas.length; ++i) { 171 IResourceDelta projDelta = projDeltas[i]; 172 if ((projDelta.getFlags() & IResourceDelta.OPEN) != 0) { 173 if (sel.contains(projDelta.getResource())) { 174 selectionChanged(getStructuredSelection()); 175 return; 176 } 177 } 178 } 179 } 180 } 181 } 182 183 187 public void run() { 188 try { 189 if (hasOtherClosedProjects() && promptToOpenWithReferences()) { 190 runOpenWithReferences(); 191 } 192 ISchedulingRule rule = null; 193 IResourceRuleFactory factory = ResourcesPlugin.getWorkspace().getRuleFactory(); 196 Iterator resources = getSelectedResources().iterator(); 197 while (resources.hasNext()) { 198 IProject project = (IProject) resources.next(); 199 rule = MultiRule.combine(rule, factory.modifyRule(project)); 200 } 201 runInBackground(rule); 202 } catch (OperationCanceledException e) { 203 } 205 } 206 207 210 private void runOpenWithReferences() { 211 final List resources = new ArrayList (getActionResources()); 212 Job job = new WorkspaceJob(removeMnemonics(getText())) { 213 214 217 private void doOpenWithReferences(IProject project, IProgressMonitor monitor) throws CoreException { 218 if (!project.exists() || project.isOpen()) { 219 return; 220 } 221 project.open(new SubProgressMonitor(monitor, 1000)); 222 IProject[] references = project.getReferencedProjects(); 223 for (int i = 0; i < references.length; i++) { 224 doOpenWithReferences(references[i], monitor); 225 } 226 } 227 228 public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException { 229 try { 230 monitor.beginTask("", countClosedProjects() * 1000); monitor.setTaskName(getOperationMessage()); 233 for (Iterator it = resources.iterator(); it.hasNext();) { 234 doOpenWithReferences((IProject) it.next(), monitor); 235 } 236 } finally { 237 monitor.done(); 238 } 239 return Status.OK_STATUS; 240 } 241 }; 242 job.setRule(ResourcesPlugin.getWorkspace().getRoot()); 243 job.setUser(true); 244 job.schedule(); 245 } 246 247 250 protected boolean shouldPerformResourcePruning() { 251 return false; 252 } 253 254 259 protected boolean updateSelection(IStructuredSelection s) { 260 263 if (!selectionIsOfType(IResource.PROJECT)) { 264 return false; 265 } 266 267 Iterator resources = getSelectedResources().iterator(); 268 while (resources.hasNext()) { 269 IProject currentResource = (IProject) resources.next(); 270 if (!currentResource.isOpen()) { 271 return true; 272 } 273 } 274 return false; 275 } 276 } 277 | Popular Tags |