Package DisplayProject.controls

Source Code of DisplayProject.controls.TreeFieldUI

/*
Copyright (c) 2003-2009 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package DisplayProject.controls;

import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.TreePath;

import DisplayProject.DisplayNode;

/**
* Create a UI to control the tree. Forte allowed the "+" next to the tree
* to indicate that the tree could be expanded when the isFolder attribute
* was set, and we need to reproduce this. Much of this code is copied from
* the superclass.
*
* CraigM: 10/04/2008 - Moved from factory class.
*
* @author Tim
*/
class TreeFieldUI extends BasicTreeUI {
    protected Icon expandedIcon;
    protected Icon collapsedIcon;

    /**
     * Default constructor
     */
    public TreeFieldUI() {
    }
    /**
     * Static factory method to Create the related UI Component
     *
     * @param c
     * @return The created ComponentUI
     */
    public static ComponentUI createUI(JComponent c) {
        return new TreeFieldUI();
    }

    /* (non-Javadoc)
     * @see javax.swing.plaf.basic.BasicTreeUI#paintExpandControl(java.awt.Graphics, java.awt.Rectangle, java.awt.Insets, java.awt.Rectangle, javax.swing.tree.TreePath, int, boolean, boolean, boolean)
     */
    @Override
    protected void paintExpandControl(Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path,
            int row, boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf) {

      // override this method to get the same behaviour that forte had for its trees

        Object value = path.getLastPathComponent();
        if (value instanceof DisplayNode) {
            DisplayNode dn = (DisplayNode)value;

            if (dn.isFolder()) {
                int middleXOfKnob = bounds.x - (getRightChildIndent() - 1);
                int middleYOfKnob = bounds.y + (bounds.height / 2);

                Icon icon = null;
                if (dn.isOpened() && dn.getChildCount() > 0) {
                    icon = getExpandedIcon();
                }
                else {
                    icon = getCollapsedIcon();
                }
                if (icon != null) {
                        drawCentered(tree, g, icon, middleXOfKnob, middleYOfKnob );
                }
            }

        }
        else {
            super.paintExpandControl(g, clipBounds, insets, bounds, path, row, isExpanded, hasBeenExpanded, isLeaf);
        }
    }
   
    /* (non-Javadoc)
     * @see javax.swing.plaf.basic.BasicTreeUI#isLocationInExpandControl(javax.swing.tree.TreePath, int, int)
     */
    @Override
    protected boolean isLocationInExpandControl(TreePath path,
                        int mouseX, int mouseY) {
        /**
         * Returns true if <code>mouseX</code> and <code>mouseY</code> fall
         * in the area of row that is used to expand/collapse the node and
         * the node at <code>row</code> does not represent a leaf.
         */
     
        if (path == null) {
            return false;
        }

        Object value = path.getLastPathComponent();
        if (value instanceof DisplayNode) {
            DisplayNode dn = (DisplayNode)value;
            if (dn.isFolder()) {
                int                     boxWidth;
                Insets                  i = tree.getInsets();

                if(getExpandedIcon() != null)
                    boxWidth = getExpandedIcon().getIconWidth();
                else
                    boxWidth = 8;

                int boxLeftX = getRowX(tree.getRowForPath(path),
                            path.getPathCount() - 1) - getRightChildIndent() -
                            boxWidth / 2 + i.left;

                int boxRightX = boxLeftX + boxWidth;

                return mouseX >= boxLeftX && mouseX <= boxRightX;
            }
            else {
                return false;
            }
        }
        else {
            return super.isLocationInExpandControl(path, mouseX, mouseY);
        }
    }
}
TOP

Related Classes of DisplayProject.controls.TreeFieldUI

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.