public class TagCheckRenderer extends JCheckBox implements TreeCellRenderer{
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus){
String stringValue = tree.convertValueToText(value, selected,
expanded, leaf, row, hasFocus);
TagCheckNode node = (TagCheckNode)value;
if(!node.isEditable()){
JLabel label = new JLabel(stringValue);
label.setComponentOrientation(tree.getComponentOrientation());
if(hasFocus||node.isSelected()){ label.setBackground(UIManager.getColor("Tree.selectionBackground"));
}
else{ label.setBackground(tree.getBackground());
}
return label;
}
this.setText(stringValue);
if(!leaf){ this.setEnabled(false);
this.setSelected(false);
TagCheckNode child = null;
//只要有一个叶子子节点Tag被选中,该节点就为被选中的状态
for(Enumeration enum = node.children(); enum.hasMoreElements() ;){ child = (TagCheckNode)enum.nextElement();
if(child.isLeaf()&&child.isSelected()){ this.setSelected(true);
break;
}
}
}
else{ this.setSelected(node.isSelected());
this.setEnabled(tree.isEnabled());
}
if(hasFocus){ this.setBackground(UIManager.getColor("Tree.selectionBackground"));
}
else{ this.setBackground(tree.getBackground());
}
this.setComponentOrientation(tree.getComponentOrientation());
return this;
}
}
public Component getTreeCellRendererComponent(JTree tree,
Object value,
boolean selected,
boolean expanded,
boolean leaf,
int row,
boolean hasFocus){
String stringValue = tree.convertValueToText(value, selected,
expanded, leaf, row, hasFocus);
TagCheckNode node = (TagCheckNode)value;
if(!node.isEditable()){
JLabel label = new JLabel(stringValue);
label.setComponentOrientation(tree.getComponentOrientation());
if(hasFocus||node.isSelected()){ label.setBackground(UIManager.getColor("Tree.selectionBackground"));
}
else{ label.setBackground(tree.getBackground());
}
return label;
}
this.setText(stringValue);
if(!leaf){ this.setEnabled(false);
this.setSelected(false);
TagCheckNode child = null;
//只要有一个叶子子节点Tag被选中,该节点就为被选中的状态
for(Enumeration enum = node.children(); enum.hasMoreElements() ;){ child = (TagCheckNode)enum.nextElement();
if(child.isLeaf()&&child.isSelected()){ this.setSelected(true);
break;
}
}
}
else{ this.setSelected(node.isSelected());
this.setEnabled(tree.isEnabled());
}
if(hasFocus){ this.setBackground(UIManager.getColor("Tree.selectionBackground"));
}
else{ this.setBackground(tree.getBackground());
}
this.setComponentOrientation(tree.getComponentOrientation());
return this;
}
}
private boolean isSelected;
private boolean isCloned = false;
private boolean isEditable = true;
public TagCheckNode() { this(null);
}
public TagCheckNode(Object userObject) { this(userObject, true, false);
}
public TagCheckNode(Object userObject, boolean allowsChildren
, boolean isSelected) { super(userObject, allowsChildren);
this.isSelected = isSelected;
}
public void setSelected(boolean isSelected) { this.isSelected = isSelected;
/*
//children is selected together
if (children != null) {
Enumeration enum = children.elements();
while (enum.hasMoreElements()) {
TagCheckNode node = (TagCheckNode)enum.nextElement();
node.setSelected(isSelected);
}
}
*/
}
public boolean isSelected() { return isSelected;
}
public Object clone(){
TagCheckNode nodeObj = (TagCheckNode) super.clone();
if(this.children!=null){ nodeObj.children = new Vector(this.children.size());
for(int i = 0 ; i < this.children.size(); i++){ nodeObj.add((TagCheckNode)((TagCheckNode)this.children.get(i)).clone());
}
}
nodeObj.isCloned = true;
nodeObj.isSelected = false;
nodeObj.isEditable = this.isEditable;
return nodeObj;
}
public boolean isCloned(){ return this.isCloned;
}
public boolean isEditable(){ return this.isEditable;
}
public void setEditable(boolean is){ this.isEditable = is;
}