1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.richclient.form.binding.swing;
17  
18  import java.text.NumberFormat;
19  
20  import javax.swing.JFormattedTextField;
21  
22  import org.springframework.binding.form.FieldMetadata;
23  import org.springframework.binding.support.TestBean;
24  
25  public class FormattedTextFieldBindingTests extends BindingAbstractTests {
26  
27      private static final Long initialValue = new Long(99);
28  
29      private JFormattedTextField ftc;
30  
31      private FormattedTextFieldBinding b;
32      
33      protected TestBean createTestBean() {
34          TestBean testBean = super.createTestBean();
35          testBean.setNumberProperty(initialValue);
36          return testBean;
37      }
38  
39      protected String setUpBinding() {   
40          b = new FormattedTextFieldBinding(new JFormattedTextField(NumberFormat.getInstance()), fm, "numberProperty", null);
41          ftc = (JFormattedTextField)b.getControl();
42          return "numberProperty";
43      }
44      
45      public void testInitialValue() {
46          assertEquals(initialValue, vm.getValue());
47          assertEquals(initialValue, ftc.getValue());
48      }
49  
50      public void testComponentTracksEnabledChanges() {
51          assertEquals(true, ftc.isEnabled());
52          fm.setEnabled(false);
53          assertEquals(false, ftc.isEnabled());
54          fm.setEnabled(true);
55          assertEquals(true, ftc.isEnabled());
56      }
57  
58      public void testComponentTracksReadOnlyChanges() {
59          FieldMetadata state = fm.getFieldMetadata("numberProperty");
60          assertEquals(true, ftc.isEditable());
61          state.setReadOnly(true);
62          assertEquals(false, ftc.isEditable());
63          state.setReadOnly(false);
64          assertEquals(true, ftc.isEditable());
65      }
66  
67      public void testComponentUpdatesValueModel() {
68          Long one = new Long(1);
69          Long two = new Long(2);
70          ftc.setValue(one);
71          assertTrue(vm.getValue() instanceof Long);
72          assertTrue(one.compareTo((Long)vm.getValue()) == 0);
73          ftc.setValue(null);
74          assertEquals(null, vm.getValue());
75          ftc.setValue(two);
76          assertEquals(two, vm.getValue());
77          ftc.setValue(null);
78          assertEquals(null, vm.getValue());
79      }
80  
81      public void testValueModelUpdatesComponent() {
82          Long one = new Long(1);
83          Long two = new Long(2);
84          vm.setValue(one);
85          assertEquals(one, ftc.getValue());
86          vm.setValue(null);
87          assertEquals(null, ftc.getValue());
88          vm.setValue(two);
89          assertEquals(two, ftc.getValue());
90          vm.setValue(null);
91          assertEquals(null, ftc.getValue());
92      }
93  }