📄 checkboxtreeviewer.java
字号:
/** * Gathers the grayed states of the given widget and its * descendents, following a pre-order traversal of the tree. * * @param result a writeable list of elements (element type: <code>Object</code>) * @param widget the widget */ private void internalCollectGrayed(List result, Widget widget) { Item[] items = getChildren(widget); for (int i = 0; i < items.length; i++) { Item item = items[i]; if (item instanceof TreeItem && ((TreeItem) item).getGrayed()) { Object data = item.getData(); if (data != null) { result.add(data); } } internalCollectGrayed(result, item); } } /** * Sets the checked state of all items to correspond to the given set of checked elements. * * @param checkedElements the set (element type: <code>Object</code>) of elements which are checked * @param widget the widget */ private void internalSetChecked(CustomHashtable checkedElements, Widget widget) { Item[] items = getChildren(widget); for (int i = 0; i < items.length; i++) { TreeItem item = (TreeItem) items[i]; Object data = item.getData(); if (data != null) { boolean checked = checkedElements.containsKey(data); if (checked != item.getChecked()) { item.setChecked(checked); } } internalSetChecked(checkedElements, item); } } /** * Sets the grayed state of all items to correspond to the given set of grayed elements. * * @param grayedElements the set (element type: <code>Object</code>) of elements which are grayed * @param widget the widget */ private void internalSetGrayed(CustomHashtable grayedElements, Widget widget) { Item[] items = getChildren(widget); for (int i = 0; i < items.length; i++) { TreeItem item = (TreeItem) items[i]; Object data = item.getData(); if (data != null) { boolean grayed = grayedElements.containsKey(data); if (grayed != item.getGrayed()) { item.setGrayed(grayed); } } internalSetGrayed(grayedElements, item); } } /* (non-Javadoc) * Method declared on Viewer. */ protected void preservingSelection(Runnable updateCode) { int n = getItemCount(getControl()); CustomHashtable checkedNodes = newHashtable(n * 2 + 1); CustomHashtable grayedNodes = newHashtable(n * 2 + 1); gatherState(checkedNodes, grayedNodes, getControl()); super.preservingSelection(updateCode); applyState(checkedNodes, grayedNodes, getControl()); } /* (non-Javadoc) * Method declared on ICheckable. */ public void removeCheckStateListener(ICheckStateListener listener) { checkStateListeners.remove(listener); } /* (non-Javadoc) * Method declared on ICheckable. */ public boolean setChecked(Object element, boolean state) { Assert.isNotNull(element); Widget widget = internalExpand(element, false); if (widget instanceof TreeItem) { ((TreeItem) widget).setChecked(state); return true; } return false; } /** * Sets the checked state for the children of the given item. * * @param item the item * @param state <code>true</code> if the item should be checked, * and <code>false</code> if it should be unchecked */ private void setCheckedChildren(Item item, boolean state) { createChildren(item); Item[] items = getChildren(item); if (items != null) { for (int i = 0; i < items.length; i++) { Item it = items[i]; if (it.getData() != null && (it instanceof TreeItem)) { TreeItem treeItem = (TreeItem) it; treeItem.setChecked(state); setCheckedChildren(treeItem, state); } } } } /** * Sets which elements are checked in this viewer's tree. * The given list contains the elements that are to be checked; * all other elements are to be unchecked. * <p> * This method is typically used when restoring the interesting * state of a viewer captured by an earlier call to <code>getCheckedElements</code>. * </p> * * @param elements the array of checked elements * @see #getCheckedElements */ public void setCheckedElements(Object[] elements) { assertElementsNotNull(elements); CustomHashtable checkedElements = newHashtable(elements.length * 2 + 1); for (int i = 0; i < elements.length; ++i) { Object element = elements[i]; // Ensure item exists for element internalExpand(element, false); checkedElements.put(element, element); } Control tree = getControl(); tree.setRedraw(false); internalSetChecked(checkedElements, tree); tree.setRedraw(true); } /** * Sets the grayed state for the given element in this viewer. * * @param element the element * @param state <code>true</code> if the item should be grayed, * and <code>false</code> if it should be ungrayed * @return <code>true</code> if the gray state could be set, * and <code>false</code> otherwise */ public boolean setGrayed(Object element, boolean state) { Assert.isNotNull(element); Widget widget = internalExpand(element, false); if (widget instanceof TreeItem) { ((TreeItem) widget).setGrayed(state); return true; } return false; } /** * Check and gray the selection rather than calling both * setGrayed and setChecked as an optimization. * @param element the item being checked * @param state a boolean indicating selection or deselection * @return boolean indicating success or failure. */ public boolean setGrayChecked(Object element, boolean state) { Assert.isNotNull(element); Widget widget = internalExpand(element, false); if (widget instanceof TreeItem) { TreeItem item = (TreeItem) widget; item.setChecked(state); item.setGrayed(state); return true; } return false; } /** * Sets which elements are grayed in this viewer's tree. * The given list contains the elements that are to be grayed; * all other elements are to be ungrayed. * <p> * This method is typically used when restoring the interesting * state of a viewer captured by an earlier call to <code>getGrayedElements</code>. * </p> * * @param elements the array of grayed elements * * @see #getGrayedElements */ public void setGrayedElements(Object[] elements) { assertElementsNotNull(elements); CustomHashtable grayedElements = newHashtable(elements.length * 2 + 1); for (int i = 0; i < elements.length; ++i) { Object element = elements[i]; // Ensure item exists for element internalExpand(element, false); grayedElements.put(element, element); } Control tree = getControl(); tree.setRedraw(false); internalSetGrayed(grayedElements, tree); tree.setRedraw(true); } /** * Sets the grayed state for the given element and its parents * in this viewer. * * @param element the element * @param state <code>true</code> if the item should be grayed, * and <code>false</code> if it should be ungrayed * @return <code>true</code> if the element is visible and the gray * state could be set, and <code>false</code> otherwise * @see #setGrayed */ public boolean setParentsGrayed(Object element, boolean state) { Assert.isNotNull(element); Widget widget = internalExpand(element, false); if (widget instanceof TreeItem) { TreeItem item = (TreeItem) widget; item.setGrayed(state); item = item.getParentItem(); while (item != null) { item.setGrayed(state); item = item.getParentItem(); } return true; } return false; } /** * Sets the checked state for the given element and its visible * children in this viewer. * Assumes that the element has been expanded before. To enforce * that the item is expanded, call <code>expandToLevel</code> * for the element. * * @param element the element * @param state <code>true</code> if the item should be checked, * and <code>false</code> if it should be unchecked * @return <code>true</code> if the checked state could be set, * and <code>false</code> otherwise */ public boolean setSubtreeChecked(Object element, boolean state) { Widget widget = internalExpand(element, false); if (widget instanceof TreeItem) { TreeItem item = (TreeItem) widget; item.setChecked(state); setCheckedChildren(item, state); return true; } return false; } /** * Sets to the given value the checked state for all elements in this viewer. * * @param state <code>true</code> if the element should be checked, * and <code>false</code> if it should be unchecked * * @since 3.2 */ public void setAllChecked(boolean state) { setAllChecked(state, getTree().getItems()); } /** * Set the checked state of items and thier children to state. * @param state * @param items */ private void setAllChecked(boolean state, TreeItem[] items) { for (int i = 0; i < items.length; i++) { items[i].setChecked(state); TreeItem[] children = items[i].getItems(); setAllChecked(state, children); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -