e680c8554b97590c1fb68e3d6dced20488c9d347
[debian/openrocket] / src / net / sf / openrocket / gui / main / componenttree / ComponentTree.java
1 package net.sf.openrocket.gui.main.componenttree;
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.DropMode;
10 import javax.swing.Icon;
11 import javax.swing.JTree;
12
13 import net.sf.openrocket.document.OpenRocketDocument;
14
15
16 public class ComponentTree extends JTree {
17         private static final long serialVersionUID = 1L;
18         
19         public ComponentTree(OpenRocketDocument document) {
20                 super();
21                 this.setModel(new ComponentTreeModel(document.getRocket(), this));
22                 this.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                 this.setDragEnabled(true);
39                 this.setDropMode(DropMode.INSERT);
40                 this.setTransferHandler(new ComponentTreeTransferHandler(document));
41                 
42                 // Expand whole tree by default
43                 expandTree();
44         }
45         
46         public void expandTree() {
47                 for (int i = 0; i < getRowCount(); i++)
48                         expandRow(i);
49                 
50         }
51         
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);
55                 
56                 // Implementation:
57                 
58                 private final static int width = 9;
59                 private final static int height = 9;
60                 private final static BasicStroke stroke = new BasicStroke(2);
61                 private boolean plus;
62                 
63                 private TreeIcon(boolean plus) {
64                         this.plus = plus;
65                 }
66                 
67                 public void paintIcon(Component c, Graphics g, int x, int y) {
68                         Graphics2D g2 = (Graphics2D) g.create();
69                         
70                         g2.setColor(Color.WHITE);
71                         g2.fillRect(x, y, width, height);
72                         
73                         g2.setColor(Color.DARK_GRAY);
74                         g2.drawRect(x, y, width, height);
75                         
76                         g2.setStroke(stroke);
77                         g2.drawLine(x + 3, y + (height + 1) / 2, x + width - 2, y + (height + 1) / 2);
78                         if (plus)
79                                 g2.drawLine(x + (width + 1) / 2, y + 3, x + (width + 1) / 2, y + height - 2);
80                         
81                         g2.dispose();
82                 }
83                 
84                 public int getIconWidth() {
85                         return width;
86                 }
87                 
88                 public int getIconHeight() {
89                         return height;
90                 }
91         }
92         
93 }