updates for 0.9.3
[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.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.setToggleClickCount(0);
22                 
23                 javax.swing.plaf.basic.BasicTreeUI ui = new javax.swing.plaf.basic.BasicTreeUI();
24                 this.setUI(ui);
25
26                 ui.setExpandedIcon(TreeIcon.MINUS);
27                 ui.setCollapsedIcon(TreeIcon.PLUS);
28                 
29                 ui.setLeftChildIndent(15);
30                 
31
32                 setBackground(Color.WHITE);
33                 setShowsRootHandles(false);
34                 
35                 setCellRenderer(new ComponentTreeRenderer());
36                 
37                 // Expand whole tree by default
38                 expandTree();
39         }
40         
41         
42         public void expandTree() {
43                 for (int i=0; i<getRowCount(); i++)
44                         expandRow(i);
45                 
46         }
47         
48         private static class TreeIcon implements Icon{
49                 public static final Icon PLUS = new TreeIcon(true);
50                 public static final Icon MINUS = new TreeIcon(false);
51                 
52                 // Implementation:
53                 
54             private final static int width = 9;
55             private final static int height = 9;
56             private final static BasicStroke stroke = new BasicStroke(2);
57             private boolean plus;
58             
59             private TreeIcon(boolean plus) {
60                 this.plus = plus;
61             }
62             
63             public void paintIcon(Component c, Graphics g, int x, int y) {
64                 Graphics2D g2 = (Graphics2D)g.create();
65                 
66                 g2.setColor(Color.WHITE);
67                 g2.fillRect(x,y,width,height);
68                 
69                 g2.setColor(Color.DARK_GRAY);
70                 g2.drawRect(x,y,width,height);
71                 
72                 g2.setStroke(stroke);
73                 g2.drawLine(x+3, y+(height+1)/2, x+width-2, y+(height+1)/2);
74                 if (plus)
75                         g2.drawLine(x+(width+1)/2, y+3, x+(width+1)/2, y+height-2);
76                 
77                 g2.dispose();
78             }
79             
80             public int getIconWidth() {
81                 return width;
82             }
83             
84             public int getIconHeight() {
85                 return height;
86             }
87         }
88         
89 }
90
91