📄 abstracttreeviewer.java
字号:
*/ private int insertionPosition(Item[] items, ViewerComparator comparator, int lastInsertion, Object element, TreePath parentPath) { int size = items.length; if (comparator == null) { return size; } int min = lastInsertion, max = size - 1; while (min <= max) { int mid = (min + max) / 2; Object data = items[mid].getData(); int compare = internalCompare(comparator, parentPath, data, element); if (compare == 0) { return mid;//Return if we already match } if (compare < 0) { min = mid + 1; } else { max = mid - 1; } } return min; } /** * Returns the index where the item should be inserted. It uses sorter to * determine the correct position, if sorter is not assigned, returns the * index of the element after the last. * * @param parent The parent widget * @param sorter The sorter to use. * @param startIndex * the start index to start search for position from this allows * optimising search for multiple elements that are sorted * themself. * @param element * element to find position for. * @param currentSize * the current size of the collection * @return the index to use when inserting the element. * */ /** * Returns the index where the item should be inserted. * * @param parent * The parent widget the element will be inserted into. * @param element * The element to insert. * @return int */ protected int indexForElement(Widget parent, Object element) { ViewerComparator comparator = getComparator(); TreePath parentPath = internalGetSorterParentPath(parent, comparator); Item[] items = getChildren(parent); int count = items.length; if (comparator == null) { return count; } int min = 0, max = count - 1; while (min <= max) { int mid = (min + max) / 2; Object data = items[mid].getData(); int compare = internalCompare(comparator, parentPath, data, element); if (compare == 0) { // find first item > element while (compare == 0) { ++mid; if (mid >= count) { break; } data = items[mid].getData(); compare = internalCompare(comparator, parentPath, data, element); } return mid; } if (compare < 0) { min = mid + 1; } else { max = mid - 1; } } return min; } /** * Return the tree path that should be used as the parent path for the * given widget and sorter. A <code>null</code> is returned if either * the sorter is not a {@link TreePathViewerSorter} or if the parent * widget is not an {@link Item} (i.e. is the root of the tree). * * @param parent the parent widget * @param sorter the sorter * @return the tree path that should be used as the parent path for the * given widget and sorter */ private TreePath internalGetSorterParentPath(Widget parent, ViewerComparator comparator) { TreePath path; if (comparator instanceof TreePathViewerSorter && parent instanceof Item) { Item item = (Item) parent; path = getTreePathFromItem(item); } else { path = null; } return path; } /** * Compare the two elements using the given sorter. If the * sorter is a {@link TreePathViewerSorter}, the provided * tree path will be used. If the tree path is null and the * sorter is a tree path sorter, then the elements are root elements * @param sorter the sorter * @param parentPath the path of the elements' parent * @param e1 the first element * @param e2 the seocnd element * @return the result of comparing the two elements */ private int internalCompare(ViewerComparator comparator, TreePath parentPath, Object e1, Object e2) { if (comparator instanceof TreePathViewerSorter) { TreePathViewerSorter tpvs = (TreePathViewerSorter) comparator; return tpvs.compare(this, parentPath, e1, e2); } return comparator.compare(this, e1, e2); } /* (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#getSortedChildren(java.lang.Object) */ protected Object[] getSortedChildren(Object parentElementOrTreePath) { Object[] result = getFilteredChildren(parentElementOrTreePath); ViewerComparator comparator = getComparator(); if (parentElementOrTreePath!=null && comparator instanceof TreePathViewerSorter) { TreePathViewerSorter tpvs = (TreePathViewerSorter) comparator; // be sure we're not modifying the original array from the model result = (Object[]) result.clone(); TreePath path = null; if (parentElementOrTreePath instanceof TreePath) { path = (TreePath) parentElementOrTreePath; } else { Object parent = parentElementOrTreePath; Widget w = internalGetWidgetToSelect(parent); if (w != null) { path = internalGetSorterParentPath(w, comparator); } } tpvs.sort(this, path, result); } else if (comparator != null) { // be sure we're not modifying the original array from the model result = (Object[]) result.clone(); comparator.sort(this, result); } return result; } /* (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#getFilteredChildren(java.lang.Object) */ protected Object[] getFilteredChildren(Object parentElementOrTreePath) { Object[] result = getRawChildren(parentElementOrTreePath); ViewerFilter[] filters = getFilters(); for (int i = 0; i < filters.length; i++) { ViewerFilter filter = filters[i]; result = filter.filter(this, parentElementOrTreePath, result); } return result; } /** * Adds the given child element to this viewer as a child of the given * parent element. If this viewer does not have a sorter, the element is * added at the end of the parent's list of children; otherwise, the * element is inserted at the appropriate position. * <p> * This method should be called (by the content provider) when a single * element has been added to the model, in order to cause the viewer to * accurately reflect the model. This method only affects the viewer, not * the model. Note that there is another method for efficiently processing * the simultaneous addition of multiple elements. * </p> * * @param parentElementOrTreePath * the parent element or path * @param childElement * the child element */ public void add(Object parentElementOrTreePath, Object childElement) { add(parentElementOrTreePath, new Object[] { childElement }); } /** * Adds the given SWT selection listener to the given SWT control. * * @param control * the SWT control * @param listener * the SWT selection listener * @deprecated */ protected void addSelectionListener(Control control, SelectionListener listener) { // do nothing } /** * Adds a listener for expand and collapse events in this viewer. Has no * effect if an identical listener is already registered. * * @param listener * a tree viewer listener */ public void addTreeListener(ITreeViewerListener listener) { treeListeners.add(listener); } /** * Adds the given SWT tree listener to the given SWT control. * * @param control * the SWT control * @param listener * the SWT tree listener */ protected abstract void addTreeListener(Control control, TreeListener listener); /* (non-Javadoc) @see StructuredViewer#associate(Object, Item) */ protected void associate(Object element, Item item) { Object data = item.getData(); if (data != null && data != element && equals(data, element)) { // workaround for PR 1FV62BT // assumption: elements are equal but not identical // -> remove from map but don't touch children unmapElement(data, item); item.setData(element); mapElement(element, item); } else { // recursively disassociate all super.associate(element, item); } } /** * Collapses all nodes of the viewer's tree, starting with the root. This * method is equivalent to <code>collapseToLevel(ALL_LEVELS)</code>. */ public void collapseAll() { Object root = getRoot(); if (root != null) { collapseToLevel(root, ALL_LEVELS); } } /** * Collapses the subtree rooted at the given element or tree path to the given level. * * @param elementOrTreePath * the element or tree path * @param level * non-negative level, or <code>ALL_LEVELS</code> to collapse * all levels of the tree */ public void collapseToLevel(Object elementOrTreePath, int level) { Assert.isNotNull(elementOrTreePath); Widget w = internalGetWidgetToSelect(elementOrTreePath); if (w != null) { internalCollapseToLevel(w, level); } } /** * Creates all children for the given widget. * <p> * The default implementation of this framework method assumes that <code>widget.getData()</code> * returns the element corresponding to the node. Note: the node is not * visually expanded! You may have to call <code>parent.setExpanded(true)</code>. * </p> * * @param widget * the widget */ protected void createChildren(final Widget widget) { final Item[] tis = getChildren(widget); if (tis != null && tis.length > 0) { Object data = tis[0].getData(); if (data != null) { return; // children already there! } } BusyIndicator.showWhile(widget.getDisplay(), new Runnable() { public void run() { // fix for PR 1FW89L7: // don't complain and remove all "dummies" ... if (tis != null) { for (int i = 0; i < tis.length; i++) { if (tis[i].getData() != null) { disassociate(tis[i]); Assert.isTrue(tis[i].getData() == null, "Second or later child is non -null");//$NON-NLS-1$ } tis[i].dispose(); } } Object d = widget.getData(); if (d != null) { Object parentElement = d; Object[] children; if (isTreePathContentProvider() && widget instanceof Item) { TreePath path = getTreePathFromItem((Item)widget); children = getSortedChildren(path); } else { children = getSortedChildren(parentElement); } for (int i = 0; i < children.length; i++) { createTreeItem(widget, children[i], -1); } } } }); } /** * Creates a single item for the given parent and synchronizes it with the * given element. * * @param parent * the parent widget * @param element * the element * @param index * if non-negative, indicates the position to insert the item * into its parent */ protected void createTreeItem(Widget parent, Object element, int index) { Item item = newItem(parent, SWT.NULL, index); updateItem(item, element); updatePlus(item, element); } /** * The <code>AbstractTreeViewer</code> implementation of this method also * recurses over children of the corresponding element. */ protected void disassociate(Item item) { super.disassociate(item); // recursively unmapping the items is only required when // the hash map is used. In the other case disposing // an item will recursively dispose its children. if (usingElementMap()) { disassociateChildren(item); } } /** * Disassociates the children of the given SWT item from their * corresponding elements. * * @param item * the widget */ private void disassociateChildren(Item item) { Item[] items = getChildren(item); for (int i = 0; i < items.length; i++) { if (items[i].getData() != null) { disassociate(items[i]); } } } /* (non-Javadoc) Method declared on StructuredViewer. */ protected Widget doFindInputItem(Object element) { // compare with root Object root = getRoot(); if (root == null) { return null; } if (equals(root, element)) { return getControl(); } return null; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -