⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 checkboxtreeviewer.java

📁 jfa2ce 源码帮助开发人员更好的理解运用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: *     IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.viewers;import java.util.ArrayList;import java.util.List;import org.eclipse.core.runtime.ListenerList;import org.eclipse.jface.util.Assert;import org.eclipse.jface.util.SafeRunnable;import org.eclipse.swt.SWT;import org.eclipse.swt.events.SelectionEvent;import org.eclipse.swt.widgets.Composite;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.Item;import org.eclipse.swt.widgets.Tree;import org.eclipse.swt.widgets.TreeItem;import org.eclipse.swt.widgets.Widget;/** * A concrete tree-structured viewer based on an SWT <code>Tree</code> * control with checkboxes on each node. * <p> * This class is not intended to be subclassed outside the viewer framework.  * It is designed to be instantiated with a pre-existing SWT tree control and configured * with a domain-specific content provider, label provider, element filter (optional), * and element sorter (optional). * </p> */public class CheckboxTreeViewer extends TreeViewer implements ICheckable {    /**     * List of check state listeners (element type: <code>ICheckStateListener</code>).     */    private ListenerList checkStateListeners = new ListenerList();    /**     * Last item clicked on, or <code>null</code> if none.     */    private TreeItem lastClickedItem = null;    /**     * Creates a tree viewer on a newly-created tree control under the given parent.     * The tree control is created using the SWT style bits: <code>CHECK</code> and <code>BORDER</code>.     * The viewer has no input, no content provider, a default label provider,      * no sorter, and no filters.     *     * @param parent the parent control     */    public CheckboxTreeViewer(Composite parent) {        this(parent, SWT.BORDER);    }    /**     * Creates a tree viewer on a newly-created tree control under the given parent.     * The tree control is created using the given SWT style bits, plus the <code>CHECK</code> style bit.     * The viewer has no input, no content provider, a default label provider,      * no sorter, and no filters.     *     * @param parent the parent control     * @param style the SWT style bits     */    public CheckboxTreeViewer(Composite parent, int style) {        this(new Tree(parent, SWT.CHECK | style));    }    /**     * Creates a tree viewer on the given tree control.     * The <code>SWT.CHECK</code> style bit must be set on the given tree control.     * The viewer has no input, no content provider, a default label provider,      * no sorter, and no filters.     *     * @param tree the tree control     */    public CheckboxTreeViewer(Tree tree) {        super(tree);    }    /* (non-Javadoc)     * Method declared on ICheckable.     */    public void addCheckStateListener(ICheckStateListener listener) {        checkStateListeners.add(listener);    }    /**     * Applies the checked and grayed states of the given widget and its     * descendents.     *     * @param checked a set of elements (element type: <code>Object</code>)      * @param grayed a set of elements (element type: <code>Object</code>)      * @param widget the widget     */    private void applyState(CustomHashtable checked, CustomHashtable grayed,            Widget widget) {        Item[] items = getChildren(widget);        for (int i = 0; i < items.length; i++) {            Item item = items[i];            if (item instanceof TreeItem) {                Object data = item.getData();                if (data != null) {                    TreeItem ti = (TreeItem) item;                    ti.setChecked(checked.containsKey(data));                    ti.setGrayed(grayed.containsKey(data));                }            }            applyState(checked, grayed, item);        }    }    /**     * Notifies any check state listeners that the check state of an element has changed.     * Only listeners registered at the time this method is called are notified.     *     * @param event a check state changed event     *     * @see ICheckStateListener#checkStateChanged     */    protected void fireCheckStateChanged(final CheckStateChangedEvent event) {        Object[] array = checkStateListeners.getListeners();        for (int i = 0; i < array.length; i++) {            final ICheckStateListener l = (ICheckStateListener) array[i];            SafeRunnable.run(new SafeRunnable() {                public void run() {                    l.checkStateChanged(event);                }            });        }    }    /**     * Gathers the checked and grayed states of the given widget and its     * descendents.     *     * @param checked a writeable set of elements (element type: <code>Object</code>)      * @param grayed a writeable set of elements (element type: <code>Object</code>)      * @param widget the widget     */    private void gatherState(CustomHashtable checked, CustomHashtable grayed,            Widget widget) {        Item[] items = getChildren(widget);        for (int i = 0; i < items.length; i++) {            Item item = items[i];            if (item instanceof TreeItem) {                Object data = item.getData();                if (data != null) {                    TreeItem ti = (TreeItem) item;                    if (ti.getChecked()) {						checked.put(data, data);					}                    if (ti.getGrayed()) {						grayed.put(data, data);					}                }            }            gatherState(checked, grayed, item);        }    }    /* (non-Javadoc)     * Method declared on ICheckable.     */    public boolean getChecked(Object element) {        Widget widget = findItem(element);        if (widget instanceof TreeItem) {			return ((TreeItem) widget).getChecked();		}        return false;    }    /**     * Returns a list of checked elements in this viewer's tree,      * including currently hidden ones that are marked as     * checked but are under a collapsed ancestor.     * <p>     * This method is typically used when preserving the interesting     * state of a viewer; <code>setCheckedElements</code> is used during the restore.     * </p>     *     * @return the array of checked elements     *     * @see #setCheckedElements     */    public Object[] getCheckedElements() {        ArrayList v = new ArrayList();        Control tree = getControl();        internalCollectChecked(v, tree);        return v.toArray();    }    /**     * Returns the grayed state of the given element.     *     * @param element the element     * @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 TreeItem) {            return ((TreeItem) widget).getGrayed();        }        return false;    }    /**     * Returns a list of grayed elements in this viewer's tree,      * including currently hidden ones that are marked as     * grayed but are under a collapsed ancestor.     * <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() {        List result = new ArrayList();        internalCollectGrayed(result, getControl());        return result.toArray();    }    /* (non-Javadoc)     * Method declared on StructuredViewer.     */    protected void handleDoubleSelect(SelectionEvent event) {        if (lastClickedItem != null) {            TreeItem item = lastClickedItem;            Object data = item.getData();            if (data != null) {                boolean state = item.getChecked();                setChecked(data, !state);                fireCheckStateChanged(new CheckStateChangedEvent(this, data,                        !state));            }            lastClickedItem = null;        } else {			super.handleDoubleSelect(event);		}    }    /* (non-Javadoc)     * Method declared on StructuredViewer.     */    protected void handleSelect(SelectionEvent event) {        lastClickedItem = null;        if (event.detail == SWT.CHECK) {            TreeItem item = (TreeItem) event.item;            lastClickedItem = item;            super.handleSelect(event);            Object data = item.getData();            if (data != null) {                fireCheckStateChanged(new CheckStateChangedEvent(this, data,                        item.getChecked()));            }        } else {			super.handleSelect(event);		}    }    /**     * Gathers the checked 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 internalCollectChecked(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).getChecked()) {                Object data = item.getData();                if (data != null) {					result.add(data);				}            }            internalCollectChecked(result, item);        }    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -