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.Container;
023: import java.util.Collection;
024:
025: import javax.swing.JCheckBox;
026: import javax.swing.JMenu;
027: import javax.swing.JPanel;
028: import javax.swing.JPopupMenu;
029: import javax.swing.JTextArea;
030:
031: import org.apache.jmeter.config.Arguments;
032: import org.apache.jmeter.config.gui.ArgumentsPanel;
033: import org.apache.jmeter.gui.AbstractJMeterGuiComponent;
034: import org.apache.jmeter.gui.action.ActionNames;
035: import org.apache.jmeter.gui.util.FileListPanel;
036: import org.apache.jmeter.gui.util.MenuFactory;
037: import org.apache.jmeter.gui.util.VerticalPanel;
038: import org.apache.jmeter.testelement.TestElement;
039: import org.apache.jmeter.testelement.TestPlan;
040: import org.apache.jmeter.testelement.property.JMeterProperty;
041: import org.apache.jmeter.threads.gui.ThreadGroupGui;
042: import org.apache.jmeter.util.JMeterUtils;
043:
044: /**
045: * JMeter GUI component representing the test plan which will be executed when
046: * the test is run.
047: *
048: */
049: public class TestPlanGui extends AbstractJMeterGuiComponent {
050:
051: /**
052: * A checkbox allowing the user to specify whether or not JMeter should do
053: * functional testing.
054: */
055: private JCheckBox functionalMode;
056:
057: private JCheckBox serializedMode;
058:
059: /** A panel allowing the user to define variables. */
060: private ArgumentsPanel argsPanel;
061:
062: FileListPanel browseJar = null;
063:
064: /**
065: * Create a new TestPlanGui.
066: */
067: public TestPlanGui() {
068: init();
069: }
070:
071: /**
072: * When a user right-clicks on the component in the test tree, or selects
073: * the edit menu when the component is selected, the component will be asked
074: * to return a JPopupMenu that provides all the options available to the
075: * user from this component.
076: * <p>
077: * The TestPlan will return a popup menu allowing you to add ThreadGroups,
078: * Listeners, Configuration Elements, Assertions, PreProcessors,
079: * PostProcessors, and Timers.
080: *
081: * @return a JPopupMenu appropriate for the component.
082: */
083: public JPopupMenu createPopupMenu() {
084: JPopupMenu pop = new JPopupMenu();
085: JMenu addMenu = new JMenu(JMeterUtils.getResString("add")); // $NON-NLS-1$
086: addMenu.add(MenuFactory.makeMenuItem(new ThreadGroupGui()
087: .getStaticLabel(), ThreadGroupGui.class.getName(),
088: ActionNames.ADD));
089: addMenu.add(MenuFactory.makeMenu(MenuFactory.CONFIG_ELEMENTS,
090: ActionNames.ADD));
091: addMenu.add(MenuFactory.makeMenu(MenuFactory.TIMERS,
092: ActionNames.ADD));
093: addMenu.add(MenuFactory.makeMenu(MenuFactory.PRE_PROCESSORS,
094: ActionNames.ADD));
095: addMenu.add(MenuFactory.makeMenu(MenuFactory.ASSERTIONS,
096: ActionNames.ADD));
097: addMenu.add(MenuFactory.makeMenu(MenuFactory.POST_PROCESSORS,
098: ActionNames.ADD));
099: addMenu.add(MenuFactory.makeMenu(MenuFactory.LISTENERS,
100: ActionNames.ADD));
101: pop.add(addMenu);
102: MenuFactory.addPasteResetMenu(pop);
103: MenuFactory.addFileMenu(pop);
104: return pop;
105: }
106:
107: /* Implements JMeterGUIComponent.createTestElement() */
108: public TestElement createTestElement() {
109: TestPlan tp = new TestPlan();
110: modifyTestElement(tp);
111: return tp;
112: }
113:
114: /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
115: public void modifyTestElement(TestElement plan) {
116: super .configureTestElement(plan);
117: if (plan instanceof TestPlan) {
118: TestPlan tp = (TestPlan) plan;
119: tp.setFunctionalMode(functionalMode.isSelected());
120: tp.setSerialized(serializedMode.isSelected());
121: tp.setUserDefinedVariables((Arguments) argsPanel
122: .createTestElement());
123: tp.setTestPlanClasspathArray(browseJar.getFiles());
124: }
125: }
126:
127: public String getLabelResource() {
128: return "test_plan"; // $NON-NLS-1$
129: }
130:
131: /**
132: * This is the list of menu categories this gui component will be available
133: * under. This implementation returns null, since the TestPlan appears at
134: * the top level of the tree and cannot be added elsewhere.
135: *
136: * @return a Collection of Strings, where each element is one of the
137: * constants defined in MenuFactory
138: */
139: public Collection getMenuCategories() {
140: return null;
141: }
142:
143: /**
144: * A newly created component can be initialized with the contents of a Test
145: * Element object by calling this method. The component is responsible for
146: * querying the Test Element object for the relevant information to display
147: * in its GUI.
148: *
149: * @param el
150: * the TestElement to configure
151: */
152: public void configure(TestElement el) {
153: super .configure(el);
154: if (el instanceof TestPlan) {
155: TestPlan tp = (TestPlan) el;
156: functionalMode.setSelected(tp.isFunctionalMode());
157: serializedMode.setSelected(tp.isSerialized());
158: final JMeterProperty udv = tp
159: .getUserDefinedVariablesAsProperty();
160: if (udv != null) {
161: argsPanel.configure((Arguments) udv.getObjectValue());
162: }
163: browseJar.setFiles(tp.getTestPlanClasspathArray());
164: }
165: }
166:
167: /**
168: * Create a panel allowing the user to define variables for the test.
169: *
170: * @return a panel for user-defined variables
171: */
172: private JPanel createVariablePanel() {
173: argsPanel = new ArgumentsPanel(JMeterUtils
174: .getResString("user_defined_variables")); // $NON-NLS-1$
175:
176: return argsPanel;
177: }
178:
179: protected Container createClassPathPanel() {
180: browseJar = new FileListPanel(JMeterUtils
181: .getResString("test_plan_classpath_browse"), ".jar"); // $NON-NLS-1$ $NON-NLS-2$
182: return browseJar;
183: }
184:
185: /**
186: * Initialize the components and layout of this component.
187: */
188: private void init() {
189: setLayout(new BorderLayout(10, 10));
190: setBorder(makeBorder());
191:
192: add(makeTitlePanel(), BorderLayout.NORTH);
193:
194: add(createVariablePanel(), BorderLayout.CENTER);
195:
196: VerticalPanel southPanel = new VerticalPanel();
197: serializedMode = new JCheckBox(JMeterUtils
198: .getResString("testplan.serialized")); // $NON-NLS-1$
199: southPanel.add(serializedMode);
200: functionalMode = new JCheckBox(JMeterUtils
201: .getResString("functional_mode")); // $NON-NLS-1$
202: southPanel.add(functionalMode);
203: JTextArea explain = new JTextArea(JMeterUtils
204: .getResString("functional_mode_explanation")); // $NON-NLS-1$
205: explain.setEditable(false);
206: explain.setBackground(this .getBackground());
207: southPanel.add(explain);
208: southPanel.add(createClassPathPanel());
209:
210: add(southPanel, BorderLayout.SOUTH);
211: }
212:
213: public void clearGui() {
214: super .clearGui();
215: functionalMode.setSelected(false);
216: serializedMode.setSelected(false);
217: argsPanel.clear();
218: browseJar.clearFiles();
219: }
220: }
|