1 7 8 package org.jdesktop.swing.binding; 9 10 import org.jdesktop.swing.data.DataModel; 11 import org.jdesktop.swing.data.MetaData; 12 13 import javax.swing.JComponent ; 14 import javax.swing.JSpinner ; 15 import javax.swing.SpinnerModel ; 16 import javax.swing.event.ChangeEvent ; 17 import javax.swing.event.ChangeListener ; 18 19 29 public class SpinnerBinding extends AbstractBinding { 30 private JComponent component; 31 private SpinnerModel spinnerModel; 32 33 public SpinnerBinding(JSpinner spinner, 34 DataModel dataModel, String fieldName) { 35 super(spinner, dataModel, fieldName, AbstractBinding.AUTO_VALIDATE); 36 initModel(spinner.getModel()); 37 } 38 39 public SpinnerBinding(JSpinner spinner, 40 DataModel dataModel, String fieldName, 41 int validationPolicy) { 42 super(spinner, dataModel, fieldName, validationPolicy); 43 initModel(spinner.getModel()); 44 } 45 46 public SpinnerBinding(JComponent component, SpinnerModel spinnerModel, 47 DataModel dataModel, String fieldName, 48 int validationPolicy) { 49 super(component, dataModel, fieldName, validationPolicy); 50 initModel(spinnerModel); 51 } 52 53 public JComponent getComponent() { 54 return component; 55 } 56 57 protected void setComponent(JComponent component) { 58 this.component = component; 59 } 60 61 protected Object getComponentValue(){ 62 return spinnerModel.getValue(); 63 } 64 65 protected void setComponentValue(Object value) { 66 if (value != null) { 67 spinnerModel.setValue(value); 68 } 69 } 70 71 private void initModel(SpinnerModel spinnerModel) { 72 this.spinnerModel = spinnerModel; 73 spinnerModel.addChangeListener(new ChangeListener () { 74 public void stateChanged(ChangeEvent e) { 75 if (!pulling) { 76 setModified(true); 77 } 78 } 79 }); 80 } 81 82 } | Popular Tags |