📄 abstracttreeviewer.java
字号:
/* (non-Javadoc) Method declared on StructuredViewer. */ protected Widget doFindItem(Object element) { // compare with root Object root = getRoot(); if (root == null) { return null; } Item[] items = getChildren(getControl()); if (items != null) { for (int i = 0; i < items.length; i++) { Widget o = internalFindItem(items[i], element); if (o != null) { return o; } } } return null; } /** * Copies the attributes of the given element into the given SWT item. * * @param item * the SWT item * @param element * the element */ protected abstract void doUpdateItem(Item item, Object element); /* (non-Javadoc) Method declared on StructuredViewer. */ protected void doUpdateItem(Widget widget, Object element, boolean fullMap) { if (widget instanceof Item) { Item item = (Item) widget; // ensure that backpointer is correct if (fullMap) { associate(element, item); } else { Object data = item.getData(); if (data != null) { unmapElement(data, item); } item.setData(element); mapElement(element, item); } // update icon and label SafeRunnable.run(new UpdateItemSafeRunnable(item, element)); } } /** * Expands all nodes of the viewer's tree, starting with the root. This * method is equivalent to <code>expandToLevel(ALL_LEVELS)</code>. */ public void expandAll() { expandToLevel(ALL_LEVELS); } /** * Expands the root of the viewer's tree to the given level. * * @param level * non-negative level, or <code>ALL_LEVELS</code> to expand all * levels of the tree */ public void expandToLevel(int level) { expandToLevel(getRoot(), level); } /** * Expands all ancestors of the given element or tree path so that the given element * becomes visible in this viewer's tree control, and then expands the * subtree rooted at the given element to the given level. * * @param elementOrTreePath * the element * @param level * non-negative level, or <code>ALL_LEVELS</code> to expand all * levels of the tree */ public void expandToLevel(Object elementOrTreePath, int level) { Widget w = internalExpand(elementOrTreePath, true); if (w != null) { internalExpandToLevel(w, level); } } /** * Fires a tree collapsed event. Only listeners registered at the time this * method is called are notified. * * @param event * the tree expansion event * @see ITreeViewerListener#treeCollapsed */ protected void fireTreeCollapsed(final TreeExpansionEvent event) { Object[] listeners = treeListeners.getListeners(); for (int i = 0; i < listeners.length; ++i) { final ITreeViewerListener l = (ITreeViewerListener) listeners[i]; SafeRunnable.run(new SafeRunnable() { public void run() { l.treeCollapsed(event); } }); } } /** * Fires a tree expanded event. Only listeners registered at the time this * method is called are notified. * * @param event * the tree expansion event * @see ITreeViewerListener#treeExpanded */ protected void fireTreeExpanded(final TreeExpansionEvent event) { Object[] listeners = treeListeners.getListeners(); for (int i = 0; i < listeners.length; ++i) { final ITreeViewerListener l = (ITreeViewerListener) listeners[i]; SafeRunnable.run(new SafeRunnable() { public void run() { l.treeExpanded(event); } }); } } /** * Returns the auto-expand level. * * @return non-negative level, or <code>ALL_LEVELS</code> if all levels * of the tree are expanded automatically * @see #setAutoExpandLevel */ public int getAutoExpandLevel() { return expandToLevel; } /** * Returns the SWT child items for the given SWT widget. * * @param widget * the widget * @return the child items */ protected abstract Item[] getChildren(Widget widget); /** * Get the child for the widget at index. Note that the default * implementation is not very effecient and should be overridden * if this class is implemented. * @param widget the widget to check * @param index the index of the widget * @return Item or <code>null</code> if widget is not a type * that can contain items. * * @throws ArrayIndexOutOfBoundsException if the index is not valid. * @since 3.1 */ protected Item getChild (Widget widget, int index) { return getChildren(widget)[index]; } /** * Returns whether the given SWT item is expanded or collapsed. * * @param item * the item * @return <code>true</code> if the item is considered expanded and * <code>false</code> if collapsed */ protected abstract boolean getExpanded(Item item); /** * Returns a list of elements corresponding to expanded nodes in this * viewer's tree, including currently hidden ones that are marked as * expanded but are under a collapsed ancestor. * <p> * This method is typically used when preserving the interesting state of a * viewer; <code>setExpandedElements</code> is used during the restore. * </p> * * @return the array of expanded elements * @see #setExpandedElements */ public Object[] getExpandedElements() { ArrayList items = new ArrayList(); internalCollectExpandedItems(items, getControl()); ArrayList result = new ArrayList(items.size()); for(Iterator it = items.iterator(); it.hasNext();) { Item item = (Item) it.next(); Object data = item.getData(); if (data != null) { result.add(data); } } return result.toArray(); } /** * Returns whether the node corresponding to the given element or tree path is expanded * or collapsed. * * @param elementOrTreePath * the element * @return <code>true</code> if the node is expanded, and <code>false</code> * if collapsed */ public boolean getExpandedState(Object elementOrTreePath) { Assert.isNotNull(elementOrTreePath); Widget item = internalGetWidgetToSelect(elementOrTreePath); if (item instanceof Item) { return getExpanded((Item) item); } return false; } /** * Returns the number of child items of the given SWT control. * * @param control * the control * @return the number of children */ protected abstract int getItemCount(Control control); /** * Returns the number of child items of the given SWT item. * * @param item * the item * @return the number of children */ protected abstract int getItemCount(Item item); /** * Returns the child items of the given SWT item. * * @param item * the item * @return the child items */ protected abstract Item[] getItems(Item item); /** * Returns the item after the given item in the tree, or <code>null</code> * if there is no next item. * * @param item * the item * @param includeChildren * <code>true</code> if the children are considered in * determining which item is next, and <code>false</code> if * subtrees are ignored * @return the next item, or <code>null</code> if none */ protected Item getNextItem(Item item, boolean includeChildren) { if (item == null) { return null; } if (includeChildren && getExpanded(item)) { Item[] children = getItems(item); if (children != null && children.length > 0) { return children[0]; } } //next item is either next sibling or next sibling of first //parent that has a next sibling. Item parent = getParentItem(item); if (parent == null) { return null; } Item[] siblings = getItems(parent); if (siblings != null){ if(siblings.length <= 1) { return getNextItem(parent, false); } for (int i = 0; i < siblings.length; i++) { if (siblings[i] == item && i < (siblings.length - 1)) { return siblings[i + 1]; } } } return getNextItem(parent, false); } /** * Returns the parent item of the given item in the tree, or <code>null</code> * if there is no parent item. * * @param item * the item * @return the parent item, or <code>null</code> if none */ protected abstract Item getParentItem(Item item); /** * Returns the item before the given item in the tree, or <code>null</code> * if there is no previous item. * * @param item * the item * @return the previous item, or <code>null</code> if none */ protected Item getPreviousItem(Item item) { //previous item is either right-most visible descendent of previous //sibling or parent Item parent = getParentItem(item); if (parent == null) { return null; } Item[] siblings = getItems(parent); if (siblings.length == 0 || siblings[0] == item) { return parent; } Item previous = siblings[0]; for (int i = 1; i < siblings.length; i++) { if (siblings[i] == item) { return rightMostVisibleDescendent(previous); } previous = siblings[i]; } return null; } /* (non-Javadoc) Method declared on StructuredViewer. */ protected Object[] getRawChildren(Object parentElementOrTreePath) { Object parent; TreePath path; if (parentElementOrTreePath instanceof TreePath) { path = (TreePath) parentElementOrTreePath; parent = path.getLastSegment(); } else { parent = parentElementOrTreePath; path = null; } if (parent != null) { if (equals(parent, getRoot())) { return super.getRawChildren(parent); } IContentProvider cp = getContentProvider(); if (cp instanceof ITreePathContentProvider) { ITreePathContentProvider tpcp = (ITreePathContentProvider) cp; if (path == null) { // A path was not provided so try and find one Widget w = findItem(parent); if (w instanceof Item) { Item item = (Item) w; path = getTreePathFromItem(item); } if (path == null) { path = new TreePath(new Object[] { parent }); } } Object[] result = tpcp.getChildren(path); if (result != null) { return result; } } else if (cp instanceof ITreeContentProvider) { ITreeContentProvider tcp = (ITreeContentProvider) cp; Object[] result = tcp.getChildren(parent); if (result != null) { return result; } } } return new Object[0]; } /** * Returns all selected items for the given SWT control. * * @param control * the control * @return the list of selected items */ protected abstract Item[] getSelection(Control control); /* (non-Javadoc) Method declared on StructuredViewer. */ protected List getSelectionFromWidget() { Widget[] items = getSelection(getControl()); ArrayList list = new ArrayList(items.length); for (int i = 0; i < items.length; i++) { Widget item = items[i]; Object e = item.getData(); if (e != null) { list.add(e); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -