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 javax.swing.JTextField;
19  
20  import org.springframework.binding.form.FieldMetadata;
21  
22  public class TextComponentBindingAbstractTests extends BindingAbstractTests {
23  
24      private JTextField tc;
25  
26      private TextComponentBinding b;
27  
28      protected String setUpBinding() {        
29          b = new TextComponentBinding(new JTextField(), fm, "simpleProperty");
30          tc = (JTextField)b.getControl();
31          return "simpleProperty";
32      }
33  
34      public void testComponentTracksEnabledChanges() {
35          assertEquals(true, tc.isEnabled());
36          fm.setEnabled(false);
37          assertEquals(false, tc.isEnabled());
38          fm.setEnabled(true);
39          assertEquals(true, tc.isEnabled());
40      }
41  
42      public void testComponentTracksReadOnlyChanges() {
43          FieldMetadata state = fm.getFieldMetadata("simpleProperty");
44          assertEquals(true, tc.isEditable());
45          state.setReadOnly(true);
46          assertEquals(false, tc.isEditable());
47          state.setReadOnly(false);
48          assertEquals(true, tc.isEditable());
49      }
50  
51      public void testComponentUpdatesValueModel() {
52          tc.setText("1");
53          assertEquals("1", vm.getValue());
54          tc.setText(null);
55          assertEquals("", vm.getValue());
56          tc.setText("2");
57          assertEquals("2", vm.getValue());
58          tc.setText("");
59          assertEquals("", vm.getValue());
60      }
61  
62      public void testValueModelUpdatesComponent() {
63          vm.setValue("1");
64          assertEquals("1", tc.getText());
65          vm.setValue(null);
66          assertEquals("", tc.getText());
67          vm.setValue("2");
68          assertEquals("2", tc.getText());
69          vm.setValue("");
70          assertEquals("", tc.getText());
71      }
72  }