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.binding.value.swing;
017    
018    import javax.swing.JFormattedTextField;
019    import javax.swing.text.DefaultFormatter;
020    
021    import org.springframework.core.enums.ShortCodedLabeledEnum;
022    
023    /**
024     * @author Keith Donald
025     */
026    public abstract class ValueCommitPolicy extends ShortCodedLabeledEnum {
027            public static final ValueCommitPolicy AS_YOU_TYPE = new ValueCommitPolicy(0, "as_you_type") {
028                    public void configure(JFormattedTextField textField, DefaultFormatter formatter) {
029                            textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
030                            formatter.setOverwriteMode(false);
031                            formatter.setAllowsInvalid(true);
032                            formatter.setCommitsOnValidEdit(true);
033                    }
034            };
035    
036            public static final ValueCommitPolicy FOCUS_LOST = new ValueCommitPolicy(1, "focus_lost") {
037                    public void configure(JFormattedTextField textField, DefaultFormatter formatter) {
038                            textField.setFocusLostBehavior(JFormattedTextField.COMMIT);
039                            formatter.setOverwriteMode(false);
040                            formatter.setAllowsInvalid(true);
041                            formatter.setCommitsOnValidEdit(false);
042                    }
043            };
044    
045            public static final ValueCommitPolicy ON_SUBMIT = new ValueCommitPolicy(2, "on_submit") {
046                    public void configure(JFormattedTextField textField, DefaultFormatter formatter) {
047                            textField.setFocusLostBehavior(JFormattedTextField.PERSIST);
048                            formatter.setOverwriteMode(false);
049                            formatter.setAllowsInvalid(true);
050                            formatter.setCommitsOnValidEdit(false);
051                    }
052            };
053    
054            private ValueCommitPolicy(int code, String label) {
055                    super(code, label); 
056            }
057    
058            public abstract void configure(JFormattedTextField textField, DefaultFormatter formatter);
059    }