1 12 13 package org.eclipse.ui.views.properties; 14 15 import java.util.ArrayList ; 16 import java.util.Arrays ; 17 import java.util.HashMap ; 18 import java.util.HashSet ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.eclipse.core.runtime.ListenerList; 25 import org.eclipse.jface.action.IStatusLineManager; 26 import org.eclipse.jface.viewers.CellEditor; 27 import org.eclipse.jface.viewers.ICellEditorListener; 28 import org.eclipse.jface.viewers.ISelection; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.SelectionChangedEvent; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.jface.viewers.Viewer; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.custom.TreeEditor; 35 import org.eclipse.swt.events.ControlAdapter; 36 import org.eclipse.swt.events.ControlEvent; 37 import org.eclipse.swt.events.DisposeEvent; 38 import org.eclipse.swt.events.DisposeListener; 39 import org.eclipse.swt.events.KeyAdapter; 40 import org.eclipse.swt.events.KeyEvent; 41 import org.eclipse.swt.events.MouseAdapter; 42 import org.eclipse.swt.events.MouseEvent; 43 import org.eclipse.swt.events.SelectionAdapter; 44 import org.eclipse.swt.events.SelectionEvent; 45 import org.eclipse.swt.events.TreeEvent; 46 import org.eclipse.swt.events.TreeListener; 47 import org.eclipse.swt.graphics.Image; 48 import org.eclipse.swt.graphics.Point; 49 import org.eclipse.swt.graphics.Rectangle; 50 import org.eclipse.swt.widgets.Composite; 51 import org.eclipse.swt.widgets.Control; 52 import org.eclipse.swt.widgets.Tree; 53 import org.eclipse.swt.widgets.TreeColumn; 54 import org.eclipse.swt.widgets.TreeItem; 55 import org.eclipse.swt.widgets.Widget; 56 import org.eclipse.ui.internal.views.properties.PropertiesMessages; 57 58 67 68 class PropertySheetViewer extends Viewer { 69 private Object [] input; 71 72 private IPropertySheetEntry rootEntry; 74 75 private PropertySheetCategory[] categories; 77 78 private Tree tree; 80 81 86 private HashMap entryToItemMap = new HashMap (); 87 88 private TreeEditor treeEditor; 89 90 private static String [] columnLabels = { 91 PropertiesMessages.PropertyViewer_property, PropertiesMessages.PropertyViewer_value }; 92 93 private static String MISCELLANEOUS_CATEGORY_NAME = PropertiesMessages.PropertyViewer_misc; 94 95 private int columnToEdit = 1; 97 98 private CellEditor cellEditor; 99 100 private IPropertySheetEntryListener entryListener; 101 102 private ICellEditorListener editorListener; 103 104 private boolean isShowingCategories = true; 106 107 private boolean isShowingExpertProperties = false; 109 110 private IStatusLineManager statusLineManager; 112 113 private ListenerList activationListeners = new ListenerList(); 115 116 private PropertySheetSorter sorter = new PropertySheetSorter(); 118 119 126 public PropertySheetViewer(Composite parent) { 127 tree = new Tree(parent, SWT.FULL_SELECTION | SWT.SINGLE 128 | SWT.HIDE_SELECTION); 129 130 tree.setLinesVisible(true); 132 tree.setHeaderVisible(true); 133 134 addColumns(); 136 137 hookControl(); 139 140 treeEditor = new TreeEditor(tree); 142 143 createEntryListener(); 145 createEditorListener(); 146 } 147 148 154 private void activateCellEditor(TreeItem item) { 155 tree.showSelection(); 157 158 IPropertySheetEntry activeEntry = (IPropertySheetEntry) item.getData(); 160 161 cellEditor = activeEntry.getEditor(tree); 164 165 if (cellEditor == null) { 166 return; 168 } 169 170 cellEditor.activate(); 172 173 Control control = cellEditor.getControl(); 175 if (control == null) { 176 cellEditor.deactivate(); 177 cellEditor = null; 178 return; 179 } 180 181 cellEditor.addListener(editorListener); 183 184 CellEditor.LayoutData layout = cellEditor.getLayoutData(); 186 treeEditor.horizontalAlignment = layout.horizontalAlignment; 187 treeEditor.grabHorizontal = layout.grabHorizontal; 188 treeEditor.minimumWidth = layout.minimumWidth; 189 treeEditor.setEditor(control, item, columnToEdit); 190 191 setErrorMessage(cellEditor.getErrorMessage()); 193 194 cellEditor.setFocus(); 196 197 fireCellEditorActivated(cellEditor); 199 } 200 201 208 209 void addActivationListener(ICellEditorActivationListener listener) { 210 activationListeners.add(listener); 211 } 212 213 216 private void addColumns() { 217 218 TreeColumn[] columns = tree.getColumns(); 220 for (int i = 0; i < columnLabels.length; i++) { 221 String string = columnLabels[i]; 222 if (string != null) { 223 TreeColumn column; 224 if (i < columns.length) { 225 column = columns[i]; 226 } else { 227 column = new TreeColumn(tree, 0); 228 } 229 column.setText(string); 230 } 231 } 232 233 tree.addControlListener(new ControlAdapter() { 234 public void controlResized(ControlEvent e) { 235 Rectangle area = tree.getClientArea(); 236 TreeColumn[] columns = tree.getColumns(); 237 if (area.width > 0) { 238 columns[0].setWidth(area.width * 40 / 100); 239 columns[1].setWidth(area.width - columns[0].getWidth() - 4); 240 tree.removeControlListener(this); 241 } 242 } 243 }); 244 245 } 246 247 251 private void applyEditorValue() { 252 TreeItem treeItem = treeEditor.getItem(); 253 if (treeItem == null || treeItem.isDisposed()) { 255 return; 256 } 257 IPropertySheetEntry entry = (IPropertySheetEntry) treeItem.getData(); 258 entry.applyEditorValue(); 259 } 260 261 267 private void createChildren(Widget widget) { 268 TreeItem[] childItems = getChildItems(widget); 270 271 if (childItems.length > 0) { 272 Object data = childItems[0].getData(); 273 if (data != null) { 274 return; 276 } 277 childItems[0].dispose(); 279 } 280 281 Object node = widget.getData(); 283 List children = getChildren(node); 284 if (children.isEmpty()) { 285 return; 287 } 288 for (int i = 0; i < children.size(); i++) { 289 createItem(children.get(i), widget, i); 291 } 292 } 293 294 297 private void createEditorListener() { 298 editorListener = new ICellEditorListener() { 299 public void cancelEditor() { 300 deactivateCellEditor(); 301 } 302 303 public void editorValueChanged(boolean oldValidState, 304 boolean newValidState) { 305 } 307 308 public void applyEditorValue() { 309 } 311 }; 312 } 313 314 317 private void createEntryListener() { 318 entryListener = new IPropertySheetEntryListener() { 319 public void childEntriesChanged(IPropertySheetEntry entry) { 320 if (entry == rootEntry) { 322 updateChildrenOf(entry, tree); 323 } else { 324 TreeItem item = findItem(entry); 325 if (item != null) { 326 updateChildrenOf(entry, item); 327 } 328 } 329 } 330 331 public void valueChanged(IPropertySheetEntry entry) { 332 TreeItem item = findItem(entry); 334 if (item != null) { 335 updateEntry(entry, item); 336 } 337 } 338 339 public void errorMessageChanged(IPropertySheetEntry entry) { 340 setErrorMessage(entry.getErrorText()); 342 } 343 }; 344 } 345 346 357 private void createItem(Object node, Widget parent, int index) { 358 TreeItem item; 360 if (parent instanceof TreeItem) { 361 item = new TreeItem((TreeItem) parent, SWT.NONE, index); 362 } else { 363 item = new TreeItem((Tree) parent, SWT.NONE, index); 364 } 365 366 item.setData(node); 368 369 entryToItemMap.put(node, item); 371 372 item.addDisposeListener(new DisposeListener() { 375 public void widgetDisposed(DisposeEvent e) { 376 Object possibleEntry = e.widget.getData(); 377 if (possibleEntry != null) 378 entryToItemMap.remove(possibleEntry); 379 } 380 }); 381 382 if (node instanceof IPropertySheetEntry) { 384 ((IPropertySheetEntry) node) 385 .addPropertySheetEntryListener(entryListener); 386 } 387 388 if (node instanceof IPropertySheetEntry) { 390 updateEntry((IPropertySheetEntry) node, item); 391 } else { 392 updateCategory((PropertySheetCategory) node, item); 393 } 394 } 395 396 399 400 void deactivateCellEditor() { 401 treeEditor.setEditor(null, null, columnToEdit); 402 if (cellEditor != null) { 403 cellEditor.deactivate(); 404 fireCellEditorDeactivated(cellEditor); 405 cellEditor.removeListener(editorListener); 406 cellEditor = null; 407 } 408 setErrorMessage(null); 410 } 411 412 416 private void entrySelectionChanged() { 417 SelectionChangedEvent changeEvent = new SelectionChangedEvent(this, 418 getSelection()); 419 fireSelectionChanged(changeEvent); 420 } 421 422 432 private TreeItem findItem(IPropertySheetEntry entry) { 433 TreeItem[] items = tree.getItems(); 435 for (int i = 0; i < items.length; i++) { 436 TreeItem item = items[i]; 437 TreeItem findItem = findItem(entry, item); 438 if (findItem != null) { 439 return findItem; 440 } 441 } 442 return null; 443 } 444 445 457 private TreeItem findItem(IPropertySheetEntry entry, TreeItem item) { 458 Object mapItem = entryToItemMap.get(entry); 460 if (mapItem != null && mapItem instanceof TreeItem) 461 return (TreeItem) mapItem; 462 463 if (entry == item.getData()) { 465 return item; 466 } 467 468 TreeItem[] items = item.getItems(); 470 for (int i = 0; i < items.length; i++) { 471 TreeItem childItem = items[i]; 472 TreeItem findItem = findItem(entry, childItem); 473 if (findItem != null) { 474 return findItem; 475 } 476 } 477 return null; 478 } 479 480 487 private void fireCellEditorActivated(CellEditor activatedCellEditor) { 488 Object [] listeners = activationListeners.getListeners(); 489 for (int i = 0; i < listeners.length; ++i) { 490 ((ICellEditorActivationListener) listeners[i]) 491 .cellEditorActivated(activatedCellEditor); 492 } 493 } 494 495 502 private void fireCellEditorDeactivated(CellEditor activatedCellEditor) { 503 Object [] listeners = activationListeners.getListeners(); 504 for (int i = 0; i < listeners.length; ++i) { 505 ((ICellEditorActivationListener) listeners[i]) 506 .cellEditorDeactivated(activatedCellEditor); 507 } 508 } 509 510 516 public CellEditor getActiveCellEditor() { 517 return cellEditor; 518 } 519 520 private TreeItem[] getChildItems(Widget widget) { 521 if (widget instanceof Tree) { 522 return ((Tree) widget).getItems(); 523 } 524 else if (widget instanceof TreeItem) { 525 return ((TreeItem) widget).getItems(); 526 } 527 return new TreeItem[0]; 529 } 530 531 539 private List getChildren(Object node) { 540 IPropertySheetEntry entry = null; 542 PropertySheetCategory category = null; 543 if (node instanceof IPropertySheetEntry) { 544 entry = (IPropertySheetEntry) node; 545 } else { 546 category = (PropertySheetCategory) node; 547 } 548 549 List children; 551 if (category == null) { 552 children = getChildren(entry); 553 } else { 554 children = getChildren(category); 555 } 556 557 return children; 558 } 559 560 567 private List getChildren(IPropertySheetEntry entry) { 568 if (entry == rootEntry && isShowingCategories) { 572 if (categories.length > 1 573 || (categories.length == 1 && !categories[0] 574 .getCategoryName().equals( 575 MISCELLANEOUS_CATEGORY_NAME))) { 576 return Arrays.asList(categories); 577 } 578 } 579 580 return getSortedEntries(getFilteredEntries(entry.getChildEntries())); 582 } 583 584 592 private List getChildren(PropertySheetCategory category) { 593 return getSortedEntries(getFilteredEntries(category.getChildEntries())); 594 } 595 596 599 public Control getControl() { 600 return tree; 601 } 602 603 610 private List getFilteredEntries(IPropertySheetEntry[] entries) { 611 if (isShowingExpertProperties) { 613 return Arrays.asList(entries); 614 } 615 616 List filteredEntries = new ArrayList (entries.length); 618 for (int i = 0; i < entries.length; i++) { 619 IPropertySheetEntry entry = entries[i]; 620 if (entry != null) { 621 String [] filters = entry.getFilters(); 622 boolean expert = false; 623 if (filters != null) { 624 for (int j = 0; j < filters.length; j++) { 625 if (filters[j].equals(IPropertySheetEntry.FILTER_ID_EXPERT)) { 626 expert = true; 627 break; 628 } 629 } 630 } 631 if (!expert) { 632 filteredEntries.add(entry); 633 } 634 } 635 } 636 return filteredEntries; 637 } 638 639 646 private List getSortedEntries(List unsortedEntries) { 647 IPropertySheetEntry[] propertySheetEntries = (IPropertySheetEntry[]) unsortedEntries 648 .toArray(new IPropertySheetEntry[unsortedEntries.size()]); 649 sorter.sort(propertySheetEntries); 650 return Arrays.asList(propertySheetEntries); 651 } 652 653 654 660 public Object getInput() { 661 return input; 662 } 663 664 670 public IPropertySheetEntry getRootEntry() { 671 return rootEntry; 672 } 673 674 683 public ISelection getSelection() { 684 if (tree.getSelectionCount() == 0) { 685 return StructuredSelection.EMPTY; 686 } 687 TreeItem[] sel = tree.getSelection(); 688 List entries = new ArrayList (sel.length); 689 for (int i = 0; i < sel.length; i++) { 690 TreeItem ti = sel[i]; 691 Object data = ti.getData(); 692 if (data instanceof IPropertySheetEntry) { 693 entries.add(data); 694 } 695 } 696 return new StructuredSelection(entries); 697 } 698 699 707 private void handleSelect(TreeItem selection) { 708 if (cellEditor != null) { 710 applyEditorValue(); 711 deactivateCellEditor(); 712 } 713 714 TreeItem[] sel = new TreeItem[] { selection }; 716 if (sel.length == 0) { 717 setMessage(null); 718 setErrorMessage(null); 719 } else { 720 Object object = sel[0].getData(); if (object instanceof IPropertySheetEntry) { 722 IPropertySheetEntry activeEntry = (IPropertySheetEntry) object; 724 725 setMessage(activeEntry.getDescription()); 727 728 activateCellEditor(sel[0]); 730 } 731 } 732 entrySelectionChanged(); 733 } 734 735 742 private void handleTreeCollapse(TreeEvent event) { 743 if (cellEditor != null) { 744 applyEditorValue(); 745 deactivateCellEditor(); 746 } 747 } 748 749 761 private void handleTreeExpand(TreeEvent event) { 762 createChildren(event.item); 763 } 764 765 768 769 void hideCategories() { 770 isShowingCategories = false; 771 categories = null; 772 refresh(); 773 } 774 775 778 779 void hideExpert() { 780 isShowingExpertProperties = false; 781 refresh(); 782 } 783 784 787 private void hookControl() { 788 tree.addSelectionListener(new SelectionAdapter() { 792 795 public void widgetSelected(SelectionEvent e) { 796 if (cellEditor == null || !cellEditor.isActivated()) { 799 updateStatusLine(e.item); 800 } 801 } 802 803 806 public void widgetDefaultSelected(SelectionEvent e) { 807 handleSelect((TreeItem) e.item); 808 } 809 }); 810 tree.addMouseListener(new MouseAdapter() { 812 public void mouseDown(MouseEvent event) { 813 Point pt = new Point(event.x, event.y); 815 TreeItem item = tree.getItem(pt); 816 if (item != null) { 817 handleSelect(item); 818 } 819 } 820 }); 821 822 tree.addTreeListener(new TreeListener() { 825 public void treeExpanded(final TreeEvent event) { 826 handleTreeExpand(event); 827 } 828 829 public void treeCollapsed(final TreeEvent event) { 830 handleTreeCollapse(event); 831 } 832 }); 833 834 tree.addKeyListener(new KeyAdapter() { 836 public void keyReleased(KeyEvent e) { 837 if (e.character == SWT.ESC) { 838 deactivateCellEditor(); 839 } else if (e.keyCode == SWT.F5) { 840 setInput(getInput()); 842 } 843 } 844 }); 845 } 846 847 851 protected void updateStatusLine(Widget item) { 852 setMessage(null); 853 setErrorMessage(null); 854 855 if (item != null) { 857 if (item.getData() instanceof PropertySheetEntry) { 858 PropertySheetEntry psEntry = (PropertySheetEntry) item.getData(); 859 860 String desc = psEntry.getDescription(); 862 if (desc != null && desc.length() > 0) { 863 setMessage(psEntry.getDescription()); 864 } else { 865 setMessage(psEntry.getDisplayName()); 866 } 867 } 868 869 else if (item.getData() instanceof PropertySheetCategory) { 870 PropertySheetCategory psCat = (PropertySheetCategory) item.getData(); 871 setMessage(psCat.getCategoryName()); 872 } 873 } 874 } 875 876 884 public void refresh() { 885 if (rootEntry != null) { 886 updateChildrenOf(rootEntry, tree); 887 } 888 } 889 890 897 898 void removeActivationListener(ICellEditorActivationListener listener) { 899 activationListeners.remove(listener); 900 } 901 902 909 private void removeItem(TreeItem item) { 910 Object data = item.getData(); 911 if (data instanceof IPropertySheetEntry) { 912 ((IPropertySheetEntry) data) 913 .removePropertySheetEntryListener(entryListener); 914 } 915 item.setData(null); 916 917 entryToItemMap.remove(data); 919 920 item.dispose(); 921 } 922 923 926 public void resetProperties() { 927 IStructuredSelection selection = (IStructuredSelection) getSelection(); 929 930 Iterator itr = selection.iterator(); 932 while (itr.hasNext()) { 933 ((IPropertySheetEntry) itr.next()).resetPropertyValue(); 934 } 935 } 936 937 943 private void setErrorMessage(String errorMessage) { 944 if (statusLineManager != null) { 946 statusLineManager.setErrorMessage(errorMessage); 947 } 948 } 949 950 961 public void setInput(Object newInput) { 962 applyEditorValue(); 964 deactivateCellEditor(); 966 967 input = (Object []) newInput; 969 if (input == null) { 970 input = new Object [0]; 971 } 972 973 if (rootEntry != null) { 974 rootEntry.setValues(input); 975 updateChildrenOf(rootEntry, tree); 977 } 978 } 979 980 987 private void setMessage(String message) { 988 if (statusLineManager != null) { 990 statusLineManager.setMessage(message); 991 } 992 } 993 994 1001 public void setRootEntry(IPropertySheetEntry root) { 1002 if (rootEntry != null) { 1004 rootEntry.removePropertySheetEntryListener(entryListener); 1005 } 1006 1007 rootEntry = root; 1008 1009 tree.setData(rootEntry); 1011 1012 rootEntry.addPropertySheetEntryListener(entryListener); 1015 1016 setInput(input); 1019 } 1020 1021 1025 public void setSelection(ISelection selection, boolean reveal) { 1026 } 1028 1029 1039 public void setSorter(PropertySheetSorter sorter) { 1040 if (null == sorter) { 1041 sorter = new PropertySheetSorter(); 1042 } 1043 this.sorter = sorter; 1044 } 1045 1046 1052 public void setStatusLineManager(IStatusLineManager manager) { 1053 statusLineManager = manager; 1054 } 1055 1056 1059 1060 void showCategories() { 1061 isShowingCategories = true; 1062 refresh(); 1063 } 1064 1065 1068 1069 void showExpert() { 1070 isShowingExpertProperties = true; 1071 refresh(); 1072 } 1073 1074 1077 private void updateCategories() { 1078 if (categories == null) { 1080 categories = new PropertySheetCategory[0]; 1081 } 1082 1083 List childEntries = getFilteredEntries(rootEntry.getChildEntries()); 1085 1086 if (childEntries.size() == 0) { 1088 categories = new PropertySheetCategory[0]; 1089 return; 1090 } 1091 1092 Map categoryCache = new HashMap (categories.length * 2 + 1); 1094 for (int i = 0; i < categories.length; i++) { 1095 categories[i].removeAllEntries(); 1096 categoryCache.put(categories[i].getCategoryName(), categories[i]); 1097 } 1098 1099 List categoriesToRemove = new ArrayList (Arrays.asList(categories)); 1101 1102 PropertySheetCategory misc = (PropertySheetCategory) categoryCache 1104 .get(MISCELLANEOUS_CATEGORY_NAME); 1105 if (misc == null) { 1106 misc = new PropertySheetCategory(MISCELLANEOUS_CATEGORY_NAME); 1107 } 1108 boolean addMisc = false; 1109 1110 for (int i = 0; i < childEntries.size(); i++) { 1111 IPropertySheetEntry childEntry = (IPropertySheetEntry) childEntries 1112 .get(i); 1113 String categoryName = childEntry.getCategory(); 1114 if (categoryName == null) { 1115 misc.addEntry(childEntry); 1116 addMisc = true; 1117 categoriesToRemove.remove(misc); 1118 } else { 1119 PropertySheetCategory category = (PropertySheetCategory) categoryCache 1120 .get(categoryName); 1121 if (category == null) { 1122 category = new PropertySheetCategory(categoryName); 1123 categoryCache.put(categoryName, category); 1124 } else { 1125 categoriesToRemove.remove(category); 1126 } 1127 category.addEntry(childEntry); 1128 } 1129 } 1130 1131 if (addMisc) { 1133 categoryCache.put(MISCELLANEOUS_CATEGORY_NAME, misc); 1134 } 1135 1136 ArrayList categoryList = new ArrayList (); 1140 Set seen = new HashSet (childEntries.size()); 1141 for (int i = 0; i < childEntries.size(); i++) { 1142 IPropertySheetEntry childEntry = (IPropertySheetEntry) childEntries 1143 .get(i); 1144 String categoryName = childEntry.getCategory(); 1145 if (categoryName != null && !seen.contains(categoryName)) { 1146 seen.add(categoryName); 1147 PropertySheetCategory category = (PropertySheetCategory) categoryCache 1148 .get(categoryName); 1149 if (category != null) { 1150 categoryList.add(category); 1151 } 1152 } 1153 } 1154 if (addMisc && !seen.contains(MISCELLANEOUS_CATEGORY_NAME)) { 1155 categoryList.add(misc); 1156 } 1157 1158 PropertySheetCategory[] categoryArray = (PropertySheetCategory[]) categoryList 1159 .toArray(new PropertySheetCategory[categoryList.size()]); 1160 sorter.sort(categoryArray); 1161 categories = categoryArray; 1162 } 1163 1164 1172 private void updateCategory(PropertySheetCategory category, 1173 TreeItem item) { 1174 item.setData(category); 1176 1177 entryToItemMap.put(category, item); 1179 1180 item.setText(0, category.getCategoryName()); 1182 item.setText(1, ""); 1184 if (category.getAutoExpand()) { 1186 createChildren(item); 1188 item.setExpanded(true); 1189 category.setAutoExpand(false); 1190 } else { 1191 updatePlus(category, item); 1194 } 1195 } 1196 1197 1209 private void updateChildrenOf(Object node, Widget widget) { 1210 IPropertySheetEntry entry = null; 1212 PropertySheetCategory category = null; 1213 if (node instanceof IPropertySheetEntry) { 1214 entry = (IPropertySheetEntry) node; 1215 } else { 1216 category = (PropertySheetCategory) node; 1217 } 1218 1219 TreeItem[] childItems = getChildItems(widget); 1221 1222 TreeItem item = null; 1224 if (widget instanceof TreeItem) { 1225 item = (TreeItem) widget; 1226 } 1227 if (item != null && !item.getExpanded()) { 1228 for (int i = 0; i < childItems.length; i++) { 1230 if (childItems[i].getData() != null) { 1231 removeItem(childItems[i]); 1232 } 1233 } 1234 1235 if (category != null || entry.hasChildEntries()) { 1237 childItems = getChildItems(widget); 1245 if (childItems.length == 0) { 1246 new TreeItem(item, SWT.NULL); 1247 } 1248 } 1249 return; 1250 } 1251 1252 if (node == rootEntry && isShowingCategories) { 1254 updateCategories(); 1256 } 1257 List children = getChildren(node); 1258 1259 Set set = new HashSet (childItems.length * 2 + 1); 1261 1262 for (int i = 0; i < childItems.length; i++) { 1263 Object data = childItems[i].getData(); 1264 if (data != null) { 1265 Object e = data; 1266 int ix = children.indexOf(e); 1267 if (ix < 0) { removeItem(childItems[i]); 1269 } else { set.add(e); 1271 } 1272 } else if (data == null) { childItems[i].dispose(); 1274 } 1275 } 1276 1277 int oldCnt = -1; 1279 if (widget == tree) { 1280 oldCnt = tree.getItemCount(); 1281 } 1282 1283 int newSize = children.size(); 1285 for (int i = 0; i < newSize; i++) { 1286 Object el = children.get(i); 1287 if (!set.contains(el)) { 1288 createItem(el, widget, i); 1289 } 1290 } 1291 1292 if (widget == tree && oldCnt == 0 && tree.getItemCount() == 1) { 1294 tree.setRedraw(false); 1295 tree.setRedraw(true); 1296 } 1297 1298 childItems = getChildItems(widget); 1300 1301 for (int i = 0; i < newSize; i++) { 1305 Object el = children.get(i); 1306 if (el instanceof IPropertySheetEntry) { 1307 updateEntry((IPropertySheetEntry) el, childItems[i]); 1308 } else { 1309 updateCategory((PropertySheetCategory) el, childItems[i]); 1310 updateChildrenOf(el, childItems[i]); 1311 } 1312 } 1313 entrySelectionChanged(); 1316 } 1317 1318 1326 private void updateEntry(IPropertySheetEntry entry, TreeItem item) { 1327 item.setData(entry); 1329 1330 entryToItemMap.put(entry, item); 1332 1333 item.setText(0, entry.getDisplayName()); 1335 item.setText(1, entry.getValueAsString()); 1336 Image image = entry.getImage(); 1337 if (item.getImage(1) != image) { 1338 item.setImage(1, image); 1339 } 1340 1341 updatePlus(entry, item); 1343 } 1344 1345 1352 private void updatePlus(Object node, TreeItem item) { 1353 IPropertySheetEntry entry = null; 1355 PropertySheetCategory category = null; 1356 if (node instanceof IPropertySheetEntry) { 1357 entry = (IPropertySheetEntry) node; 1358 } else { 1359 category = (PropertySheetCategory) node; 1360 } 1361 1362 boolean hasPlus = item.getItemCount() > 0; 1363 boolean needsPlus = category != null || entry.hasChildEntries(); 1364 boolean removeAll = false; 1365 boolean addDummy = false; 1366 1367 if (hasPlus != needsPlus) { 1368 if (needsPlus) { 1369 addDummy = true; 1370 } else { 1371 removeAll = true; 1372 } 1373 } 1374 if (removeAll) { 1375 TreeItem[] items = item.getItems(); 1377 for (int i = 0; i < items.length; i++) { 1378 removeItem(items[i]); 1379 } 1380 } 1381 1382 if (addDummy) { 1383 new TreeItem(item, SWT.NULL); } 1386 } 1387} 1388 | Popular Tags |