1 package net.sf.openrocket.gui.main.componenttree;
3 import java.awt.BasicStroke;
5 import java.awt.Component;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
9 import javax.swing.DropMode;
10 import javax.swing.Icon;
11 import javax.swing.JTree;
13 import net.sf.openrocket.document.OpenRocketDocument;
16 public class ComponentTree extends JTree {
17 private static final long serialVersionUID = 1L;
19 public ComponentTree(OpenRocketDocument document) {
21 this.setModel(new ComponentTreeModel(document.getRocket(), this));
22 this.setToggleClickCount(0);
24 javax.swing.plaf.basic.BasicTreeUI ui = new javax.swing.plaf.basic.BasicTreeUI();
27 ui.setExpandedIcon(TreeIcon.MINUS);
28 ui.setCollapsedIcon(TreeIcon.PLUS);
30 ui.setLeftChildIndent(15);
33 setBackground(Color.WHITE);
34 setShowsRootHandles(false);
36 setCellRenderer(new ComponentTreeRenderer());
38 this.setDragEnabled(true);
39 this.setDropMode(DropMode.INSERT);
40 this.setTransferHandler(new ComponentTreeTransferHandler(document));
42 // Expand whole tree by default
46 public void expandTree() {
47 for (int i = 0; i < getRowCount(); i++)
52 private static class TreeIcon implements Icon {
53 public static final Icon PLUS = new TreeIcon(true);
54 public static final Icon MINUS = new TreeIcon(false);
58 private final static int width = 9;
59 private final static int height = 9;
60 private final static BasicStroke stroke = new BasicStroke(2);
63 private TreeIcon(boolean plus) {
67 public void paintIcon(Component c, Graphics g, int x, int y) {
68 Graphics2D g2 = (Graphics2D) g.create();
70 g2.setColor(Color.WHITE);
71 g2.fillRect(x, y, width, height);
73 g2.setColor(Color.DARK_GRAY);
74 g2.drawRect(x, y, width, height);
77 g2.drawLine(x + 3, y + (height + 1) / 2, x + width - 2, y + (height + 1) / 2);
79 g2.drawLine(x + (width + 1) / 2, y + 3, x + (width + 1) / 2, y + height - 2);
84 public int getIconWidth() {
88 public int getIconHeight() {