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.control.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024:
025: import javax.swing.Box;
026: import javax.swing.JLabel;
027: import javax.swing.JPanel;
028: import javax.swing.JTextField;
029:
030: import org.apache.jmeter.control.RunTime;
031: import org.apache.jmeter.testelement.TestElement;
032: import org.apache.jmeter.util.JMeterUtils;
033:
034: /**
035: * The user interface for a controller which specifies that its subcomponents
036: * should be executed some number of seconds in a loop. This component can be
037: * used standalone or embedded into some other component.
038: *
039: */
040:
041: public class RunTimeGui extends AbstractControllerGui implements
042: ActionListener {
043: /**
044: * A field allowing the user to specify the number of seconds the controller
045: * should loop.
046: */
047: private JTextField seconds;
048:
049: /**
050: * Boolean indicating whether or not this component should display its name.
051: * If true, this is a standalone component. If false, this component is
052: * intended to be used as a subpanel for another component.
053: */
054: private boolean displayName = true;
055:
056: /**
057: * Create a new LoopControlPanel as a standalone component.
058: */
059: public RunTimeGui() {
060: this (true);
061: }
062:
063: /**
064: * Create a new LoopControlPanel as either a standalone or an embedded
065: * component.
066: *
067: * @param displayName
068: * indicates whether or not this component should display its
069: * name. If true, this is a standalone component. If false, this
070: * component is intended to be used as a subpanel for another
071: * component.
072: */
073: public RunTimeGui(boolean displayName) {
074: this .displayName = displayName;
075: init();
076: setState(1);
077: }
078:
079: /**
080: * A newly created component can be initialized with the contents of a Test
081: * Element object by calling this method. The component is responsible for
082: * querying the Test Element object for the relevant information to display
083: * in its GUI.
084: *
085: * @param element
086: * the TestElement to configure
087: */
088: public void configure(TestElement element) {
089: super .configure(element);
090: if (element instanceof RunTime) {
091: setState(((RunTime) element).getRuntimeString());
092: } else {
093: setState(1);
094: }
095: }
096:
097: /* Implements JMeterGUIComponent.createTestElement() */
098: public TestElement createTestElement() {
099: RunTime lc = new RunTime();
100: modifyTestElement(lc);
101: return lc;
102: }
103:
104: /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
105: public void modifyTestElement(TestElement lc) {
106: configureTestElement(lc);
107: if (lc instanceof RunTime) {
108: if (seconds.getText().length() > 0) {
109: ((RunTime) lc).setRuntime(seconds.getText());
110: } else {
111: ((RunTime) lc).setRuntime(0);
112: }
113: }
114: }
115:
116: /**
117: * Implements JMeterGUIComponent.clearGui
118: */
119: public void clearGui() {
120: super .clearGui();
121:
122: seconds.setText("1"); // $NON-NLS-1$
123: }
124:
125: /**
126: * Invoked when an action occurs. This implementation assumes that the
127: * target component is the infinite seconds checkbox.
128: *
129: * @param event
130: * the event that has occurred
131: */
132: public void actionPerformed(ActionEvent event) {
133: seconds.setEnabled(true);
134: }
135:
136: public String getLabelResource() {
137: return "runtime_controller_title"; // $NON-NLS-1$
138: }
139:
140: /**
141: * Initialize the GUI components and layout for this component.
142: */
143: private void init() {
144: // The Loop Controller panel can be displayed standalone or inside
145: // another panel. For standalone, we want to display the TITLE, NAME,
146: // etc. (everything). However, if we want to display it within another
147: // panel, we just display the Loop Count fields (not the TITLE and
148: // NAME).
149:
150: // Standalone
151: if (displayName) {
152: setLayout(new BorderLayout(0, 5));
153: setBorder(makeBorder());
154: add(makeTitlePanel(), BorderLayout.NORTH);
155:
156: JPanel mainPanel = new JPanel(new BorderLayout());
157: mainPanel.add(createLoopCountPanel(), BorderLayout.NORTH);
158: add(mainPanel, BorderLayout.CENTER);
159: } else {
160: // Embedded
161: setLayout(new BorderLayout());
162: add(createLoopCountPanel(), BorderLayout.NORTH);
163: }
164: }
165:
166: /**
167: * Create a GUI panel containing the components related to the number of
168: * seconds which should be executed.
169: *
170: * @return a GUI panel containing the loop count components
171: */
172: private JPanel createLoopCountPanel() {
173: JPanel loopPanel = new JPanel(new BorderLayout(5, 0));
174:
175: // SECONDS LABEL
176: JLabel secondsLabel = new JLabel(JMeterUtils
177: .getResString("runtime_seconds")); // $NON-NLS-1$
178: loopPanel.add(secondsLabel, BorderLayout.WEST);
179:
180: JPanel loopSubPanel = new JPanel(new BorderLayout(5, 0));
181:
182: // TEXT FIELD
183: seconds = new JTextField("1", 5); // $NON-NLS-1$
184: secondsLabel.setLabelFor(seconds);
185: loopSubPanel.add(seconds, BorderLayout.CENTER);
186:
187: loopPanel.add(loopSubPanel, BorderLayout.CENTER);
188:
189: loopPanel
190: .add(Box.createHorizontalStrut(secondsLabel
191: .getPreferredSize().width
192: + seconds.getPreferredSize().width),
193: BorderLayout.NORTH);
194:
195: return loopPanel;
196: }
197:
198: /**
199: * Set the number of seconds which should be reflected in the GUI. The
200: * secsCount parameter should contain the String representation of an
201: * integer. This integer will be treated as the number of seconds. If this
202: * integer is less than 0, the number of seconds will be assumed to be
203: * infinity.
204: *
205: * @param secsCount
206: * the String representation of the number of seconds
207: */
208: private void setState(String secsCount) {
209: seconds.setText(secsCount);
210: seconds.setEnabled(true);
211: }
212:
213: /**
214: * Set the number of seconds which should be reflected in the GUI. If the
215: * secsCount is less than 0, the number of seconds will be assumed to be
216: * infinity.
217: *
218: * @param secsCount
219: * the number of seconds
220: */
221: private void setState(long secsCount) {
222: seconds.setEnabled(true);
223: seconds.setText("" + secsCount); // $NON-NLS-1$
224: }
225: }
|