jivetreenode.java

来自「开源项目openfire的完整源程序」· Java 代码 · 共 239 行

JAVA
239
字号
/**
 * $Revision: $
 * $Date: $
 *
 * Copyright (C) 2006 Jive Software. All rights reserved.
 *
 * This software is published under the terms of the GNU Lesser Public License (LGPL),
 * a copy of which is included in this distribution.
 */

package org.jivesoftware.spark.component;

import org.jivesoftware.resource.SparkRes;

import javax.swing.Icon;
import javax.swing.tree.DefaultMutableTreeNode;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

/**
 * <code>JiveTreeNode</code> class is a better implementation than using the
 * DefaultMutableTree node. This allows better searching of children/parents as well
 * as handling of icons and drag and drop events.
 *
 * @author Derek DeMoro
 */

public class JiveTreeNode extends DefaultMutableTreeNode implements Transferable {
    private Icon closedImage = null;
    private Icon openImage = null;

    /**
     * Default Drag and Drop to use for Node detection.
     */
    public static final DataFlavor[] DATA_FLAVORS = {new DataFlavor(JiveTreeNode.class, "JiveTreeNodeFlavor")};
    private Object associatedObject;

    /*
    * Create node with closedImage.
    * @param userObject  Name to display
    * @param allowsChildren Specify if node allows children
    * @param img Specify closedImage to use.
    */
    public JiveTreeNode(TreeFolder folder) {
        super(folder.getDisplayName(), true);
        closedImage = SparkRes.getImageIcon(SparkRes.FOLDER_CLOSED);
        openImage = SparkRes.getImageIcon(SparkRes.FOLDER);
        associatedObject = folder;
    }

    /**
     * Create parent node.
     *
     * @param name           the name of the node.
     * @param allowsChildren true if the node allows children.
     */
    public JiveTreeNode(String name, boolean allowsChildren) {
        super(name, allowsChildren);
        if (allowsChildren) {
            closedImage = SparkRes.getImageIcon(SparkRes.FOLDER_CLOSED);
            openImage = SparkRes.getImageIcon(SparkRes.FOLDER);
        }
    }

    /**
     * Creates a new JiveTreeNode.
     *
     * @param o              the object to use.
     * @param allowsChildren true if it allows children.
     */
    public JiveTreeNode(Object o, boolean allowsChildren) {
        super(o, allowsChildren);
    }

    /**
     * Creates a new JiveTreeNode from a TreeItem.
     *
     * @param item the <code>TreeItem</code>
     */
    public JiveTreeNode(TreeItem item) {
        super(item.getDisplayName(), false);
        associatedObject = item;
    }

    /**
     * Creates a new JiveTreeNode from a TreeFolder.
     *
     * @param folder the <code>TreeFolder</code>.
     * @param img    the image to use in the node.
     */
    public JiveTreeNode(TreeFolder folder, Icon img) {
        this(folder);
        closedImage = img;
    }

    /**
     * Createa new JiveTreeNode from a TreeItem and Image.
     *
     * @param item the <code>TreeItem</code> to use.
     * @param img  the image to use in the node.
     */
    public JiveTreeNode(TreeItem item, Icon img) {
        this(item);
        closedImage = img;
    }

    /**
     * Creates a new JiveTreeNode.
     *
     * @param userobject the object to use in the node. Note: By default, the node
     *                   will not allow children.
     */
    public JiveTreeNode(String userobject) {
        super(userobject);
    }

    /**
     * Creates a new JiveTreeNode.
     *
     * @param userObject    the userObject to use.
     * @param allowChildren true if it allows children.
     * @param icon          the image to use in the node.
     */
    public JiveTreeNode(String userObject, boolean allowChildren, Icon icon) {
        super(userObject, allowChildren);
        closedImage = icon;
        openImage = icon;
    }

    /**
     * Returns the default image used.
     *
     * @return the default image used.
     */
    public Icon getIcon() {
        return closedImage;
    }

    /**
     * Return the icon that is displayed when the node is expanded.
     *
     * @return the open icon.
     */
    public Icon getOpenIcon() {
        return openImage;
    }

    /**
     * Returns the icon that is displayed when the node is collapsed.
     *
     * @return the closed icon.
     */
    public Icon getClosedIcon() {
        return closedImage;
    }

    /**
     * Sets the default icon.
     *
     * @param icon the icon.
     */
    public void setIcon(Icon icon) {
        closedImage = icon;
    }

    /**
     * Returns the associated object used. The associated object is used to store associated data objects
     * along with the node.
     *
     * @return the object.
     */
    public Object getAssociatedObject() {
        return associatedObject;
    }

    /**
     * Returns the associated object.
     *
     * @param o the associated object.
     */
    public void setAssociatedObject(Object o) {
        this.associatedObject = o;
    }

    /**
     * Returns true if a parent with the specified name is found.
     *
     * @param parentName the name of the parent.
     * @return true if parent found.
     */
    public final boolean hasParent(String parentName) {
        JiveTreeNode parent = (JiveTreeNode)getParent();
        while (true) {
            if (parent.getAssociatedObject() == null) {
                break;
            }
            final TreeFolder folder = (TreeFolder)parent.getAssociatedObject();
            if (folder.getDisplayName().equals(parentName)) {
                return true;
            }
            parent = (JiveTreeNode)parent.getParent();
        }
        return false;
    }


    /**
     * Transferable implementation
     */
    public DataFlavor[] getTransferDataFlavors() {
        return DATA_FLAVORS;
    }

    public boolean isDataFlavorSupported(DataFlavor flavor) {
        if (flavor == DATA_FLAVORS[0]) {
            return true;
        }

        return false;
    }

    public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException {
        if (this.isDataFlavorSupported(flavor)) {
            return this;
        }

        throw new UnsupportedFlavorException(flavor);
    }


}



⌨️ 快捷键说明

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