KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > binding > SpinnerBinding


1 /*
2  * $Id: SpinnerBinding.java,v 1.1.1.1 2004/06/16 01:43:39 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

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 JavaDoc;
14 import javax.swing.JSpinner JavaDoc;
15 import javax.swing.SpinnerModel JavaDoc;
16 import javax.swing.event.ChangeEvent JavaDoc;
17 import javax.swing.event.ChangeListener JavaDoc;
18
19 /**
20  * Class which binds a component that supports setting a value within
21  * a sequence of values (JSpinner) to a field in a data model.
22  * Although this binding is most commonly used for spinners, it may
23  * be used with any component that defines a SpinnerModel to represent
24  * its current value.
25
26  * @author Amy Fowler
27  * @version 1.0
28  */

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