1 package net.sf.openrocket.gui.components;
3 import java.awt.BasicStroke;
5 import java.awt.Component;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
9 import javax.swing.Icon;
10 import javax.swing.JTree;
11 import javax.swing.tree.TreeModel;
12 import javax.swing.tree.TreeNode;
13 import javax.swing.tree.TreePath;
15 public class BasicTree extends JTree {
23 public BasicTree(TreeNode node) {
29 private void setDefaultOptions() {
30 this.setToggleClickCount(0);
32 javax.swing.plaf.basic.BasicTreeUI plainUI = new javax.swing.plaf.basic.BasicTreeUI();
34 plainUI.setExpandedIcon(TreeIcon.MINUS);
35 plainUI.setCollapsedIcon(TreeIcon.PLUS);
36 plainUI.setLeftChildIndent(15);
39 this.setBackground(Color.WHITE);
40 this.setShowsRootHandles(false);
45 * Expand the entire tree structure. All nodes will be visible after the call.
47 public void expandTree() {
48 for (int i = 0; i < getRowCount(); i++)
56 public void treeDidChange() {
57 super.treeDidChange();
59 * Expand the childless nodes to prevent leaf nodes from looking expandable.
61 expandChildlessNodes();
65 * Expand all nodes in the tree that are visible and have no children. This can be used
66 * to avoid the situation where a non-leaf node is marked as being expandable, but when
67 * expanding it it has no children.
69 private void expandChildlessNodes() {
70 TreeModel model = this.getModel();
74 Object root = model.getRoot();
75 expandChildlessNodes(model, new TreePath(root));
78 private void expandChildlessNodes(TreeModel model, TreePath path) {
79 Object object = path.getLastPathComponent();
80 if (this.isVisible(path)) {
81 int count = model.getChildCount(object);
83 this.expandPath(path);
85 for (int i = 0; i < count; i++) {
86 expandChildlessNodes(model, path.pathByAddingChild(model.getChild(object, i)));
94 * Plain-looking tree expand/collapse icons.
96 private static class TreeIcon implements Icon {
97 public static final Icon PLUS = new TreeIcon(true);
98 public static final Icon MINUS = new TreeIcon(false);
102 private final static int width = 9;
103 private final static int height = 9;
104 private final static BasicStroke stroke = new BasicStroke(2);
105 private boolean plus;
107 private TreeIcon(boolean plus) {
112 public void paintIcon(Component c, Graphics g, int x, int y) {
113 Graphics2D g2 = (Graphics2D) g.create();
116 g2.setColor(Color.WHITE);
117 g2.fillRect(x, y, width, height);
120 g2.setColor(Color.DARK_GRAY);
121 g2.drawRect(x, y, width, height);
124 g2.setStroke(stroke);
125 g2.drawLine(x + 3, y + (height + 1) / 2, x + width - 2, y + (height + 1) / 2);
129 g2.drawLine(x + (width + 1) / 2, y + 3, x + (width + 1) / 2, y + height - 2);
136 public int getIconWidth() {
141 public int getIconHeight() {