KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/SizeAssertionGui.java,v 1.14.2.1 2004/11/07 02:57:44 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.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.FocusEvent JavaDoc;
24 import java.awt.event.FocusListener JavaDoc;
25
26 import javax.swing.BorderFactory JavaDoc;
27 import javax.swing.Box JavaDoc;
28 import javax.swing.ButtonGroup JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JOptionPane JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JRadioButton JavaDoc;
33 import javax.swing.JTextField JavaDoc;
34
35 import org.apache.jmeter.assertions.SizeAssertion;
36 import org.apache.jmeter.testelement.TestElement;
37 import org.apache.jmeter.util.JMeterUtils;
38 import org.apache.jorphan.gui.layout.VerticalLayout;
39 import org.apache.jorphan.logging.LoggingManager;
40 import org.apache.log.Logger;
41
42 /**
43  * @version $Revision: 1.14.2.1 $ Last updated: $Date: 2004/11/07 02:57:44 $
44  */

45 public class SizeAssertionGui
46     extends AbstractAssertionGui
47     implements FocusListener JavaDoc, ActionListener JavaDoc
48 {
49     transient private static Logger log = LoggingManager.getLoggerForClass();
50
51     private JTextField JavaDoc size;
52     private JRadioButton JavaDoc equalButton,
53         notequalButton,
54         greaterthanButton,
55         lessthanButton,
56         greaterthanequalButton,
57         lessthanequalButton;
58     private int execState; //store the operator
59

60     public SizeAssertionGui()
61     {
62         init();
63     }
64
65     public String JavaDoc getLabelResource()
66     {
67         return "size_assertion_title";
68     }
69
70     public String JavaDoc getSizeAttributesTitle()
71     {
72         return JMeterUtils.getResString("size_assertion_size_test");
73     }
74
75     public TestElement createTestElement()
76     {
77         SizeAssertion el = new SizeAssertion();
78         modifyTestElement(el);
79         return el;
80     }
81
82     /**
83      * Modifies a given TestElement to mirror the data in the gui components.
84      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
85      */

86     public void modifyTestElement(TestElement el)
87     {
88         configureTestElement(el);
89         String JavaDoc sizeString = size.getText();
90         long assertionSize = 0;
91         try
92         {
93             assertionSize = Long.parseLong(sizeString);
94         }
95         catch (NumberFormatException JavaDoc e)
96         {
97             assertionSize = Long.MAX_VALUE;
98         }
99         ((SizeAssertion) el).setAllowedSize(assertionSize);
100         ((SizeAssertion) el).setCompOper(getState());
101     }
102
103     public void configure(TestElement el)
104     {
105         super.configure(el);
106         SizeAssertion assertion = (SizeAssertion) el;
107         size.setText(String.valueOf(assertion.getAllowedSize()));
108         setState(assertion.getCompOper());
109     }
110
111     /**
112      * Set the state of the radio Button
113      */

114     public void setState(int state)
115     {
116         if (state == SizeAssertion.EQUAL)
117         {
118             equalButton.setSelected(true);
119             execState = state;
120         }
121         else if (state == SizeAssertion.NOTEQUAL)
122         {
123             notequalButton.setSelected(true);
124             execState = state;
125         }
126         else if (state == SizeAssertion.GREATERTHAN)
127         {
128             greaterthanButton.setSelected(true);
129             execState = state;
130         }
131         else if (state == SizeAssertion.LESSTHAN)
132         {
133             lessthanButton.setSelected(true);
134             execState = state;
135         }
136         else if (state == SizeAssertion.GREATERTHANEQUAL)
137         {
138             greaterthanequalButton.setSelected(true);
139             execState = state;
140         }
141         else if (state == SizeAssertion.LESSTHANEQUAL)
142         {
143             lessthanequalButton.setSelected(true);
144             execState = state;
145         }
146     }
147
148     /**
149      * Get the state of the radio Button
150      */

151     public int getState()
152     {
153         return execState;
154     }
155     
156     private void init()
157     {
158         setLayout(
159             new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
160         setBorder(makeBorder());
161
162         add(makeTitlePanel());
163
164         // USER_INPUT
165
JPanel JavaDoc sizePanel = new JPanel JavaDoc();
166         sizePanel.setBorder(
167             BorderFactory.createTitledBorder(
168                 BorderFactory.createEtchedBorder(),
169                 getSizeAttributesTitle()));
170
171         sizePanel.add(
172             new JLabel JavaDoc(JMeterUtils.getResString("size_assertion_label")));
173         size = new JTextField JavaDoc(5);
174         size.addFocusListener(this);
175         sizePanel.add(size);
176
177         sizePanel.add(createComparatorButtonPanel());
178
179         add(sizePanel);
180     }
181
182     private Box JavaDoc createComparatorButtonPanel()
183     {
184         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
185
186         equalButton = createComparatorButton("=", SizeAssertion.EQUAL, group);
187         notequalButton =
188             createComparatorButton("!=", SizeAssertion.NOTEQUAL, group);
189         greaterthanButton =
190             createComparatorButton(">", SizeAssertion.GREATERTHAN, group);
191         lessthanButton =
192             createComparatorButton("<", SizeAssertion.LESSTHAN, group);
193         greaterthanequalButton =
194             createComparatorButton(">=", SizeAssertion.GREATERTHANEQUAL, group);
195         lessthanequalButton =
196             createComparatorButton("<=", SizeAssertion.LESSTHANEQUAL, group);
197
198         equalButton.setSelected(true);
199         execState = Integer.parseInt(equalButton.getActionCommand());
200
201         //Put the check boxes in a column in a panel
202
Box JavaDoc checkPanel = Box.createVerticalBox();
203         JLabel JavaDoc compareLabel =
204             new JLabel JavaDoc(
205                 JMeterUtils.getResString("size_assertion_comparator_label"));
206         checkPanel.add(compareLabel);
207         checkPanel.add(equalButton);
208         checkPanel.add(notequalButton);
209         checkPanel.add(greaterthanButton);
210         checkPanel.add(lessthanButton);
211         checkPanel.add(greaterthanequalButton);
212         checkPanel.add(lessthanequalButton);
213         return checkPanel;
214     }
215
216     private JRadioButton JavaDoc createComparatorButton(
217         String JavaDoc name,
218         int value,
219         ButtonGroup JavaDoc group)
220     {
221         JRadioButton JavaDoc button = new JRadioButton JavaDoc(name);
222         button.setActionCommand(String.valueOf(value));
223         button.addActionListener(this);
224         group.add(button);
225         return button;
226     }
227
228     public void focusLost(FocusEvent JavaDoc e)
229     {
230         boolean isInvalid = false;
231         String JavaDoc sizeString = size.getText();
232         if (sizeString != null)
233         {
234             try
235             {
236                 long assertionSize = Long.parseLong(sizeString);
237                 if (assertionSize < 0)
238                 {
239                     isInvalid = true;
240                 }
241             }
242             catch (NumberFormatException JavaDoc ex)
243             {
244                 isInvalid = true;
245             }
246             if (isInvalid)
247             {
248                 log.warn("SizeAssertionGui: Not a valid number!");
249                 JOptionPane.showMessageDialog(
250                     null,
251                     JMeterUtils.getResString("size_assertion_input_error"),
252                     "Error",
253                     JOptionPane.ERROR_MESSAGE);
254             }
255         }
256     }
257
258     public void focusGained(FocusEvent JavaDoc e)
259     {
260     }
261
262     public void actionPerformed(ActionEvent JavaDoc e)
263     {
264         int comparator = Integer.parseInt(e.getActionCommand());
265         execState = comparator;
266     }
267 }
268
Popular Tags