KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > assertions > gui > DurationAssertionGui


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/DurationAssertionGui.java,v 1.17 2004/03/05 01:32:13 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.assertions.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.event.FocusEvent JavaDoc;
23 import java.awt.event.FocusListener JavaDoc;
24
25 import javax.swing.BorderFactory JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JOptionPane JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JTextField JavaDoc;
30
31 import org.apache.jmeter.assertions.DurationAssertion;
32 import org.apache.jmeter.gui.util.HorizontalPanel;
33 import org.apache.jmeter.testelement.TestElement;
34 import org.apache.jmeter.util.JMeterUtils;
35 import org.apache.jorphan.logging.LoggingManager;
36 import org.apache.log.Logger;
37
38 /**
39  * @version $Revision: 1.17 $ Last updated: $Date: 2004/03/05 01:32:13 $
40  */

41 public class DurationAssertionGui
42     extends AbstractAssertionGui
43     implements FocusListener JavaDoc
44 {
45     transient private static Logger log = LoggingManager.getLoggerForClass();
46
47     private JTextField JavaDoc duration;
48
49     public DurationAssertionGui()
50     {
51         init();
52     }
53
54     public String JavaDoc getLabelResource()
55     {
56         return "duration_assertion_title";
57     }
58
59     public String JavaDoc getDurationAttributesTitle()
60     {
61         return JMeterUtils.getResString("duration_assertion_duration_test");
62     }
63
64     public TestElement createTestElement()
65     {
66         DurationAssertion el = new DurationAssertion();
67         modifyTestElement(el);
68         return el;
69     }
70
71     /**
72      * Modifies a given TestElement to mirror the data in the gui components.
73      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
74      */

75     public void modifyTestElement(TestElement el)
76     {
77         configureTestElement(el);
78         String JavaDoc durationString = duration.getText();
79         long assertionDuration = 0;
80         try
81         {
82             assertionDuration = Long.parseLong(durationString);
83         }
84         catch (NumberFormatException JavaDoc e)
85         {
86             assertionDuration = Long.MAX_VALUE;
87         }
88         ((DurationAssertion) el).setAllowedDuration(assertionDuration);
89     }
90
91     public void configure(TestElement el)
92     {
93         super.configure(el);
94         DurationAssertion assertion = (DurationAssertion) el;
95         duration.setText(String.valueOf(assertion.getAllowedDuration()));
96     }
97
98     private void init()
99     {
100         setLayout(new BorderLayout JavaDoc(0, 10));
101         setBorder(makeBorder());
102
103         add(makeTitlePanel(), BorderLayout.NORTH);
104
105         JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
106
107         // USER_INPUT
108
HorizontalPanel durationPanel = new HorizontalPanel();
109         durationPanel.setBorder(
110             BorderFactory.createTitledBorder(
111                 BorderFactory.createEtchedBorder(),
112                 getDurationAttributesTitle()));
113
114         durationPanel.add(
115             new JLabel JavaDoc(JMeterUtils.getResString("duration_assertion_label")));
116
117         duration = new JTextField JavaDoc(5);
118         duration.addFocusListener(this);
119         durationPanel.add(duration);
120
121         mainPanel.add(durationPanel, BorderLayout.NORTH);
122         add(mainPanel, BorderLayout.CENTER);
123     }
124
125     public void focusLost(FocusEvent JavaDoc e)
126     {
127         boolean isInvalid = false;
128         String JavaDoc durationString = duration.getText();
129         if (durationString != null)
130         {
131             try
132             {
133                 long assertionDuration = Long.parseLong(durationString);
134                 if (assertionDuration < 0)
135                 {
136                     isInvalid = true;
137                 }
138             }
139             catch (NumberFormatException JavaDoc ex)
140             {
141                 isInvalid = true;
142             }
143             if (isInvalid)
144             {
145                 log.warn("DurationAssertionGui: Not a valid number!");
146                 JOptionPane.showMessageDialog(
147                     null,
148                     JMeterUtils.getResString("duration_assertion_input_error"),
149                     "Error",
150                     JOptionPane.ERROR_MESSAGE);
151             }
152         }
153     }
154
155     public void focusGained(FocusEvent JavaDoc e)
156     {
157     }
158 }
159
Popular Tags