1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package coras.client.ui;
22
23 import java.awt.CardLayout;
24 import java.awt.event.ItemEvent;
25 import java.awt.event.ItemListener;
26
27 import javax.swing.ComboBoxModel;
28 import javax.swing.JComboBox;
29 import javax.swing.JPanel;
30 import javax.swing.JTextField;
31 /***
32 * @author fvr
33 *
34 * TODO To change the template for this generated type comment go to
35 * Window - Preferences - Java - Code Style - Code Templates
36 */
37 public class CorasComboBox extends JPanel {
38
39 public static final String SELECTED_ITEM = "selectedItem";
40
41 private JComboBox comboBox = null;
42 private JTextField textField = null;
43 private boolean editable = true;
44
45 private ItemListener itemListener = new ItemListener() {
46 public void itemStateChanged(ItemEvent e) {
47 if (e.getStateChange() == ItemEvent.SELECTED) {
48 firePropertyChange(SELECTED_ITEM, null, getComboBox().getSelectedItem());
49 }
50 }
51 };
52
53 /***
54 * This is the default constructor
55 */
56 public CorasComboBox() {
57 super();
58 initialize();
59 getComboBox().addItemListener(itemListener);
60 }
61
62 public synchronized void setModel(ComboBoxModel newModel) {
63 getComboBox().setModel(newModel);
64 Object item = getComboBox().getSelectedItem();
65 textField.setText(item != null ? item.toString() : "");
66 }
67
68 /***
69 * @return
70 */
71 public Object getSelectedItem() {
72 return getComboBox().getSelectedItem();
73 }
74
75 public synchronized void setSelectedItem(Object item) {
76 getComboBox().setSelectedItem(item);
77 item = getComboBox().getSelectedItem();
78 textField.setText(item != null ? item.toString() : "");
79 }
80
81 public Object[] getSelectedObjects() {
82 return getComboBox().getSelectedObjects();
83 }
84
85 public boolean isEditable() {
86 return editable;
87 }
88
89 public synchronized void setEditable(boolean editable) {
90 if (this.editable == editable) {
91 return;
92 }
93 this.editable = editable;
94 CardLayout layout = (CardLayout) getLayout();
95 if (editable) {
96 layout.show(this, getComboBox().getName());
97 } else {
98 Object item = getComboBox().getSelectedItem();
99 textField.setText(item != null ? item.toString() : "");
100 layout.show(this, getTextField().getName());
101 }
102 repaint();
103 }
104
105 public boolean isComboBoxEditable() {
106 return getComboBox().isEditable();
107 }
108
109 public void setComboBoxEditable(boolean editable) {
110 getComboBox().setEditable(editable);
111 }
112
113 /***
114 * This method initializes this
115 *
116 * @return void
117 */
118 private void initialize() {
119 this.setLayout(new CardLayout());
120 this.setSize(300,200);
121 this.add(getComboBox(), getComboBox().getName());
122 this.add(getTextField(), getTextField().getName());
123 }
124 /***
125 * This method initializes jComboBox
126 *
127 * @return javax.swing.JComboBox
128 */
129 private JComboBox getComboBox() {
130 if (comboBox == null) {
131 comboBox = new JComboBox();
132 comboBox.setName("comboBox");
133 }
134 return comboBox;
135 }
136 /***
137 * This method initializes jTextField
138 *
139 * @return javax.swing.JTextField
140 */
141 private JTextField getTextField() {
142 if (textField == null) {
143 textField = new JTextField();
144 textField.setName("textField");
145 textField.setEditable(false);
146
147 }
148 return textField;
149 }
150
151 }