1 11 package org.eclipse.debug.internal.ui.viewers.model; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.Map.Entry; 17 18 import org.eclipse.core.runtime.IAdaptable; 19 import org.eclipse.core.runtime.PlatformObject; 20 import org.eclipse.debug.internal.ui.viewers.model.provisional.IColumnPresentation; 21 import org.eclipse.debug.internal.ui.viewers.model.provisional.IColumnPresentationFactory; 22 import org.eclipse.debug.internal.ui.viewers.model.provisional.IElementEditor; 23 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelChangedListener; 24 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelSelectionPolicy; 25 import org.eclipse.debug.internal.ui.viewers.model.provisional.IModelSelectionPolicyFactory; 26 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext; 27 import org.eclipse.debug.internal.ui.viewers.model.provisional.IViewerUpdateListener; 28 import org.eclipse.debug.internal.ui.viewers.model.provisional.PresentationContext; 29 import org.eclipse.debug.internal.ui.views.launch.DebugElementAdapterFactory; 30 import org.eclipse.jface.resource.ImageDescriptor; 31 import org.eclipse.jface.viewers.CellEditor; 32 import org.eclipse.jface.viewers.ICellModifier; 33 import org.eclipse.jface.viewers.IContentProvider; 34 import org.eclipse.jface.viewers.ILazyTreePathContentProvider; 35 import org.eclipse.jface.viewers.ISelection; 36 import org.eclipse.jface.viewers.IStructuredSelection; 37 import org.eclipse.jface.viewers.StructuredSelection; 38 import org.eclipse.jface.viewers.TreePath; 39 import org.eclipse.jface.viewers.TreeViewer; 40 import org.eclipse.swt.SWT; 41 import org.eclipse.swt.events.ControlEvent; 42 import org.eclipse.swt.events.ControlListener; 43 import org.eclipse.swt.events.DisposeEvent; 44 import org.eclipse.swt.events.PaintEvent; 45 import org.eclipse.swt.events.PaintListener; 46 import org.eclipse.swt.graphics.Color; 47 import org.eclipse.swt.graphics.Font; 48 import org.eclipse.swt.graphics.Image; 49 import org.eclipse.swt.widgets.Composite; 50 import org.eclipse.swt.widgets.Control; 51 import org.eclipse.swt.widgets.Event; 52 import org.eclipse.swt.widgets.Item; 53 import org.eclipse.swt.widgets.Listener; 54 import org.eclipse.swt.widgets.Tree; 55 import org.eclipse.swt.widgets.TreeColumn; 56 import org.eclipse.swt.widgets.TreeItem; 57 import org.eclipse.swt.widgets.Widget; 58 import org.eclipse.ui.IMemento; 59 60 65 public class InternalTreeModelViewer extends TreeViewer { 66 67 private IPresentationContext fContext; 68 69 72 private IColumnPresentation fColumnPresentation = null; 73 74 78 private Map fVisibleColumns = new HashMap (); 79 80 83 private Map fColumnSizes = new HashMap (); 84 85 89 private Map fColumnOrder = new HashMap (); 90 91 96 private Map fShowColumns = new HashMap (); 97 98 101 private static final String TREE_PATH_KEY = "TREE_PATH_KEY"; 103 106 private static final String COLUMN_SIZES = "COLUMN_SIZES"; 111 private static final String COLUMN_ORDER = "COLUMN_ORDER"; 116 private static final String VISIBLE_COLUMNS = "VISIBLE_COLUMNS"; 121 private static final String SHOW_COLUMNS = "SHOW_COLUMNS"; 126 private static final String SIZE = "SIZE"; 130 private static final String COLUMN = "COLUMN"; 132 135 private boolean fInserting = false; 136 137 140 private boolean fNotifyUnmap = true; 141 142 147 class ColumnListener implements ControlListener { 148 151 public void controlMoved(ControlEvent e) { 152 persistColumnOrder(); 153 } 154 155 158 public void controlResized(ControlEvent e) { 159 persistColumnSizes(); 160 } 161 } 162 163 private ColumnListener fListener = new ColumnListener(); 164 165 168 class CellModifierProxy implements ICellModifier { 169 170 private ICellModifier fModifier; 171 172 175 public boolean canModify(Object element, String property) { 176 IElementEditor editor = getElementEditorAdapter(element); 177 if (editor != null) { 178 fModifier = editor.getCellModifier(getPresentationContext(), element); 179 if (fModifier != null) { 180 if (fModifier.canModify(element, property)) { 181 CellEditor cellEditor = editor.getCellEditor(getPresentationContext(), property, element, (Composite)getControl()); 183 if (cellEditor != null) { 184 disposeCellEditors(); 185 CellEditor[] newEditors = new CellEditor[getVisibleColumns().length]; 186 for (int i = 0; i < newEditors.length; i++) { 187 newEditors[i] = cellEditor; 188 } 189 setCellEditors(newEditors); 190 return true; 191 } 192 } 193 } 194 } 195 return false; 196 } 197 198 201 public Object getValue(Object element, String property) { 202 if (fModifier != null) { 203 return fModifier.getValue(element, property); 204 } 205 return null; 206 } 207 208 211 public void modify(Object element, String property, Object value) { 212 if (fModifier != null) { 213 if (element instanceof Item) { 214 element = ((Item)element).getData(); 215 } 216 fModifier.modify(element, property, value); 217 } 218 } 219 220 223 protected void dispose() { 224 fModifier = null; 225 disposeCellEditors(); 226 setCellEditors(null); 227 } 228 229 232 protected void disposeCellEditors() { 233 CellEditor[] cellEditors = getCellEditors(); 234 if (cellEditors != null) { 235 for (int i = 0; i < cellEditors.length; i++) { 236 CellEditor editor = cellEditors[i]; 237 if (editor != null) { 238 editor.dispose(); 239 } 240 } 241 } 242 } 243 244 250 protected IElementEditor getElementEditorAdapter(Object input) { 251 if (input instanceof IElementEditor) { 252 return (IElementEditor) input; 253 } 254 if (input instanceof IAdaptable) { 255 IAdaptable adaptable = (IAdaptable) input; 256 return (IElementEditor) adaptable.getAdapter(IElementEditor.class); 257 } 258 return null; 259 } 260 } 261 262 private CellModifierProxy fCellModifier; 263 264 268 public InternalTreeModelViewer(Composite parent, int style, IPresentationContext context) { 269 super(parent, style); 270 if ((style & SWT.VIRTUAL) == 0) { 271 throw new IllegalArgumentException ("style must include SWT.VIRTUAL"); } 273 setUseHashlookup(true); 274 fCellModifier = new CellModifierProxy(); 275 fContext = context; 276 setContentProvider(createContentProvider()); 277 setLabelProvider(createLabelProvider()); 278 } 279 280 283 protected TreeModelContentProvider createContentProvider() 284 { 285 return new TreeModelContentProvider(); 286 } 287 288 291 protected TreeModelLabelProvider createLabelProvider() 292 { 293 return new TreeModelLabelProvider(this); 294 } 295 296 303 protected void hookControl(Control control) { 304 Tree treeControl = (Tree) control; 305 treeControl.addListener(SWT.SetData, new Listener() { 306 public void handleEvent(Event event) { 307 TreeItem item = (TreeItem) event.item; 309 preserveItem(item); 310 } 311 }); 312 super.hookControl(control); 313 } 314 315 318 private void preserveItem(TreeItem item) { 319 Object [] labels = (Object []) item.getData(LabelUpdate.PREV_LABEL_KEY); 320 if (labels != null) { 321 for (int i = 0; i < labels.length; i++) { 322 if (labels[i] != null) { 323 item.setText(i, (String )labels[i]); 324 } 325 } 326 } 327 Object [] images = (Object []) item.getData(LabelUpdate.PREV_IMAGE_KEY); 328 if (images != null) { 329 for (int i = 0; i < images.length; i++) { 330 item.setImage(i, (Image) images[i]); 331 } 332 } 333 Object [] fonts = (Object []) item.getData(LabelUpdate.PREV_FONT_KEY); 334 if (fonts != null) { 335 for (int i = 0; i < fonts.length; i++) { 336 item.setFont(i, (Font) fonts[i]); 337 } 338 } 339 Object [] foregrounds = (Object []) item.getData(LabelUpdate.PREV_FOREGROUND_KEY); 340 if (foregrounds != null) { 341 for (int i = 0; i < foregrounds.length; i++) { 342 item.setForeground(i, (Color) foregrounds[i]); 343 } 344 } 345 Object [] backgrounds = (Object []) item.getData(LabelUpdate.PREV_BACKGROUND_KEY); 346 if (backgrounds != null) { 347 for (int i = 0; i < backgrounds.length; i++) { 348 item.setBackground(i, (Color) backgrounds[i]); 349 } 350 } 351 } 352 353 359 protected void handleInvalidSelection(ISelection selection, ISelection newSelection) { 360 IModelSelectionPolicy selectionPolicy = getSelectionPolicy(selection); 361 if (selectionPolicy != null) { 362 ISelection temp = newSelection; 363 newSelection = selectionPolicy.replaceInvalidSelection(selection, newSelection); 364 if (temp != newSelection) { 365 if (newSelection == null) { 366 newSelection = new StructuredSelection(); 367 } 368 super.setSelection(newSelection, false); 371 return; 372 } 373 } 374 super.handleInvalidSelection(selection, newSelection); 375 } 376 377 378 381 protected void handleDispose(DisposeEvent event) { 382 if (fColumnPresentation != null) { 383 fColumnPresentation.dispose(); 384 } 385 fCellModifier.dispose(); 386 fContext.dispose(); 387 super.handleDispose(event); 388 } 389 390 395 public IPresentationContext getPresentationContext() { 396 return fContext; 397 } 398 399 protected void unmapElement(Object element, Widget widget) { 400 if (fNotifyUnmap) { 401 IContentProvider provider = getContentProvider(); 403 if (provider instanceof ModelContentProvider) { 404 ((ModelContentProvider) provider).unmapPath((TreePath) widget.getData(TREE_PATH_KEY)); 405 } 406 } 407 super.unmapElement(element, widget); 408 } 409 410 protected void associate(Object element, Item item) { 411 Object data = item.getData(); 413 if (data != null && data != element && equals(data, element)) { 414 try { 417 fNotifyUnmap = false; 418 super.associate(element, item); 419 } finally { 420 fNotifyUnmap = true; 421 } 422 } else { 423 super.associate(element, item); 424 } 425 } 426 427 433 protected void mapElement(Object element, Widget widget) { 434 super.mapElement(element, widget); 435 if (widget instanceof Item) { 436 widget.setData(TREE_PATH_KEY, getTreePathFromItem((Item)widget)); 437 } else { 438 widget.setData(TREE_PATH_KEY, ModelContentProvider.EMPTY_TREE_PATH); 439 } 440 } 441 442 448 public void insert(Object parentElementOrTreePath, Object element, int position) { 449 try { 450 fInserting = true; 451 super.insert(parentElementOrTreePath, element, position); 452 } finally { 453 fInserting = false; 454 } 455 } 456 457 463 protected boolean hasFilters() { 464 if (fInserting) { 465 return false; 466 } 467 return super.hasFilters(); 468 } 469 470 473 protected void inputChanged(Object input, Object oldInput) { 474 super.inputChanged(input, oldInput); 475 resetColumns(input); 476 } 477 478 483 protected void resetColumns(Object input) { 484 if (input != null) { 485 IColumnPresentationFactory factory = getColumnPresenetationFactoryAdapter(input); 487 PresentationContext context = (PresentationContext) getPresentationContext(); 488 String type = null; 489 if (factory != null) { 490 type = factory.getColumnPresentationId(context, input); 491 } 492 if (type != null) { 493 if (fColumnPresentation != null) { 494 if (!fColumnPresentation.getId().equals(type)) { 495 fColumnPresentation.dispose(); 497 fColumnPresentation = null; 498 } 499 } 500 if (fColumnPresentation == null) { 501 fColumnPresentation = factory.createColumnPresentation(context, input); 502 if (fColumnPresentation != null) { 503 fColumnPresentation.init(context); 504 configureColumns(); 505 } 506 } 507 } else { 508 if (fColumnPresentation != null) { 509 fColumnPresentation.dispose(); 510 fColumnPresentation = null; 511 configureColumns(); 512 } 513 } 514 } 515 } 516 517 523 protected IColumnPresentationFactory getColumnPresenetationFactoryAdapter(Object input) { 524 if (input instanceof IColumnPresentationFactory) { 525 return (IColumnPresentationFactory) input; 526 } 527 if (input instanceof IAdaptable) { 528 IAdaptable adaptable = (IAdaptable) input; 529 return (IColumnPresentationFactory) adaptable.getAdapter(IColumnPresentationFactory.class); 530 } 531 return null; 532 } 533 534 539 protected void configureColumns() { 540 if (fColumnPresentation != null) { 541 IColumnPresentation build = null; 542 if (isShowColumns(fColumnPresentation.getId())) { 543 build = fColumnPresentation; 544 } 545 buildColumns(build); 546 } else { 547 buildColumns(null); 549 } 550 } 551 552 558 public void setShowColumns(boolean show) { 559 if (show) { 560 if (!isShowColumns()) { 561 fShowColumns.remove(fColumnPresentation.getId()); 562 } 563 } else { 564 if (isShowColumns()){ 565 fShowColumns.put(fColumnPresentation.getId(), Boolean.FALSE); 566 } 567 } 568 refreshColumns(); 569 } 570 571 574 public void resetColumnSizes(String [] columnIds) { 575 for (int i = 0; i < columnIds.length; i++) { 576 fColumnSizes.remove(columnIds[i]); 577 } 578 } 579 580 586 public void setVisibleColumns(String [] ids) { 587 if (ids != null && ids.length == 0) { 588 ids = null; 589 } 590 IColumnPresentation presentation = getColumnPresentation(); 591 if (presentation != null) { 592 fColumnOrder.remove(presentation.getId()); 593 fVisibleColumns.remove(presentation.getId()); 594 if (ids != null) { 595 String [] columns = presentation.getInitialColumns(); 597 if (columns.length == ids.length) { 598 for (int i = 0; i < columns.length; i++) { 599 if (!ids[i].equals(columns[i])) { 600 fVisibleColumns.put(presentation.getId(), ids); 601 break; 602 } 603 } 604 } else { 605 fVisibleColumns.put(presentation.getId(), ids); 606 } 607 } 608 PresentationContext presentationContext = (PresentationContext) getPresentationContext(); 609 presentationContext.setColumns(getVisibleColumns()); 610 refreshColumns(); 611 } 612 } 613 614 617 protected void refreshColumns() { 618 configureColumns(); 619 refresh(); 620 } 621 622 627 public boolean isShowColumns() { 628 if (fColumnPresentation != null) { 629 return isShowColumns(fColumnPresentation.getId()); 630 } 631 return false; 632 } 633 634 639 public boolean canToggleColumns() { 640 return fColumnPresentation != null && fColumnPresentation.isOptional(); 641 } 642 643 protected boolean isShowColumns(String columnPresentationId) { 644 Boolean bool = (Boolean ) fShowColumns.get(columnPresentationId); 645 if (bool == null) { 646 return true; 647 } 648 return bool.booleanValue(); 649 } 650 651 658 protected void buildColumns(IColumnPresentation presentation) { 659 Tree tree = getTree(); 661 final TreeColumn[] columns = tree.getColumns(); 662 String [] visibleColumnIds = getVisibleColumns(); 663 for (int i = 0; i < columns.length; i++) { 664 TreeColumn treeColumn = columns[i]; 665 treeColumn.removeControlListener(fListener); 666 treeColumn.dispose(); 667 } 668 PresentationContext presentationContext = (PresentationContext) getPresentationContext(); 669 if (presentation != null) { 670 for (int i = 0; i < visibleColumnIds.length; i++) { 671 String id = visibleColumnIds[i]; 672 String header = presentation.getHeader(id); 673 TreeColumn column = new TreeColumn(tree, SWT.LEFT, i); 675 column.setMoveable(true); 676 column.setText(header); 677 column.setWidth(1); 678 ImageDescriptor image = presentation.getImageDescriptor(id); 679 if (image != null) { 680 column.setImage(((TreeModelLabelProvider)getLabelProvider()).getImage(image)); 681 } 682 column.setData(id); 683 } 684 int[] order = (int[]) fColumnOrder.get(presentation.getId()); 685 if (order != null) { 686 tree.setColumnOrder(order); 687 } 688 tree.setHeaderVisible(true); 689 tree.setLinesVisible(true); 690 presentationContext.setColumns(getVisibleColumns()); 691 setColumnProperties(getVisibleColumns()); 692 setCellModifier(fCellModifier); 693 } else { 694 tree.setHeaderVisible(false); 695 tree.setLinesVisible(false); 696 presentationContext.setColumns(null); 697 setCellModifier(null); 698 setColumnProperties(null); 699 } 700 701 702 int avg = tree.getSize().x; 703 if (visibleColumnIds != null) 704 avg /= visibleColumnIds.length; 705 706 if (avg == 0) { 707 tree.addPaintListener(new PaintListener() { 708 public void paintControl(PaintEvent e) { 709 Tree tree2 = getTree(); 710 String [] visibleColumns = getVisibleColumns(); 711 if (visibleColumns != null) { 712 int avg1 = tree2.getSize().x / visibleColumns.length; 713 initColumns(avg1); 714 } 715 tree2.removePaintListener(this); 716 } 717 }); 718 } else { 719 initColumns(avg); 720 } 721 } 722 723 private void initColumns(int widthHint) { 724 TreeColumn[] columns = getTree().getColumns(); 725 for (int i = 0; i < columns.length; i++) { 726 TreeColumn treeColumn = columns[i]; 727 Integer width = (Integer ) fColumnSizes.get(treeColumn.getData()); 728 if (width == null) { 729 treeColumn.setWidth(widthHint); 730 } else { 731 treeColumn.setWidth(width.intValue()); 732 } 733 treeColumn.addControlListener(fListener); 734 } 735 } 736 737 743 public IColumnPresentation getColumnPresentation() { 744 return fColumnPresentation; 745 } 746 747 753 public String [] getVisibleColumns() { 754 if (isShowColumns()) { 755 IColumnPresentation presentation = getColumnPresentation(); 756 if (presentation != null) { 757 String [] columns = (String []) fVisibleColumns.get(presentation.getId()); 758 if (columns == null) { 759 return presentation.getInitialColumns(); 760 } 761 return columns; 762 } 763 } 764 return null; 765 } 766 767 770 protected void persistColumnSizes() { 771 Tree tree = getTree(); 772 TreeColumn[] columns = tree.getColumns(); 773 for (int i = 0; i < columns.length; i++) { 774 TreeColumn treeColumn = columns[i]; 775 Object id = treeColumn.getData(); 776 fColumnSizes.put(id, new Integer (treeColumn.getWidth())); 777 } 778 } 779 780 783 protected void persistColumnOrder() { 784 IColumnPresentation presentation = getColumnPresentation(); 785 if (presentation != null) { 786 Tree tree = getTree(); 787 int[] order = tree.getColumnOrder(); 788 if (order.length > 0) { 789 for (int i = 0; i < order.length; i++) { 790 if (i != order[i]) { 791 fColumnOrder.put(presentation.getId(), order); 793 return; 794 } 795 } 796 } 797 fColumnOrder.remove(presentation.getId()); 799 } 800 } 801 802 807 public void saveState(IMemento memento) { 808 if (!fColumnSizes.isEmpty()) { 809 Iterator iterator = fColumnSizes.entrySet().iterator(); 810 while (iterator.hasNext()) { 811 Map.Entry entry = (Entry) iterator.next(); 812 IMemento sizes = memento.createChild(COLUMN_SIZES, (String )entry.getKey()); 813 sizes.putInteger(SIZE, ((Integer )entry.getValue()).intValue()); 814 } 815 } 816 if (!fShowColumns.isEmpty()) { 817 Iterator iterator = fShowColumns.entrySet().iterator(); 818 while (iterator.hasNext()) { 819 Map.Entry entry = (Entry) iterator.next(); 820 IMemento sizes = memento.createChild(SHOW_COLUMNS, (String )entry.getKey()); 821 sizes.putString(SHOW_COLUMNS, ((Boolean )entry.getValue()).toString()); 822 } 823 } 824 if (!fVisibleColumns.isEmpty()) { 825 Iterator iterator = fVisibleColumns.entrySet().iterator(); 826 while (iterator.hasNext()) { 827 Map.Entry entry = (Entry) iterator.next(); 828 String id = (String ) entry.getKey(); 829 IMemento visible = memento.createChild(VISIBLE_COLUMNS, id); 830 String [] columns = (String []) entry.getValue(); 831 visible.putInteger(SIZE, columns.length); 832 for (int i = 0; i < columns.length; i++) { 833 visible.putString(COLUMN+Integer.toString(i), columns[i]); 834 } 835 } 836 } 837 if (!fColumnOrder.isEmpty()) { 838 Iterator iterator = fColumnOrder.entrySet().iterator(); 839 while (iterator.hasNext()) { 840 Map.Entry entry = (Entry) iterator.next(); 841 String id = (String ) entry.getKey(); 842 IMemento orderMemento = memento.createChild(COLUMN_ORDER, id); 843 int[] order = (int[]) entry.getValue(); 844 orderMemento.putInteger(SIZE, order.length); 845 for (int i = 0; i < order.length; i++) { 846 orderMemento.putInteger(COLUMN+Integer.toString(i), order[i]); 847 } 848 } 849 } 850 } 851 852 857 public void initState(IMemento memento) { 858 IMemento[] mementos = memento.getChildren(COLUMN_SIZES); 859 for (int i = 0; i < mementos.length; i++) { 860 IMemento child = mementos[i]; 861 String id = child.getID(); 862 Integer size = child.getInteger(SIZE); 863 if (size != null) { 864 fColumnSizes.put(id, size); 865 } 866 } 867 mementos = memento.getChildren(SHOW_COLUMNS); 868 for (int i = 0; i < mementos.length; i++) { 869 IMemento child = mementos[i]; 870 String id = child.getID(); 871 Boolean bool = Boolean.valueOf(child.getString(SHOW_COLUMNS)); 872 if (!bool.booleanValue()) { 873 fShowColumns.put(id, bool); 874 } 875 } 876 mementos = memento.getChildren(VISIBLE_COLUMNS); 877 for (int i = 0; i < mementos.length; i++) { 878 IMemento child = mementos[i]; 879 String id = child.getID(); 880 Integer integer = child.getInteger(SIZE); 881 if (integer != null) { 882 int length = integer.intValue(); 883 String [] columns = new String [length]; 884 for (int j = 0; j < length; j++) { 885 columns[j] = child.getString(COLUMN+Integer.toString(j)); 886 } 887 fVisibleColumns.put(id, columns); 888 } 889 } 890 mementos = memento.getChildren(COLUMN_ORDER); 891 for (int i = 0; i < mementos.length; i++) { 892 IMemento child = mementos[i]; 893 String id = child.getID(); 894 Integer integer = child.getInteger(SIZE); 895 if (integer != null) { 896 int length = integer.intValue(); 897 int[] order = new int[length]; 898 for (int j = 0; j < length; j++) { 899 order[j] = child.getInteger(COLUMN+Integer.toString(j)).intValue(); 900 } 901 fColumnOrder.put(id, order); 902 } 903 } 904 } 905 906 914 protected boolean overrideSelection(ISelection current, ISelection candidate) { 915 IModelSelectionPolicy selectionPolicy = getSelectionPolicy(current); 916 if (selectionPolicy == null) { 917 return true; 918 } 919 if (selectionPolicy.contains(candidate, getPresentationContext())) { 920 return selectionPolicy.overrides(current, candidate, getPresentationContext()); 921 } 922 return !selectionPolicy.isSticky(current, getPresentationContext()); 923 } 924 925 932 protected IModelSelectionPolicy getSelectionPolicy(ISelection selection) { 933 if (selection instanceof IStructuredSelection) { 934 IStructuredSelection ss = (IStructuredSelection) selection; 935 Object element = ss.getFirstElement(); 936 if (element instanceof IAdaptable) { 937 IAdaptable adaptable = (IAdaptable) element; 938 IModelSelectionPolicyFactory factory = (IModelSelectionPolicyFactory) adaptable.getAdapter(IModelSelectionPolicyFactory.class); 939 if (factory == null && !(element instanceof PlatformObject)) { 940 factory = (IModelSelectionPolicyFactory) new DebugElementAdapterFactory().getAdapter(element, IModelSelectionPolicyFactory.class); 943 } 944 if (factory != null) { 945 return factory.createModelSelectionPolicyAdapter(adaptable, getPresentationContext()); 946 } 947 } 948 } 949 return null; 950 } 951 952 958 public void setSelection(ISelection selection, boolean reveal) { 959 if (!overrideSelection(getSelection(), selection)) { 960 return; 961 } 962 super.setSelection(selection, reveal); 963 } 964 965 973 public void setSelection(ISelection selection, boolean reveal, boolean force) { 974 if (force) { 975 super.setSelection(selection, reveal); 976 } else { 977 setSelection(selection, reveal); 978 } 979 } 980 981 986 public void addViewerUpdateListener(IViewerUpdateListener listener) { 987 ((ModelContentProvider)getContentProvider()).addViewerUpdateListener(listener); 988 } 989 990 995 public void removeViewerUpdateListener(IViewerUpdateListener listener) { 996 ModelContentProvider cp = (ModelContentProvider)getContentProvider(); 997 if (cp != null) { 998 cp.removeViewerUpdateListener(listener); 999 } 1000 } 1001 1002 1007 public void addModelChangedListener(IModelChangedListener listener) { 1008 ((ModelContentProvider)getContentProvider()).addModelChangedListener(listener); 1009 } 1010 1011 1016 public void removeModelChangedListener(IModelChangedListener listener) { 1017 ModelContentProvider cp = (ModelContentProvider)getContentProvider(); 1018 if (cp != null) { 1019 cp.removeModelChangedListener(listener); 1020 } 1021 } 1022 1023 1026 protected void doUpdateItem(final Item item, Object element) { 1027 if (!(item instanceof TreeItem)) { 1028 return; 1029 } 1030 TreeItem treeItem = (TreeItem) item; 1031 if (treeItem.isDisposed()) { 1032 unmapElement(element, treeItem); 1033 return; 1034 } 1035 1036 ((TreeModelLabelProvider)getLabelProvider()).update(getTreePathFromItem(item), getViewerRowFromItem(treeItem)); 1037 1038 if (item.isDisposed()) { 1041 unmapElement(element, item); 1042 } 1043 } 1044 1045 1048 boolean populateVitrualItems() { 1049 Tree tree = getTree(); 1050 return populateVitrualItems(TreePath.EMPTY, tree.getItems()); 1051 } 1052 1053 1056 private boolean populateVitrualItems(TreePath parentPath, TreeItem[] items) { 1057 boolean queued = false; 1058 for (int i = 0; i < items.length; i++) { 1059 TreeItem treeItem = items[i]; 1060 if (treeItem.getData() == null) { 1061 queued = true; 1062 ((ILazyTreePathContentProvider)getContentProvider()).updateElement(parentPath, i); 1063 } 1064 if (treeItem.getExpanded()) { 1065 queued = populateVitrualItems(parentPath.createChildPath(treeItem.getData()), treeItem.getItems()) | queued; 1066 } 1067 } 1068 return queued; 1069 } 1070 1071 void addLabelUpdateListener(ILabelUpdateListener listener) { 1072 ((TreeModelLabelProvider)getLabelProvider()).addLabelUpdateListener(listener); 1073 } 1074 1075 void removeLabelUpdateListener(ILabelUpdateListener listener) { 1076 ((TreeModelLabelProvider)getLabelProvider()).removeLabelUpdateListener(listener); 1077 } 1078 1079 1086 Widget findItem(TreePath path) { 1087 if (path.getSegmentCount() == 0) { 1088 return getTree(); 1089 } 1090 Widget[] items = super.findItems(path.getLastSegment()); 1091 if (items.length == 1) { 1092 return items[0]; 1093 } 1094 for (int i = 0; i < items.length; i++) { 1095 if (getTreePathFromItem((Item)items[i]).equals(path)) { 1096 return items[i]; 1097 } 1098 } 1099 return null; 1100 } 1101 1102 public Item[] getChildren(Widget widget) { 1103 return super.getChildren(widget); 1104 } 1105 1106 1111 protected TreePath getTreePathFromItem(Item item) { 1112 return super.getTreePathFromItem(item); 1113 } 1114 1115 1119 1127 protected void internalRefreshStruct(Widget widget, Object element, 1128 boolean updateLabels) { 1129 if (widget instanceof Tree) { 1131 ((Tree) widget).clearAll(true); 1132 } else if (widget instanceof TreeItem) { 1133 ((TreeItem) widget).clearAll(true); 1134 } 1135 int index = 0; 1136 Widget parent = null; 1137 if (widget instanceof TreeItem) { 1138 TreeItem treeItem = (TreeItem) widget; 1139 parent = treeItem.getParentItem(); 1140 if (parent == null) { 1141 parent = treeItem.getParent(); 1142 } 1143 if (parent instanceof Tree) { 1144 index = ((Tree) parent).indexOf(treeItem); 1145 } else { 1146 index = ((TreeItem) parent).indexOf(treeItem); 1147 } 1148 } 1149 virtualRefreshExpandedItems(parent, widget, element, index); 1150 } 1151 1152 1163 private void virtualRefreshExpandedItems(Widget parent, Widget widget, Object element, int index) { 1164 if (widget instanceof Tree) { 1165 if (element == null) { 1166 ((Tree) widget).setItemCount(0); 1167 return; 1168 } 1169 virtualLazyUpdateChildCount(widget, getChildren(widget).length); 1170 } else if (((TreeItem) widget).getExpanded()) { 1171 preserveItem((TreeItem)widget); 1173 virtualLazyUpdateWidget(parent, index); 1175 } else { 1176 return; 1177 } 1178 Item[] items = getChildren(widget); 1179 for (int i = 0; i < items.length; i++) { 1180 Item item = items[i]; 1181 Object data = item.getData(); 1182 virtualRefreshExpandedItems(widget, item, data, i); 1183 } 1184 } 1185 1186 1193 private void virtualLazyUpdateChildCount(Widget widget, int currentChildCount) { 1194 TreePath treePath; 1195 if (widget instanceof Item) { 1196 treePath = getTreePathFromItem((Item) widget); 1197 } else { 1198 treePath = TreePath.EMPTY; 1199 } 1200 ((ILazyTreePathContentProvider) getContentProvider()) 1201 .updateChildCount(treePath, currentChildCount); 1202 } 1203 1204 1212 private void virtualLazyUpdateWidget(Widget widget, int index) { 1213 TreePath treePath; 1214 if (widget instanceof Item) { 1215 if (widget.getData() == null) { 1216 return; 1223 } 1224 treePath = getTreePathFromItem((Item) widget); 1225 } else { 1226 treePath = TreePath.EMPTY; 1227 } 1228 ((ILazyTreePathContentProvider) getContentProvider()) 1229 .updateElement(treePath, index); 1230 } 1231} 1232 | Popular Tags |