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.timers.gui;
020:
021: import java.awt.Dimension;
022:
023: import javax.swing.BorderFactory;
024: import javax.swing.Box;
025: import javax.swing.JComponent;
026: import javax.swing.JLabel;
027: import javax.swing.JOptionPane;
028: import javax.swing.JPanel;
029: import javax.swing.JTextField;
030:
031: import org.apache.jmeter.gui.util.FocusRequester;
032: import org.apache.jmeter.testelement.TestElement;
033: import org.apache.jmeter.timers.GaussianRandomTimer;
034: import org.apache.jmeter.timers.RandomTimer;
035: import org.apache.jmeter.util.JMeterUtils;
036: import org.apache.jorphan.gui.layout.VerticalLayout;
037:
038: /**
039: * Implementation of a gaussian random timer.
040: *
041: */
042: public class GaussianRandomTimerGui extends AbstractTimerGui {
043:
044: private static final String DELAY_FIELD = "Delay Field";
045:
046: private static final String RANGE_FIELD = "Range Field";
047:
048: private static final String DEFAULT_DELAY = "300"; // $NON-NLS-1$
049:
050: private static final String DEFAULT_RANGE = "100.0"; // $NON-NLS-1$
051:
052: private JTextField delayField;
053:
054: private JTextField rangeField;
055:
056: /**
057: * No-arg constructor.
058: */
059: public GaussianRandomTimerGui() {
060: init();
061: }
062:
063: /**
064: * Handle an error.
065: *
066: * @param e
067: * the Exception that was thrown.
068: * @param thrower
069: * the JComponent that threw the Exception.
070: */
071: public static void error(Exception e, JComponent thrower) {
072: JOptionPane.showMessageDialog(thrower, e, "Error",
073: JOptionPane.ERROR_MESSAGE);
074: }
075:
076: /**
077: * Create the test element underlying this GUI component.
078: *
079: * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
080: */
081: public TestElement createTestElement() {
082: RandomTimer timer = new GaussianRandomTimer();
083: modifyTestElement(timer);
084: return timer;
085: }
086:
087: /**
088: * Modifies a given TestElement to mirror the data in the gui components.
089: *
090: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
091: */
092: public void modifyTestElement(TestElement timer) {
093: this .configureTestElement(timer);
094: ((RandomTimer) timer).setDelay(delayField.getText());
095: ((RandomTimer) timer).setRange(rangeField.getText());
096: }
097:
098: /**
099: * Configure this GUI component from the underlying TestElement.
100: *
101: * @see org.apache.jmeter.gui.JMeterGUIComponent#configure(TestElement)
102: */
103: public void configure(TestElement el) {
104: super .configure(el);
105: delayField.setText(el.getPropertyAsString(RandomTimer.DELAY));
106: rangeField.setText(el.getPropertyAsString(RandomTimer.RANGE));
107: }
108:
109: public String getLabelResource() {
110: return "gaussian_timer_title";//$NON-NLS-1$
111: }
112:
113: /**
114: * Initialize this component.
115: */
116: private void init() {
117: setLayout(new VerticalLayout(5, VerticalLayout.BOTH));
118: setBorder(makeBorder());
119:
120: add(makeTitlePanel());
121:
122: JPanel threadDelayPropsPanel = new JPanel();
123: threadDelayPropsPanel.setLayout(new VerticalLayout(5,
124: VerticalLayout.LEFT));
125: threadDelayPropsPanel.setBorder(BorderFactory
126: .createTitledBorder(JMeterUtils
127: .getResString("thread_delay_properties")));//$NON-NLS-1$
128:
129: // DELAY DEVIATION
130: Box delayDevPanel = Box.createHorizontalBox();
131: delayDevPanel.add(new JLabel(JMeterUtils
132: .getResString("gaussian_timer_range")));//$NON-NLS-1$
133: delayDevPanel.add(Box.createHorizontalStrut(5));
134:
135: rangeField = new JTextField(6);
136: rangeField.setText(DEFAULT_RANGE);
137: rangeField.setName(RANGE_FIELD);
138: delayDevPanel.add(rangeField);
139:
140: threadDelayPropsPanel.add(delayDevPanel);
141:
142: // AVG DELAY
143: Box avgDelayPanel = Box.createHorizontalBox();
144: avgDelayPanel.add(new JLabel(JMeterUtils
145: .getResString("gaussian_timer_delay")));//$NON-NLS-1$
146: avgDelayPanel.add(Box.createHorizontalStrut(5));
147:
148: delayField = new JTextField(6);
149: delayField.setText(DEFAULT_DELAY);
150: delayField.setName(DELAY_FIELD);
151: avgDelayPanel.add(delayField);
152:
153: threadDelayPropsPanel.add(avgDelayPanel);
154: threadDelayPropsPanel.setMaximumSize(new Dimension(
155: threadDelayPropsPanel.getMaximumSize().width,
156: threadDelayPropsPanel.getPreferredSize().height));
157: add(threadDelayPropsPanel);
158:
159: // Set the initial focus to the delay field
160: new FocusRequester(rangeField);
161: }
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see org.apache.jmeter.gui.JMeterGUIComponent#clearGui()
167: */
168: public void clearGui() {
169: rangeField.setText(DEFAULT_RANGE);
170: delayField.setText(DEFAULT_DELAY);
171: super.clearGui();
172: }
173: }
|