1   /*
2    * Copyright 2002-2004 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * the License at
7    * 
8    * http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package org.springframework.binding.value.swing;
17  
18  import java.awt.event.FocusEvent;
19  import java.awt.event.FocusListener;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.swing.JFormattedTextField;
25  import javax.swing.text.BadLocationException;
26  import javax.swing.text.Document;
27  
28  /**
29   * A JFormattedTextField that has methods to simulate user interaction
30   * 
31   * @author Oliver Hutchison
32   */
33  public class TestableJTextComponent extends JFormattedTextField {
34  
35      private List focusListeners = new ArrayList();
36  
37      /**
38       * Simulates gaining focus.
39       */
40      public void gainFocus() {
41          FocusEvent focusEvent = new FocusEvent(this, FocusEvent.FOCUS_GAINED);
42          for (Iterator i = focusListeners.iterator(); i.hasNext();) {
43              ((FocusListener)i.next()).focusGained(focusEvent);
44          }
45      }
46  
47      /**
48       * Simulates losing focus.
49       */
50      public void loseFocus() {
51          FocusEvent focusEvent = new FocusEvent(this, FocusEvent.FOCUS_LOST);
52          for (Iterator i = focusListeners.iterator(); i.hasNext();) {
53              ((FocusListener)i.next()).focusLost(focusEvent);
54          }
55      }
56  
57      /**
58       * Simulates text being typed.
59       */
60      public void typeText(String text) {
61          Document doc = getDocument();
62          for (int i = 0; i < text.length(); i++) {
63              try {
64                  doc.insertString(getCaretPosition(), new String(text.substring(i, i + 1)), null);
65                  setCaretPosition(getCaretPosition() + 1);
66              }
67              catch (BadLocationException e) {
68                  throw new UnsupportedOperationException(e.getMessage());
69              }
70          }
71      }
72  
73      /**
74       * Simulates backspace being pressed.
75       */
76      public void typeBackSpace() {
77          Document doc = getDocument();
78          try {
79              doc.remove(getCaretPosition() - 1, 1);
80              setCaretPosition(getCaretPosition() - 1);
81          }
82          catch (BadLocationException e) {
83              throw new UnsupportedOperationException(e.getMessage());
84          }
85      }
86  
87      public void addFocusListener(FocusListener listener) {
88          super.addFocusListener(listener);
89          if (focusListeners != null) {
90              focusListeners.add(listener);
91          }
92      }
93  }