package com.vci.client.ui.swing;
|
|
import java.awt.Component;
|
|
import javax.swing.DefaultListCellRenderer;
|
import javax.swing.JComboBox;
|
import javax.swing.JLabel;
|
import javax.swing.JList;
|
import javax.swing.JTree;
|
import javax.swing.UIManager;
|
import javax.swing.plaf.ComboBoxUI;
|
import javax.swing.plaf.basic.ComboPopup;
|
import javax.swing.plaf.metal.MetalComboBoxUI;
|
import javax.swing.tree.TreeCellRenderer;
|
import javax.swing.tree.TreeNode;
|
import javax.swing.tree.TreePath;
|
|
import com.sun.java.swing.plaf.motif.MotifComboBoxUI;
|
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI;
|
|
public class JTreeComboBox extends JComboBox{
|
|
/**
|
*
|
*/
|
private static final long serialVersionUID = -2564967923898399676L;
|
|
private JTree tree;
|
|
public JTreeComboBox(){
|
this(new JTree());
|
}
|
|
public JTreeComboBox(JTree tree){
|
this.setTree(tree);
|
}
|
|
public void setTree(JTree tree){
|
this.tree = tree;
|
if(tree != null){
|
this.setSelectedItem(tree.getSelectionPath());
|
this.setRenderer(new JTreeComboBoxRenderer());
|
}
|
this.updateUI();
|
}
|
|
public JTree getTree(){
|
return tree;
|
}
|
|
public void setSelectedItem(Object o){
|
tree.setSelectionPath((TreePath)o);
|
getModel().setSelectedItem(o);
|
}
|
|
public void updateUI(){
|
ComboBoxUI cui = (ComboBoxUI)UIManager.getUI(this);
|
if(cui instanceof MetalComboBoxUI){
|
cui = new MetalJTreeComboBoxUI();
|
} else if(cui instanceof MotifComboBoxUI){
|
cui = new MotifJTreeComboBoxUI();
|
} else {
|
cui = new WindowsJTreeComboBoxUI();
|
}
|
setUI(cui);
|
}
|
|
class MetalJTreeComboBoxUI extends MetalComboBoxUI{
|
protected ComboPopup createPopup(){
|
return new TreePopup(comboBox);
|
}
|
}
|
|
class WindowsJTreeComboBoxUI extends WindowsComboBoxUI{
|
protected ComboPopup createPopup(){
|
return new TreePopup(comboBox);
|
}
|
}
|
|
class MotifJTreeComboBoxUI extends MotifComboBoxUI{
|
protected ComboPopup createPopup(){
|
return new TreePopup(comboBox);
|
}
|
}
|
|
class JTreeComboBoxRenderer extends DefaultListCellRenderer{
|
public Component getListCellRendererComponent(JList list, Object value,
|
int index, boolean isSelected, boolean cellHasFocus){
|
if(value != null){
|
TreePath path = (TreePath)value;
|
TreeNode node = (TreeNode)path.getLastPathComponent();
|
value = node;
|
TreeCellRenderer r = tree.getCellRenderer();
|
JLabel lb = (JLabel)r.getTreeCellRendererComponent(
|
tree, value, isSelected, false, node.isLeaf(), index,
|
cellHasFocus);
|
return lb;
|
}
|
return super.getListCellRendererComponent(list, value, index,
|
isSelected, cellHasFocus);
|
}
|
}
|
}
|