1 18 19 package org.apache.jorphan.gui; 20 21 import java.awt.Insets ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.ItemEvent ; 25 import java.awt.event.ItemListener ; 26 import java.util.ArrayList ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 30 import javax.swing.BorderFactory ; 31 import javax.swing.JButton ; 32 import javax.swing.JComboBox ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.event.ChangeEvent ; 36 import javax.swing.event.ChangeListener ; 37 38 public class JLabeledChoice extends JPanel implements JLabeledField 39 { 40 private JLabel mLabel = new JLabel (); 41 private JComboBox choiceList; 42 43 private ArrayList mChangeListeners = new ArrayList (3); 45 46 private JButton delete, add; 47 48 51 public JLabeledChoice() 52 { 53 super(); 54 choiceList = new JComboBox (); 55 init(); 56 } 57 58 public List getComponentList() 59 { 60 List comps = new LinkedList (); 61 comps.add(mLabel); 62 comps.add(choiceList); 63 return comps; 64 } 65 66 public JLabeledChoice(String pLabel, boolean editable) 67 { 68 super(); 69 choiceList = new JComboBox (); 70 mLabel.setText(pLabel); 71 choiceList.setEditable(editable); 72 init(); 73 } 74 75 public void setEditable(boolean editable) 76 { 77 choiceList.setEditable(false); } 79 80 public void addValue(String item) 81 { 82 choiceList.addItem(item); 83 } 84 85 public void setValues(String [] items) 86 { 87 choiceList.removeAllItems(); 88 for (int i = 0; i < items.length; i++) 89 { 90 choiceList.addItem(items[i]); 91 } 92 } 93 94 100 public JLabeledChoice(String pLabel, String [] items) 101 { 102 super(); 103 mLabel.setText(pLabel); 104 choiceList = new JComboBox (items); 105 choiceList.setEditable(false); 106 init(); 107 } 108 109 public JLabeledChoice(String pLabel, String [] items, boolean editable) 110 { 111 super(); 112 mLabel.setText(pLabel); 113 choiceList = new JComboBox (items); 114 choiceList.setEditable(editable); 115 init(); 116 } 117 118 121 private void init() 122 { 123 127 choiceList.setBorder(BorderFactory.createLoweredBevelBorder()); 128 choiceList.addItemListener(new ItemListener () 132 { 133 139 public void itemStateChanged(ItemEvent e) 140 { 141 if (e.getStateChange() == ItemEvent.SELECTED) 142 { 143 notifyChangeListeners(); 144 } 145 } 146 }); 147 148 this.add(mLabel); 150 this.add(choiceList); 151 if (choiceList.isEditable()) 152 { 153 add = new JButton ("Add"); 154 add.setMargin(new Insets (1, 1, 1, 1)); 155 add.addActionListener(new AddListener()); 156 this.add(add); 157 delete = new JButton ("Del"); 158 delete.setMargin(new Insets (1, 1, 1, 1)); 159 delete.addActionListener(new DeleteListener()); 160 this.add(delete); 161 } 162 163 } 164 165 170 public void setLabel(String pLabel) 171 { 172 mLabel.setText(pLabel); 173 } 174 175 180 public void setText(String pText) 181 { 182 choiceList.setSelectedItem(pText); 183 } 184 185 190 public String getText() 191 { 192 return (String ) choiceList.getSelectedItem(); 193 } 194 195 public Object [] getSelectedItems() 196 { 197 return choiceList.getSelectedObjects(); 198 } 199 200 public String [] getItems() 201 { 202 String [] items = new String [choiceList.getItemCount()]; 203 for (int i = 0; i < items.length; i++) 204 { 205 items[i] = (String ) choiceList.getItemAt(i); 206 } 207 return items; 208 } 209 210 215 public String getLabel() 216 { 217 return mLabel.getText(); 218 } 219 220 228 public void addChangeListener(ChangeListener pChangeListener) 229 { 230 mChangeListeners.add(pChangeListener); 231 } 232 233 238 public void removeChangeListener(ChangeListener pChangeListener) 239 { 240 mChangeListeners.remove(pChangeListener); 241 } 242 243 247 private void notifyChangeListeners() 248 { 249 ChangeEvent ce = new ChangeEvent (this); 250 for (int index = 0; index < mChangeListeners.size(); index++) 251 { 252 ((ChangeListener ) mChangeListeners.get(index)).stateChanged(ce); 253 } 254 } 255 256 private class AddListener implements ActionListener 257 { 258 259 public void actionPerformed(ActionEvent e) 260 { 261 Object item = choiceList.getSelectedItem(); 262 int index = choiceList.getSelectedIndex(); 263 if (!item.equals(choiceList.getItemAt(index))) 264 { 265 choiceList.addItem(item); 266 } 267 choiceList.setSelectedItem(item); 268 notifyChangeListeners(); 269 } 270 } 271 272 private class DeleteListener implements ActionListener 273 { 274 275 public void actionPerformed(ActionEvent e) 276 { 277 if (choiceList.getItemCount() > 1) 278 { 279 choiceList.removeItemAt(choiceList.getSelectedIndex()); 280 notifyChangeListeners(); 281 } 282 } 283 } 284 } | Popular Tags |