2 * CheckTreeManager.java
4 package net.sf.openrocket.gui.print.components;
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;
16 * This class manages mouse clicks within the JTree, handling selection/deselections within the JCheckBox of each cell in the tree.
18 public class CheckTreeManager extends MouseAdapter implements TreeSelectionListener {
20 /** The selection model. */
21 private CheckTreeSelectionModel selectionModel;
22 /** The actual JTree instance. */
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;
28 * Construct a check box tree manager.
30 * @param theTree the actual tree being managed
32 public CheckTreeManager (RocketPrintTree 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);
40 for (int x = 0; x < tree.getRowCount(); x++) {
41 tree.getSelectionModel().setSelectionPath(tree.getPathForRow(x));
45 public void addTreeSelectionListener (TreeSelectionListener tsl) {
46 selectionModel.addTreeSelectionListener(tsl);
50 * Called when the mouse clicks within the tree.
52 * @param me the event that triggered this
55 public void mouseClicked (MouseEvent me) {
56 TreePath path = tree.getPathForLocation(me.getX(), me.getY());
60 if (me.getX() > tree.getPathBounds(path).x + hotspot) {
64 boolean selected = selectionModel.isPathSelected(path, true);
65 selectionModel.removeTreeSelectionListener(this);
69 selectionModel.removeSelectionPath(path);
72 selectionModel.addSelectionPath(path);
76 selectionModel.addTreeSelectionListener(this);
82 * Get the selection model being used by this manager.
84 * @return the selection model
86 public CheckTreeSelectionModel getSelectionModel () {
87 return selectionModel;
91 * Notify the tree that it changed.
96 public void valueChanged (TreeSelectionEvent e) {