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.BorderLayout;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.FocusEvent;
025: import java.awt.event.KeyEvent;
026: import java.awt.event.KeyListener;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.ButtonGroup;
030: import javax.swing.JCheckBox;
031: import javax.swing.JComboBox;
032: import javax.swing.JLabel;
033: import javax.swing.JOptionPane;
034: import javax.swing.JPanel;
035: import javax.swing.JRadioButton;
036: import javax.swing.JTextField;
037:
038: import org.apache.jmeter.assertions.HTMLAssertion;
039: import org.apache.jmeter.gui.util.FilePanel;
040: import org.apache.jmeter.gui.util.HorizontalPanel;
041: import org.apache.jmeter.gui.util.VerticalPanel;
042: import org.apache.jmeter.testelement.TestElement;
043: import org.apache.jmeter.util.JMeterUtils;
044: import org.apache.jorphan.logging.LoggingManager;
045: import org.apache.log.Logger;
046:
047: /**
048: * GUI for HTMLAssertion
049: */
050: public class HTMLAssertionGui extends AbstractAssertionGui implements
051: KeyListener, ActionListener {
052:
053: private static final Logger log = LoggingManager
054: .getLoggerForClass();
055:
056: private static final long serialVersionUID = 1L;
057:
058: // Names for the fields
059: private static final String WARNING_THRESHOLD_FIELD = "warningThresholdField"; // $NON-NLS-1$
060:
061: private static final String ERROR_THRESHOLD_FIELD = "errorThresholdField"; // $NON-NLS-1$
062:
063: // instance attributes
064: private JTextField errorThresholdField = null;
065:
066: private JTextField warningThresholdField = null;
067:
068: private JCheckBox errorsOnly = null;
069:
070: private JComboBox docTypeBox = null;
071:
072: private JRadioButton htmlRadioButton = null;
073:
074: private JRadioButton xhtmlRadioButton = null;
075:
076: private JRadioButton xmlRadioButton = null;
077:
078: private FilePanel filePanel = null;
079:
080: /**
081: * The constructor.
082: */
083: public HTMLAssertionGui() {
084: init();
085: }
086:
087: /**
088: * Returns the label to be shown within the JTree-Component.
089: */
090: public String getLabelResource() {
091: return "html_assertion_title"; // $NON-NLS-1$
092: }
093:
094: /**
095: * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
096: */
097: public TestElement createTestElement() {
098: HTMLAssertion el = new HTMLAssertion();
099: modifyTestElement(el);
100: return el;
101: }
102:
103: /**
104: * Modifies a given TestElement to mirror the data in the gui components.
105: *
106: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
107: */
108: public void modifyTestElement(TestElement inElement) {
109:
110: log.debug("HTMLAssertionGui.modifyTestElement() called");
111:
112: configureTestElement(inElement);
113:
114: String errorThresholdString = errorThresholdField.getText();
115: long errorThreshold = 0;
116:
117: try {
118: errorThreshold = Long.parseLong(errorThresholdString);
119: } catch (NumberFormatException e) {
120: errorThreshold = 0;
121: }
122: ((HTMLAssertion) inElement).setErrorThreshold(errorThreshold);
123:
124: String warningThresholdString = warningThresholdField.getText();
125: long warningThreshold = 0;
126: try {
127: warningThreshold = Long.parseLong(warningThresholdString);
128: } catch (NumberFormatException e) {
129: warningThreshold = 0;
130: }
131: ((HTMLAssertion) inElement)
132: .setWarningThreshold(warningThreshold);
133:
134: String docTypeString = docTypeBox.getSelectedItem().toString();
135: ((HTMLAssertion) inElement).setDoctype(docTypeString);
136:
137: boolean trackErrorsOnly = errorsOnly.isSelected();
138: ((HTMLAssertion) inElement).setErrorsOnly(trackErrorsOnly);
139:
140: if (htmlRadioButton.isSelected()) {
141: ((HTMLAssertion) inElement).setHTML();
142: } else if (xhtmlRadioButton.isSelected()) {
143: ((HTMLAssertion) inElement).setXHTML();
144: } else {
145: ((HTMLAssertion) inElement).setXML();
146: }
147: ((HTMLAssertion) inElement)
148: .setFilename(filePanel.getFilename());
149: }
150:
151: /**
152: * Implements JMeterGUIComponent.clearGui
153: */
154: public void clearGui() {
155: super .clearGui();
156:
157: docTypeBox.setSelectedIndex(0);
158: htmlRadioButton.setSelected(true);
159: xhtmlRadioButton.setSelected(false);
160: xmlRadioButton.setSelected(false);
161: errorThresholdField.setText("0"); //$NON-NLS-1$
162: warningThresholdField.setText("0"); //$NON-NLS-1$
163: filePanel.setFilename(""); //$NON-NLS-1$
164: errorsOnly.setSelected(false);
165: }
166:
167: /**
168: * Configures the associated test element.
169: *
170: * @param inElement
171: */
172: public void configure(TestElement inElement) {
173: super .configure(inElement);
174: HTMLAssertion lAssertion = (HTMLAssertion) inElement;
175: errorThresholdField.setText(String.valueOf(lAssertion
176: .getErrorThreshold()));
177: warningThresholdField.setText(String.valueOf(lAssertion
178: .getWarningThreshold()));
179: errorsOnly.setSelected(lAssertion.isErrorsOnly());
180: docTypeBox.setSelectedItem(lAssertion.getDoctype());
181: if (lAssertion.isHTML()) {
182: htmlRadioButton.setSelected(true);
183: } else if (lAssertion.isXHTML()) {
184: xhtmlRadioButton.setSelected(true);
185: } else {
186: xmlRadioButton.setSelected(true);
187: }
188: if (lAssertion.isErrorsOnly()) {
189: warningThresholdField.setEnabled(false);
190: warningThresholdField.setEditable(false);
191: } else {
192: warningThresholdField.setEnabled(true);
193: warningThresholdField.setEditable(true);
194: }
195: filePanel.setFilename(lAssertion.getFilename());
196: }
197:
198: /**
199: * Inits the GUI.
200: */
201: private void init() {
202:
203: setLayout(new BorderLayout(0, 10));
204: setBorder(makeBorder());
205:
206: add(makeTitlePanel(), BorderLayout.NORTH);
207:
208: JPanel mainPanel = new JPanel(new BorderLayout());
209:
210: // USER_INPUT
211: VerticalPanel assertionPanel = new VerticalPanel();
212: assertionPanel.setBorder(BorderFactory.createTitledBorder(
213: BorderFactory.createEtchedBorder(), "Tidy Settings"));
214:
215: // doctype
216: HorizontalPanel docTypePanel = new HorizontalPanel();
217: docTypeBox = new JComboBox(new Object[] { "omit", "auto",
218: "strict", "loose" });
219: // docTypePanel.add(new
220: // JLabel(JMeterUtils.getResString("duration_assertion_label"))); //$NON-NLS-1$
221: docTypePanel.add(new JLabel("Doctype:"));
222: docTypePanel.add(docTypeBox);
223: assertionPanel.add(docTypePanel);
224:
225: // format (HMTL, XHTML, XML)
226: VerticalPanel formatPanel = new VerticalPanel();
227: formatPanel.setBorder(BorderFactory.createTitledBorder(
228: BorderFactory.createEtchedBorder(), "Format"));
229: htmlRadioButton = new JRadioButton("HTML", true); //$NON-NLS-1$
230: xhtmlRadioButton = new JRadioButton("XHTML", false); //$NON-NLS-1$
231: xmlRadioButton = new JRadioButton("XML", false); //$NON-NLS-1$
232: ButtonGroup buttonGroup = new ButtonGroup();
233: buttonGroup.add(htmlRadioButton);
234: buttonGroup.add(xhtmlRadioButton);
235: buttonGroup.add(xmlRadioButton);
236: formatPanel.add(htmlRadioButton);
237: formatPanel.add(xhtmlRadioButton);
238: formatPanel.add(xmlRadioButton);
239: assertionPanel.add(formatPanel);
240:
241: // errors only
242: errorsOnly = new JCheckBox("Errors only", false);
243: errorsOnly.addActionListener(this );
244: assertionPanel.add(errorsOnly);
245:
246: // thresholds
247: HorizontalPanel thresholdPanel = new HorizontalPanel();
248: thresholdPanel.add(new JLabel("Error threshold:"));
249: errorThresholdField = new JTextField("0", 5); // $NON-NLS-1$
250: errorThresholdField.setName(ERROR_THRESHOLD_FIELD);
251: errorThresholdField.addKeyListener(this );
252: thresholdPanel.add(errorThresholdField);
253: thresholdPanel.add(new JLabel("Warning threshold:"));
254: warningThresholdField = new JTextField("0", 5); // $NON-NLS-1$
255: warningThresholdField.setName(WARNING_THRESHOLD_FIELD);
256: warningThresholdField.addKeyListener(this );
257: thresholdPanel.add(warningThresholdField);
258: assertionPanel.add(thresholdPanel);
259:
260: // file panel
261: filePanel = new FilePanel(JMeterUtils
262: .getResString("file_visualizer_output_file"), ".txt"); //$NON-NLS-1$ //$NON-NLS-2$
263: assertionPanel.add(filePanel);
264:
265: mainPanel.add(assertionPanel, BorderLayout.NORTH);
266: add(mainPanel, BorderLayout.CENTER);
267: }
268:
269: /**
270: * This method is called if one of the threshold field looses the focus
271: *
272: * @param inEvent
273: */
274: public void focusLost(FocusEvent inEvent) {
275: log.debug("HTMLAssertionGui.focusLost() called");
276:
277: String errorThresholdString = errorThresholdField.getText();
278: if (errorThresholdString != null) {
279: boolean isInvalid = false;
280: try {
281: long errorThreshold = Long
282: .parseLong(errorThresholdString);
283: if (errorThreshold < 0) {
284: isInvalid = true;
285: }
286: } catch (NumberFormatException ex) {
287: isInvalid = true;
288: }
289: if (isInvalid) {
290: log
291: .warn("HTMLAssertionGui: Error threshold Not a valid number!");
292: JOptionPane.showMessageDialog(null,
293: "Threshold for errors is invalid", "Error",
294: JOptionPane.ERROR_MESSAGE);
295: }
296: }
297:
298: String warningThresholdString = warningThresholdField.getText();
299: if (warningThresholdString != null) {
300: boolean isInvalid = false;
301: try {
302: long warningThreshold = Long
303: .parseLong(warningThresholdString);
304: if (warningThreshold < 0) {
305: isInvalid = true;
306: }
307: } catch (NumberFormatException ex) {
308: isInvalid = true;
309: }
310: if (isInvalid) {
311: log
312: .warn("HTMLAssertionGui: Error threshold Not a valid number!");
313: JOptionPane.showMessageDialog(null,
314: "Threshold for warnings is invalid", "Error",
315: JOptionPane.ERROR_MESSAGE);
316: }
317: }
318: }
319:
320: /**
321: * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
322: */
323: public void focusGained(FocusEvent e) {
324:
325: }
326:
327: /**
328: * This method is called from erros-only checkbox
329: *
330: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
331: */
332: public void actionPerformed(ActionEvent e) {
333: if (errorsOnly.isSelected()) {
334: warningThresholdField.setEnabled(false);
335: warningThresholdField.setEditable(false);
336: } else {
337: warningThresholdField.setEnabled(true);
338: warningThresholdField.setEditable(true);
339: }
340: }
341:
342: public void keyPressed(KeyEvent e) {
343: }
344:
345: public void keyReleased(KeyEvent e) {
346: String fieldName = e.getComponent().getName();
347:
348: if (fieldName.equals(WARNING_THRESHOLD_FIELD)) {
349: validateInteger(warningThresholdField);
350: }
351:
352: if (fieldName.equals(ERROR_THRESHOLD_FIELD)) {
353: validateInteger(errorThresholdField);
354: }
355: }
356:
357: private void validateInteger(JTextField field) {
358: try {
359: Integer.parseInt(field.getText());
360: } catch (NumberFormatException nfe) {
361: int length = field.getText().length();
362: if (length > 0) {
363: JOptionPane.showMessageDialog(this ,
364: "Only digits allowed", "Invalid data",
365: JOptionPane.WARNING_MESSAGE);
366: // Drop the last character:
367: field.setText(field.getText().substring(0, length - 1));
368: }
369: }
370:
371: }
372:
373: public void keyTyped(KeyEvent e) {
374: }
375:
376: }
|