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.awt.event.FocusEvent;
019    import java.awt.event.FocusListener;
020    import java.util.ArrayList;
021    import java.util.Iterator;
022    import java.util.List;
023    
024    import javax.swing.JFormattedTextField;
025    import javax.swing.text.BadLocationException;
026    import javax.swing.text.Document;
027    
028    /**
029     * A JFormattedTextField that has methods to simulate user interaction
030     * 
031     * @author Oliver Hutchison
032     */
033    public class TestableJTextComponent extends JFormattedTextField {
034    
035        private List focusListeners = new ArrayList();
036    
037        /**
038         * Simulates gaining focus.
039         */
040        public void gainFocus() {
041            FocusEvent focusEvent = new FocusEvent(this, FocusEvent.FOCUS_GAINED);
042            for (Iterator i = focusListeners.iterator(); i.hasNext();) {
043                ((FocusListener)i.next()).focusGained(focusEvent);
044            }
045        }
046    
047        /**
048         * Simulates losing focus.
049         */
050        public void loseFocus() {
051            FocusEvent focusEvent = new FocusEvent(this, FocusEvent.FOCUS_LOST);
052            for (Iterator i = focusListeners.iterator(); i.hasNext();) {
053                ((FocusListener)i.next()).focusLost(focusEvent);
054            }
055        }
056    
057        /**
058         * Simulates text being typed.
059         */
060        public void typeText(String text) {
061            Document doc = getDocument();
062            for (int i = 0; i < text.length(); i++) {
063                try {
064                    doc.insertString(getCaretPosition(), new String(text.substring(i, i + 1)), null);
065                    setCaretPosition(getCaretPosition() + 1);
066                }
067                catch (BadLocationException e) {
068                    throw new UnsupportedOperationException(e.getMessage());
069                }
070            }
071        }
072    
073        /**
074         * Simulates backspace being pressed.
075         */
076        public void typeBackSpace() {
077            Document doc = getDocument();
078            try {
079                doc.remove(getCaretPosition() - 1, 1);
080                setCaretPosition(getCaretPosition() - 1);
081            }
082            catch (BadLocationException e) {
083                throw new UnsupportedOperationException(e.getMessage());
084            }
085        }
086    
087        public void addFocusListener(FocusListener listener) {
088            super.addFocusListener(listener);
089            if (focusListeners != null) {
090                focusListeners.add(listener);
091            }
092        }
093    }