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 javax.swing.BorderFactory;
022: import javax.swing.Box;
023: import javax.swing.JComponent;
024: import javax.swing.JLabel;
025: import javax.swing.JOptionPane;
026: import javax.swing.JPanel;
027: import javax.swing.JTextField;
028:
029: import org.apache.jmeter.gui.util.FocusRequester;
030: import org.apache.jmeter.testelement.TestElement;
031: import org.apache.jmeter.timers.RandomTimer;
032: import org.apache.jmeter.timers.UniformRandomTimer;
033: import org.apache.jmeter.util.JMeterUtils;
034: import org.apache.jorphan.gui.layout.VerticalLayout;
035:
036: /**
037: * Implementation of a uniform random timer.
038: *
039: */
040: public class UniformRandomTimerGui extends AbstractTimerGui {
041:
042: // TODO - are these gui item names needed?
043: private static final String DELAY_FIELD = "Delay Field";
044:
045: private static final String RANGE_FIELD = "Range Field";
046:
047: private static final String DEFAULT_DELAY = "0"; // $NON-NLS-1$
048:
049: private static final String DEFAULT_RANGE = "100.0";// $NON-NLS-1$
050:
051: private JTextField delayField;
052:
053: private JTextField rangeField;
054:
055: /**
056: * No-arg constructor.
057: */
058: public UniformRandomTimerGui() {
059: init();
060: }
061:
062: /**
063: * Handle an error.
064: *
065: * @param e
066: * the Exception that was thrown.
067: * @param thrower
068: * the JComponent that threw the Exception.
069: */
070: public static void error(Exception e, JComponent thrower) {
071: JOptionPane.showMessageDialog(thrower, e, "Error",
072: JOptionPane.ERROR_MESSAGE);
073: }
074:
075: public String getLabelResource() {
076: return "uniform_timer_title"; //$NON-NLS-1$
077: }
078:
079: /**
080: * Create the test element underlying this GUI component.
081: *
082: * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
083: */
084: public TestElement createTestElement() {
085: RandomTimer timer = new UniformRandomTimer();
086: modifyTestElement(timer);
087: return timer;
088: }
089:
090: /**
091: * Modifies a given TestElement to mirror the data in the gui components.
092: *
093: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
094: */
095: public void modifyTestElement(TestElement timer) {
096: this .configureTestElement(timer);
097: ((RandomTimer) timer).setDelay(delayField.getText());
098: ((RandomTimer) timer).setRange(rangeField.getText());
099: }
100:
101: /**
102: * Configure this GUI component from the underlying TestElement.
103: *
104: * @see org.apache.jmeter.gui.JMeterGUIComponent#configure(TestElement)
105: */
106: public void configure(TestElement el) {
107: super .configure(el);
108: delayField.setText(el.getPropertyAsString(RandomTimer.DELAY));
109: rangeField.setText(el.getPropertyAsString(RandomTimer.RANGE));
110: }
111:
112: /**
113: * Initialize this component.
114: */
115: private void init() {
116: setLayout(new VerticalLayout(5, VerticalLayout.BOTH));
117: setBorder(makeBorder());
118:
119: add(makeTitlePanel());
120:
121: // THREAD DELAY PROPERTIES
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("uniform_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("uniform_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:
155: add(threadDelayPropsPanel);
156:
157: // Set the initial focus to the range field
158: new FocusRequester(rangeField);
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see org.apache.jmeter.gui.JMeterGUIComponent#clearGui()
165: */
166: public void clearGui() {
167: rangeField.setText(DEFAULT_RANGE);
168: delayField.setText(DEFAULT_DELAY);
169: super.clearGui();
170: }
171: }
|