1 11 12 package org.eclipse.ui.views.bookmarkexplorer; 13 14 import java.util.ArrayList ; 15 16 import org.eclipse.core.resources.IMarker; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.resources.IWorkspace; 19 import org.eclipse.core.resources.IWorkspaceRoot; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.jface.action.Action; 24 import org.eclipse.jface.action.IMenuListener; 25 import org.eclipse.jface.action.IMenuManager; 26 import org.eclipse.jface.action.IToolBarManager; 27 import org.eclipse.jface.action.MenuManager; 28 import org.eclipse.jface.action.Separator; 29 import org.eclipse.jface.dialogs.IDialogSettings; 30 import org.eclipse.jface.viewers.ColumnLayoutData; 31 import org.eclipse.jface.viewers.ColumnPixelData; 32 import org.eclipse.jface.viewers.ColumnWeightData; 33 import org.eclipse.jface.viewers.ILabelProvider; 34 import org.eclipse.jface.viewers.IOpenListener; 35 import org.eclipse.jface.viewers.ISelectionChangedListener; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 import org.eclipse.jface.viewers.OpenEvent; 38 import org.eclipse.jface.viewers.SelectionChangedEvent; 39 import org.eclipse.jface.viewers.StructuredSelection; 40 import org.eclipse.jface.viewers.StructuredViewer; 41 import org.eclipse.jface.viewers.TableLayout; 42 import org.eclipse.jface.viewers.TableViewer; 43 import org.eclipse.swt.SWT; 44 import org.eclipse.swt.dnd.Clipboard; 45 import org.eclipse.swt.dnd.DND; 46 import org.eclipse.swt.dnd.DragSourceAdapter; 47 import org.eclipse.swt.dnd.DragSourceEvent; 48 import org.eclipse.swt.dnd.DragSourceListener; 49 import org.eclipse.swt.dnd.TextTransfer; 50 import org.eclipse.swt.dnd.Transfer; 51 import org.eclipse.swt.events.KeyAdapter; 52 import org.eclipse.swt.events.KeyEvent; 53 import org.eclipse.swt.events.SelectionAdapter; 54 import org.eclipse.swt.events.SelectionEvent; 55 import org.eclipse.swt.events.SelectionListener; 56 import org.eclipse.swt.widgets.Composite; 57 import org.eclipse.swt.widgets.Menu; 58 import org.eclipse.swt.widgets.ScrollBar; 59 import org.eclipse.swt.widgets.Scrollable; 60 import org.eclipse.swt.widgets.Shell; 61 import org.eclipse.swt.widgets.Table; 62 import org.eclipse.swt.widgets.TableColumn; 63 import org.eclipse.ui.IActionBars; 64 import org.eclipse.ui.IMemento; 65 import org.eclipse.ui.IPageLayout; 66 import org.eclipse.ui.ISharedImages; 67 import org.eclipse.ui.IViewSite; 68 import org.eclipse.ui.IWorkbenchActionConstants; 69 import org.eclipse.ui.PartInitException; 70 import org.eclipse.ui.PlatformUI; 71 import org.eclipse.ui.actions.ActionFactory; 72 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 73 import org.eclipse.ui.internal.views.bookmarkexplorer.BookmarkMessages; 74 import org.eclipse.ui.part.IShowInSource; 75 import org.eclipse.ui.part.IShowInTargetList; 76 import org.eclipse.ui.part.MarkerTransfer; 77 import org.eclipse.ui.part.ShowInContext; 78 import org.eclipse.ui.part.ViewPart; 79 import org.eclipse.ui.plugin.AbstractUIPlugin; 80 import org.eclipse.ui.views.navigator.ShowInNavigatorAction; 81 82 95 public class BookmarkNavigator extends ViewPart { 96 private Table table; 97 98 private TableViewer viewer; 99 100 private OpenBookmarkAction openAction; 101 102 private CopyBookmarkAction copyAction; 103 104 private PasteBookmarkAction pasteAction; 105 106 private RemoveBookmarkAction removeAction; 107 108 private EditBookmarkAction editAction; 109 110 private SelectAllAction selectAllAction; 111 112 private ShowInNavigatorAction showInNavigatorAction; 113 114 private SortByAction sortByDescriptionAction; 115 116 private SortByAction sortByResourceAction; 117 118 private SortByAction sortByFolderAction; 119 120 private SortByAction sortByLineAction; 121 122 private SortByAction sortByCreationTime; 123 124 private ChangeSortDirectionAction sortAscendingAction; 125 126 private ChangeSortDirectionAction sortDescendingAction; 127 128 private IMemento memento; 129 130 private BookmarkSorter comparator; 131 132 private Clipboard clipboard; 133 134 private final String columnHeaders[] = { 135 BookmarkMessages.ColumnIcon_header, 136 BookmarkMessages.ColumnDescription_header, 137 BookmarkMessages.ColumnResource_header, 138 BookmarkMessages.ColumnFolder_header, 139 BookmarkMessages.ColumnLocation_header }; 140 141 private ColumnLayoutData columnLayouts[] = { 142 new ColumnPixelData(16, false, true), new ColumnWeightData(200), 143 new ColumnWeightData(75), new ColumnWeightData(150), 144 new ColumnWeightData(60) }; 145 146 private static final String TAG_SELECTION = "selection"; 149 private static final String TAG_ID = "id"; 151 private static final String TAG_MARKER = "marker"; 153 private static final String TAG_RESOURCE = "resource"; 155 private static final String TAG_VERTICAL_POSITION = "verticalPosition"; 157 private static final String TAG_HORIZONTAL_POSITION = "horizontalPosition"; 159 class SortByAction extends Action { 160 161 private int column; 162 163 166 public SortByAction(int column) { 167 this.column = column; 168 } 169 170 public void run() { 171 comparator.setTopPriority(column); 172 updateSortState(); 173 viewer.refresh(); 174 IDialogSettings workbenchSettings = getPlugin().getDialogSettings(); 175 IDialogSettings settings = workbenchSettings 176 .getSection("BookmarkSortState"); if (settings == null) { 178 settings = workbenchSettings.addNewSection("BookmarkSortState"); } 180 comparator.saveState(settings); 181 } 182 } 183 184 class ChangeSortDirectionAction extends Action { 185 186 private int direction; 187 188 191 public ChangeSortDirectionAction(int direction) { 192 this.direction = direction; 193 } 194 195 public void run() { 196 comparator.setTopPriorityDirection(direction); 197 updateSortState(); 198 viewer.refresh(); 199 IDialogSettings workbenchSettings = getPlugin().getDialogSettings(); 200 IDialogSettings settings = workbenchSettings 201 .getSection("BookmarkSortState"); if (settings == null) { 203 settings = workbenchSettings.addNewSection("BookmarkSortState"); } 205 comparator.saveState(settings); 206 } 207 } 208 209 212 public BookmarkNavigator() { 213 super(); 214 } 215 216 219 void addContributions() { 220 ISharedImages sharedImages = PlatformUI.getWorkbench() 221 .getSharedImages(); 222 223 openAction = new OpenBookmarkAction(this); 225 openAction 226 .setImageDescriptor(IDEWorkbenchPlugin.getIDEImageDescriptor("elcl16/gotoobj_tsk.gif")); 228 copyAction = new CopyBookmarkAction(this); 229 copyAction.setImageDescriptor(sharedImages 230 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); 231 232 pasteAction = new PasteBookmarkAction(this); 233 pasteAction.setImageDescriptor(sharedImages 234 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE)); 235 236 removeAction = new RemoveBookmarkAction(this); 237 removeAction.setImageDescriptor(sharedImages 238 .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); 239 removeAction.setDisabledImageDescriptor(sharedImages 240 .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED)); 241 242 editAction = new EditBookmarkAction(this); 243 244 selectAllAction = new SelectAllAction(this); 245 showInNavigatorAction = new ShowInNavigatorAction(getViewSite() 246 .getPage(), viewer); 247 248 handleSelectionChanged(StructuredSelection.EMPTY); 250 251 MenuManager mgr = new MenuManager(); 254 mgr.setRemoveAllWhenShown(true); 255 mgr.addMenuListener(new IMenuListener() { 256 public void menuAboutToShow(IMenuManager mgr) { 257 fillContextMenu(mgr); 258 } 259 }); 260 Menu menu = mgr.createContextMenu(viewer.getControl()); 261 viewer.getControl().setMenu(menu); 262 getSite().registerContextMenu(mgr, viewer); 263 264 IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager(); 266 tbm.add(removeAction); 267 tbm.add(openAction); 268 tbm.update(false); 269 270 IActionBars actionBars = getViewSite().getActionBars(); 272 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), 273 copyAction); 274 actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), 275 pasteAction); 276 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), 277 removeAction); 278 actionBars.setGlobalActionHandler(ActionFactory.SELECT_ALL.getId(), 279 selectAllAction); 280 281 viewer.addOpenListener(new IOpenListener() { 283 public void open(OpenEvent event) { 284 openAction.run(); 285 } 286 }); 287 viewer.addSelectionChangedListener(new ISelectionChangedListener() { 288 public void selectionChanged(SelectionChangedEvent event) { 289 handleSelectionChanged((IStructuredSelection) event 290 .getSelection()); 291 } 292 }); 293 viewer.getControl().addKeyListener(new KeyAdapter() { 294 public void keyPressed(KeyEvent e) { 295 handleKeyPressed(e); 296 } 297 }); 298 } 299 300 303 public void createPartControl(Composite parent) { 304 clipboard = new Clipboard(parent.getDisplay()); 305 createTable(parent); 306 viewer = new TableViewer(table); 307 createColumns(); 308 309 comparator = new BookmarkSorter(); 310 viewer.setContentProvider(new BookmarkContentProvider(this)); 311 viewer.setLabelProvider(new BookmarkLabelProvider(this)); 312 viewer.setInput(ResourcesPlugin.getWorkspace().getRoot()); 313 viewer.setComparator(comparator); 314 315 IDialogSettings workbenchSettings = getPlugin().getDialogSettings(); 316 IDialogSettings settings = workbenchSettings 317 .getSection("BookmarkSortState"); comparator.restoreState(settings); 319 320 addContributions(); 321 initDragAndDrop(); 322 createSortActions(); 323 fillActionBars(); 324 updateSortState(); 325 updatePasteEnablement(); 326 327 getSite().setSelectionProvider(viewer); 328 329 if (memento != null) { 330 restoreState(memento); 331 } 332 memento = null; 333 334 PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), 335 IBookmarkHelpContextIds.BOOKMARK_VIEW); 336 } 337 338 public void dispose() { 339 if (clipboard != null) { 340 clipboard.dispose(); 341 } 342 } 343 344 350 void fillContextMenu(IMenuManager manager) { 351 manager.add(openAction); 352 manager.add(copyAction); 353 updatePasteEnablement(); 354 manager.add(pasteAction); 355 manager.add(removeAction); 356 manager.add(selectAllAction); 357 manager.add(showInNavigatorAction); 358 manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS)); 359 manager.add(new Separator()); 360 manager.add(editAction); 361 } 362 363 366 public Object getAdapter(Class adapter) { 367 if (adapter == IShowInSource.class) { 368 return new IShowInSource() { 369 public ShowInContext getShowInContext() { 370 return new ShowInContext(null, getViewer().getSelection()); 371 } 372 }; 373 } 374 if (adapter == IShowInTargetList.class) { 375 return new IShowInTargetList() { 376 public String [] getShowInTargetIds() { 377 return new String [] { IPageLayout.ID_RES_NAV }; 378 } 379 380 }; 381 } 382 return super.getAdapter(adapter); 383 } 384 385 388 static AbstractUIPlugin getPlugin() { 389 return (AbstractUIPlugin) Platform.getPlugin(PlatformUI.PLUGIN_ID); 390 } 391 392 395 Shell getShell() { 396 return getViewSite().getShell(); 397 } 398 399 405 StructuredViewer getViewer() { 406 return viewer; 407 } 408 409 412 IWorkspace getWorkspace() { 413 return ResourcesPlugin.getWorkspace(); 414 } 415 416 419 void handleKeyPressed(KeyEvent event) { 420 if (event.character == SWT.DEL && event.stateMask == 0 421 && removeAction.isEnabled()) { 422 removeAction.run(); 423 } 424 } 425 426 431 void handleSelectionChanged(IStructuredSelection selection) { 432 openAction.selectionChanged(selection); 434 removeAction.selectionChanged(selection); 435 editAction.selectionChanged(selection); 436 selectAllAction.selectionChanged(selection); 437 showInNavigatorAction.selectionChanged(selection); 438 } 439 440 443 public void init(IViewSite site, IMemento memento) throws PartInitException { 444 super.init(site, memento); 445 this.memento = memento; 446 } 447 448 451 protected void initDragAndDrop() { 452 int operations = DND.DROP_COPY; 453 Transfer[] transferTypes = new Transfer[] { 454 MarkerTransfer.getInstance(), TextTransfer.getInstance() }; 455 DragSourceListener listener = new DragSourceAdapter() { 456 public void dragSetData(DragSourceEvent event) { 457 performDragSetData(event); 458 } 459 460 public void dragFinished(DragSourceEvent event) { 461 } 462 }; 463 viewer.addDragSupport(operations, transferTypes, listener); 464 } 465 466 470 void performDragSetData(DragSourceEvent event) { 471 if (MarkerTransfer.getInstance().isSupportedType(event.dataType)) { 472 event.data = ((IStructuredSelection) viewer.getSelection()) 473 .toArray(); 474 return; 475 } 476 if (TextTransfer.getInstance().isSupportedType(event.dataType)) { 477 Object [] markers = ((IStructuredSelection) viewer.getSelection()) 478 .toArray(); 479 if (markers != null) { 480 StringBuffer buffer = new StringBuffer (); 481 ILabelProvider provider = (ILabelProvider) getViewer() 482 .getLabelProvider(); 483 for (int i = 0; i < markers.length; i++) { 484 if (i > 0) { 485 buffer.append(System.getProperty("line.separator")); } 487 String text = provider.getText(markers[i]); 488 if(text != null) { 489 buffer.append(text); 490 } 491 } 492 event.data = buffer.toString(); 493 } 494 return; 495 } 496 } 497 498 void restoreState(IMemento memento) { 499 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 500 IMemento selectionMem = memento.getChild(TAG_SELECTION); 501 if (selectionMem != null) { 502 ArrayList selectionList = new ArrayList (); 503 IMemento markerMems[] = selectionMem.getChildren(TAG_MARKER); 504 for (int i = 0; i < markerMems.length; i++) { 505 try { 506 long id = new Long (markerMems[i].getString(TAG_ID)) 507 .longValue(); 508 IResource resource = root.findMember(markerMems[i] 509 .getString(TAG_RESOURCE)); 510 if (resource != null) { 511 IMarker marker = resource.findMarker(id); 512 if (marker != null) { 513 selectionList.add(marker); 514 } 515 } 516 } catch (CoreException e) { 517 } 518 } 519 viewer.setSelection(new StructuredSelection(selectionList)); 520 } 521 522 Scrollable scrollable = (Scrollable) viewer.getControl(); 523 ScrollBar bar = scrollable.getVerticalBar(); 525 if (bar != null) { 526 try { 527 String posStr = memento.getString(TAG_VERTICAL_POSITION); 528 int position; 529 position = new Integer (posStr).intValue(); 530 bar.setSelection(position); 531 } catch (NumberFormatException e) { 532 } 533 } 534 bar = scrollable.getHorizontalBar(); 535 if (bar != null) { 536 try { 537 String posStr = memento.getString(TAG_HORIZONTAL_POSITION); 538 int position; 539 position = new Integer (posStr).intValue(); 540 bar.setSelection(position); 541 } catch (NumberFormatException e) { 542 } 543 } 544 545 updateSortState(); 546 viewer.refresh(); 547 } 548 549 public void saveState(IMemento memento) { 550 if (viewer == null) { 551 if (this.memento != null) { 552 memento.putMemento(this.memento); 553 } 554 return; 555 } 556 557 Scrollable scrollable = (Scrollable) viewer.getControl(); 558 Object markers[] = ((IStructuredSelection) viewer.getSelection()) 559 .toArray(); 560 if (markers.length > 0) { 561 IMemento selectionMem = memento.createChild(TAG_SELECTION); 562 for (int i = 0; i < markers.length; i++) { 563 IMemento elementMem = selectionMem.createChild(TAG_MARKER); 564 IMarker marker = (IMarker) markers[i]; 565 elementMem.putString(TAG_RESOURCE, marker.getResource() 566 .getFullPath().toString()); 567 elementMem.putString(TAG_ID, String.valueOf(marker.getId())); 568 } 569 } 570 571 ScrollBar bar = scrollable.getVerticalBar(); 573 int position = bar != null ? bar.getSelection() : 0; 574 memento.putString(TAG_VERTICAL_POSITION, String.valueOf(position)); 575 bar = scrollable.getHorizontalBar(); 577 position = bar != null ? bar.getSelection() : 0; 578 memento.putString(TAG_HORIZONTAL_POSITION, String.valueOf(position)); 579 580 } 581 582 585 public void setFocus() { 586 if (viewer != null) { 587 viewer.getControl().setFocus(); 588 } 589 } 590 591 void createColumns() { 592 SelectionListener headerListener = new SelectionAdapter() { 593 602 public void widgetSelected(SelectionEvent e) { 603 int column = table.indexOf((TableColumn) e.widget) - 1; 605 if (column == comparator.getTopPriority()) { 606 comparator.reverseTopPriority(); 607 } else { 608 comparator.setTopPriority(column); 609 } 610 updateSortState(); 611 viewer.refresh(); 612 IDialogSettings workbenchSettings = getPlugin() 613 .getDialogSettings(); 614 IDialogSettings settings = workbenchSettings 615 .getSection("BookmarkSortState"); if (settings == null) { 617 settings = workbenchSettings 618 .addNewSection("BookmarkSortState"); } 620 comparator.saveState(settings); 621 } 622 }; 623 624 TableLayout layout = new TableLayout(); 625 table.setLayout(layout); 626 table.setHeaderVisible(true); 627 for (int i = 0; i < columnHeaders.length; i++) { 628 layout.addColumnData(columnLayouts[i]); 629 TableColumn tc = new TableColumn(table, SWT.NONE, i); 630 tc.setResizable(columnLayouts[i].resizable); 631 tc.setText(columnHeaders[i]); 632 if (i > 0) { 633 tc.addSelectionListener(headerListener); 634 } 635 } 636 } 637 638 641 void createTable(Composite parent) { 642 table = new Table(parent, SWT.H_SCROLL | SWT.V_SCROLL | SWT.MULTI 643 | SWT.FULL_SELECTION); 644 table.setLinesVisible(true); 645 } 647 648 651 void fillActionBars() { 652 IActionBars actionBars = getViewSite().getActionBars(); 653 IMenuManager menu = actionBars.getMenuManager(); 654 IMenuManager submenu = new MenuManager(BookmarkMessages.SortMenuGroup_text); 655 menu.add(submenu); 656 submenu.add(sortByDescriptionAction); 657 submenu.add(sortByResourceAction); 658 submenu.add(sortByFolderAction); 659 submenu.add(sortByLineAction); 660 submenu.add(sortByCreationTime); 661 submenu.add(new Separator()); 662 submenu.add(sortAscendingAction); 663 submenu.add(sortDescendingAction); 664 } 665 666 void createSortActions() { 667 sortByDescriptionAction = new SortByAction(BookmarkSorter.DESCRIPTION); 668 sortByDescriptionAction.setText(BookmarkMessages.ColumnDescription_text); 669 PlatformUI.getWorkbench().getHelpSystem().setHelp( 670 sortByDescriptionAction, 671 IBookmarkHelpContextIds.SORT_DESCRIPTION_ACTION); 672 673 sortByResourceAction = new SortByAction(BookmarkSorter.RESOURCE); 674 sortByResourceAction.setText(BookmarkMessages.ColumnResource_text); 675 PlatformUI.getWorkbench().getHelpSystem().setHelp(sortByResourceAction, 676 IBookmarkHelpContextIds.SORT_RESOURCE_ACTION); 677 678 sortByFolderAction = new SortByAction(BookmarkSorter.FOLDER); 679 sortByFolderAction.setText(BookmarkMessages.ColumnFolder_text); 680 PlatformUI.getWorkbench().getHelpSystem().setHelp(sortByFolderAction, 681 IBookmarkHelpContextIds.SORT_FOLDER_ACTION); 682 683 sortByLineAction = new SortByAction(BookmarkSorter.LOCATION); 684 sortByLineAction.setText(BookmarkMessages.ColumnLocation_text); 685 PlatformUI.getWorkbench().getHelpSystem().setHelp(sortByLineAction, 686 IBookmarkHelpContextIds.SORT_LOCATION_ACTION); 687 688 sortByCreationTime = new SortByAction(BookmarkSorter.CREATION_TIME); 689 sortByCreationTime.setText(BookmarkMessages.ColumnCreationTime_text); 690 PlatformUI.getWorkbench().getHelpSystem().setHelp(sortByCreationTime, 691 IBookmarkHelpContextIds.SORT_CREATION_TIME_ACTION); 692 693 sortAscendingAction = new ChangeSortDirectionAction( 694 BookmarkSorter.ASCENDING); 695 sortAscendingAction.setText(BookmarkMessages.SortDirectionAscending_text); 696 PlatformUI.getWorkbench().getHelpSystem().setHelp(sortAscendingAction, 697 IBookmarkHelpContextIds.SORT_ASCENDING_ACTION); 698 699 sortDescendingAction = new ChangeSortDirectionAction( 700 BookmarkSorter.DESCENDING); 701 sortDescendingAction.setText(BookmarkMessages.SortDirectionDescending_text); 702 PlatformUI.getWorkbench().getHelpSystem().setHelp(sortDescendingAction, 703 IBookmarkHelpContextIds.SORT_DESCENDING_ACTION); 704 } 705 706 void updateSortState() { 707 int column = comparator.getTopPriority(); 708 sortByDescriptionAction 709 .setChecked(column == BookmarkSorter.DESCRIPTION); 710 sortByResourceAction.setChecked(column == BookmarkSorter.RESOURCE); 711 sortByFolderAction.setChecked(column == BookmarkSorter.FOLDER); 712 sortByLineAction.setChecked(column == BookmarkSorter.LOCATION); 713 sortByCreationTime.setChecked(column == BookmarkSorter.CREATION_TIME); 714 715 int direction = comparator.getTopPriorityDirection(); 716 sortAscendingAction.setChecked(direction == BookmarkSorter.ASCENDING); 717 sortDescendingAction.setChecked(direction == BookmarkSorter.DESCENDING); 718 } 719 720 723 void updatePasteEnablement() { 724 MarkerTransfer transfer = MarkerTransfer.getInstance(); 726 IMarker[] markerData = (IMarker[]) getClipboard().getContents(transfer); 727 boolean canPaste = false; 728 if (markerData != null) { 729 for (int i = 0; i < markerData.length; i++) { 730 try { 731 if (markerData[i].getType().equals(IMarker.BOOKMARK)) { 732 canPaste = true; 733 break; 734 } 735 } catch (CoreException e) { 736 canPaste = false; 737 } 738 } 739 } 740 pasteAction.setEnabled(canPaste); 741 } 742 743 Clipboard getClipboard() { 744 return clipboard; 745 } 746 747 } 748 749 | Popular Tags |