KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/assertions/gui/HTMLAssertionGui.java,v 1.1.2.2 2005/03/13 15:21:16 sebb Exp $
2
/*
3  * Copyright 2003-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.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.FocusEvent JavaDoc;
25 import java.awt.event.FocusListener JavaDoc;
26
27 import javax.swing.BorderFactory JavaDoc;
28 import javax.swing.ButtonGroup JavaDoc;
29 import javax.swing.JCheckBox JavaDoc;
30 import javax.swing.JComboBox JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JOptionPane JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JRadioButton JavaDoc;
35 import javax.swing.JTextField JavaDoc;
36 import javax.swing.event.ChangeEvent JavaDoc;
37 import javax.swing.event.ChangeListener JavaDoc;
38
39 import org.apache.jmeter.assertions.HTMLAssertion;
40 import org.apache.jmeter.gui.util.FilePanel;
41 import org.apache.jmeter.gui.util.HorizontalPanel;
42 import org.apache.jmeter.gui.util.VerticalPanel;
43 import org.apache.jmeter.testelement.TestElement;
44 import org.apache.jmeter.util.JMeterUtils;
45 import org.apache.jorphan.logging.LoggingManager;
46 import org.apache.log.Logger;
47
48 /**
49  * GUI for HTMLAssertion
50  */

