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 javax.swing.JComponent;
019    import javax.swing.JFormattedTextField;
020    
021    import org.springframework.binding.form.FormModel;
022    import org.springframework.binding.value.ValueModel;
023    import org.springframework.binding.value.swing.FormattedTextFieldAdapter;
024    import org.springframework.binding.value.swing.ValueCommitPolicy;
025    import org.springframework.richclient.form.binding.support.AbstractBinding;
026    
027    /**
028     * TODO: this is probably very broken. Need to do extensive testing.
029     *  
030     * @author Oliver Hutchison
031     */
032    public class FormattedTextFieldBinding extends AbstractBinding {
033    
034        private final JFormattedTextField formattedTextField;
035    
036        public FormattedTextFieldBinding(JFormattedTextField formattedTextField, FormModel formModel,
037                String formPropertyPath, Class requiredSourceClass) {
038            super(formModel, formPropertyPath, requiredSourceClass);
039            this.formattedTextField = formattedTextField;
040        }
041    
042        protected JComponent doBindControl() {
043            final ValueModel valueModel = getValueModel();
044            formattedTextField.setValue(valueModel.getValue());
045            // TODO: implement ValueCommitPolicies
046            new FormattedTextFieldAdapter(formattedTextField, valueModel, ValueCommitPolicy.AS_YOU_TYPE);
047            return formattedTextField;
048        }
049    
050        protected void readOnlyChanged() {
051            formattedTextField.setEditable(!isReadOnly());
052        }
053    
054        protected void enabledChanged() {
055            formattedTextField.setEnabled(isEnabled());
056        }
057    }