1 18 19 package org.apache.jmeter.assertions.gui; 20 21 import java.awt.BorderLayout ; 22 import java.awt.event.FocusEvent ; 23 import java.awt.event.FocusListener ; 24 25 import javax.swing.BorderFactory ; 26 import javax.swing.JLabel ; 27 import javax.swing.JOptionPane ; 28 import javax.swing.JPanel ; 29 import javax.swing.JTextField ; 30 31 import org.apache.jmeter.assertions.DurationAssertion; 32 import org.apache.jmeter.gui.util.HorizontalPanel; 33 import org.apache.jmeter.testelement.TestElement; 34 import org.apache.jmeter.util.JMeterUtils; 35 import org.apache.jorphan.logging.LoggingManager; 36 import org.apache.log.Logger; 37 38 41 public class DurationAssertionGui 42 extends AbstractAssertionGui 43 implements FocusListener 44 { 45 transient private static Logger log = LoggingManager.getLoggerForClass(); 46 47 private JTextField duration; 48 49 public DurationAssertionGui() 50 { 51 init(); 52 } 53 54 public String getLabelResource() 55 { 56 return "duration_assertion_title"; 57 } 58 59 public String getDurationAttributesTitle() 60 { 61 return JMeterUtils.getResString("duration_assertion_duration_test"); 62 } 63 64 public TestElement createTestElement() 65 { 66 DurationAssertion el = new DurationAssertion(); 67 modifyTestElement(el); 68 return el; 69 } 70 71 75 public void modifyTestElement(TestElement el) 76 { 77 configureTestElement(el); 78 String durationString = duration.getText(); 79 long assertionDuration = 0; 80 try 81 { 82 assertionDuration = Long.parseLong(durationString); 83 } 84 catch (NumberFormatException e) 85 { 86 assertionDuration = Long.MAX_VALUE; 87 } 88 ((DurationAssertion) el).setAllowedDuration(assertionDuration); 89 } 90 91 public void configure(TestElement el) 92 { 93 super.configure(el); 94 DurationAssertion assertion = (DurationAssertion) el; 95 duration.setText(String.valueOf(assertion.getAllowedDuration())); 96 } 97 98 private void init() 99 { 100 setLayout(new BorderLayout (0, 10)); 101 setBorder(makeBorder()); 102 103 add(makeTitlePanel(), BorderLayout.NORTH); 104 105 JPanel mainPanel = new JPanel (new BorderLayout ()); 106 107 HorizontalPanel durationPanel = new HorizontalPanel(); 109 durationPanel.setBorder( 110 BorderFactory.createTitledBorder( 111 BorderFactory.createEtchedBorder(), 112 getDurationAttributesTitle())); 113 114 durationPanel.add( 115 new JLabel (JMeterUtils.getResString("duration_assertion_label"))); 116 117 duration = new JTextField (5); 118 duration.addFocusListener(this); 119 durationPanel.add(duration); 120 121 mainPanel.add(durationPanel, BorderLayout.NORTH); 122 add(mainPanel, BorderLayout.CENTER); 123 } 124 125 public void focusLost(FocusEvent e) 126 { 127 boolean isInvalid = false; 128 String durationString = duration.getText(); 129 if (durationString != null) 130 { 131 try 132 { 133 long assertionDuration = Long.parseLong(durationString); 134 if (assertionDuration < 0) 135 { 136 isInvalid = true; 137 } 138 } 139 catch (NumberFormatException ex) 140 { 141 isInvalid = true; 142 } 143 if (isInvalid) 144 { 145 log.warn("DurationAssertionGui: Not a valid number!"); 146 JOptionPane.showMessageDialog( 147 null, 148 JMeterUtils.getResString("duration_assertion_input_error"), 149 "Error", 150 JOptionPane.ERROR_MESSAGE); 151 } 152 } 153 } 154 155 public void focusGained(FocusEvent e) 156 { 157 } 158 } 159 | Popular Tags |