1 18 19 package org.apache.jmeter.assertions.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.ActionEvent ; 23 import java.awt.event.ActionListener ; 24 import java.awt.event.FocusEvent ; 25 import java.awt.event.FocusListener ; 26 27 import javax.swing.BorderFactory ; 28 import javax.swing.ButtonGroup ; 29 import javax.swing.JCheckBox ; 30 import javax.swing.JComboBox ; 31 import javax.swing.JLabel ; 32 import javax.swing.JOptionPane ; 33 import javax.swing.JPanel ; 34 import javax.swing.JRadioButton ; 35 import javax.swing.JTextField ; 36 import javax.swing.event.ChangeEvent ; 37 import javax.swing.event.ChangeListener ; 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 51 public class HTMLAssertionGui extends AbstractAssertionGui implements FocusListener , ActionListener , ChangeListener { 52 53 private JTextField errorThresholdField = null; 55 private JTextField warningThresholdField = null; 56 private JCheckBox errorsOnly = null; 57 private JComboBox docTypeBox = null; 58 private JRadioButton htmlRadioButton = null; 59 private JRadioButton xhtmlRadioButton = null; 60 private JRadioButton xmlRadioButton = null; 61 private FilePanel filePanel = null; 62 63 transient private static Logger log = LoggingManager.getLoggerForClass(); 65 66 69 public HTMLAssertionGui() { 70 init(); 71 } 72 73 public String getLabelResource() 74 { 75 return "html_assertion_label"; } 77 78 81 public TestElement createTestElement() { 82 HTMLAssertion el = new HTMLAssertion(); 83 modifyTestElement(el); 84 return el; 85 } 86 87 92 public void modifyTestElement(TestElement inElement) { 93 94 log.debug("HTMLAssertionGui.modifyTestElement() called"); 95 96 configureTestElement(inElement); 97 98 String errorThresholdString = errorThresholdField.getText(); 99 long errorThreshold = 0; 100 101 try { 102 errorThreshold = Long.parseLong(errorThresholdString); 103 } catch (NumberFormatException e) { 104 errorThreshold = 0; 105 } 106 ((HTMLAssertion) inElement).setErrorThreshold(errorThreshold); 107 108 String warningThresholdString = warningThresholdField.getText(); 109 long warningThreshold = 0; 110 try { 111 warningThreshold = Long.parseLong(warningThresholdString); 112 } catch (NumberFormatException e) { 113 warningThreshold = 0; 114 } 115 ((HTMLAssertion) inElement).setWarningThreshold(warningThreshold); 116 117 String 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 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 161 private void init() { 162 163 setLayout(new BorderLayout (0, 10)); 164 setBorder(makeBorder()); 165 166 add(makeTitlePanel(), BorderLayout.NORTH); 167 168 JPanel mainPanel = new JPanel (new BorderLayout ()); 169 170 VerticalPanel assertionPanel = new VerticalPanel(); 172 assertionPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Tidy Settings")); 173 174 HorizontalPanel docTypePanel = new HorizontalPanel(); 176 docTypeBox = new JComboBox (new Object [] { "omit", "auto", "strict", "loose" }); 177 docTypeBox.addFocusListener(this); 178 docTypePanel.add(new JLabel ("Doctype:")); 180 docTypePanel.add(docTypeBox); 181 assertionPanel.add(docTypePanel); 182 183 VerticalPanel formatPanel = new VerticalPanel(); 185 formatPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Format")); 186 htmlRadioButton = new JRadioButton ("HTML", true); 187 xhtmlRadioButton = new JRadioButton ("XHTML", false); 188 xmlRadioButton = new JRadioButton ("XML", false); 189 ButtonGroup buttonGroup = new ButtonGroup (); 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 errorsOnly = new JCheckBox ("Errors only", false); 200 errorsOnly.addFocusListener(this); 201 errorsOnly.addActionListener(this); 202 assertionPanel.add(errorsOnly); 203 204 HorizontalPanel thresholdPanel = new HorizontalPanel(); 206 thresholdPanel.add(new JLabel ("Error threshold:")); 207 errorThresholdField = new JTextField ("0", 5); 208 errorThresholdField.addFocusListener(this); 209 thresholdPanel.add(errorThresholdField); 210 thresholdPanel.add(new JLabel ("Warning threshold:")); 211 warningThresholdField = new JTextField ("0", 5); 212 warningThresholdField.addFocusListener(this); 213 thresholdPanel.add(warningThresholdField); 214 assertionPanel.add(thresholdPanel); 215 216 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 229 public void focusLost(FocusEvent inEvent) { 230 log.debug("HTMLAssertionGui.focusLost() called"); 231 232 String 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 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 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 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 270 public void focusGained(FocusEvent e) { 271 272 } 273 274 278 public void actionPerformed(ActionEvent 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 291 public void stateChanged(ChangeEvent e) { 292 log.debug("HTMLAssertionGui.stateChanged() called"); 293 } 294 295 } 296 | Popular Tags |