51 public class HTMLAssertionGui extends AbstractAssertionGui implements FocusListener JavaDoc, ActionListener JavaDoc, ChangeListener JavaDoc {
52
53   //instance attributes
54
private JTextField JavaDoc errorThresholdField = null;
55   private JTextField JavaDoc warningThresholdField = null;
56   private JCheckBox JavaDoc errorsOnly = null;
57   private JComboBox JavaDoc docTypeBox = null;
58   private JRadioButton JavaDoc htmlRadioButton = null;
59   private JRadioButton JavaDoc xhtmlRadioButton = null;
60   private JRadioButton JavaDoc xmlRadioButton = null;
61   private FilePanel filePanel = null;
62
63   //class attributes
64
transient private static Logger log = LoggingManager.getLoggerForClass();
65
66   /**
67    * The constructor.
68    */

69   public HTMLAssertionGui() {
70     init();
71   }
72
73   public String JavaDoc getLabelResource()
74   {
75       return "html_assertion_label"; //$NON-NLS-1$
76
}
77
78   /**
79    * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
80    */

81   public TestElement createTestElement() {
82     HTMLAssertion el = new HTMLAssertion();
83     modifyTestElement(el);
84     return el;
85   }
86
87   /**
88    * Modifies a given TestElement to mirror the data in the gui components.
89    *
90    * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
91    */

92   public void modifyTestElement(TestElement inElement) {
93
94     log.debug("HTMLAssertionGui.modifyTestElement() called");
95
96     configureTestElement(inElement);
97
98     String JavaDoc errorThresholdString = errorThresholdField.getText();
99     long errorThreshold = 0;
100
101     try {
102       errorThreshold = Long.parseLong(errorThresholdString);
103     } catch (NumberFormatException JavaDoc e) {
104       errorThreshold = 0;
105     }
106     ((HTMLAssertion) inElement).setErrorThreshold(errorThreshold);
107
108     String JavaDoc warningThresholdString = warningThresholdField.getText();
109     long warningThreshold = 0;
110     try {
111       warningThreshold = Long.parseLong(warningThresholdString);
112     } catch (NumberFormatException JavaDoc e) {
113       warningThreshold = 0;
114     }
115     ((HTMLAssertion) inElement).setWarningThreshold(warningThreshold);
116
117     String JavaDoc docTypeString = docTypeBox.getSelectedItem().toString();
118     ((HTMLAssertion) inElement).setDoctype(docTypeString);
119
120     boolean trackErrorsOnly = errorsOnly.isSelected();
121     ((HTMLAssertion) inElement).setErrorsOnly(trackErrorsOnly);
122
123     if (htmlRadioButton.isSelected()) {
124       ((HTMLAssertion) inElement).setHTML();
125     } else if (xhtmlRadioButton.isSelected()) {
126       ((HTMLAssertion) inElement).setXHTML();
127     } else {
128       ((HTMLAssertion) inElement).setXML();
129     }
130     ((HTMLAssertion) inElement).setFilename(filePanel.getFilename());
131   }
132
133   /**
134    * Configures the associated test element.
135    * @param inElement
136    */

137   public void configure(TestElement inElement) {
138     super.configure(inElement);
139     HTMLAssertion lAssertion = (HTMLAssertion) inElement;
140     errorThresholdField.setText(String.valueOf(lAssertion.getErrorThreshold()));
141     warningThresholdField.setText(String.valueOf(lAssertion.getWarningThreshold()));
142     errorsOnly.setSelected(lAssertion.isErrorsOnly());
143     docTypeBox.setSelectedItem(lAssertion.getDoctype());
144     if (lAssertion.isHTML()) {
145       htmlRadioButton.setSelected(true);
146     } else if (lAssertion.isXHTML()) {
147       xhtmlRadioButton.setSelected(true);
148     } else {
149       xmlRadioButton.setSelected(true);
150     }
151     if (lAssertion.isErrorsOnly()) {
152       warningThresholdField.setEnabled(false);
153       warningThresholdField.setEditable(false);
154     }
155     filePanel.setFilename(lAssertion.getFilename());
156   }
157
158   /**
159    * Inits the GUI.
160    */

161   private void init() {
162
163     setLayout(new BorderLayout JavaDoc(0, 10));
164     setBorder(makeBorder());
165
166     add(makeTitlePanel(), BorderLayout.NORTH);
167
168     JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
169
170     // USER_INPUT
171
VerticalPanel assertionPanel = new VerticalPanel();
172     assertionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tidy Settings"));
173
174     //doctype
175
HorizontalPanel docTypePanel = new HorizontalPanel();
176     docTypeBox = new JComboBox JavaDoc(new Object JavaDoc[] { "omit", "auto", "strict", "loose" });
177     docTypeBox.addFocusListener(this);
178     //docTypePanel.add(new JLabel(JMeterUtils.getResString("duration_assertion_label")));
179
docTypePanel.add(new JLabel JavaDoc("Doctype:"));
180     docTypePanel.add(docTypeBox);
181     assertionPanel.add(docTypePanel);
182
183     //format (HMTL, XHTML, XML)
184
VerticalPanel formatPanel = new VerticalPanel();
185     formatPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Format"));
186     htmlRadioButton = new JRadioButton JavaDoc("HTML", true);
187     xhtmlRadioButton = new JRadioButton JavaDoc("XHTML", false);
188     xmlRadioButton = new JRadioButton JavaDoc("XML", false);
189     ButtonGroup JavaDoc buttonGroup = new ButtonGroup JavaDoc();
190     buttonGroup.add(htmlRadioButton);
191     buttonGroup.add(xhtmlRadioButton);
192     buttonGroup.add(xmlRadioButton);
193     formatPanel.add(htmlRadioButton);
194     formatPanel.add(xhtmlRadioButton);
195     formatPanel.add(xmlRadioButton);
196     assertionPanel.add(formatPanel);
197
198     //errors only
199
errorsOnly = new JCheckBox JavaDoc("Errors only", false);
200     errorsOnly.addFocusListener(this);
201     errorsOnly.addActionListener(this);
202     assertionPanel.add(errorsOnly);
203
204     //thresholds
205
HorizontalPanel thresholdPanel = new HorizontalPanel();
206     thresholdPanel.add(new JLabel JavaDoc("Error threshold:"));
207     errorThresholdField = new JTextField JavaDoc("0", 5);
208     errorThresholdField.addFocusListener(this);
209     thresholdPanel.add(errorThresholdField);
210     thresholdPanel.add(new JLabel JavaDoc("Warning threshold:"));
211     warningThresholdField = new JTextField JavaDoc("0", 5);
212     warningThresholdField.addFocusListener(this);
213     thresholdPanel.add(warningThresholdField);
214     assertionPanel.add(thresholdPanel);
215
216     //file panel
217
filePanel = new FilePanel(JMeterUtils.getResString("file_visualizer_output_file"), ".txt");
218     filePanel.addChangeListener(this);
219     assertionPanel.add(filePanel);
220
221     mainPanel.add(assertionPanel, BorderLayout.NORTH);
222     add(mainPanel, BorderLayout.CENTER);
223   }
224
225   /**
226    * This method is called if one of the threshold field looses the focus
227    * @param inEvent
228    */

229   public void focusLost(FocusEvent JavaDoc inEvent) {
230     log.debug("HTMLAssertionGui.focusLost() called");
231
232     String JavaDoc errorThresholdString = errorThresholdField.getText();
233     if (errorThresholdString != null) {
234       boolean isInvalid = false;
235       try {
236         long errorThreshold = Long.parseLong(errorThresholdString);
237         if (errorThreshold < 0) {
238           isInvalid = true;
239         }
240       } catch (NumberFormatException JavaDoc ex) {
241         isInvalid = true;
242       }
243       if (isInvalid) {
244         log.warn("HTMLAssertionGui: Error threshold Not a valid number!");
245         JOptionPane.showMessageDialog(null, "Threshold for errors is invalid", "Error", JOptionPane.ERROR_MESSAGE);
246       }
247     }
248
249     String JavaDoc warningThresholdString = warningThresholdField.getText();
250     if (warningThresholdString != null) {
251       boolean isInvalid = false;
252       try {
253         long warningThreshold = Long.parseLong(warningThresholdString);
254         if (warningThreshold < 0) {
255           isInvalid = true;
256         }
257       } catch (NumberFormatException JavaDoc ex) {
258         isInvalid = true;
259       }
260       if (isInvalid) {
261         log.warn("HTMLAssertionGui: Error threshold Not a valid number!");
262         JOptionPane.showMessageDialog(null, "Threshold for warnings is invalid", "Error", JOptionPane.ERROR_MESSAGE);
263       }
264     }
265   }
266
267   /**
268    * @see java.awt.event.FocusListener#focusGained(java.awt.event.FocusEvent)
269    */

270   public void focusGained(FocusEvent JavaDoc e) {
271
272   }
273
274   /**
275    * This method is called from erros-only checkbox
276    * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
277    */

278   public void actionPerformed(ActionEvent JavaDoc e) {
279     if (errorsOnly.isSelected()) {
280       warningThresholdField.setEnabled(false);
281       warningThresholdField.setEditable(false);
282     } else {
283       warningThresholdField.setEnabled(true);
284       warningThresholdField.setEditable(true);
285     }
286   }
287
288   /** This method is called on change of output file input
289    * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
290    */

291   public void stateChanged(ChangeEvent JavaDoc e) {
292     log.debug("HTMLAssertionGui.stateChanged() called");
293   }
294
295 }
296
Popular Tags