📄 abstracttreeviewer.java
字号:
return list; } /* * Overridden in AbstractTreeViewer to fix bug 108102 (code copied from StructuredViewer * to avoid introducing new API) * (non-Javadoc) * @see org.eclipse.jface.viewers.StructuredViewer#handleDoubleSelect(org.eclipse.swt.events.SelectionEvent) */ protected void handleDoubleSelect(SelectionEvent event) { // handle case where an earlier selection listener disposed the control. Control control = getControl(); if (control != null && !control.isDisposed()) { // If the double-clicked element can be obtained from the event, use it // otherwise get it from the control. Some controls like List do // not have the notion of item. // For details, see bug 90161 [Navigator] DefaultSelecting folders shouldn't always expand first one ISelection selection; if (event.item != null && event.item.getData() != null) { // changes to fix bug 108102 follow TreePath treePath = getTreePathFromItem((Item) event.item); selection = new TreeSelection(treePath); // end of changes } else { selection = getSelection(); updateSelection(selection); } fireDoubleClick(new DoubleClickEvent(this, selection)); } } /** * Handles a tree collapse event from the SWT widget. * * @param event * the SWT tree event */ protected void handleTreeCollapse(TreeEvent event) { if (event.item.getData() != null) { fireTreeCollapsed(new TreeExpansionEvent(this, event.item.getData())); } } /** * Handles a tree expand event from the SWT widget. * * @param event * the SWT tree event */ protected void handleTreeExpand(TreeEvent event) { if (!(getContentProvider() instanceof ILazyTreeContentProvider)) { createChildren(event.item); } if (event.item.getData() != null) { fireTreeExpanded(new TreeExpansionEvent(this, event.item.getData())); } } /* (non-Javadoc) Method declared on Viewer. */ protected void hookControl(Control control) { super.hookControl(control); addTreeListener(control, new TreeListener() { public void treeExpanded(TreeEvent event) { handleTreeExpand(event); } public void treeCollapsed(TreeEvent event) { handleTreeCollapse(event); } }); } /* * (non-Javadoc) Method declared on StructuredViewer. Builds the initial * tree and handles the automatic expand feature. */ protected void inputChanged(Object input, Object oldInput) { preservingSelection(new Runnable() { public void run() { Control tree = getControl(); boolean useRedraw = true; // (size > REDRAW_THRESHOLD) || (table.getItemCount() > // REDRAW_THRESHOLD); if (useRedraw) { tree.setRedraw(false); } removeAll(tree); tree.setData(getRoot()); createChildren(tree); internalExpandToLevel(tree, expandToLevel); if (useRedraw) { tree.setRedraw(true); } } }); } /** * Recursively collapses the subtree rooted at the given widget to the * given level. * <p> * </p> * Note that the default implementation of this method does not call <code>setRedraw</code>. * * @param widget * the widget * @param level * non-negative level, or <code>ALL_LEVELS</code> to collapse * all levels of the tree */ protected void internalCollapseToLevel(Widget widget, int level) { if (level == ALL_LEVELS || level > 0) { if (widget instanceof Item) { setExpanded((Item) widget, false); } if (level == ALL_LEVELS || level > 1) { Item[] children = getChildren(widget); if (children != null) { int nextLevel = (level == ALL_LEVELS ? ALL_LEVELS : level - 1); for (int i = 0; i < children.length; i++) { internalCollapseToLevel(children[i], nextLevel); } } } } } /** * Recursively collects all expanded items from the given widget. * * @param result * a list (element type: <code>Item</code>) into which to * collect the elements * @param widget * the widget */ private void internalCollectExpandedItems(List result, Widget widget) { Item[] items = getChildren(widget); for (int i = 0; i < items.length; i++) { Item item = items[i]; if (getExpanded(item)) { result.add(item); } internalCollectExpandedItems(result, item); } } /** * Tries to create a path of tree items for the given element or tree path. * This method recursively walks up towards the root of the tree and in the * case of an element (rather than a tree path) assumes that * <code>getParent</code> returns the correct parent of an element. * * @param elementOrPath * the element * @param expand * <code>true</code> if all nodes on the path should be * expanded, and <code>false</code> otherwise * @return Widget */ protected Widget internalExpand(Object elementOrPath, boolean expand) { if (elementOrPath == null) { return null; } Widget w = internalGetWidgetToSelect(elementOrPath); if (w == null) { if (equals(elementOrPath, getRoot())) { // stop at root return null; } // my parent has to create me Object parent = getParentElement(elementOrPath); if (parent != null) { Widget pw = internalExpand(parent, false); if (pw != null) { // let my parent create me createChildren(pw); Object element = internalToElement(elementOrPath); w = internalFindChild(pw, element); if (expand && pw instanceof Item) { // expand parent items top-down Item item = (Item) pw; LinkedList toExpandList = new LinkedList(); while (item != null && !getExpanded(item)) { toExpandList.addFirst(item); item = getParentItem(item); } for(Iterator it = toExpandList.iterator(); it.hasNext();) { Item toExpand = (Item) it.next(); setExpanded(toExpand, true); } } } } } return w; } /** * If the argument is a tree path, returns its last segment, otherwise * return the argument * @param elementOrPath an element or a tree path * @return the element, or the last segment of the tree path */ private Object internalToElement(Object elementOrPath) { if (elementOrPath instanceof TreePath) { return ((TreePath)elementOrPath).getLastSegment(); } return elementOrPath; } /** * This method takes a tree path or an element. If the argument is not a * tree path, returns the parent of the given element or <code>null</code> * if the parent is not known. If the argument is a tree path with more than * one segment, returns its parent tree path, otherwise returns * <code>null</code>. * * @param elementOrTreePath * @return the parent element, or parent path, or <code>null</code> * * @since 3.2 */ protected Object getParentElement(Object elementOrTreePath) { if(elementOrTreePath instanceof TreePath) { TreePath treePath = (TreePath)elementOrTreePath; if(treePath.getSegmentCount() <= 1) { return null; } return (treePath).getParentPath(); } IContentProvider cp = getContentProvider(); if (cp instanceof ITreePathContentProvider) { ITreePathContentProvider tpcp = (ITreePathContentProvider) cp; TreePath[] paths = tpcp.getParents(elementOrTreePath); if (paths.length > 0) { if (paths[0].getSegmentCount() == 0) { return getInput(); } return paths[0].getLastSegment(); } } if (cp instanceof ITreeContentProvider) { ITreeContentProvider tcp = (ITreeContentProvider) cp; return tcp.getParent(elementOrTreePath); } return null; } /** * Returns the widget to be selected for the given element or tree path. * * @param elementOrTreePath the element or tree path to select * @return the widget to be selected, or <code>null</code> if not found * * @since 3.1 */ protected Widget internalGetWidgetToSelect(Object elementOrTreePath) { if(elementOrTreePath instanceof TreePath) { TreePath treePath = (TreePath) elementOrTreePath; if(treePath.getSegmentCount()==0) { return null; } Widget[] candidates = findItems(treePath.getLastSegment()); for (int i = 0; i < candidates.length; i++) { Widget candidate = candidates[i]; if(!(candidate instanceof Item)) { continue; } if(treePath.equals(getTreePathFromItem((Item) candidate), getComparer())) { return candidate; } } return null; } return findItem(elementOrTreePath); } /** * Recursively expands the subtree rooted at the given widget to the given * level. * <p> * </p> * Note that the default implementation of this method does not call <code>setRedraw</code>. * * @param widget * the widget * @param level * non-negative level, or <code>ALL_LEVELS</code> to collapse * all levels of the tree */ protected void internalExpandToLevel(Widget widget, int level) { if (level == ALL_LEVELS || level > 0) { createChildren(widget); if (widget instanceof Item) { setExpanded((Item) widget, true); } if (level == ALL_LEVELS || level > 1) { Item[] children = getChildren(widget); if (children != null) { int newLevel = (level == ALL_LEVELS ? ALL_LEVELS : level - 1); for (int i = 0; i < children.length; i++) { internalExpandToLevel(children[i], newLevel); } } } } } /** * Non-recursively tries to find the given element as a child of the given * parent (item or tree). * * @param parent * the parent item * @param element * the element * @return Widget */ private Widget internalFindChild(Widget parent, Object element) { Item[] items = getChildren(parent); for (int i = 0; i < items.length; i++) { Item item = items[i]; Object data = item.getData(); if (data != null && equals(data, element)) { return item; } } return null; } /** * Recursively tries to find the given element. * * @param parent * the parent item * @param element * the element * @return Widget */ private Widget internalFindItem(Item parent, Object element) { // compare with node Object data = parent.getData(); if (data != null) { if (equals(data, element)) { return parent; } } // recurse over children Item[] items = getChildren(parent); for (int i = 0; i < items.length; i++) { Item item = items[i]; Widget o = internalFindItem(item, element); if (o != null) { return o; } } return null; } /* (non-Javadoc) Method declared on StructuredViewer. */ protected void internalRefresh(Object element) { internalRefresh(element, true); } /* (non-Javadoc) Method declared on StructuredViewer. */ protected void internalRefresh(Object element, boolean updateLabels) { // If element is null, do a full refresh. if (element == null) { internalRefresh(getControl(), getRoot(), true, updateLabels); return; } Widget[] items = findItems(element); if (items.length != 0) { for (int i = 0; i < items.length; i++) { // pick up structure changes too internalRefresh(items[i], element, true, updateLabels); } } } /** * Refreshes the tree starting at the given widget. * <p> * EXPERIMENTAL. Not to be used except by JDT. * This method was added to support JDT's explorations
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -