01: package net.sf.thingamablog.gui.app;
02:
03: import java.awt.BorderLayout;
04: import java.awt.Component;
05:
06: import javax.swing.JPanel;
07: import javax.swing.JTree;
08: import javax.swing.tree.TreeCellRenderer;
09: import javax.swing.tree.TreePath;
10:
11: public class CheckTreeCellRenderer extends JPanel implements
12: TreeCellRenderer {
13: /**
14: *
15: */
16: private static final long serialVersionUID = 1L;
17: private CheckTreeSelectionModel selectionModel;
18: private TreeCellRenderer delegate;
19: private TristateCheckBox checkBox = new TristateCheckBox();
20:
21: public CheckTreeCellRenderer(TreeCellRenderer delegate,
22: CheckTreeSelectionModel selectionModel) {
23: this .delegate = delegate;
24: this .selectionModel = selectionModel;
25: setLayout(new BorderLayout());
26: setOpaque(false);
27: checkBox.setOpaque(false);
28: }
29:
30: public Component getTreeCellRendererComponent(JTree tree,
31: Object value, boolean selected, boolean expanded,
32: boolean leaf, int row, boolean hasFocus) {
33: Component renderer = delegate.getTreeCellRendererComponent(
34: tree, value, selected, expanded, leaf, row, hasFocus);
35:
36: TreePath path = tree.getPathForRow(row);
37: if (path != null) {
38: if (selectionModel.isPathSelected(path, true))
39: checkBox.setState(TristateCheckBox.SELECTED);
40: else
41: checkBox
42: .setState(selectionModel
43: .isPartiallySelected(path) ? TristateCheckBox.DONT_CARE
44: : TristateCheckBox.NOT_SELECTED);
45: }
46: removeAll();
47: add(checkBox, BorderLayout.WEST);
48: add(renderer, BorderLayout.CENTER);
49: return this;
50: }
51: }
|