1   /*
2    * Copyright 2002-2004 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of 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,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.springframework.richclient.form.binding.swing;
17  
18  import java.util.Collections;
19  
20  import javax.swing.JComboBox;
21  import javax.swing.event.ListDataEvent;
22  
23  import org.springframework.binding.support.TestLabeledEnum;
24  
25  public class LabeledEnumComboBoxBindingAbstractTests extends BindingAbstractTests {
26  
27      private LabeledEnumComboBoxBinding cbb;
28  
29      private JComboBox cb;
30  
31      protected String setUpBinding() {
32          cbb = (LabeledEnumComboBoxBinding) new LabeledEnumComboBoxBinder().bind(fm, "enumProperty", Collections.EMPTY_MAP);
33          cb = (JComboBox) cbb.getControl();
34          return "enumProperty";
35      }
36  
37      public void testValueModelUpdatesComponent() {
38          TestListDataListener tldl = new TestListDataListener();
39          cb.getModel().addListDataListener(tldl);
40  
41          assertEquals(null, cb.getSelectedItem());
42          assertEquals(-1, cb.getSelectedIndex());
43          tldl.assertCalls(0);
44  
45          vm.setValue(TestLabeledEnum.ONE);
46          assertEquals(TestLabeledEnum.ONE, cb.getSelectedItem());
47          assertEquals(1, cb.getSelectedIndex());
48          tldl.assertEvent(1, ListDataEvent.CONTENTS_CHANGED, -1, -1);
49  
50          vm.setValue(TestLabeledEnum.TWO);
51          assertEquals(TestLabeledEnum.TWO, cb.getSelectedItem());
52          assertEquals(2, cb.getSelectedIndex());
53          tldl.assertEvent(2, ListDataEvent.CONTENTS_CHANGED, -1, -1);
54  
55          vm.setValue(null);
56          assertEquals(null, cb.getSelectedItem());
57          assertEquals(-1, cb.getSelectedIndex());
58          tldl.assertEvent(3, ListDataEvent.CONTENTS_CHANGED, -1, -1);
59  
60          vm.setValue(null);
61          tldl.assertCalls(3);
62      }
63  
64      public void testComponentUpdatesValueModel() {
65          cb.setSelectedIndex(1);
66          assertEquals(TestLabeledEnum.ONE, vm.getValue());
67  
68          cb.setSelectedItem(TestLabeledEnum.TWO);
69          assertEquals(TestLabeledEnum.TWO, vm.getValue());
70  
71          cb.setSelectedIndex(-1);
72          assertEquals(null, vm.getValue());
73      }
74  
75      public void testComponentTracksEnabledChanges() {
76          assertTrue(cb.isEnabled());
77  
78          fm.getFieldMetadata("enumProperty").setEnabled(false);
79          assertFalse(cb.isEnabled());
80  
81          fm.getFieldMetadata("enumProperty").setEnabled(true);
82          assertTrue(cb.isEnabled());
83      }
84  
85      public void testComponentTracksReadOnlyChanges() {
86          assertTrue(cb.isEnabled());
87  
88          fm.getFieldMetadata("enumProperty").setReadOnly(true);
89          assertFalse(cb.isEnabled());
90  
91          fm.getFieldMetadata("enumProperty").setReadOnly(false);
92          assertTrue(cb.isEnabled());
93      }
94  }