001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.springframework.richclient.form.binding.swing;
017    
018    import java.util.Collections;
019    
020    import javax.swing.JComboBox;
021    import javax.swing.event.ListDataEvent;
022    
023    import org.springframework.binding.support.TestLabeledEnum;
024    
025    public class LabeledEnumComboBoxBindingAbstractTests extends BindingAbstractTests {
026    
027        private LabeledEnumComboBoxBinding cbb;
028    
029        private JComboBox cb;
030    
031        protected String setUpBinding() {
032            cbb = (LabeledEnumComboBoxBinding) new LabeledEnumComboBoxBinder().bind(fm, "enumProperty", Collections.EMPTY_MAP);
033            cb = (JComboBox) cbb.getControl();
034            return "enumProperty";
035        }
036    
037        public void testValueModelUpdatesComponent() {
038            TestListDataListener tldl = new TestListDataListener();
039            cb.getModel().addListDataListener(tldl);
040    
041            assertEquals(null, cb.getSelectedItem());
042            assertEquals(-1, cb.getSelectedIndex());
043            tldl.assertCalls(0);
044    
045            vm.setValue(TestLabeledEnum.ONE);
046            assertEquals(TestLabeledEnum.ONE, cb.getSelectedItem());
047            assertEquals(1, cb.getSelectedIndex());
048            tldl.assertEvent(1, ListDataEvent.CONTENTS_CHANGED, -1, -1);
049    
050            vm.setValue(TestLabeledEnum.TWO);
051            assertEquals(TestLabeledEnum.TWO, cb.getSelectedItem());
052            assertEquals(2, cb.getSelectedIndex());
053            tldl.assertEvent(2, ListDataEvent.CONTENTS_CHANGED, -1, -1);
054    
055            vm.setValue(null);
056            assertEquals(null, cb.getSelectedItem());
057            assertEquals(-1, cb.getSelectedIndex());
058            tldl.assertEvent(3, ListDataEvent.CONTENTS_CHANGED, -1, -1);
059    
060            vm.setValue(null);
061            tldl.assertCalls(3);
062        }
063    
064        public void testComponentUpdatesValueModel() {
065            cb.setSelectedIndex(1);
066            assertEquals(TestLabeledEnum.ONE, vm.getValue());
067    
068            cb.setSelectedItem(TestLabeledEnum.TWO);
069            assertEquals(TestLabeledEnum.TWO, vm.getValue());
070    
071            cb.setSelectedIndex(-1);
072            assertEquals(null, vm.getValue());
073        }
074    
075        public void testComponentTracksEnabledChanges() {
076            assertTrue(cb.isEnabled());
077    
078            fm.getFieldMetadata("enumProperty").setEnabled(false);
079            assertFalse(cb.isEnabled());
080    
081            fm.getFieldMetadata("enumProperty").setEnabled(true);
082            assertTrue(cb.isEnabled());
083        }
084    
085        public void testComponentTracksReadOnlyChanges() {
086            assertTrue(cb.isEnabled());
087    
088            fm.getFieldMetadata("enumProperty").setReadOnly(true);
089            assertFalse(cb.isEnabled());
090    
091            fm.getFieldMetadata("enumProperty").setReadOnly(false);
092            assertTrue(cb.isEnabled());
093        }
094    }