1 11 package org.eclipse.debug.ui; 12 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import org.eclipse.debug.internal.ui.DebugUIPlugin; 23 import org.eclipse.debug.internal.ui.DelegatingModelPresentation; 24 import org.eclipse.debug.internal.ui.LazyModelPresentation; 25 import org.eclipse.jface.action.ActionContributionItem; 26 import org.eclipse.jface.action.IAction; 27 import org.eclipse.jface.action.IContributionItem; 28 import org.eclipse.jface.action.IMenuListener; 29 import org.eclipse.jface.action.IMenuManager; 30 import org.eclipse.jface.action.IToolBarManager; 31 import org.eclipse.jface.action.MenuManager; 32 import org.eclipse.jface.preference.IPreferenceStore; 33 import org.eclipse.jface.text.TextViewer; 34 import org.eclipse.jface.viewers.DoubleClickEvent; 35 import org.eclipse.jface.viewers.IBaseLabelProvider; 36 import org.eclipse.jface.viewers.IDoubleClickListener; 37 import org.eclipse.jface.viewers.StructuredViewer; 38 import org.eclipse.jface.viewers.Viewer; 39 import org.eclipse.swt.SWT; 40 import org.eclipse.swt.events.KeyAdapter; 41 import org.eclipse.swt.events.KeyEvent; 42 import org.eclipse.swt.widgets.Composite; 43 import org.eclipse.swt.widgets.Control; 44 import org.eclipse.swt.widgets.Menu; 45 import org.eclipse.ui.IActionBars; 46 import org.eclipse.ui.IMemento; 47 import org.eclipse.ui.IPartListener2; 48 import org.eclipse.ui.IViewPart; 49 import org.eclipse.ui.IViewSite; 50 import org.eclipse.ui.IWorkbenchPage; 51 import org.eclipse.ui.IWorkbenchPart; 52 import org.eclipse.ui.IWorkbenchPartReference; 53 import org.eclipse.ui.PartInitException; 54 import org.eclipse.ui.PlatformUI; 55 import org.eclipse.ui.actions.ActionFactory; 56 import org.eclipse.ui.part.IPage; 57 import org.eclipse.ui.part.MessagePage; 58 import org.eclipse.ui.part.Page; 59 import org.eclipse.ui.part.PageBook; 60 import org.eclipse.ui.part.PageBookView; 61 import org.eclipse.ui.texteditor.IUpdate; 62 63 97 98 public abstract class AbstractDebugView extends PageBookView implements IDebugView, IDoubleClickListener { 99 100 104 private Viewer fViewer = null; 105 106 109 private MessagePage fMessagePage = null; 110 111 115 private Map fActionMap = null; 116 117 121 private List fUpdateables = null; 122 123 127 private List fContextMenuManagers; 128 129 133 private IMemento fMemento; 134 135 138 private boolean fIsVisible = false; 139 140 145 private DebugViewPartListener fPartListener= null; 146 147 151 private String fEarlyMessage= null; 152 153 private static Set fgGlobalActionIds; 154 static { 155 fgGlobalActionIds = new HashSet (); 156 fgGlobalActionIds.add(SELECT_ALL_ACTION); 157 fgGlobalActionIds.add(COPY_ACTION); 158 fgGlobalActionIds.add(CUT_ACTION); 159 fgGlobalActionIds.add(PASTE_ACTION); 160 fgGlobalActionIds.add(FIND_ACTION); 161 fgGlobalActionIds.add(ActionFactory.UNDO.getId()); 162 fgGlobalActionIds.add(ActionFactory.REDO.getId()); 163 } 164 165 169 private class DebugViewPartListener implements IPartListener2 { 170 174 public void partVisible(IWorkbenchPartReference ref) { 175 IWorkbenchPart part= ref.getPart(false); 176 if (part == AbstractDebugView.this) { 177 fIsVisible = true; 178 becomesVisible(); 179 } 180 } 181 184 public void partHidden(IWorkbenchPartReference ref) { 185 IWorkbenchPart part= ref.getPart(false); 186 if (part == AbstractDebugView.this) { 187 fIsVisible = false; 188 becomesHidden(); 189 } 190 } 191 194 public void partActivated(IWorkbenchPartReference ref) { 195 } 196 197 200 public void partBroughtToTop(IWorkbenchPartReference ref) { 201 } 202 203 206 public void partClosed(IWorkbenchPartReference ref) { 207 } 208 209 212 public void partDeactivated(IWorkbenchPartReference ref) { 213 } 214 215 218 public void partOpened(IWorkbenchPartReference ref) { 219 } 220 221 224 public void partInputChanged(IWorkbenchPartReference ref){ 225 } 226 227 } 228 229 232 public AbstractDebugView() { 233 fActionMap = new HashMap (5); 234 fUpdateables= new ArrayList (3); 235 } 236 237 245 public Object getAdapter(Class adapter) { 246 if (adapter == IDebugView.class) { 247 return this; 248 } 249 if (adapter == IDebugModelPresentation.class) { 250 StructuredViewer viewer = getStructuredViewer(); 251 if (viewer != null) { 252 IBaseLabelProvider labelProvider = viewer.getLabelProvider(); 253 if (labelProvider instanceof IDebugModelPresentation) { 254 return labelProvider; 255 } 256 } 257 } 258 return super.getAdapter(adapter); 259 } 260 261 265 class ViewerPage extends Page { 266 269 public void createControl(Composite parent) { 270 Viewer viewer = createViewer(parent); 271 setViewer(viewer); 272 } 273 274 277 public Control getControl() { 278 return getDefaultControl(); 279 } 280 281 284 public void setFocus() { 285 Viewer viewer= getViewer(); 286 if (viewer != null) { 287 Control c = viewer.getControl(); 288 if (!c.isFocusControl()) { 289 c.setFocus(); 290 } 291 } 292 } 293 294 } 295 296 317 public void createPartControl(Composite parent) { 318 registerPartListener(); 319 super.createPartControl(parent); 320 createActions(); 321 initializeToolBar(); 322 Viewer viewer = getViewer(); 323 if (viewer != null) { 324 createContextMenu(viewer.getControl()); 325 } 326 String helpId = getHelpContextId(); 327 if (helpId != null) { 328 PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, helpId); 329 } 330 if (viewer != null) { 331 getViewer().getControl().addKeyListener(new KeyAdapter() { 332 public void keyPressed(KeyEvent e) { 333 handleKeyPressed(e); 334 } 335 }); 336 if (getViewer() instanceof StructuredViewer) { 337 ((StructuredViewer)getViewer()).addDoubleClickListener(this); 338 } 339 } 340 setMessagePage(new MessagePage()); 342 getMessagePage().createControl(getPageBook()); 343 initPage(getMessagePage()); 344 345 if (fEarlyMessage != null) { showMessage(fEarlyMessage); 347 fEarlyMessage= null; 348 } 349 } 350 351 356 protected IPage createDefaultPage(PageBook book) { 357 ViewerPage page = new ViewerPage(); 358 page.createControl(book); 359 initPage(page); 360 return page; 361 } 362 363 371 protected abstract Viewer createViewer(Composite parent); 372 373 378 protected abstract void createActions(); 379 380 386 protected abstract String getHelpContextId(); 387 388 391 public void dispose() { 392 saveAllCheckedActionStates(); 393 deregisterPartListener(); 394 if (getViewer() instanceof StructuredViewer) { 395 ((StructuredViewer)getViewer()).removeDoubleClickListener(this); 396 } 397 setViewer(null); 398 fActionMap.clear(); 399 super.dispose(); 400 } 401 402 409 protected void saveAllCheckedActionStates() { 410 IToolBarManager tbm= getViewSite().getActionBars().getToolBarManager(); 411 IContributionItem[] items= tbm.getItems(); 412 for (int i = 0; i < items.length; i++) { 413 IContributionItem iContributionItem = items[i]; 414 if (iContributionItem instanceof ActionContributionItem) { 415 ActionContributionItem item= (ActionContributionItem)iContributionItem; 416 IAction action= item.getAction(); 417 if (action.getStyle() == IAction.AS_CHECK_BOX && action.isEnabled()) { 418 saveCheckedActionState(action); 419 } 420 } 421 } 422 } 423 424 433 protected void saveCheckedActionState(IAction action) { 434 String prefKey = generatePreferenceKey(action); 435 IPreferenceStore prefStore = getPreferenceStore(); 436 prefStore.setValue(prefKey, action.isChecked()); 437 } 438 439 448 protected String generatePreferenceKey(IAction action) { 449 return getViewSite().getId() + '+' + action.getId(); 450 } 451 452 459 protected IPreferenceStore getPreferenceStore() { 460 return DebugUIPlugin.getDefault().getPreferenceStore(); 461 } 462 463 466 public Viewer getViewer() { 467 return fViewer; 468 } 469 470 477 protected StructuredViewer getStructuredViewer() { 478 if (getViewer() instanceof StructuredViewer) { 479 return (StructuredViewer)getViewer(); 480 } 481 return null; 482 } 483 484 491 protected TextViewer getTextViewer() { 492 if (getViewer() instanceof TextViewer) { 493 return (TextViewer)getViewer(); 494 } 495 return null; 496 } 497 498 501 public IDebugModelPresentation getPresentation(String id) { 502 if (getViewer() instanceof StructuredViewer) { 503 IBaseLabelProvider lp = ((StructuredViewer)getViewer()).getLabelProvider(); 504 if (lp instanceof DelegatingModelPresentation) { 505 return ((DelegatingModelPresentation)lp).getPresentation(id); 506 } 507 if (lp instanceof LazyModelPresentation) { 508 if (((LazyModelPresentation)lp).getDebugModelIdentifier().equals(id)) { 509 return (IDebugModelPresentation)lp; 510 } 511 } 512 } 513 return null; 514 } 515 516 529 protected void createContextMenu(Control menuControl) { 530 MenuManager menuMgr= new MenuManager("#PopUp"); menuMgr.setRemoveAllWhenShown(true); 532 menuMgr.addMenuListener(new IMenuListener() { 533 public void menuAboutToShow(IMenuManager mgr) { 534 fillContextMenu(mgr); 535 } 536 }); 537 Menu menu= menuMgr.createContextMenu(menuControl); 538 menuControl.setMenu(menu); 539 540 if (getSite() != null) { 542 getSite().registerContextMenu(menuMgr, getViewer()); 543 } 544 addContextMenuManager(menuMgr); 545 } 546 547 552 public IMenuManager getContextMenuManager() { 553 if (fContextMenuManagers != null) { 554 fContextMenuManagers.get(fContextMenuManagers.size() - 1); 555 } 556 return null; 557 } 558 559 565 public List getContextMenuManagers() { 566 return fContextMenuManagers; 567 } 568 569 575 protected abstract void fillContextMenu(IMenuManager menu); 576 577 590 protected void initializeToolBar() { 591 final IToolBarManager tbm= getViewSite().getActionBars().getToolBarManager(); 592 configureToolBar(tbm); 593 getViewSite().getActionBars().updateActionBars(); 594 595 Runnable r = new Runnable () { 598 public void run() { 599 if (!isAvailable()) { 600 return; 601 } 602 IContributionItem[] items = tbm.getItems(); 603 if (items != null) { 604 for (int i = 0; i < items.length; i++) { 605 if (items[i] instanceof ActionContributionItem) { 606 IAction action = ((ActionContributionItem)items[i]).getAction(); 607 if (action.getStyle() == IAction.AS_CHECK_BOX) { 608 initActionState(action); 609 if (action.isChecked()) { 610 action.run(); 611 } 612 } 613 } 614 } 615 setMemento(null); 616 } 617 updateObjects(); 618 } 619 }; 620 asyncExec(r); 621 } 622 623 631 protected void initActionState(IAction action) { 632 String id = action.getId(); 633 if (id != null && action.isEnabled()) { 634 String prefKey = generatePreferenceKey(action); 635 boolean checked = getPreferenceStore().getBoolean(prefKey); 636 action.setChecked(checked); 637 } 638 } 639 640 643 public void init(IViewSite site, IMemento memento) throws PartInitException { 644 super.init(site, memento); 645 setMemento(memento); 647 } 648 649 655 protected void setViewer(Viewer viewer) { 656 fViewer = viewer; 657 } 658 659 667 protected abstract void configureToolBar(IToolBarManager tbm); 668 669 672 public void setAction(String actionID, IAction action) { 673 if (action == null) { 674 Object removedAction= fActionMap.remove(actionID); 675 fUpdateables.remove(removedAction); 676 } else { 677 fActionMap.put(actionID, action); 678 if (action instanceof IUpdate) { 679 fUpdateables.add(action); 680 } 681 } 682 if (fgGlobalActionIds.contains(actionID)) { 683 IActionBars actionBars = getViewSite().getActionBars(); 684 actionBars.setGlobalActionHandler(actionID, action); 685 } 686 } 687 688 691 public IAction getAction(String actionID) { 692 return (IAction) fActionMap.get(actionID); 693 } 694 695 698 public void updateObjects() { 699 Iterator actions = fUpdateables.iterator(); 700 while (actions.hasNext()) { 701 ((IUpdate)actions.next()).update(); 702 } 703 } 704 705 711 protected void handleKeyPressed(KeyEvent event) { 712 if (event.character == SWT.DEL && event.stateMask == 0) { 713 IAction action = getAction(REMOVE_ACTION); 714 if (action != null && action.isEnabled()) { 715 action.run(); 716 } 717 } 718 } 719 720 726 public void doubleClick(DoubleClickEvent event) { 727 IAction action = getAction(DOUBLE_CLICK_ACTION); 728 if (action != null && !event.getSelection().isEmpty() && action.isEnabled()) { 729 action.run(); 730 } 731 } 732 733 739 public void asyncExec(Runnable r) { 740 if (isAvailable()) { 741 getControl().getDisplay().asyncExec(r); 742 } 743 } 744 745 751 protected Control getControl() { 752 return getViewer().getControl(); 753 } 754 755 761 public void syncExec(Runnable r) { 762 if (isAvailable()) { 763 getControl().getDisplay().syncExec(r); 764 } 765 } 766 767 771 protected IMemento getMemento() { 772 return fMemento; 773 } 774 775 779 protected void setMemento(IMemento memento) { 780 fMemento = memento; 781 } 782 783 790 protected IViewPart findView(String id) { 791 IWorkbenchPage page = getSite().getPage(); 792 IViewPart view = null; 793 if (page != null) { 794 view = page.findView(id); 795 } 796 return view; 797 } 798 799 802 protected boolean isImportant(IWorkbenchPart part) { 803 return false; 804 } 805 806 809 protected PageRec doCreatePage(IWorkbenchPart part) { 810 return null; 811 } 812 813 816 protected void doDestroyPage(IWorkbenchPart part, PageRec pageRecord) { 817 } 818 819 822 protected IWorkbenchPart getBootstrapPart() { 823 return null; 824 } 825 826 834 protected Control getDefaultControl() { 835 Viewer viewer = getViewer(); 836 if (viewer != null) { 837 return viewer.getControl(); 838 } 839 return null; 840 } 841 842 847 private void setMessagePage(MessagePage page) { 848 fMessagePage = page; 849 } 850 851 856 protected MessagePage getMessagePage() { 857 return fMessagePage; 858 } 859 860 866 public void showMessage(String message) { 867 if (getPageBook().isDisposed()) { 868 return; 869 } 870 if (getMessagePage() == null) { 871 fEarlyMessage= message; 873 return; 874 } 875 getMessagePage().setMessage(message); 876 getPageBook().showPage(getMessagePage().getControl()); 877 } 878 879 882 public void showViewer() { 883 if (getPageBook().isDisposed()) { 884 return; 885 } 886 getPageBook().showPage(getDefaultPage().getControl()); 887 } 888 889 896 public boolean isAvailable() { 897 return !(getViewer() == null || getViewer().getControl() == null || getViewer().getControl().isDisposed()); 898 } 899 902 public void add(IUpdate updatable) { 903 if (!fUpdateables.contains(updatable)) { 904 fUpdateables.add(updatable); 905 } 906 } 907 908 911 public void remove(IUpdate updatable) { 912 fUpdateables.remove(updatable); 913 } 914 915 921 public void addContextMenuManager(IMenuManager contextMenuManager) { 922 if (fContextMenuManagers == null) { 923 fContextMenuManagers= new ArrayList (); 924 } 925 fContextMenuManagers.add(contextMenuManager); 926 } 927 928 933 protected void becomesVisible() { 934 } 935 936 941 protected void becomesHidden() { 942 } 943 944 950 public boolean isVisible() { 951 return fIsVisible; 952 } 953 954 960 protected void registerPartListener() { 961 if (fPartListener == null) { 962 fPartListener= new DebugViewPartListener(); 963 getSite().getPage().addPartListener(fPartListener); 964 } 965 } 966 967 972 protected void deregisterPartListener() { 973 if (fPartListener != null) { 974 getSite().getPage().removePartListener(fPartListener); 975 fPartListener = null; 976 } 977 } 978 979 987 public Map getPresentationAttributes(String modelId) { 988 IDebugModelPresentation presentation = getPresentation(modelId); 989 if (presentation instanceof DelegatingModelPresentation) { 990 return ((DelegatingModelPresentation)presentation).getAttributeMap(); 991 } else if (presentation instanceof LazyModelPresentation) { 992 return ((LazyModelPresentation)presentation).getAttributeMap(); 993 } 994 return new HashMap (); 995 } 996 } 997 998 999 | Popular Tags |