]> git.gag.com Git - debian/openrocket/blob - src/net/sf/openrocket/gui/main/componenttree/ComponentTree.java
version 1.1.3
[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 import javax.swing.ToolTipManager;
13
14 import net.sf.openrocket.document.OpenRocketDocument;
15
16
17 public class ComponentTree extends JTree {
18         private static final long serialVersionUID = 1L;
19         
20         public ComponentTree(OpenRocketDocument document) {
21                 super();
22                 this.setModel(new ComponentTreeModel(document.getRocket(), this));
23                 this.setToggleClickCount(0);
24                 
25                 javax.swing.plaf.basic.BasicTreeUI ui = new javax.swing.plaf.basic.BasicTreeUI();
26                 this.setUI(ui);
27                 
28                 ui.setExpandedIcon(TreeIcon.MINUS);
29                 ui.setCollapsedIcon(TreeIcon.PLUS);
30                 
31                 ui.setLeftChildIndent(15);
32                 
33
34                 setBackground(Color.WHITE);
35                 setShowsRootHandles(false);
36                 
37                 setCellRenderer(new ComponentTreeRenderer());
38                 
39                 this.setDragEnabled(true);
40                 this.setDropMode(DropMode.INSERT);
41                 this.setTransferHandler(new ComponentTreeTransferHandler(document));
42                 
43                 // Expand whole tree by default
44                 expandTree();
45                 
46                 // Enable tooltips for this component
47                 ToolTipManager.sharedInstance().registerComponent(this);
48                 
49         }
50         
51         
52         public void expandTree() {
53                 for (int i = 0; i < getRowCount(); i++)
54                         expandRow(i);
55                 
56         }
57         
58         private static class TreeIcon implements Icon {
59                 public static final Icon PLUS = new TreeIcon(true);
60                 public static final Icon MINUS = new TreeIcon(false);
61                 
62                 // Implementation:
63                 
64                 private final static int width = 9;
65                 private final static int height = 9;
66                 private final static BasicStroke stroke = new BasicStroke(2);
67                 private boolean plus;
68                 
69                 private TreeIcon(boolean plus) {
70                         this.plus = plus;
71                 }
72                 
73                 public void paintIcon(Component c, Graphics g, int x, int y) {
74                         Graphics2D g2 = (Graphics2D) g.create();
75                         
76                         g2.setColor(Color.WHITE);
77                         g2.fillRect(x, y, width, height);
78                         
79                         g2.setColor(Color.DARK_GRAY);
80                         g2.drawRect(x, y, width, height);
81                         
82                         g2.setStroke(stroke);
83                         g2.drawLine(x + 3, y + (height + 1) / 2, x + width - 2, y + (height + 1) / 2);
84                         if (plus)
85                                 g2.drawLine(x + (width + 1) / 2, y + 3, x + (width + 1) / 2, y + height - 2);
86                         
87                         g2.dispose();
88                 }
89                 
90                 public int getIconWidth() {
91                         return width;
92                 }
93                 
94                 public int getIconHeight() {
95                         return height;
96                 }
97         }
98         
99 }