1 11 package org.eclipse.team.internal.ui.actions; 12 13 import java.lang.reflect.Array ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.*; 16 import java.util.List ; 17 18 import org.eclipse.core.commands.*; 19 import org.eclipse.core.resources.*; 20 import org.eclipse.core.resources.mapping.ResourceMapping; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.jface.action.IAction; 23 import org.eclipse.jface.dialogs.MessageDialog; 24 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 25 import org.eclipse.jface.operation.IRunnableWithProgress; 26 import org.eclipse.jface.viewers.*; 27 import org.eclipse.swt.custom.BusyIndicator; 28 import org.eclipse.swt.widgets.*; 29 import org.eclipse.team.core.RepositoryProvider; 30 import org.eclipse.team.core.TeamException; 31 import org.eclipse.team.internal.core.TeamPlugin; 32 import org.eclipse.team.internal.ui.*; 33 import org.eclipse.ui.*; 34 import org.eclipse.ui.handlers.HandlerUtil; 35 import org.eclipse.ui.ide.ResourceUtil; 36 37 46 public abstract class TeamAction extends AbstractHandler implements IObjectActionDelegate, IViewActionDelegate, IWorkbenchWindowActionDelegate, IActionDelegate2 { 47 private IStructuredSelection selection; 49 50 private Shell shell; 52 53 public final static int PROGRESS_DIALOG = 1; 56 public final static int PROGRESS_BUSYCURSOR = 2; 57 58 private IWorkbenchPart targetPart; 59 private IWorkbenchWindow window; 60 61 private ISelectionListener selectionListener = new ISelectionListener() { 62 public void selectionChanged(IWorkbenchPart part, ISelection selection) { 63 if(selection instanceof IStructuredSelection) 64 TeamAction.this.selection = (IStructuredSelection)selection; 65 } 66 }; 67 68 76 public static Object [] getSelectedAdaptables(ISelection selection, Class c) { 77 ArrayList result = null; 78 if (selection != null && !selection.isEmpty()) { 79 result = new ArrayList(); 80 Iterator elements = ((IStructuredSelection) selection).iterator(); 81 while (elements.hasNext()) { 82 Object adapter = getAdapter(elements.next(), c); 83 if (c.isInstance(adapter)) { 84 result.add(adapter); 85 } 86 } 87 } 88 if (result != null && !result.isEmpty()) { 89 return result.toArray((Object [])Array.newInstance(c, result.size())); 90 } 91 return (Object [])Array.newInstance(c, 0); 92 } 93 94 103 public static Object getAdapter(Object adaptable, Class c) { 104 if (c.isInstance(adaptable)) { 105 return adaptable; 106 } 107 if (adaptable instanceof IAdaptable) { 108 IAdaptable a = (IAdaptable) adaptable; 109 Object adapter = a.getAdapter(c); 110 if (c.isInstance(adapter)) { 111 return adapter; 112 } 113 } 114 return null; 115 } 116 117 122 protected IProject[] getSelectedProjects() { 123 IResource[] selectedResources = getSelectedResources(); 124 if (selectedResources.length == 0) return new IProject[0]; 125 ArrayList projects = new ArrayList(); 126 for (int i = 0; i < selectedResources.length; i++) { 127 IResource resource = selectedResources[i]; 128 if (resource.getType() == IResource.PROJECT) { 129 projects.add(resource); 130 } 131 } 132 return (IProject[]) projects.toArray(new IProject[projects.size()]); 133 } 134 135 143 protected Object [] getAdaptedSelection(Class c) { 144 return getSelectedAdaptables(selection, c); 145 } 146 147 152 protected IResource[] getSelectedResources() { 153 return Utils.getContributedResources(getSelection().toArray()); 154 } 155 156 protected IStructuredSelection getSelection() { 157 if (selection == null) 158 selection = StructuredSelection.EMPTY; 159 return selection; 160 } 161 162 168 protected ResourceMapping[] getSelectedResourceMappings(String providerId) { 169 Object [] elements = getSelection().toArray(); 170 ArrayList providerMappings = new ArrayList(); 171 for (int i = 0; i < elements.length; i++) { 172 Object object = elements[i]; 173 Object adapted = getResourceMapping(object); 174 if (adapted instanceof ResourceMapping) { 175 ResourceMapping mapping = (ResourceMapping) adapted; 176 if (providerId == null || isMappedToProvider(mapping, providerId)) { 177 providerMappings.add(mapping); 178 } 179 } 180 } 181 return (ResourceMapping[]) providerMappings.toArray(new ResourceMapping[providerMappings.size()]); 182 } 183 184 private Object getResourceMapping(Object object) { 185 if (object instanceof ResourceMapping) 186 return (ResourceMapping)object; 187 return Utils.getResourceMapping(object); 188 } 189 190 private boolean isMappedToProvider(ResourceMapping element, String providerId) { 191 IProject[] projects = element.getProjects(); 192 for (int k = 0; k < projects.length; k++) { 193 IProject project = projects[k]; 194 RepositoryProvider provider = RepositoryProvider.getProvider(project); 195 if (provider != null && provider.getID().equals(providerId)) { 196 return true; 197 } 198 } 199 return false; 200 } 201 202 207 protected Shell getShell() { 208 if (shell != null) { 209 return shell; 210 } else if (targetPart != null) { 211 return targetPart.getSite().getShell(); 212 } else if (window != null) { 213 return window.getShell(); 214 } else { 215 IWorkbench workbench = TeamUIPlugin.getPlugin().getWorkbench(); 216 if (workbench == null) return null; 217 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); 218 if (window == null) return null; 219 return window.getShell(); 220 } 221 } 222 230 final protected void run(final IRunnableWithProgress runnable, final String problemMessage, int progressKind) { 231 final Exception [] exceptions = new Exception [] {null}; 232 switch (progressKind) { 233 case PROGRESS_BUSYCURSOR : 234 BusyIndicator.showWhile(Display.getCurrent(), new Runnable () { 235 public void run() { 236 try { 237 runnable.run(new NullProgressMonitor()); 238 } catch (InvocationTargetException e) { 239 exceptions[0] = e; 240 } catch (InterruptedException e) { 241 exceptions[0] = null; 242 } 243 } 244 }); 245 break; 246 default : 247 case PROGRESS_DIALOG : 248 try { 249 new ProgressMonitorDialog(getShell()).run(true, true, runnable); 250 } catch (InvocationTargetException e) { 251 exceptions[0] = e; 252 } catch (InterruptedException e) { 253 exceptions[0] = null; 254 } 255 break; 256 } 257 if (exceptions[0] != null) { 258 handle(exceptions[0], null, problemMessage); 259 } 260 } 261 262 265 public void selectionChanged(IAction action, ISelection selection) { 266 if (selection instanceof IStructuredSelection) { 267 this.selection = (IStructuredSelection) selection; 268 if (action != null) { 269 setActionEnablement(action); 270 } 271 } 272 } 273 274 283 protected void setActionEnablement(IAction action) { 284 action.setEnabled(isEnabled()); 285 } 286 287 293 protected boolean isEnabledForException(TeamException exception) { 294 if (exception.getStatus().getCode() == IResourceStatus.OUT_OF_SYNC_LOCAL) { 295 return true; 297 } 298 TeamPlugin.log(exception); 300 return false; 301 } 302 303 306 public void setActivePart(IAction action, IWorkbenchPart targetPart) { 307 if(targetPart != null) { 308 this.shell = targetPart.getSite().getShell(); 309 this.targetPart = targetPart; 310 } 311 } 312 319 protected void handle(Exception exception, String title, String message) { 320 Utils.handleError(getShell(), exception, title, message); 321 } 322 323 330 protected Hashtable getProviderMapping(IResource[] resources) { 331 Hashtable result = new Hashtable(); 332 for (int i = 0; i < resources.length; i++) { 333 RepositoryProvider provider = RepositoryProvider.getProvider(resources[i].getProject()); 334 List list = (List )result.get(provider); 335 if (list == null) { 336 list = new ArrayList(); 337 result.put(provider, list); 338 } 339 list.add(resources[i]); 340 } 341 return result; 342 } 343 344 347 protected IWorkbenchPart getTargetPart() { 348 if(targetPart == null) { 349 IWorkbenchPage page = TeamUIPlugin.getActivePage(); 350 if (page != null) { 351 targetPart = page.getActivePart(); 352 } 353 } 354 return targetPart; 355 356 } 357 358 362 protected IWorkbenchPage getTargetPage() { 363 if (getTargetPart() == null) return TeamUIPlugin.getActivePage(); 364 return getTargetPart().getSite().getPage(); 365 } 366 367 374 protected IViewPart showView(String viewId) { 375 try { 376 return getTargetPage().showView(viewId); 377 } catch (PartInitException pe) { 378 return null; 379 } 380 } 381 382 385 public void init(IViewPart view) { 386 if(view != null) { 387 this.shell = view.getSite().getShell(); 388 this.targetPart = view; 389 } 390 } 391 392 public void init(IWorkbenchWindow window) { 393 this.window = window; 394 this.shell = window.getShell(); 395 window.getSelectionService().addPostSelectionListener(selectionListener); 396 } 397 398 public IWorkbenchWindow getWindow() { 399 return window; 400 } 401 402 public void dispose() { 403 super.dispose(); 404 if(window != null) { 405 window.getSelectionService().removePostSelectionListener(selectionListener); 406 } 407 selection = null; 409 window = null; 410 targetPart = null; 411 shell = null; 412 } 413 414 417 protected abstract void execute(IAction action) 418 throws InvocationTargetException , InterruptedException ; 419 420 433 public Object execute(ExecutionEvent event) throws ExecutionException { 434 IWorkbenchWindow activeWorkbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event); 435 if (activeWorkbenchWindow != null) { 436 ISelection selection = HandlerUtil.getCurrentSelection(event); 437 if (selection != null) { 438 IWorkbenchPart part = HandlerUtil.getActivePart(event); 439 try { 440 execute(activeWorkbenchWindow, part, selection); 441 } catch (InvocationTargetException e) { 442 throw new ExecutionException(TeamUIMessages.TeamAction_errorTitle, e); 443 } catch (InterruptedException e) { 444 } 446 } 447 } 448 return null; 449 } 450 451 private void execute(IWorkbenchWindow activeWorkbenchWindow, 452 IWorkbenchPart part, ISelection selection) 453 throws InvocationTargetException , InterruptedException { 454 if (part != null && part instanceof IEditorPart) { 457 IEditorInput input = ((IEditorPart) part).getEditorInput(); 458 IFile file = ResourceUtil.getFile(input); 459 if (file != null) { 460 selectionChanged((IAction) null, new StructuredSelection(file)); 461 } 462 } else { 463 selectionChanged((IAction) null, selection); 465 } 466 if (isEnabled()) { 468 execute((IAction) null); 469 } else { 470 MessageDialog.openInformation(activeWorkbenchWindow.getShell(), 471 TeamUIMessages.TeamAction_handlerNotEnabledTitle, 472 TeamUIMessages.TeamAction_handlerNotEnabledMessage); 473 } 474 } 475 476 479 public void run(IAction action) { 480 try { 481 execute(action); 482 } catch (InvocationTargetException e) { 483 handle(e); 485 } catch (InterruptedException e) { 486 } 488 } 489 490 497 protected void handle(Exception e) { 498 handle(e, TeamUIMessages.TeamAction_errorTitle, null); 499 } 500 501 506 public void init(IAction action) { 507 } 508 509 514 final public void runWithEvent(IAction action, Event event) { 515 run(action); 516 } 517 518 } 519 | Popular Tags |