001    /*
002     * Copyright 2002-2004 the original author or authors.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * 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, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.form.binding.swing;
017    
018    import java.text.NumberFormat;
019    
020    import javax.swing.JFormattedTextField;
021    
022    import org.springframework.binding.form.FieldMetadata;
023    import org.springframework.binding.support.TestBean;
024    
025    public class FormattedTextFieldBindingTests extends BindingAbstractTests {
026    
027        private static final Long initialValue = new Long(99);
028    
029        private JFormattedTextField ftc;
030    
031        private FormattedTextFieldBinding b;
032        
033        protected TestBean createTestBean() {
034            TestBean testBean = super.createTestBean();
035            testBean.setNumberProperty(initialValue);
036            return testBean;
037        }
038    
039        protected String setUpBinding() {   
040            b = new FormattedTextFieldBinding(new JFormattedTextField(NumberFormat.getInstance()), fm, "numberProperty", null);
041            ftc = (JFormattedTextField)b.getControl();
042            return "numberProperty";
043        }
044        
045        public void testInitialValue() {
046            assertEquals(initialValue, vm.getValue());
047            assertEquals(initialValue, ftc.getValue());
048        }
049    
050        public void testComponentTracksEnabledChanges() {
051            assertEquals(true, ftc.isEnabled());
052            fm.setEnabled(false);
053            assertEquals(false, ftc.isEnabled());
054            fm.setEnabled(true);
055            assertEquals(true, ftc.isEnabled());
056        }
057    
058        public void testComponentTracksReadOnlyChanges() {
059            FieldMetadata state = fm.getFieldMetadata("numberProperty");
060            assertEquals(true, ftc.isEditable());
061            state.setReadOnly(true);
062            assertEquals(false, ftc.isEditable());
063            state.setReadOnly(false);
064            assertEquals(true, ftc.isEditable());
065        }
066    
067        public void testComponentUpdatesValueModel() {
068            Long one = new Long(1);
069            Long two = new Long(2);
070            ftc.setValue(one);
071            assertTrue(vm.getValue() instanceof Long);
072            assertTrue(one.compareTo((Long)vm.getValue()) == 0);
073            ftc.setValue(null);
074            assertEquals(null, vm.getValue());
075            ftc.setValue(two);
076            assertEquals(two, vm.getValue());
077            ftc.setValue(null);
078            assertEquals(null, vm.getValue());
079        }
080    
081        public void testValueModelUpdatesComponent() {
082            Long one = new Long(1);
083            Long two = new Long(2);
084            vm.setValue(one);
085            assertEquals(one, ftc.getValue());
086            vm.setValue(null);
087            assertEquals(null, ftc.getValue());
088            vm.setValue(two);
089            assertEquals(two, ftc.getValue());
090            vm.setValue(null);
091            assertEquals(null, ftc.getValue());
092        }
093    }