001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.assertions.gui;
020:
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025:
026: import javax.swing.BorderFactory;
027: import javax.swing.Box;
028: import javax.swing.ButtonGroup;
029: import javax.swing.JLabel;
030: import javax.swing.JOptionPane;
031: import javax.swing.JPanel;
032: import javax.swing.JRadioButton;
033: import javax.swing.JTextField;
034:
035: import org.apache.jmeter.assertions.SizeAssertion;
036: import org.apache.jmeter.testelement.TestElement;
037: import org.apache.jmeter.util.JMeterUtils;
038: import org.apache.jorphan.gui.layout.VerticalLayout;
039:
040: public class SizeAssertionGui extends AbstractAssertionGui implements
041: FocusListener, ActionListener {
042:
043: private JTextField size;
044:
045: private JRadioButton equalButton, notequalButton,
046: greaterthanButton, lessthanButton, greaterthanequalButton,
047: lessthanequalButton;
048:
049: private int execState; // store the operator
050:
051: public SizeAssertionGui() {
052: init();
053: }
054:
055: public String getLabelResource() {
056: return "size_assertion_title"; //$NON-NLS-1$
057: }
058:
059: public String getSizeAttributesTitle() {
060: return JMeterUtils.getResString("size_assertion_size_test"); //$NON-NLS-1$
061: }
062:
063: public TestElement createTestElement() {
064: SizeAssertion el = new SizeAssertion();
065: modifyTestElement(el);
066: return el;
067: }
068:
069: /**
070: * Modifies a given TestElement to mirror the data in the gui components.
071: *
072: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
073: */
074: public void modifyTestElement(TestElement el) {
075: configureTestElement(el);
076: String sizeString = size.getText();
077: long assertionSize = 0;
078: try {
079: assertionSize = Long.parseLong(sizeString);
080: } catch (NumberFormatException e) {
081: assertionSize = Long.MAX_VALUE;
082: }
083: ((SizeAssertion) el).setAllowedSize(assertionSize);
084: ((SizeAssertion) el).setCompOper(getState());
085: }
086:
087: /**
088: * Implements JMeterGUIComponent.clearGui
089: */
090: public void clearGui() {
091: super .clearGui();
092:
093: size.setText(""); //$NON-NLS-1$
094: equalButton.setSelected(true);
095: notequalButton.setSelected(false);
096: greaterthanButton.setSelected(false);
097: lessthanButton.setSelected(false);
098: greaterthanequalButton.setSelected(false);
099: lessthanequalButton.setSelected(false);
100: execState = SizeAssertion.EQUAL;
101: }
102:
103: public void configure(TestElement el) {
104: super .configure(el);
105: SizeAssertion assertion = (SizeAssertion) el;
106: size.setText(String.valueOf(assertion.getAllowedSize()));
107: setState(assertion.getCompOper());
108: }
109:
110: /**
111: * Set the state of the radio Button
112: */
113: public void setState(int state) {
114: if (state == SizeAssertion.EQUAL) {
115: equalButton.setSelected(true);
116: execState = state;
117: } else if (state == SizeAssertion.NOTEQUAL) {
118: notequalButton.setSelected(true);
119: execState = state;
120: } else if (state == SizeAssertion.GREATERTHAN) {
121: greaterthanButton.setSelected(true);
122: execState = state;
123: } else if (state == SizeAssertion.LESSTHAN) {
124: lessthanButton.setSelected(true);
125: execState = state;
126: } else if (state == SizeAssertion.GREATERTHANEQUAL) {
127: greaterthanequalButton.setSelected(true);
128: execState = state;
129: } else if (state == SizeAssertion.LESSTHANEQUAL) {
130: lessthanequalButton.setSelected(true);
131: execState = state;
132: }
133: }
134:
135: /**
136: * Get the state of the radio Button
137: */
138: public int getState() {
139: return execState;
140: }
141:
142: private void init() {
143: setLayout(new VerticalLayout(5, VerticalLayout.BOTH,
144: VerticalLayout.TOP));
145: setBorder(makeBorder());
146:
147: add(makeTitlePanel());
148:
149: // USER_INPUT
150: JPanel sizePanel = new JPanel();
151: sizePanel.setBorder(BorderFactory.createTitledBorder(
152: BorderFactory.createEtchedBorder(),
153: getSizeAttributesTitle()));
154:
155: sizePanel.add(new JLabel(JMeterUtils
156: .getResString("size_assertion_label"))); //$NON-NLS-1$
157: size = new JTextField(5);
158: size.addFocusListener(this );
159: sizePanel.add(size);
160:
161: sizePanel.add(createComparatorButtonPanel());
162:
163: add(sizePanel);
164: }
165:
166: private Box createComparatorButtonPanel() {
167: ButtonGroup group = new ButtonGroup();
168:
169: equalButton = createComparatorButton(
170: "=", SizeAssertion.EQUAL, group); //$NON-NLS-1$
171: notequalButton = createComparatorButton(
172: "!=", SizeAssertion.NOTEQUAL, group); //$NON-NLS-1$
173: greaterthanButton = createComparatorButton(
174: ">", SizeAssertion.GREATERTHAN, group); //$NON-NLS-1$
175: lessthanButton = createComparatorButton(
176: "<", SizeAssertion.LESSTHAN, group); //$NON-NLS-1$
177: greaterthanequalButton = createComparatorButton(
178: ">=", SizeAssertion.GREATERTHANEQUAL, group); //$NON-NLS-1$
179: lessthanequalButton = createComparatorButton(
180: "<=", SizeAssertion.LESSTHANEQUAL, group); //$NON-NLS-1$
181:
182: equalButton.setSelected(true);
183: execState = Integer.parseInt(equalButton.getActionCommand());
184:
185: // Put the check boxes in a column in a panel
186: Box checkPanel = Box.createVerticalBox();
187: JLabel compareLabel = new JLabel(JMeterUtils
188: .getResString("size_assertion_comparator_label")); //$NON-NLS-1$
189: checkPanel.add(compareLabel);
190: checkPanel.add(equalButton);
191: checkPanel.add(notequalButton);
192: checkPanel.add(greaterthanButton);
193: checkPanel.add(lessthanButton);
194: checkPanel.add(greaterthanequalButton);
195: checkPanel.add(lessthanequalButton);
196: return checkPanel;
197: }
198:
199: private JRadioButton createComparatorButton(String name, int value,
200: ButtonGroup group) {
201: JRadioButton button = new JRadioButton(name);
202: button.setActionCommand(String.valueOf(value));
203: button.addActionListener(this );
204: group.add(button);
205: return button;
206: }
207:
208: public void focusLost(FocusEvent e) {
209: boolean isInvalid = false;
210: String sizeString = size.getText();
211: if (sizeString != null) {
212: try {
213: long assertionSize = Long.parseLong(sizeString);
214: if (assertionSize < 0) {
215: isInvalid = true;
216: }
217: } catch (NumberFormatException ex) {
218: isInvalid = true;
219: }
220: if (isInvalid) {
221: JOptionPane.showMessageDialog(null, JMeterUtils
222: .getResString("size_assertion_input_error"), //$NON-NLS-1$
223: "Error", JOptionPane.ERROR_MESSAGE);
224: }
225: }
226: }
227:
228: public void focusGained(FocusEvent e) {
229: }
230:
231: public void actionPerformed(ActionEvent e) {
232: int comparator = Integer.parseInt(e.getActionCommand());
233: execState = comparator;
234: }
235: }
|