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);
}
}
}