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 org.springframework.binding.form.FormModel;
019    import org.springframework.richclient.form.binding.Binding;
020    import org.springframework.richclient.form.binding.support.AbstractBinder;
021    import org.springframework.richclient.form.binding.swing.text.DocumentFactory;
022    import org.springframework.util.Assert;
023    
024    import javax.swing.*;
025    import javax.swing.text.JTextComponent;
026    import java.util.Map;
027    
028    /**
029     * @author Oliver Hutchison
030     */
031    public class TextComponentBinder extends AbstractBinder {
032        private String promptKey;
033        private boolean convertEmptyStringToNull;
034        private DocumentFactory documentFactory;
035        private boolean readOnly;
036        private boolean selectAllOnFocus;
037    
038        public TextComponentBinder() {    
039            super(String.class);
040        }
041    
042        protected Binding doBind(JComponent control, FormModel formModel, String formPropertyPath, Map context) {
043            Assert.isTrue(control instanceof JTextComponent, "Control must be an instance of JTextComponent.");
044            TextComponentBinding textComponentBinding = new TextComponentBinding((JTextComponent) control, formModel, formPropertyPath);
045            textComponentBinding.setConvertEmptyStringToNull(convertEmptyStringToNull);
046            textComponentBinding.setPromptKey(promptKey);
047            textComponentBinding.setReadOnly(readOnly);
048            textComponentBinding.setSelectAllOnFocus(selectAllOnFocus);
049            return textComponentBinding;
050        }
051    
052        protected JTextComponent createTextComponent()
053        {
054             return getComponentFactory().createTextField();
055        }
056    
057        protected JComponent createControl(Map context) {
058            JTextComponent textComponent = createTextComponent();
059            if (getDocumentFactory() != null) {
060                textComponent.setDocument(getDocumentFactory().createDocument());
061            }
062            return textComponent;
063        }
064    
065        public boolean isConvertEmptyStringToNull()
066        {
067            return convertEmptyStringToNull;
068        }
069    
070        public void setConvertEmptyStringToNull(boolean convertEmptyStringToNull)
071        {
072            this.convertEmptyStringToNull = convertEmptyStringToNull;
073        }
074    
075        public String getPromptKey()
076        {
077            return promptKey;
078        }
079    
080        public void setPromptKey(String promptKey)
081        {
082            this.promptKey = promptKey;
083        }
084    
085        public DocumentFactory getDocumentFactory()
086        {
087            return documentFactory;
088        }
089    
090        public void setDocumentFactory(DocumentFactory documentFactory)
091        {
092            this.documentFactory = documentFactory;
093        }
094    
095        public boolean isReadOnly()
096        {
097            return readOnly;
098        }
099    
100        public void setReadOnly(boolean readOnly)
101        {
102            this.readOnly = readOnly;
103        }
104    
105        public boolean isSelectAllOnFocus()
106        {
107            return selectAllOnFocus;
108        }
109    
110        public void setSelectAllOnFocus(boolean selectAllOnFocus)
111        {
112            this.selectAllOnFocus = selectAllOnFocus;
113        }
114    }
115