1 7 8 package org.jdesktop.swing.binding; 9 10 import org.jdesktop.swing.data.DataModel; 11 import org.jdesktop.swing.data.EnumeratedMetaData; 12 import org.jdesktop.swing.data.MetaData; 13 14 import java.awt.event.ActionEvent ; 15 import java.awt.event.ActionListener ; 16 import java.beans.PropertyChangeEvent ; 17 import java.beans.PropertyChangeListener ; 18 19 import javax.swing.ComboBoxModel ; 20 import javax.swing.DefaultComboBoxModel ; 21 import javax.swing.JComponent ; 22 import javax.swing.JComboBox ; 23 import javax.swing.MutableComboBoxModel ; 24 25 31 public class ComboBoxBinding extends AbstractBinding { 32 private JComboBox comboBox; 33 37 public ComboBoxBinding(JComboBox combobox, 38 DataModel dataModel, String fieldName) { 39 super(combobox, dataModel, fieldName, Binding.AUTO_VALIDATE_NONE); 40 } 41 42 public JComponent getComponent() { 43 return comboBox; 44 } 45 46 protected void setComponent(JComponent component) { 47 comboBox = (JComboBox ) component; 48 configureDropDown(); 49 comboBox.addActionListener(new ActionListener () { 50 public void actionPerformed(ActionEvent e) { 51 if (!pulling) { 52 setModified(true); 53 } 54 } 55 }); 56 } 57 58 59 private void configureDropDown() { 60 if (metaData instanceof EnumeratedMetaData) { 61 updateComboBoxModel(((EnumeratedMetaData) metaData).getEnumeration()); 62 } 63 } 64 65 protected void updateComboBoxModel(Object [] items) { 66 DefaultComboBoxModel model = new DefaultComboBoxModel (items); 67 model.setSelectedItem(getComponentValue()); 68 comboBox.setModel(model); 70 setValidState(UNVALIDATED); 71 } 72 73 protected void installMetaDataListener() { 74 PropertyChangeListener l = new PropertyChangeListener () { 75 76 public void propertyChange(PropertyChangeEvent evt) { 77 if ("enumeration".equals(evt.getPropertyName())) { 78 updateComboBoxModel((Object []) evt.getNewValue()); 79 } 80 81 } 82 83 }; 84 metaData.addPropertyChangeListener(l); 85 } 86 87 protected Object getComponentValue(){ 88 return comboBox.getSelectedItem(); 89 } 90 91 protected void setComponentValue(Object value) { 92 comboBox.getModel().setSelectedItem(value); 93 } 94 95 } 96 | Popular Tags |