create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / gui / print / components / CheckTreeManager.java
1 /*
2  * CheckTreeManager.java
3  */
4 package net.sf.openrocket.gui.print.components;
5
6 import javax.swing.JCheckBox;
7 import javax.swing.JTree;
8 import javax.swing.event.TreeSelectionEvent;
9 import javax.swing.event.TreeSelectionListener;
10 import javax.swing.tree.DefaultTreeCellRenderer;
11 import javax.swing.tree.TreePath;
12 import java.awt.event.MouseAdapter;
13 import java.awt.event.MouseEvent;
14
15 /**
16  * This class manages mouse clicks within the JTree, handling selection/deselections within the JCheckBox of each cell in the tree.
17  */
18 public class CheckTreeManager extends MouseAdapter implements TreeSelectionListener {
19     
20     /** The selection model. */
21     private CheckTreeSelectionModel selectionModel;
22     /** The actual JTree instance. */
23     private JTree tree;
24     /** The number of pixels of width of the check box.  Clicking anywhere within the box will trigger actions. */
25     int hotspot = new JCheckBox().getPreferredSize().width;
26
27     /**
28      * Construct a check box tree manager.
29      * 
30      * @param theTree  the actual tree being managed
31      */
32     public CheckTreeManager (RocketPrintTree theTree) {
33         tree = theTree;
34         selectionModel = new CheckTreeSelectionModel(tree.getModel());
35         theTree.setCheckBoxSelectionModel(selectionModel);
36         tree.setCellRenderer(new CheckTreeCellRenderer((DefaultTreeCellRenderer)tree.getCellRenderer(), selectionModel));
37         tree.addMouseListener(this);
38         selectionModel.addTreeSelectionListener(this);
39         
40         for (int x = 0; x < tree.getRowCount(); x++) {
41             tree.getSelectionModel().setSelectionPath(tree.getPathForRow(x));
42         }
43     }
44
45     public void addTreeSelectionListener (TreeSelectionListener tsl) {
46         selectionModel.addTreeSelectionListener(tsl);
47     }
48     
49     /**
50      * Called when the mouse clicks within the tree.
51      * 
52      * @param me  the event that triggered this
53      */
54     @Override
55     public void mouseClicked (MouseEvent me) {
56         TreePath path = tree.getPathForLocation(me.getX(), me.getY());
57         if (path == null) {
58             return;
59         }
60         if (me.getX() > tree.getPathBounds(path).x + hotspot) {
61             return;
62         }
63
64         boolean selected = selectionModel.isPathSelected(path, true);
65         selectionModel.removeTreeSelectionListener(this);
66
67         try {
68             if (selected) {
69                 selectionModel.removeSelectionPath(path);
70             }
71             else {
72                 selectionModel.addSelectionPath(path);
73             }
74         }
75         finally {
76             selectionModel.addTreeSelectionListener(this);
77             tree.treeDidChange();
78         }
79     }
80
81     /**
82      * Get the selection model being used by this manager.
83      * 
84      * @return the selection model
85      */
86     public CheckTreeSelectionModel getSelectionModel () {
87         return selectionModel;
88     }
89
90     /**
91      * Notify the tree that it changed.
92      * 
93      * @param e unused 
94      */
95     @Override
96     public void valueChanged (TreeSelectionEvent e) {
97         tree.treeDidChange();
98     }
99 }