📄 checkboxtableviewer.java
字号:
* @return <code>true</code> if the element is grayed, * and <code>false</code> if not grayed */ public boolean getGrayed(Object element) { Widget widget = findItem(element); if (widget instanceof TableItem) { return ((TableItem) widget).getGrayed(); } return false; } /** * Returns a list of elements corresponding to grayed nodes in this * viewer. * <p> * This method is typically used when preserving the interesting * state of a viewer; <code>setGrayedElements</code> is used during the restore. * </p> * * @return the array of grayed elements * @see #setGrayedElements */ public Object[] getGrayedElements() { TableItem[] children = getTable().getItems(); List v = new ArrayList(children.length); for (int i = 0; i < children.length; i++) { TableItem item = children[i]; if (item.getGrayed()) { v.add(item.getData()); } } return v.toArray(); } /* (non-Javadoc) * Method declared on StructuredViewer. */ public void handleSelect(SelectionEvent event) { if (event.detail == SWT.CHECK) { super.handleSelect(event); // this will change the current selection TableItem item = (TableItem) event.item; Object data = item.getData(); if (data != null) { fireCheckStateChanged(new CheckStateChangedEvent(this, data, item.getChecked())); } } else { super.handleSelect(event); } } /* (non-Javadoc) * Method declared on Viewer. */ protected void preservingSelection(Runnable updateCode) { TableItem[] children = getTable().getItems(); CustomHashtable checked = newHashtable(children.length * 2 + 1); CustomHashtable grayed = newHashtable(children.length * 2 + 1); for (int i = 0; i < children.length; i++) { TableItem item = children[i]; Object data = item.getData(); if (data != null) { if (item.getChecked()) { checked.put(data, data); } if (item.getGrayed()) { grayed.put(data, data); } } } super.preservingSelection(updateCode); children = getTable().getItems(); for (int i = 0; i < children.length; i++) { TableItem item = children[i]; Object data = item.getData(); if (data != null) { item.setChecked(checked.containsKey(data)); item.setGrayed(grayed.containsKey(data)); } } } /* (non-Javadoc) * Method declared on ICheckable. */ public void removeCheckStateListener(ICheckStateListener listener) { checkStateListeners.remove(listener); } /** * 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 */ public void setAllChecked(boolean state) { TableItem[] children = getTable().getItems(); for (int i = 0; i < children.length; i++) { TableItem item = children[i]; item.setChecked(state); } } /** * Sets to the given value the grayed state for all elements in this viewer. * * @param state <code>true</code> if the element should be grayed, * and <code>false</code> if it should be ungrayed */ public void setAllGrayed(boolean state) { TableItem[] children = getTable().getItems(); for (int i = 0; i < children.length; i++) { TableItem item = children[i]; item.setGrayed(state); } } /* (non-Javadoc) * Method declared on ICheckable. */ public boolean setChecked(Object element, boolean state) { Assert.isNotNull(element); Widget widget = findItem(element); if (widget instanceof TableItem) { ((TableItem) widget).setChecked(state); return true; } return false; } /** * Sets which nodes are checked in this viewer. * The given list contains the elements that are to be checked; * all other nodes 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 list of checked elements (element type: <code>Object</code>) * @see #getCheckedElements */ public void setCheckedElements(Object[] elements) { assertElementsNotNull(elements); CustomHashtable set = newHashtable(elements.length * 2 + 1); for (int i = 0; i < elements.length; ++i) { set.put(elements[i], elements[i]); } TableItem[] items = getTable().getItems(); for (int i = 0; i < items.length; ++i) { TableItem item = items[i]; Object element = item.getData(); if (element != null) { boolean check = set.containsKey(element); // only set if different, to avoid flicker if (item.getChecked() != check) { item.setChecked(check); } } } } /** * 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 element is visible and the gray * state could be set, and <code>false</code> otherwise */ public boolean setGrayed(Object element, boolean state) { Assert.isNotNull(element); Widget widget = findItem(element); if (widget instanceof TableItem) { ((TableItem) widget).setGrayed(state); return true; } return false; } /** * Sets which nodes are grayed in this viewer. * The given list contains the elements that are to be grayed; * all other nodes 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 set = newHashtable(elements.length * 2 + 1); for (int i = 0; i < elements.length; ++i) { set.put(elements[i], elements[i]); } TableItem[] items = getTable().getItems(); for (int i = 0; i < items.length; ++i) { TableItem item = items[i]; Object element = item.getData(); if (element != null) { boolean gray = set.containsKey(element); // only set if different, to avoid flicker if (item.getGrayed() != gray) { item.setGrayed(gray); } } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -