1 package org.catacomb.druid.swing; 2 3 import javax.swing.JPanel; 4 import java.awt.Component; 5 import javax.swing.tree.*; 6 import java.awt.BorderLayout; 7 8 import javax.swing.JTree; 9 10 11 public class CheckTreeRenderer extends JPanel implements TreeCellRenderer { 12 private static final long serialVersionUID = 1L; 13 14 private CheckTreeSelectionModel selectionModel; 15 private TreeCellRenderer delegate; 16 private TristateCheckBox checkBox = new TristateCheckBox(); 17 18 19 public CheckTreeRenderer(TreeCellRenderer delegate, CheckTreeSelectionModel selectionModel) { 20 this.delegate = delegate; 21 this.selectionModel = selectionModel; 22 setLayout(new BorderLayout()); 23 setOpaque(false); 24 checkBox.setOpaque(false); 25 } 26 27 28 public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, 29 boolean expanded, boolean leaf, int row, boolean hasFocus) { 30 Component renderer = delegate.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); 31 32 TreePath path = tree.getPathForRow(row); 33 if (path != null) { 34 if (selectionModel.isPathSelected(path, true)) 35 checkBox.setBooleanState(Boolean.TRUE); 36 else 37 checkBox.setBooleanState(selectionModel.isPartiallySelected(path) ? null : Boolean.FALSE); 38 } 39 removeAll(); 40 add(checkBox, BorderLayout.WEST); 41 add(renderer, BorderLayout.CENTER); 42 return this; 43 } 44 }