Initial commit
[debian/openrocket] / src / net / sf / openrocket / gui / main / ComponentTree.java
1 package net.sf.openrocket.gui.main;
2
3 import java.awt.BasicStroke;
4 import java.awt.Color;
5 import java.awt.Component;
6 import java.awt.Graphics;
7 import java.awt.Graphics2D;
8
9 import javax.swing.Icon;
10 import javax.swing.JTree;
11
12 import net.sf.openrocket.rocketcomponent.*;
13
14
15 public class ComponentTree extends JTree {
16         private static final long serialVersionUID = 1L;
17
18         public ComponentTree(RocketComponent root) {
19                 super();
20                 this.setModel(new ComponentTreeModel(root,this));
21 //              this.setModel(new BareComponentTreeModel(root,this));
22                 setToggleClickCount(0);
23                 
24                 javax.swing.plaf.basic.BasicTreeUI ui = new javax.swing.plaf.basic.BasicTreeUI();
25                 this.setUI(ui);
26
27                 ui.setExpandedIcon(TreeIcon.MINUS);
28                 ui.setCollapsedIcon(TreeIcon.PLUS);
29                 
30                 ui.setLeftChildIndent(15);
31                 
32
33                 setBackground(Color.WHITE);
34                 setShowsRootHandles(false);
35                 
36                 setCellRenderer(new ComponentTreeRenderer());
37                 
38                 // Expand whole tree by default
39                 expandTree();
40         }
41         
42         
43         public void expandTree() {
44                 for (int i=0; i<getRowCount(); i++)
45                         expandRow(i);
46                 
47         }
48         
49         private static class TreeIcon implements Icon{
50                 public static final Icon PLUS = new TreeIcon(true);
51                 public static final Icon MINUS = new TreeIcon(false);
52                 
53                 // Implementation:
54                 
55             private final static int width = 9;
56             private final static int height = 9;
57             private final static BasicStroke stroke = new BasicStroke(2);
58             private boolean plus;
59             
60             private TreeIcon(boolean plus) {
61                 this.plus = plus;
62             }
63             
64             public void paintIcon(Component c, Graphics g, int x, int y) {
65                 Graphics2D g2 = (Graphics2D)g.create();
66                 
67                 g2.setColor(Color.WHITE);
68                 g2.fillRect(x,y,width,height);
69                 
70                 g2.setColor(Color.DARK_GRAY);
71                 g2.drawRect(x,y,width,height);
72                 
73                 g2.setStroke(stroke);
74                 g2.drawLine(x+3, y+(height+1)/2, x+width-2, y+(height+1)/2);
75                 if (plus)
76                         g2.drawLine(x+(width+1)/2, y+3, x+(width+1)/2, y+height-2);
77                 
78                 g2.dispose();
79             }
80             
81             public int getIconWidth() {
82                 return width;
83             }
84             
85             public int getIconHeight() {
86                 return height;
87             }
88         }
89         
90 }
91
92