1 12 13 package org.eclipse.ant.internal.ui.views; 14 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 20 import org.eclipse.ant.internal.ui.IAntUIHelpContextIds; 21 import org.eclipse.ant.internal.ui.model.AntElementNode; 22 import org.eclipse.ant.internal.ui.model.AntModelLabelProvider; 23 import org.eclipse.ant.internal.ui.model.AntModelProblem; 24 import org.eclipse.ant.internal.ui.model.AntProjectNode; 25 import org.eclipse.ant.internal.ui.model.AntProjectNodeProxy; 26 import org.eclipse.ant.internal.ui.model.AntTargetNode; 27 import org.eclipse.ant.internal.ui.model.InternalTargetFilter; 28 import org.eclipse.ant.internal.ui.views.actions.AddBuildFilesAction; 29 import org.eclipse.ant.internal.ui.views.actions.AntOpenWithMenu; 30 import org.eclipse.ant.internal.ui.views.actions.FilterInternalTargetsAction; 31 import org.eclipse.ant.internal.ui.views.actions.RefreshBuildFilesAction; 32 import org.eclipse.ant.internal.ui.views.actions.RemoveAllAction; 33 import org.eclipse.ant.internal.ui.views.actions.RemoveProjectAction; 34 import org.eclipse.ant.internal.ui.views.actions.RunTargetAction; 35 import org.eclipse.ant.internal.ui.views.actions.SearchForBuildFilesAction; 36 import org.eclipse.core.resources.IFile; 37 import org.eclipse.core.resources.IResource; 38 import org.eclipse.core.resources.IResourceChangeEvent; 39 import org.eclipse.core.resources.IResourceChangeListener; 40 import org.eclipse.core.resources.IResourceDelta; 41 import org.eclipse.core.resources.ResourcesPlugin; 42 import org.eclipse.core.runtime.IPath; 43 import org.eclipse.core.runtime.Path; 44 import org.eclipse.jface.action.IMenuListener; 45 import org.eclipse.jface.action.IMenuManager; 46 import org.eclipse.jface.action.IToolBarManager; 47 import org.eclipse.jface.action.MenuManager; 48 import org.eclipse.jface.action.Separator; 49 import org.eclipse.jface.viewers.DoubleClickEvent; 50 import org.eclipse.jface.viewers.IDoubleClickListener; 51 import org.eclipse.jface.viewers.ISelection; 52 import org.eclipse.jface.viewers.ISelectionChangedListener; 53 import org.eclipse.jface.viewers.IStructuredSelection; 54 import org.eclipse.jface.viewers.SelectionChangedEvent; 55 import org.eclipse.jface.viewers.StructuredSelection; 56 import org.eclipse.jface.viewers.TreeViewer; 57 import org.eclipse.jface.viewers.Viewer; 58 import org.eclipse.jface.viewers.ViewerComparator; 59 import org.eclipse.jface.viewers.ViewerFilter; 60 import org.eclipse.jface.viewers.ViewerSorter; 61 import org.eclipse.swt.SWT; 62 import org.eclipse.swt.dnd.DND; 63 import org.eclipse.swt.dnd.FileTransfer; 64 import org.eclipse.swt.dnd.Transfer; 65 import org.eclipse.swt.events.KeyAdapter; 66 import org.eclipse.swt.events.KeyEvent; 67 import org.eclipse.swt.widgets.Composite; 68 import org.eclipse.swt.widgets.Control; 69 import org.eclipse.swt.widgets.Display; 70 import org.eclipse.swt.widgets.Menu; 71 import org.eclipse.ui.IMemento; 72 import org.eclipse.ui.IViewSite; 73 import org.eclipse.ui.IWorkbenchActionConstants; 74 import org.eclipse.ui.PartInitException; 75 import org.eclipse.ui.PlatformUI; 76 import org.eclipse.ui.XMLMemento; 77 import org.eclipse.ui.part.IShowInSource; 78 import org.eclipse.ui.part.ShowInContext; 79 import org.eclipse.ui.part.ViewPart; 80 import org.eclipse.ui.texteditor.IUpdate; 81 82 86 public class AntView extends ViewPart implements IResourceChangeListener, IShowInSource { 87 88 private Object [] restoredViewerInput= null; 89 private boolean filterInternalTargets= false; 90 private InternalTargetFilter fInternalTargetFilter= null; 91 97 private static IMemento fgTempMemento = null; 98 99 102 private static final String TAG_PROJECT = "project"; 107 private static final String KEY_ERROR = "error"; 112 private static final String KEY_WARNING = "warning"; 116 private static final String KEY_PATH = "path"; 120 private static final String KEY_NAME = "name"; 124 private static final String KEY_DEFAULT = "default"; 128 private static final String TAG_FILTER_INTERNAL_TARGETS = "filterInternalTargets"; 132 private static final String KEY_VALUE = "value"; 134 137 private TreeViewer projectViewer; 138 private AntViewContentProvider contentProvider; 139 140 144 private List updateProjectActions; 145 private AddBuildFilesAction addBuildFileAction; 147 private SearchForBuildFilesAction searchForBuildFilesAction; 148 private RefreshBuildFilesAction refreshBuildFilesAction; 149 private RemoveProjectAction removeProjectAction; 150 private RemoveAllAction removeAllAction; 151 private FilterInternalTargetsAction filterInternalTargetsAction; 152 private RunTargetAction runTargetAction; 153 private AntOpenWithMenu openWithMenu; 155 156 160 private void handleBuildFileChanged(AntProjectNode project) { 161 ((AntProjectNodeProxy)project).parseBuildFile(true); 162 Display.getDefault().asyncExec(new Runnable () { 163 public void run() { 164 projectViewer.refresh(); 166 handleSelectionChanged((IStructuredSelection) projectViewer.getSelection()); 168 } 169 }); 170 } 171 172 175 public void createPartControl(Composite parent) { 176 initializeActions(); 177 createProjectViewer(parent); 178 initializeDragAndDrop(); 179 fillMainToolBar(); 180 if (getProjects().length > 0) { 181 ResourcesPlugin.getWorkspace().addResourceChangeListener(this); 184 } 185 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IAntUIHelpContextIds.ANT_VIEW); 186 updateProjectActions(); 187 } 188 189 private void initializeDragAndDrop() { 190 int ops = DND.DROP_COPY | DND.DROP_DEFAULT; 191 Transfer[] transfers = new Transfer[] { FileTransfer.getInstance() }; 192 TreeViewer viewer = getViewer(); 193 AntViewDropAdapter adapter = new AntViewDropAdapter(this); 194 viewer.addDropSupport(ops, transfers, adapter); 195 } 196 197 203 private void createContextMenu(Viewer viewer) { 204 Control menuControl = viewer.getControl(); 205 MenuManager menuMgr = new MenuManager("#PopUp"); menuMgr.setRemoveAllWhenShown(true); 207 menuMgr.addMenuListener(new IMenuListener() { 208 public void menuAboutToShow(IMenuManager mgr) { 209 fillContextMenu(mgr); 210 } 211 }); 212 Menu menu = menuMgr.createContextMenu(menuControl); 213 menuControl.setMenu(menu); 214 215 getSite().registerContextMenu(menuMgr, viewer); 217 } 218 219 225 private void fillContextMenu(IMenuManager menu) { 226 addOpenWithMenu(menu); 227 menu.add(new Separator()); 228 menu.add(addBuildFileAction); 229 menu.add(removeProjectAction); 230 menu.add(removeAllAction); 231 menu.add(refreshBuildFilesAction); 232 233 menu.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 234 } 235 236 private void addOpenWithMenu(IMenuManager menu) { 237 AntElementNode node= getSelectionNode(); 238 if (node != null) { 239 IFile buildFile= node.getIFile(); 240 if (buildFile != null) { 241 menu.add(new Separator("group.open")); IMenuManager submenu= new MenuManager(AntViewMessages.AntView_1); 243 openWithMenu.setNode(node); 244 submenu.add(openWithMenu); 245 menu.appendToGroup("group.open", submenu); } 247 } 248 } 249 250 253 private void initializeActions() { 254 updateProjectActions= new ArrayList (5); 255 256 addBuildFileAction = new AddBuildFilesAction(this); 257 258 removeProjectAction = new RemoveProjectAction(this); 259 updateProjectActions.add(removeProjectAction); 260 261 removeAllAction = new RemoveAllAction(this); 262 updateProjectActions.add(removeAllAction); 263 264 runTargetAction = new RunTargetAction(this); 265 updateProjectActions.add(runTargetAction); 266 267 searchForBuildFilesAction = new SearchForBuildFilesAction(this); 268 269 refreshBuildFilesAction = new RefreshBuildFilesAction(this); 270 updateProjectActions.add(refreshBuildFilesAction); 271 272 openWithMenu= new AntOpenWithMenu(this.getViewSite().getPage()); 273 274 filterInternalTargetsAction= new FilterInternalTargetsAction(this); 275 } 276 277 281 private void updateProjectActions() { 282 Iterator iter = updateProjectActions.iterator(); 283 while (iter.hasNext()) { 284 ((IUpdate) iter.next()).update(); 285 } 286 } 287 288 291 private void createProjectViewer(Composite parent) { 292 projectViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI); 293 contentProvider = new AntViewContentProvider(); 294 projectViewer.setContentProvider(contentProvider); 295 296 filterInternalTargetsAction.setChecked(filterInternalTargets); 297 setFilterInternalTargets(filterInternalTargets); 298 299 projectViewer.setLabelProvider(new AntModelLabelProvider()); 300 if (fgTempMemento != null) { 301 restoreViewerInput(fgTempMemento); 302 fgTempMemento= null; 303 } 304 projectViewer.setInput(new Object [0]); 305 if (restoredViewerInput.length > 0) { 306 contentProvider.addAll(restoredViewerInput); 307 } 308 projectViewer.setComparator(new ViewerComparator() { 309 312 public int compare(Viewer viewer, Object e1, Object e2) { 313 if (e1 instanceof AntProjectNode && e2 instanceof AntProjectNode 314 || e1 instanceof AntTargetNode && e2 instanceof AntTargetNode) { 315 return e1.toString().compareToIgnoreCase(e2.toString()); 316 } 317 return 0; 318 } 319 }); 320 321 projectViewer.addSelectionChangedListener(new ISelectionChangedListener() { 322 public void selectionChanged(SelectionChangedEvent event) { 323 handleSelectionChanged((IStructuredSelection)event.getSelection()); 324 } 325 }); 326 327 projectViewer.addDoubleClickListener(new IDoubleClickListener() { 328 public void doubleClick(DoubleClickEvent event) { 329 if (!event.getSelection().isEmpty()) { 330 handleProjectViewerDoubleClick(); 331 } 332 } 333 }); 334 335 projectViewer.getControl().addKeyListener(new KeyAdapter() { 336 public void keyPressed(KeyEvent event) { 337 handleProjectViewerKeyPress(event); 338 } 339 }); 340 341 createContextMenu(projectViewer); 342 getSite().setSelectionProvider(projectViewer); 343 } 344 345 private void handleProjectViewerKeyPress(KeyEvent event) { 346 if (event.character == SWT.DEL && event.stateMask == 0) { 347 if (removeProjectAction.isEnabled()) { 348 removeProjectAction.run(); 349 } 350 } else if (event.keyCode == SWT.F5 && event.stateMask == 0) { 351 if (refreshBuildFilesAction.isEnabled()) { 352 refreshBuildFilesAction.run(); 353 } 354 } 355 } 356 357 private void handleProjectViewerDoubleClick() { 358 AntElementNode node= getSelectionNode(); 359 if (node != null) { 360 runTargetAction.run(node); 361 } 362 } 363 364 368 private void handleSelectionChanged(IStructuredSelection selection) { 369 updateProjectActions(); 370 Iterator selectionIter = selection.iterator(); 371 AntElementNode selected = null; 372 if (selectionIter.hasNext()) { 373 selected = (AntElementNode) selectionIter.next(); 374 } 375 String messageString= null; 376 if (!selectionIter.hasNext()) { 377 if (selected != null) { 378 String errorString= selected.getProblemMessage(); 379 if (errorString != null) { 380 getViewSite().getActionBars().getStatusLineManager().setErrorMessage(errorString); 381 return; 382 } 383 } 384 getViewSite().getActionBars().getStatusLineManager().setErrorMessage(null); 385 messageString= getStatusLineText(selected); 386 } 387 getViewSite().getActionBars().getStatusLineManager().setMessage(messageString); 388 } 389 390 394 private String getStatusLineText(AntElementNode node) { 395 if (node instanceof AntProjectNode) { 396 AntProjectNode project = (AntProjectNode) node; 397 StringBuffer message= new StringBuffer (project.getBuildFileName()); 398 String description= project.getDescription(); 399 if (description != null && description.length() > 0) { 400 message.append(": "); message.append(description); 402 } 403 return message.toString(); 404 } else if (node instanceof AntTargetNode) { 405 AntTargetNode target = (AntTargetNode) node; 406 StringBuffer message= new StringBuffer (); 407 Enumeration depends= target.getTarget().getDependencies(); 408 if (depends.hasMoreElements()) { 409 message.append(AntViewMessages.AntView_3); 410 message.append((String )depends.nextElement()); while (depends.hasMoreElements()) { 412 String dependancy = (String ) depends.nextElement(); 413 message.append(',').append(dependancy); 414 } 415 message.append('\"'); 416 } 417 String description= target.getTarget().getDescription(); 418 if (description != null && description.length() != 0) { 419 message.append(AntViewMessages.AntView_4); 420 message.append(description); 421 message.append('\"'); 422 } 423 return message.toString(); 424 } 425 return null; 426 } 427 428 433 public TreeViewer getViewer() { 434 return projectViewer; 435 } 436 437 443 public AntProjectNode[] getProjects() { 444 Object [] objects= contentProvider.getElements(projectViewer.getInput()); 445 AntProjectNode[] projects= new AntProjectNode[objects.length]; 446 for (int i = 0; i < projects.length; i++) { 447 projects[i] = (AntProjectNode)objects[i]; 448 } 449 return projects; 450 } 451 452 457 public void addProject(AntProjectNode project) { 458 contentProvider.add(project); 459 projectViewer.refresh(); 460 ResourcesPlugin.getWorkspace().addResourceChangeListener(this); 461 handleSelectionChanged(new StructuredSelection(project)); 462 } 463 464 469 private void removeProject(AntProjectNode project) { 470 removeProjectFromContentProvider(project); 471 projectViewer.refresh(); 472 setProjectViewerSelectionAfterDeletion(); 473 } 474 475 private void setProjectViewerSelectionAfterDeletion() { 476 Object [] children= getProjects(); 477 if (children.length > 0) { 478 ViewerSorter sorter= projectViewer.getSorter(); 479 sorter.sort(projectViewer, children); 480 IStructuredSelection selection= new StructuredSelection(children[0]); 481 projectViewer.setSelection(selection); 482 handleSelectionChanged(selection); 483 } 484 } 485 486 495 public void removeProjects(List projectNodes) { 496 Iterator iter = projectNodes.iterator(); 497 while (iter.hasNext()) { 498 AntProjectNode project = (AntProjectNode) iter.next(); 499 removeProjectFromContentProvider(project); 500 } 501 projectViewer.refresh(); 502 setProjectViewerSelectionAfterDeletion(); 503 } 504 505 510 private void removeProjectFromContentProvider(AntProjectNode project) { 511 project.dispose(); 512 contentProvider.remove(project); 513 if (getProjects().length == 0) { 514 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 515 } 516 } 517 518 521 public void removeAllProjects() { 522 AntProjectNode[] projects= getProjects(); 523 for (int i = 0; i < projects.length; i++) { 524 AntProjectNode node = projects[i]; 525 node.dispose(); 526 } 527 contentProvider.removeAll(); 529 530 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 531 updateProjectActions(); 532 } 533 534 537 public void init(IViewSite site, IMemento memento) throws PartInitException { 538 init(site); 539 restoreViewerInput(memento); 540 if (memento != null) { 541 IMemento child= memento.getChild(TAG_FILTER_INTERNAL_TARGETS); 542 if (child != null) { 543 filterInternalTargets= Boolean.valueOf(child.getString(KEY_VALUE)).booleanValue(); 544 } 545 } 546 } 547 548 553 private void restoreViewerInput(IMemento memento) { 554 if (memento == null) { 555 restoredViewerInput= new Object [0]; 556 return; 557 } 558 IMemento[] projects = memento.getChildren(TAG_PROJECT); 559 if (projects.length < 1) { 560 restoredViewerInput= new Object [0]; 561 return; 562 } 563 List projectNodes = new ArrayList (projects.length); 564 for (int i = 0; i < projects.length; i++) { 565 IMemento projectMemento = projects[i]; 566 String pathString = projectMemento.getString(KEY_PATH); 567 if (!ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(pathString)).exists()) { 568 continue; 570 } 571 String nameString = projectMemento.getString(KEY_NAME); 572 String defaultTarget= projectMemento.getString(KEY_DEFAULT); 573 String errorString = projectMemento.getString(KEY_ERROR); 574 String warningString = projectMemento.getString(KEY_WARNING); 575 576 AntProjectNodeProxy project = null; 577 if (nameString == null) { 578 nameString = ""; } 580 project = new AntProjectNodeProxy(nameString, pathString); 581 if (errorString != null && Boolean.valueOf(errorString).booleanValue()) { 582 project.setProblemSeverity(AntModelProblem.SEVERITY_ERROR); 583 } else if (warningString != null && Boolean.valueOf(warningString).booleanValue()) { 584 project.setProblemSeverity(AntModelProblem.SEVERITY_WARNING); 585 } 586 if (defaultTarget != null) { 587 project.setDefaultTargetName(defaultTarget); 588 } 589 projectNodes.add(project); 590 } 591 restoredViewerInput= projectNodes.toArray(new AntProjectNode[projectNodes.size()]); 592 } 593 594 597 public void saveState(IMemento memento) { 598 AntProjectNode[] projects = getProjects(); 600 AntProjectNode project; 601 IMemento projectMemento; 602 for (int i = 0; i < projects.length; i++) { 603 project= projects[i]; 604 projectMemento = memento.createChild(TAG_PROJECT); 605 projectMemento.putString(KEY_PATH, project.getBuildFileName()); 606 projectMemento.putString(KEY_NAME, project.getLabel()); 607 String defaultTarget= project.getDefaultTargetName(); 608 if (project.isErrorNode()) { 609 projectMemento.putString(KEY_ERROR, String.valueOf(true)); 610 } else { 611 if (project.isWarningNode()) { 612 projectMemento.putString(KEY_WARNING, String.valueOf(true)); 613 } 614 if (defaultTarget != null) { 615 projectMemento.putString(KEY_DEFAULT, defaultTarget); 616 } 617 projectMemento.putString(KEY_ERROR, String.valueOf(false)); 618 } 619 } 620 IMemento filterTargets= memento.createChild(TAG_FILTER_INTERNAL_TARGETS); 621 filterTargets.putString(KEY_VALUE, isFilterInternalTargets() ? String.valueOf(true) : String.valueOf(false)); 622 } 623 624 627 public void dispose() { 628 fgTempMemento= XMLMemento.createWriteRoot("AntViewMemento"); saveState(fgTempMemento); 630 super.dispose(); 631 if (openWithMenu != null) { 632 openWithMenu.dispose(); 633 } 634 ResourcesPlugin.getWorkspace().removeResourceChangeListener(this); 635 } 636 637 640 public void resourceChanged(IResourceChangeEvent event) { 641 IResourceDelta delta = event.getDelta(); 642 if (delta != null) { 643 AntProjectNode projects[]= getProjects(); 644 IPath buildFilePath; 645 for (int i = 0; i < projects.length; i++) { 646 buildFilePath= new Path(projects[i].getBuildFileName()); 647 IResourceDelta change= delta.findMember(buildFilePath); 648 if (change != null) { 649 handleChangeDelta(change, projects[i]); 650 } 651 } 652 } 653 } 654 655 662 private void handleChangeDelta(IResourceDelta delta, final AntProjectNode project) { 663 IResource resource= delta.getResource(); 664 if (resource.getType() != IResource.FILE) { 665 return; 666 } 667 if (delta.getKind() == IResourceDelta.REMOVED) { 668 Display.getDefault().asyncExec(new Runnable () { 669 public void run() { 670 removeProject(project); 671 } 672 }); 673 } else if (delta.getKind() == IResourceDelta.CHANGED && (delta.getFlags() & IResourceDelta.CONTENT) != 0) { 674 handleBuildFileChanged(project); 675 } 676 } 677 678 private void fillMainToolBar() { 679 IToolBarManager toolBarMgr= getViewSite().getActionBars().getToolBarManager(); 680 toolBarMgr.removeAll(); 681 682 toolBarMgr.add(addBuildFileAction); 683 toolBarMgr.add(searchForBuildFilesAction); 684 toolBarMgr.add(filterInternalTargetsAction); 685 686 toolBarMgr.add(runTargetAction); 687 toolBarMgr.add(removeProjectAction); 688 toolBarMgr.add(removeAllAction); 689 690 toolBarMgr.update(false); 691 } 692 693 private AntElementNode getSelectionNode() { 694 IStructuredSelection selection= (IStructuredSelection)getViewer().getSelection(); 695 if (selection.size() == 1) { 696 Object element= selection.getFirstElement(); 697 if (element instanceof AntElementNode) { 698 AntElementNode node= (AntElementNode) element; 699 return node; 700 } 701 } 702 return null; 703 } 704 705 708 public ShowInContext getShowInContext() { 709 AntElementNode node= getSelectionNode(); 710 if (node != null) { 711 IFile buildFile= node.getIFile(); 712 if (buildFile != null) { 713 ISelection selection= new StructuredSelection(buildFile); 714 return new ShowInContext(null, selection); 715 } 716 } 717 return null; 718 } 719 720 726 public boolean isFilterInternalTargets() { 727 return filterInternalTargets; 728 } 729 730 735 public void setFilterInternalTargets(boolean filter) { 736 filterInternalTargets= filter; 737 if (filter) { 738 projectViewer.addFilter(getInternalTargetsFilter()); 739 } else { 740 projectViewer.removeFilter(getInternalTargetsFilter()); 741 } 742 } 743 744 private ViewerFilter getInternalTargetsFilter() { 745 if (fInternalTargetFilter == null) { 746 fInternalTargetFilter= new InternalTargetFilter(); 747 } 748 return fInternalTargetFilter; 749 } 750 751 754 public void setFocus() { 755 if (getViewer() != null) { 756 getViewer().getControl().setFocus(); 757 } 758 } 759 } 760 | Popular Tags |