View Javadoc

1   /*
2    * @(#) JFormattedTextField.java Apr 11, 2005
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software are proprietary information
11   * of Delft University of Technology.
12   */
13  package org.gscg.common.gui.components.interactivespinner;
14  
15  import java.awt.Component;
16  import java.awt.event.FocusEvent;
17  import java.awt.event.KeyEvent;
18  import java.awt.event.KeyListener;
19  import java.text.Format;
20  
21  import javax.swing.JOptionPane;
22  
23  /***
24   * <p>
25   * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
26   * University of Technology </a>, the Netherlands. <br>
27   * See for project information <a
28   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
29   * 
30   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
31   * Delft, the Netherlands. All rights reserved.
32   * 
33   * See for project information <a href="http://www.simulation.tudelft.nl/">
34   * www.simulation.tudelft.nl </a>.
35   * 
36   * The source code and binary code of this software are proprietary information
37   * of Delft University of Technology.
38   * 
39   * @author <a
40   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
41   *         van Houten </a>
42   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:33:56 $
43   * @since 1.1.3
44   */
45  public class JFormattedTextField extends javax.swing.JFormattedTextField
46  		implements KeyListener
47  {
48  
49  	/***
50  	 * constructs a new JFormattedTextField
51  	 */
52  	public JFormattedTextField()
53  	{
54  		super();
55  		super.addKeyListener(this);
56  	}
57  
58  	/***
59  	 * constructs a new JFormattedTextField
60  	 * 
61  	 * @param value the value
62  	 */
63  	public JFormattedTextField(final Object value)
64  	{
65  		super(value);
66  		super.addKeyListener(this);
67  	}
68  
69  	/***
70  	 * constructs a new JFormattedTextField
71  	 * 
72  	 * @param format the format
73  	 */
74  	public JFormattedTextField(final Format format)
75  	{
76  		super(format);
77  		super.addKeyListener(this);
78  	}
79  
80  	/***
81  	 * constructs a new JFormattedTextField
82  	 * 
83  	 * @param formatter the formatter
84  	 */
85  	public JFormattedTextField(final AbstractFormatter formatter)
86  	{
87  		super(formatter);
88  		super.addKeyListener(this);
89  	}
90  
91  	/***
92  	 * constructs a new JFormattedTextField
93  	 * 
94  	 * @param factory the factory
95  	 */
96  	public JFormattedTextField(final AbstractFormatterFactory factory)
97  	{
98  		super(factory);
99  		super.addKeyListener(this);
100 	}
101 
102 	/***
103 	 * constructs a new JFormattedTextField
104 	 * 
105 	 * @param factory the factory
106 	 * @param currentValue the current value
107 	 */
108 	public JFormattedTextField(final AbstractFormatterFactory factory,
109 			final Object currentValue)
110 	{
111 		super(factory, currentValue);
112 	}
113 
114 	/***
115 	 * @see java.awt.Component#processFocusEvent(java.awt.event.FocusEvent)
116 	 */
117 	protected void processFocusEvent(final FocusEvent e)
118 	{
119 		if (e.getID() == FocusEvent.FOCUS_LOST)
120 		{
121 			try
122 			{
123 				// try to parse the text
124 				new Double(super.getText()).doubleValue();
125 				// see if we are still between the min / max values
126 				if (super.isEditValid())
127 				{
128 					super.processFocusEvent(e);
129 				} else
130 				{
131 					JOptionPane
132 							.showConfirmDialog(
133 									(Component) null,
134 									"The value you entered for the amount is either too large or too small, please adjust the value.",
135 									"Warning", JOptionPane.DEFAULT_OPTION,
136 									JOptionPane.WARNING_MESSAGE);
137 				}
138 			} catch (Exception exception)
139 			{
140 				JOptionPane
141 						.showConfirmDialog(
142 								(Component) null,
143 								"The value you entered for the amount could not be parsed succesfully "
144 										+ "\ninto a number. The value will be reset to the previous value.",
145 								"Warning", JOptionPane.DEFAULT_OPTION,
146 								JOptionPane.WARNING_MESSAGE);
147 				super.processFocusEvent(e);
148 			}
149 		} else
150 		{
151 			super.processFocusEvent(e);
152 		}
153 	}
154 
155 	/***
156 	 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
157 	 */
158 	public void keyPressed(final KeyEvent e)
159 	{
160 		// nothing to do
161 	}
162 
163 	/***
164 	 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
165 	 */
166 	public void keyReleased(final KeyEvent e)
167 	{
168 		if (KeyEvent.getKeyText(e.getKeyCode()).equalsIgnoreCase("Enter"))
169 		{
170 			this.processFocusEvent(new FocusEvent(this, FocusEvent.FOCUS_LOST));
171 		}
172 	}
173 
174 	/***
175 	 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
176 	 */
177 	public void keyTyped(final KeyEvent e)
178 	{
179 		// nothing to do
180 	}
181 }