KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > util > NumberFieldErrorListener


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/util/NumberFieldErrorListener.java,v 1.4 2004/02/13 02:40:53 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.util;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.TextComponent JavaDoc;
23 import java.awt.event.FocusAdapter JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25
26 import javax.swing.JOptionPane JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28
29 import org.apache.jmeter.util.JMeterUtils;
30
31 /**
32  * @author mstover
33  * @version $Revision: 1.4 $
34  */

35 public class NumberFieldErrorListener extends FocusAdapter JavaDoc
36 {
37
38     private static NumberFieldErrorListener listener =
39         new NumberFieldErrorListener();
40
41     public static NumberFieldErrorListener getNumberFieldErrorListener()
42     {
43         return listener;
44     }
45
46     public void focusLost(FocusEvent JavaDoc e)
47     {
48         Component JavaDoc source = (Component JavaDoc) e.getSource();
49         String JavaDoc text = "";
50         if (source instanceof JTextComponent JavaDoc)
51         {
52             text = ((JTextComponent JavaDoc) source).getText();
53         }
54         else if (source instanceof TextComponent JavaDoc)
55         {
56             text = ((TextComponent JavaDoc) source).getText();
57         }
58         try
59         {
60             Integer.parseInt(text);
61         }
62         catch (NumberFormatException JavaDoc nfe)
63         {
64             JOptionPane.showMessageDialog(
65                 source,
66                 JMeterUtils.getResString("You must enter a valid number"),
67                 JMeterUtils.getResString("Invalid data"),
68                 JOptionPane.WARNING_MESSAGE);
69             new FocusRequester(source);
70         }
71     }
72 }
73
Popular Tags