1 12 package org.eclipse.jface.viewers; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 19 import org.eclipse.core.runtime.ListenerList; 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.jface.util.IOpenEventListener; 22 import org.eclipse.jface.util.OpenStrategy; 23 import org.eclipse.jface.util.SafeRunnable; 24 import org.eclipse.swt.custom.TableTreeItem; 25 import org.eclipse.swt.dnd.DragSource; 26 import org.eclipse.swt.dnd.DragSourceListener; 27 import org.eclipse.swt.dnd.DropTarget; 28 import org.eclipse.swt.dnd.DropTargetListener; 29 import org.eclipse.swt.dnd.Transfer; 30 import org.eclipse.swt.events.DisposeEvent; 31 import org.eclipse.swt.events.SelectionAdapter; 32 import org.eclipse.swt.events.SelectionEvent; 33 import org.eclipse.swt.events.SelectionListener; 34 import org.eclipse.swt.graphics.Color; 35 import org.eclipse.swt.graphics.Font; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.swt.widgets.Item; 38 import org.eclipse.swt.widgets.TableItem; 39 import org.eclipse.swt.widgets.TreeItem; 40 import org.eclipse.swt.widgets.Widget; 41 42 54 public abstract class StructuredViewer extends ContentViewer implements IPostSelectionProvider { 55 56 61 private CustomHashtable elementMap; 62 63 68 private IElementComparer comparer; 69 70 73 private ViewerComparator sorter; 74 75 79 private List filters; 80 81 86 private boolean inChange; 87 88 94 private boolean restoreSelection; 95 96 102 private ListenerList doubleClickListeners = new ListenerList(); 103 104 110 private ListenerList openListeners = new ListenerList(); 111 112 118 private ListenerList postSelectionChangedListeners = new ListenerList(); 119 120 128 private ColorAndFontCollector colorAndFontCollector = new ColorAndFontCollector(); 129 130 133 private static Widget[] NO_WIDGETS = new Widget[0]; 134 135 144 protected class ColorAndFontCollectorWithProviders extends ColorAndFontCollector{ 145 146 IColorProvider colorProvider; 147 148 IFontProvider fontProvider; 149 150 158 public ColorAndFontCollectorWithProviders(IBaseLabelProvider provider) { 159 super(); 160 if (provider instanceof IColorProvider) { 161 colorProvider = (IColorProvider) provider; 162 } 163 if (provider instanceof IFontProvider) { 164 fontProvider = (IFontProvider) provider; 165 } 166 } 167 168 169 172 public void setFontsAndColors(Object element){ 173 174 if(fontProvider != null){ 175 if(font == null) { 176 font = fontProvider.getFont(element); 177 } 178 } 179 180 if(colorProvider == null) { 181 return; 182 } 183 if(background == null) { 185 background = colorProvider.getBackground(element); 186 } 187 188 if(foreground == null) { 189 foreground = colorProvider.getForeground(element); 190 } 191 } 192 193 198 public void applyFontsAndColors(TableItem control) { 199 200 if(colorProvider == null){ 201 if(usedDecorators){ 202 if(background != null) { 204 control.setBackground(background); 205 } 206 207 if(foreground != null) { 208 control.setForeground(foreground); 209 } 210 } 211 } 212 else{ 213 control.setBackground(background); 215 control.setForeground(foreground); 216 } 217 218 if(fontProvider == null){ 219 if(usedDecorators && font != null) { 220 control.setFont(font); 221 } 222 } else { 223 control.setFont(font); 224 } 225 226 clear(); 227 } 228 229 230 231 234 public void applyFontsAndColors(TreeItem control) { 235 236 if(colorProvider == null){ 237 if(usedDecorators){ 238 if(background != null) { 240 control.setBackground(background); 241 } 242 243 if(foreground != null) { 244 control.setForeground(foreground); 245 } 246 } 247 } 248 else{ 249 control.setBackground(background); 251 control.setForeground(foreground); 252 } 253 254 if(fontProvider == null){ 255 if(usedDecorators && font != null) { 256 control.setFont(font); 257 } 258 } else { 259 control.setFont(font); 260 } 261 262 clear(); 263 } 264 265 266 269 public void applyFontsAndColors(TableTreeItem control) { 270 271 if(colorProvider == null){ 272 if(usedDecorators){ 273 if(background != null) { 275 control.setBackground(background); 276 } 277 278 if(foreground != null) { 279 control.setForeground(foreground); 280 } 281 } 282 } 283 else{ 284 control.setBackground(background); 286 control.setForeground(foreground); 287 } 288 289 if(fontProvider == null){ 290 if(usedDecorators && font != null) { 291 control.setFont(font); 292 } 293 } else { 294 control.setFont(font); 295 } 296 297 clear(); 298 } 299 300 301 } 302 303 308 protected class ColorAndFontCollector { 309 310 Color foreground = null; 311 312 Color background = null; 313 314 Font font = null; 315 316 boolean usedDecorators = false; 317 318 322 public ColorAndFontCollector(){ 323 super(); 324 } 325 326 327 330 public void clear() { 331 foreground = null; 332 background = null; 333 font = null; 334 usedDecorators = false; 335 } 336 337 338 343 public void setFontsAndColors(Object element){ 344 } 346 347 350 public void setUsedDecorators() { 351 this.usedDecorators = true; 352 } 353 354 359 public void applyFontsAndColors(TableItem control) { 360 361 if(usedDecorators){ 362 if(background != null) { 364 control.setBackground(background); 365 } 366 367 if(foreground != null) { 368 control.setForeground(foreground); 369 } 370 371 if(font != null) { 372 control.setFont(font); 373 } 374 } 375 clear(); 376 } 377 378 383 public void applyFontsAndColors(TreeItem control) { 384 if(usedDecorators){ 385 if(background != null) { 387 control.setBackground(background); 388 } 389 390 if(foreground != null) { 391 control.setForeground(foreground); 392 } 393 394 if(font != null) { 395 control.setFont(font); 396 } 397 } 398 clear(); 399 } 400 401 406 public void applyFontsAndColors(TableTreeItem control) { 407 if(usedDecorators){ 408 if(background != null) { 410 control.setBackground(background); 411 } 412 413 if(foreground != null) { 414 control.setForeground(foreground); 415 } 416 417 if(font != null) { 418 control.setFont(font); 419 } 420 } 421 clear(); 422 } 423 424 428 public void setBackground(Color background) { 429 this.background = background; 430 } 431 435 public void setFont(Font font) { 436 this.font = font; 437 } 438 442 public void setForeground(Color foreground) { 443 this.foreground = foreground; 444 } 445 446 447 } 448 449 452 class UpdateItemSafeRunnable extends SafeRunnable { 453 private Widget widget; 454 455 private Object element; 456 457 private boolean fullMap; 458 459 UpdateItemSafeRunnable(Widget widget, Object element, boolean fullMap) { 460 this.widget = widget; 461 this.element = element; 462 this.fullMap = fullMap; 463 } 464 465 public void run() { 466 doUpdateItem(widget, element, fullMap); 467 } 468 } 469 470 474 protected StructuredViewer() { 475 } 477 478 485 public void addDoubleClickListener(IDoubleClickListener listener) { 486 doubleClickListeners.add(listener); 487 } 488 489 496 public void addOpenListener(IOpenListener listener) { 497 openListeners.add(listener); 498 } 499 500 503 public void addPostSelectionChangedListener(ISelectionChangedListener listener) { 504 postSelectionChangedListeners.add(listener); 505 } 506 507 522 public void addDragSupport(int operations, Transfer[] transferTypes, DragSourceListener listener) { 523 524 Control myControl = getControl(); 525 final DragSource dragSource = new DragSource(myControl, operations); 526 dragSource.setTransfer(transferTypes); 527 dragSource.addDragListener(listener); 528 } 529 530 545 public void addDropSupport(int operations, Transfer[] transferTypes, 546 final DropTargetListener listener) { 547 Control control = getControl(); 548 DropTarget dropTarget = new DropTarget(control, operations); 549 dropTarget.setTransfer(transferTypes); 550 dropTarget.addDropListener(listener); 551 } 552 553 562 public void addFilter(ViewerFilter filter) { 563 if (filters == null) { 564 filters = new ArrayList (); 565 } 566 filters.add(filter); 567 refresh(); 568 } 569 570 577 protected void assertElementsNotNull(Object [] elements) { 578 Assert.isNotNull(elements); 579 for (int i = 0, n = elements.length; i < n; ++i) { 580 Assert.isNotNull(elements[i]); 581 } 582 } 583 584 594 protected void associate(Object element, Item item) { 595 Object data = item.getData(); 596 if (data != element) { 597 if (data != null) { 598 disassociate(item); 599 } 600 item.setData(element); 601 } 602 mapElement(element, item); 606 } 607 608 616 protected void disassociate(Item item) { 617 Object element = item.getData(); 618 Assert.isNotNull(element); 619 unmapElement(element, item); 621 item.setData(null); 622 } 623 624 635 protected abstract Widget doFindInputItem(Object element); 636 637 648 protected abstract Widget doFindItem(Object element); 649 650 669 protected abstract void doUpdateItem(Widget item, Object element, boolean fullMap); 670 671 682 protected boolean equals(Object elementA, Object elementB) { 683 if (comparer == null) { 684 return elementA == null ? elementB == null : elementA.equals(elementB); 685 } else { 686 return elementA == null ? elementB == null : comparer.equals(elementA, elementB); 687 } 688 } 689 690 697 protected Object [] filter(Object [] elements) { 698 if (filters != null) { 699 ArrayList filtered = new ArrayList (elements.length); 700 Object root = getRoot(); 701 for (int i = 0; i < elements.length; i++) { 702 boolean add = true; 703 for (int j = 0; j < filters.size(); j++) { 704 add = ((ViewerFilter) filters.get(j)).select(this, root, elements[i]); 705 if (!add) { 706 break; 707 } 708 } 709 if (add) { 710 filtered.add(elements[i]); 711 } 712 } 713 return filtered.toArray(); 714 } 715 return elements; 716 } 717 718 733 protected final Widget findItem(Object element) { 734 Widget[] result = findItems(element); 735 return result.length == 0 ? null : result[0]; 736 } 737 738 763 protected final Widget[] findItems(Object element) { 764 Widget result = doFindInputItem(element); 765 if (result != null) { 766 return new Widget[] { result }; 767 } 768 if (usingElementMap()) { 770 Object widgetOrWidgets = elementMap.get(element); 771 if (widgetOrWidgets==null) { 772 return NO_WIDGETS; 773 } else if (widgetOrWidgets instanceof Widget) { 774 return new Widget[] {(Widget) widgetOrWidgets}; 775 } else { 776 return (Widget[])widgetOrWidgets; 777 } 778 } 779 result = doFindItem(element); 780 return result == null ? NO_WIDGETS : new Widget[] { result }; 781 } 782 783 793 protected void fireDoubleClick(final DoubleClickEvent event) { 794 Object [] listeners = doubleClickListeners.getListeners(); 795 for (int i = 0; i < listeners.length; ++i) { 796 final IDoubleClickListener l = (IDoubleClickListener) listeners[i]; 797 SafeRunnable.run(new SafeRunnable() { 798 public void run() { 799 l.doubleClick(event); 800 } 801 }); 802 } 803 } 804 805 814 protected void fireOpen(final OpenEvent event) { 815 Object [] listeners = openListeners.getListeners(); 816 for (int i = 0; i < listeners.length; ++i) { 817 final IOpenListener l = (IOpenListener) listeners[i]; 818 SafeRunnable.run(new SafeRunnable() { 819 public void run() { 820 l.open(event); 821 } 822 }); 823 } 824 } 825 826 836 protected void firePostSelectionChanged(final SelectionChangedEvent event) { 837 Object [] listeners = postSelectionChangedListeners.getListeners(); 838 for (int i = 0; i < listeners.length; ++i) { 839 final ISelectionChangedListener l = (ISelectionChangedListener) listeners[i]; 840 SafeRunnable.run(new SafeRunnable() { 841 public void run() { 842 l.selectionChanged(event); 843 } 844 }); 845 } 846 } 847 848 857 public IElementComparer getComparer() { 858 return comparer; 859 } 860 861 870 protected Object [] getFilteredChildren(Object parent) { 871 Object [] result = getRawChildren(parent); 872 if (filters != null) { 873 for (Iterator iter = filters.iterator(); iter.hasNext();) { 874 ViewerFilter f = (ViewerFilter) iter.next(); 875 result = f.filter(this, parent, result); 876 } 877 } 878 return result; 879 } 880 881 887 public ViewerFilter[] getFilters() { 888 if (filters == null) { 889 return new ViewerFilter[0]; 890 } 891 ViewerFilter[] result = new ViewerFilter[filters.size()]; 892 filters.toArray(result); 893 return result; 894 } 895 896 916 protected Item getItem(int x, int y) { 917 return null; 918 } 919 920 932 protected Object [] getRawChildren(Object parent) { 933 Object [] result = null; 934 if (parent != null) { 935 IStructuredContentProvider cp = (IStructuredContentProvider) getContentProvider(); 936 if (cp != null) { 937 result = cp.getElements(parent); 938 assertElementsNotNull(result); 939 } 940 } 941 return (result != null) ? result : new Object [0]; 942 } 943 944 954 protected Object getRoot() { 955 return getInput(); 956 } 957 958 967 public ISelection getSelection() { 968 Control control = getControl(); 969 if (control == null || control.isDisposed()) { 970 return StructuredSelection.EMPTY; 971 } 972 List list = getSelectionFromWidget(); 973 return new StructuredSelection(list); 974 } 975 976 982 protected abstract List getSelectionFromWidget(); 983 984 993 protected Object [] getSortedChildren(Object parent) { 994 Object [] result = getFilteredChildren(parent); 995 if (sorter != null) { 996 result = (Object []) result.clone(); 998 sorter.sort(this, result); 999 } 1000 return result; 1001 } 1002 1003 1015 public ViewerSorter getSorter() { 1016 if (sorter instanceof ViewerSorter) 1017 return (ViewerSorter)sorter; 1018 return null; 1019 } 1020 1021 1029 public ViewerComparator getComparator(){ 1030 return sorter; 1031 } 1032 1033 1043 protected void handleDoubleSelect(SelectionEvent event) { 1044 1046 Control control = getControl(); 1048 if (control != null && !control.isDisposed()) { 1049 ISelection selection; 1054 if (event.item != null && event.item.getData() != null) { 1055 selection = new StructuredSelection(event.item.getData()); 1056 } 1057 else { 1058 selection = getSelection(); 1059 updateSelection(selection); 1060 } 1061 fireDoubleClick(new DoubleClickEvent(this, selection)); 1062 } 1063 } 1064 1065 1075 protected void handleOpen(SelectionEvent event) { 1076 Control control = getControl(); 1077 if (control != null && !control.isDisposed()) { 1078 ISelection selection = getSelection(); 1079 fireOpen(new OpenEvent(this, selection)); 1080 } 1081 } 1082 1083 1101 protected void handleInvalidSelection(ISelection invalidSelection, ISelection newSelection) { 1102 updateSelection(newSelection); 1103 SelectionChangedEvent event = new SelectionChangedEvent(this, newSelection); 1104 firePostSelectionChanged(event); 1105 } 1106 1107 1115 protected void handleLabelProviderChanged(LabelProviderChangedEvent event) { 1116 Object [] elements = event.getElements(); 1117 if (elements != null) { 1118 update(elements, null); 1119 } else { 1120 super.handleLabelProviderChanged(event); 1121 } 1122 } 1123 1124 1134 protected void handleSelect(SelectionEvent event) { 1135 Control control = getControl(); 1137 if (control != null && !control.isDisposed()) { 1138 updateSelection(getSelection()); 1139 } 1140 } 1141 1142 1151 protected void handlePostSelect(SelectionEvent e) { 1152 SelectionChangedEvent event = new SelectionChangedEvent(this, getSelection()); 1153 firePostSelectionChanged(event); 1154 } 1155 1156 1159 protected void hookControl(Control control) { 1160 super.hookControl(control); 1161 OpenStrategy handler = new OpenStrategy(control); 1162 handler.addSelectionListener(new SelectionListener() { 1163 public void widgetSelected(SelectionEvent e) { 1164 if (!inChange) { 1168 handleSelect(e); 1169 } 1170 } 1171 1172 public void widgetDefaultSelected(SelectionEvent e) { 1173 handleDoubleSelect(e); 1174 } 1175 }); 1176 handler.addPostSelectionListener(new SelectionAdapter() { 1177 public void widgetSelected(SelectionEvent e) { 1178 handlePostSelect(e); 1179 } 1180 }); 1181 handler.addOpenListener(new IOpenEventListener() { 1182 public void handleOpen(SelectionEvent e) { 1183 StructuredViewer.this.handleOpen(e); 1184 } 1185 }); 1186 } 1187 1188 1192 protected boolean hasFilters() { 1193 return filters != null && filters.size() > 0; 1194 } 1195 1196 1202 protected abstract void internalRefresh(Object element); 1203 1204 1225 protected void internalRefresh(Object element, boolean updateLabels) { 1226 internalRefresh(element); 1227 } 1228 1229 1241 protected void mapElement(Object element, Widget item) { 1242 if (elementMap != null) { 1243 Object widgetOrWidgets = elementMap.get(element); 1244 if (widgetOrWidgets == null) { 1245 elementMap.put(element, item); 1246 } else if (widgetOrWidgets instanceof Widget) { 1247 if (widgetOrWidgets != item) { 1248 elementMap.put(element, new Widget[] { 1249 (Widget) widgetOrWidgets, item }); 1250 } 1251 } else { 1252 Widget[] widgets = (Widget[]) widgetOrWidgets; 1253 int indexOfItem = Arrays.asList(widgets).indexOf(item); 1254 if (indexOfItem == -1) { 1255 int length = widgets.length; 1256 System.arraycopy(widgets, 0, 1257 widgets = new Widget[length + 1], 0, length); 1258 widgets[length] = item; 1259 elementMap.put(element, widgets); 1260 } 1261 } 1262 } 1263 } 1264 1265 1280 protected boolean needsRefilter(Object element, String property) { 1281 if (sorter != null && sorter.isSorterProperty(element, property)) { 1282 return true; 1283 } 1284 1285 if (filters != null) { 1286 for (int i = 0, n = filters.size(); i < n; ++i) { 1287 ViewerFilter filter = (ViewerFilter) filters.get(i); 1288 if (filter.isFilterProperty(element, property)) { 1289 return true; 1290 } 1291 } 1292 } 1293 return false; 1294 } 1295 1296 1304 CustomHashtable newHashtable(int capacity) { 1305 return new CustomHashtable(capacity, getComparer()); 1306 } 1307 1308 1329 protected void preservingSelection(Runnable updateCode) { 1330 preservingSelection(updateCode, false); 1331 } 1332 1333 1359 void preservingSelection(Runnable updateCode, boolean reveal) { 1360 1361 ISelection oldSelection = null; 1362 try { 1363 oldSelection = getSelection(); 1365 inChange = restoreSelection = true; 1366 1367 updateCode.run(); 1369 1370 } finally { 1371 inChange = false; 1372 1373 if (restoreSelection) { 1375 setSelectionToWidget(oldSelection, reveal); 1376 } 1377 1378 ISelection newSelection = getSelection(); 1380 if (!newSelection.equals(oldSelection)) { 1381 handleInvalidSelection(oldSelection, newSelection); 1382 } 1383 } 1384 } 1385 1386 1389 public void refresh() { 1390 refresh(getRoot()); 1391 } 1392 1393 1414 public void refresh(boolean updateLabels) { 1415 refresh(getRoot(), updateLabels); 1416 } 1417 1418 1430 public void refresh(final Object element) { 1431 preservingSelection(new Runnable () { 1432 public void run() { 1433 internalRefresh(element); 1434 } 1435 }); 1436 } 1437 1438 1457 public void refresh(final Object element, final boolean updateLabels) { 1458 preservingSelection(new Runnable () { 1459 public void run() { 1460 internalRefresh(element, updateLabels); 1461 } 1462 }); 1463 } 1464 1465 1478 protected final void refreshItem(Widget widget, Object element) { 1479 SafeRunnable.run(new UpdateItemSafeRunnable(widget, element, true)); 1480 } 1481 1482 1489 public void removeOpenListener(IOpenListener listener) { 1490 openListeners.remove(listener); 1491 } 1492 1493 1496 public void removePostSelectionChangedListener(ISelectionChangedListener listener) { 1497 postSelectionChangedListeners.remove(listener); 1498 } 1499 1500 1507 public void removeDoubleClickListener(IDoubleClickListener listener) { 1508 doubleClickListeners.remove(listener); 1509 } 1510 1511 1521 public void removeFilter(ViewerFilter filter) { 1522 Assert.isNotNull(filter); 1523 if (filters != null) { 1524 for (Iterator i = filters.iterator(); i.hasNext();) { 1527 Object o = i.next(); 1528 if (o == filter) { 1529 i.remove(); 1530 refresh(); 1531 if (filters.size() == 0) { 1532 filters = null; 1533 } 1534 return; 1535 } 1536 } 1537 } 1538 } 1539 1540 1548 public void setFilters(ViewerFilter[] filters) { 1549 if (filters.length == 0) { 1550 resetFilters(); 1551 } else { 1552 this.filters = new ArrayList (Arrays.asList(filters)); 1553 refresh(); 1554 } 1555 } 1556 1557 1561 public void resetFilters() { 1562 if (filters != null) { 1563 filters = null; 1564 refresh(); 1565 } 1566 } 1567 1568 1575 public abstract void reveal(Object element); 1576 1577 1581 public void setContentProvider(IContentProvider provider) { 1582 assertContentProviderType(provider); 1583 super.setContentProvider(provider); 1584 } 1585 1586 1591 protected void assertContentProviderType(IContentProvider provider) { 1592 Assert.isTrue(provider instanceof IStructuredContentProvider); 1593 } 1594 1595 1599 public final void setInput(Object input) { 1600 1601 try { 1602 1604 unmapAllElements(); 1605 1606 super.setInput(input); 1607 1608 } finally { 1609 } 1611 } 1612 1613 1617 public void setSelection(ISelection selection, boolean reveal) { 1618 1634 Control control = getControl(); 1635 if (control == null || control.isDisposed()) { 1636 return; 1637 } 1638 if (!inChange) { 1639 setSelectionToWidget(selection, reveal); 1640 ISelection sel = getSelection(); 1641 updateSelection(sel); 1642 firePostSelectionChanged(new SelectionChangedEvent(this, sel)); 1643 } else { 1644 restoreSelection = false; 1645 setSelectionToWidget(selection, reveal); 1646 } 1647 } 1648 1649 1664 protected abstract void setSelectionToWidget(List l, boolean reveal); 1665 1666 1681 protected void setSelectionToWidget(ISelection selection, boolean reveal) { 1682 if (selection instanceof IStructuredSelection) { 1683 setSelectionToWidget(((IStructuredSelection) selection).toList(), reveal); 1684 } else { 1685 setSelectionToWidget((List ) null, reveal); 1686 } 1687 } 1688 1689 1699 public void setSorter(ViewerSorter sorter) { 1700 if (this.sorter != sorter) { 1701 this.sorter = sorter; 1702 refresh(); 1703 } 1704 } 1705 1706 1721 public void setComparator(ViewerComparator comparator){ 1722 if (this.sorter != comparator){ 1723 this.sorter = comparator; 1724 refresh(); 1725 } 1726 } 1727 1728 1737 public void setUseHashlookup(boolean enable) { 1738 Assert.isTrue(getInput() == null, 1739 "Can only enable the hash look up before input has been set"); if (enable) { 1741 elementMap = newHashtable(CustomHashtable.DEFAULT_CAPACITY); 1742 } else { 1743 elementMap = null; 1744 } 1745 } 1746 1747 1756 public void setComparer(IElementComparer comparer) { 1757 this.comparer = comparer; 1758 if (elementMap != null) { 1759 elementMap = new CustomHashtable(elementMap, comparer); 1760 } 1761 } 1762 1763 1768 public Widget testFindItem(Object element) { 1769 return findItem(element); 1770 } 1771 1772 1778 public Widget[] testFindItems(Object element) { 1779 return findItems(element); 1780 } 1781 1782 1789 protected void unmapAllElements() { 1790 if (elementMap != null) { 1791 elementMap = newHashtable(CustomHashtable.DEFAULT_CAPACITY); 1792 } 1793 } 1794 1795 1807 protected void unmapElement(Object element) { 1808 if (elementMap != null) { 1809 elementMap.remove(element); 1810 } 1811 } 1812 1813 1827 protected void unmapElement(Object element, Widget item) { 1828 if (elementMap != null) { 1831 Object widgetOrWidgets = elementMap.get(element); 1832 if (widgetOrWidgets == null) { 1833 return; 1835 } else if (widgetOrWidgets instanceof Widget) { 1836 if (item == widgetOrWidgets) { 1837 elementMap.remove(element); 1838 } 1839 } else { 1840 Widget[] widgets = (Widget[]) widgetOrWidgets; 1841 int indexOfItem = Arrays.asList(widgets).indexOf(item); 1842 if (indexOfItem == -1) { 1843 return; 1844 } 1845 int length = widgets.length; 1846 if (indexOfItem == 0) { 1847 if(length == 1) { 1848 elementMap.remove(element); 1849 } else { 1850 Widget[] updatedWidgets = new Widget[length - 1]; 1851 System.arraycopy(widgets, 1, updatedWidgets, 0, length -1 ); 1852 elementMap.put(element, updatedWidgets); 1853 } 1854 } else { 1855 Widget[] updatedWidgets = new Widget[length - 1]; 1856 System.arraycopy(widgets, 0, updatedWidgets, 0, indexOfItem); 1857 System.arraycopy(widgets, indexOfItem + 1, updatedWidgets, indexOfItem, length - indexOfItem - 1); 1858 elementMap.put(element, updatedWidgets); 1859 } 1860 } 1861 } 1862 } 1863 1864 1903 public void update(Object [] elements, String [] properties) { 1904 for (int i = 0; i < elements.length; ++i) { 1905 update(elements[i], properties); 1906 } 1907 } 1908 1909 1948 public void update(Object element, String [] properties) { 1949 Assert.isNotNull(element); 1950 Widget[] items = findItems(element); 1951 1952 for (int i = 0; i < items.length; i++) { 1953 internalUpdate(items[i], element, properties); 1954 } 1955 } 1956 1957 1976 protected void internalUpdate(Widget widget, Object element, String [] properties) { 1977 boolean needsRefilter = false; 1978 if (properties != null) { 1979 for (int i = 0; i < properties.length; ++i) { 1980 needsRefilter = needsRefilter(element, properties[i]); 1981 if (needsRefilter) { 1982 break; 1983 } 1984 } 1985 } 1986 if (needsRefilter) { 1987 preservingSelection(new Runnable () { 1988 public void run() { 1989 internalRefresh(getRoot()); 1990 } 1991 }); 1992 return; 1993 } 1994 1995 boolean needsUpdate; 1996 if (properties == null) { 1997 needsUpdate = true; 1998 } else { 1999 needsUpdate = false; 2000 IBaseLabelProvider labelProvider = getLabelProvider(); 2001 for (int i = 0; i < properties.length; ++i) { 2002 needsUpdate = labelProvider.isLabelProperty(element, properties[i]); 2003 if (needsUpdate) { 2004 break; 2005 } 2006 } 2007 } 2008 if (needsUpdate) { 2009 updateItem(widget, element); 2010 } 2011 } 2012 2013 2025 protected final void updateItem(Widget widget, Object element) { 2026 SafeRunnable.run(new UpdateItemSafeRunnable(widget, element, true)); 2027 } 2028 2029 2045 protected void updateSelection(ISelection selection) { 2046 SelectionChangedEvent event = new SelectionChangedEvent(this, selection); 2047 fireSelectionChanged(event); 2048 } 2049 2050 2061 protected boolean usingElementMap() { 2062 return elementMap != null; 2063 } 2064 2065 2068 public void setLabelProvider(IBaseLabelProvider labelProvider) { 2069 if (labelProvider instanceof IColorProvider || labelProvider instanceof IFontProvider) { 2070 colorAndFontCollector = new ColorAndFontCollectorWithProviders(labelProvider); 2071 } else { 2072 colorAndFontCollector = new ColorAndFontCollector(); 2073 } 2074 super.setLabelProvider(labelProvider); 2075 2076 } 2077 2078 2083 protected void buildLabel(ViewerLabel updateLabel, Object element){ 2084 2085 if (getLabelProvider() instanceof IViewerLabelProvider) { 2086 IViewerLabelProvider itemProvider = (IViewerLabelProvider) getLabelProvider(); 2087 itemProvider.updateLabel(updateLabel, element); 2088 2089 colorAndFontCollector.setUsedDecorators(); 2090 2091 if(updateLabel.hasNewBackground()) { 2092 colorAndFontCollector.setBackground(updateLabel.getBackground()); 2093 } 2094 2095 if(updateLabel.hasNewForeground()) { 2096 colorAndFontCollector.setForeground(updateLabel.getForeground()); 2097 } 2098 2099 if(updateLabel.hasNewFont()) { 2100 colorAndFontCollector.setFont(updateLabel.getFont()); 2101 } 2102 return; 2103 2104 } 2105 2106 if(getLabelProvider() instanceof ILabelProvider){ 2107 ILabelProvider labelProvider = (ILabelProvider) getLabelProvider(); 2108 updateLabel.setText(labelProvider.getText(element)); 2109 updateLabel.setImage(labelProvider.getImage(element)); 2110 } 2111 2112 } 2113 2114 2120 void buildLabel(ViewerLabel updateLabel, Object element,IViewerLabelProvider labelProvider){ 2121 2122 labelProvider.updateLabel(updateLabel, element); 2123 2124 colorAndFontCollector.setUsedDecorators(); 2125 2126 if(updateLabel.hasNewBackground()) { 2127 colorAndFontCollector.setBackground(updateLabel.getBackground()); 2128 } 2129 2130 if(updateLabel.hasNewForeground()) { 2131 colorAndFontCollector.setForeground(updateLabel.getForeground()); 2132 } 2133 2134 if(updateLabel.hasNewFont()) { 2135 colorAndFontCollector.setFont(updateLabel.getFont()); 2136 } 2137 2138 } 2139 2140 2146 void buildLabel(ViewerLabel updateLabel, TreePath elementPath,ITreePathLabelProvider labelProvider){ 2147 2148 labelProvider.updateLabel(updateLabel, elementPath); 2149 2150 colorAndFontCollector.setUsedDecorators(); 2151 2152 if(updateLabel.hasNewBackground()) { 2153 colorAndFontCollector.setBackground(updateLabel.getBackground()); 2154 } 2155 2156 if(updateLabel.hasNewForeground()) { 2157 colorAndFontCollector.setForeground(updateLabel.getForeground()); 2158 } 2159 2160 if(updateLabel.hasNewFont()) { 2161 colorAndFontCollector.setFont(updateLabel.getFont()); 2162 } 2163 2164 } 2165 2166 2172 void buildLabel(ViewerLabel updateLabel, Object element,ILabelProvider labelProvider){ 2173 updateLabel.setText(labelProvider.getText(element)); 2174 updateLabel.setImage(labelProvider.getImage(element)); 2175 } 2176 2177 2182 protected ColorAndFontCollector getColorAndFontCollector() { 2183 return colorAndFontCollector; 2184 } 2185 2186 protected void handleDispose(DisposeEvent event) { 2187 super.handleDispose(event); 2188 sorter = null; 2189 comparer = null; 2190 if (filters != null) 2191 filters.clear(); 2192 elementMap = newHashtable(1); 2193 openListeners.clear(); 2194 doubleClickListeners.clear(); 2195 colorAndFontCollector.clear(); 2196 postSelectionChangedListeners.clear(); 2197 } 2198 2199} 2200 | Popular Tags |