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:
023: import javax.swing.JCheckBox;
024: import javax.swing.JLabel;
025: import javax.swing.JPanel;
026: import javax.swing.JTextField;
027:
028: import org.apache.jmeter.control.ForeachController;
029: import org.apache.jmeter.gui.util.VerticalPanel;
030: import org.apache.jmeter.testelement.TestElement;
031: import org.apache.jmeter.util.JMeterUtils;
032:
033: /**
034: * The user interface for a foreach controller which specifies that its
035: * subcomponents should be executed some number of times in a loop. This
036: * component can be used standalone or embedded into some other component.
037: */
038:
039: public class ForeachControlPanel extends AbstractControllerGui {
040:
041: /**
042: * A field allowing the user to specify the input variable the controller
043: * should loop.
044: */
045: private JTextField inputVal;
046:
047: /**
048: * A field allowing the user to specify output variable the controller
049: * should return.
050: */
051: private JTextField returnVal;
052:
053: // Should we add the "_" separator?
054: private JCheckBox useSeparator;
055:
056: /**
057: * Boolean indicating whether or not this component should display its name.
058: * If true, this is a standalone component. If false, this component is
059: * intended to be used as a subpanel for another component.
060: */
061: private boolean displayName = true;
062:
063: /** The name of the infinite checkbox component. */
064: private static final String INPUTVAL = "Input Field"; // $NON-NLS-1$
065:
066: /** The name of the loops field component. */
067: private static final String RETURNVAL = "Return Field"; // $NON-NLS-1$
068:
069: /**
070: * Create a new LoopControlPanel as a standalone component.
071: */
072: public ForeachControlPanel() {
073: this (true);
074: }
075:
076: /**
077: * Create a new LoopControlPanel as either a standalone or an embedded
078: * component.
079: *
080: * @param displayName
081: * indicates whether or not this component should display its
082: * name. If true, this is a standalone component. If false, this
083: * component is intended to be used as a subpanel for another
084: * component.
085: */
086: public ForeachControlPanel(boolean displayName) {
087: this .displayName = displayName;
088: init();
089: }
090:
091: /**
092: * A newly created component can be initialized with the contents of a Test
093: * Element object by calling this method. The component is responsible for
094: * querying the Test Element object for the relevant information to display
095: * in its GUI.
096: *
097: * @param element
098: * the TestElement to configure
099: */
100: public void configure(TestElement element) {
101: super .configure(element);
102: inputVal.setText(((ForeachController) element)
103: .getInputValString());
104: returnVal.setText(((ForeachController) element)
105: .getReturnValString());
106: useSeparator.setSelected(((ForeachController) element)
107: .getUseSeparator());
108: }
109:
110: /* Implements JMeterGUIComponent.createTestElement() */
111: public TestElement createTestElement() {
112: ForeachController lc = new ForeachController();
113: modifyTestElement(lc);
114: return lc;
115: }
116:
117: /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
118: public void modifyTestElement(TestElement lc) {
119: configureTestElement(lc);
120: if (lc instanceof ForeachController) {
121: if (inputVal.getText().length() > 0) {
122: ((ForeachController) lc)
123: .setInputVal(inputVal.getText());
124: } else {
125: ((ForeachController) lc).setInputVal(""); // $NON-NLS-1$
126: }
127: if (returnVal.getText().length() > 0) {
128: ((ForeachController) lc).setReturnVal(returnVal
129: .getText());
130: } else {
131: ((ForeachController) lc).setReturnVal(""); // $NON-NLS-1$
132: }
133: ((ForeachController) lc).setUseSeparator(useSeparator
134: .isSelected());
135: }
136: }
137:
138: /**
139: * Implements JMeterGUIComponent.clearGui
140: */
141: public void clearGui() {
142: super .clearGui();
143:
144: inputVal.setText(""); // $NON-NLS-1$
145: returnVal.setText(""); // $NON-NLS-1$
146: useSeparator.setSelected(true);
147: }
148:
149: public String getLabelResource() {
150: return "foreach_controller_title"; // $NON-NLS-1$
151: }
152:
153: /**
154: * Initialize the GUI components and layout for this component.
155: */
156: private void init() {
157: // The Loop Controller panel can be displayed standalone or inside
158: // another panel. For standalone, we want to display the TITLE, NAME,
159: // etc. (everything). However, if we want to display it within another
160: // panel, we just display the Loop Count fields (not the TITLE and
161: // NAME).
162:
163: // Standalone
164: if (displayName) {
165: setLayout(new BorderLayout(0, 5));
166: setBorder(makeBorder());
167: add(makeTitlePanel(), BorderLayout.NORTH);
168:
169: JPanel mainPanel = new JPanel(new BorderLayout());
170: mainPanel.add(createLoopCountPanel(), BorderLayout.NORTH);
171: add(mainPanel, BorderLayout.CENTER);
172: } else {
173: // Embedded
174: setLayout(new BorderLayout());
175: add(createLoopCountPanel(), BorderLayout.NORTH);
176: }
177: }
178:
179: /**
180: * Create a GUI panel containing the components related to the number of
181: * loops which should be executed.
182: *
183: * @return a GUI panel containing the loop count components
184: */
185: private JPanel createLoopCountPanel() {
186: // JPanel loopPanel = new JPanel(new BorderLayout(5, 0));
187: VerticalPanel loopPanel = new VerticalPanel();
188:
189: // LOOP LABEL
190: JLabel inputValLabel = new JLabel(JMeterUtils
191: .getResString("foreach_input")); // $NON-NLS-1$
192: JLabel returnValLabel = new JLabel(JMeterUtils
193: .getResString("foreach_output")); // $NON-NLS-1$
194:
195: // TEXT FIELD
196: JPanel inputValSubPanel = new JPanel(new BorderLayout(5, 0));
197: inputVal = new JTextField("", 5); // $NON-NLS-1$
198: inputVal.setName(INPUTVAL);
199: inputValLabel.setLabelFor(inputVal);
200: inputValSubPanel.add(inputValLabel, BorderLayout.WEST);
201: inputValSubPanel.add(inputVal, BorderLayout.CENTER);
202:
203: // TEXT FIELD
204: JPanel returnValSubPanel = new JPanel(new BorderLayout(5, 0));
205: returnVal = new JTextField("", 5); // $NON-NLS-1$
206: returnVal.setName(RETURNVAL);
207: returnValLabel.setLabelFor(returnVal);
208: returnValSubPanel.add(returnValLabel, BorderLayout.WEST);
209: returnValSubPanel.add(returnVal, BorderLayout.CENTER);
210:
211: // Checkbox
212: useSeparator = new JCheckBox(JMeterUtils
213: .getResString("foreach_use_separator"), true); // $NON-NLS-1$
214:
215: loopPanel.add(inputValSubPanel);
216: loopPanel.add(returnValSubPanel);
217: loopPanel.add(useSeparator);
218:
219: return loopPanel;
220: }
221: }
|