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 java.text.ParseException;
019
020 import javax.swing.JFormattedTextField;
021 import javax.swing.JFormattedTextField.AbstractFormatter;
022 import javax.swing.JFormattedTextField.AbstractFormatterFactory;
023
024 import org.springframework.binding.support.TestPropertyChangeListener;
025 import org.springframework.binding.value.ValueModel;
026 import org.springframework.binding.value.support.ValueHolder;
027 import org.springframework.richclient.test.SpringRichTestCase;
028
029 /**
030 * Test cases for {@link FormattedTextFieldAdapter}
031 *
032 * @author Oliver Hutchison
033 */
034 public class FormattedTextFieldAdapterTests extends SpringRichTestCase {
035
036 private ValueModel valueModel;
037
038 private TestPropertyChangeListener valueListener;
039
040 private TestableJTextComponent comp;
041
042 public void doSetUp() throws Exception {
043 valueModel = new ValueHolder("");
044 valueListener = new TestPropertyChangeListener(ValueModel.VALUE_PROPERTY);
045 valueModel.addValueChangeListener(valueListener);
046 comp = new TestableJTextComponent();
047 comp.setFormatterFactory(new OnlyAlowLowerCaseFormatterFactory());
048 new FormattedTextFieldAdapter(comp, valueModel, ValueCommitPolicy.AS_YOU_TYPE);
049 }
050
051 public void testComponentChangeUpdatesValueModel() {
052 comp.setValue("newvalue");
053 assertEquals("newvalue", valueModel.getValue());
054 assertEquals(1, valueListener.eventCount());
055
056 comp.setText("newervalue");
057 assertEquals("newervalue", valueModel.getValue());
058 assertEquals(3, valueListener.eventCount());
059 }
060
061 public void testValueModelChangeUpdatesComponent() {
062 valueModel.setValue("newvalue");
063 assertEquals("newvalue", comp.getValue());
064 assertEquals(1, valueListener.eventCount());
065 }
066
067 public void testTypingUpdatesValueModel() {
068 comp.typeText("a");
069 assertEquals("a", valueModel.getValue());
070 assertEquals(1, valueListener.eventCount());
071
072 valueListener.reset();
073 comp.typeText("bc");
074 assertEquals("abc", valueModel.getValue());
075 assertEquals(2, valueListener.eventCount());
076
077 valueListener.reset();
078 comp.setCaretPosition(1);
079 comp.typeText("d");
080 assertEquals("adbc", valueModel.getValue());
081 assertEquals(1, valueListener.eventCount());
082
083 // type an invalid char
084 valueListener.reset();
085 comp.setCaretPosition(1);
086 comp.typeText("E");
087 assertEquals("adbc", valueModel.getValue());
088 assertEquals(0, valueListener.eventCount());
089
090 valueListener.reset();
091 comp.setCaretPosition(2);
092 comp.typeBackSpace();
093 comp.typeText("f");
094 assertEquals("afdbc", valueModel.getValue());
095 assertEquals(1, valueListener.eventCount());
096 }
097
098 private static final class OnlyAlowLowerCaseFormatterFactory extends AbstractFormatterFactory {
099 public AbstractFormatter getFormatter(JFormattedTextField tf) {
100 return new AbstractFormatter() {
101
102 public Object stringToValue(String text) throws ParseException {
103 if (text != null && !text.equals(text.toLowerCase())) {
104 throw new ParseException(text, 0);
105 }
106 return text;
107 }
108
109 public String valueToString(Object value) throws ParseException {
110 return (String) value;
111 }
112 };
113 }
114 }
115 }