ludc
2024-09-14 36c2449aec5b51e5ed4e5c6841154b746060e09a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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);
        }
    }
}