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.FlowLayout;
022: import java.util.Collection;
023: import java.util.Iterator;
024:
025: import javax.swing.DefaultComboBoxModel;
026: import javax.swing.JComboBox;
027: import javax.swing.JLabel;
028: import javax.swing.JMenu;
029: import javax.swing.JPanel;
030: import javax.swing.JPopupMenu;
031:
032: import org.apache.jmeter.control.Controller;
033: import org.apache.jmeter.control.ModuleController;
034: import org.apache.jmeter.gui.GuiPackage;
035: import org.apache.jmeter.gui.action.ActionNames;
036: import org.apache.jmeter.gui.tree.JMeterTreeNode;
037: import org.apache.jmeter.gui.util.MenuFactory;
038: import org.apache.jmeter.testelement.TestElement;
039: import org.apache.jmeter.testelement.TestPlan;
040: import org.apache.jmeter.testelement.WorkBench;
041: import org.apache.jmeter.threads.ThreadGroup;
042: import org.apache.jmeter.util.JMeterUtils;
043: import org.apache.jorphan.gui.layout.VerticalLayout;
044:
045: /**
046: * ModuleController Gui.
047: *
048: */
049: public class ModuleControllerGui extends AbstractControllerGui
050: // implements UnsharedComponent
051: {
052:
053: private JMeterTreeNode selected = null;
054:
055: private JComboBox nodes;
056:
057: private DefaultComboBoxModel nodesModel;
058:
059: private JLabel warningLabel;
060:
061: /**
062: * Initializes the gui panel for the ModuleController instance.
063: */
064: public ModuleControllerGui() {
065: init();
066: }
067:
068: public String getLabelResource() {
069: return "module_controller_title"; // $NON-NLS-1$
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see org.apache.jmeter.gui.JMeterGUIComponent#configure(TestElement)
076: */
077: public void configure(TestElement el) {
078: super .configure(el);
079: ModuleController controller = (ModuleController) el;
080: this .selected = controller.getSelectedNode();
081: if (selected == null && controller.getNodePath() != null) {
082: warningLabel.setText(JMeterUtils
083: .getResString("module_controller_warning") // $NON-NLS-1$
084: + renderPath(controller.getNodePath()));
085: } else {
086: warningLabel.setText(""); // $NON-NLS-1$
087: }
088: reinitialize();
089: }
090:
091: private String renderPath(Collection path) {
092: Iterator iter = path.iterator();
093: StringBuffer buf = new StringBuffer();
094: boolean first = true;
095: while (iter.hasNext()) {
096: if (first) {
097: first = false;
098: iter.next();
099: continue;
100: }
101: buf.append(iter.next());
102: if (iter.hasNext()) {
103: buf.append(" > "); // $NON-NLS-1$
104: }
105: }
106: return buf.toString();
107: }
108:
109: /*
110: * (non-Javadoc)
111: *
112: * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
113: */
114: public TestElement createTestElement() {
115: ModuleController mc = new ModuleController();
116: configureTestElement(mc);
117: if (selected != null) {
118: mc.setSelectedNode(selected);
119: }
120: return mc;
121: }
122:
123: /*
124: * (non-Javadoc)
125: *
126: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
127: */
128: public void modifyTestElement(TestElement element) {
129: configureTestElement(element);
130: TreeNodeWrapper tnw = (TreeNodeWrapper) nodesModel
131: .getSelectedItem();
132: if (tnw != null && tnw.getTreeNode() != null) {
133: selected = tnw.getTreeNode();
134: if (selected != null) {
135: ((ModuleController) element).setSelectedNode(selected);
136: }
137: }
138: }
139:
140: /**
141: * Implements JMeterGUIComponent.clearGui
142: */
143: public void clearGui() {
144: super .clearGui();
145:
146: nodes.setSelectedIndex(-1);
147: selected = null;
148: }
149:
150: public JPopupMenu createPopupMenu() {
151: JPopupMenu menu = new JPopupMenu();
152: JMenu addMenu = MenuFactory.makeMenus(new String[] {
153: MenuFactory.CONFIG_ELEMENTS, MenuFactory.ASSERTIONS,
154: MenuFactory.TIMERS, MenuFactory.LISTENERS, },
155: JMeterUtils.getResString("add"), // $NON-NLS-1$
156: ActionNames.ADD);
157: menu.add(addMenu);
158: MenuFactory.addEditMenu(menu, true);
159: MenuFactory.addFileMenu(menu);
160: return menu;
161: }
162:
163: private void init() {
164: setLayout(new VerticalLayout(5, VerticalLayout.BOTH,
165: VerticalLayout.TOP));
166: setBorder(makeBorder());
167: add(makeTitlePanel());
168:
169: // DROP-DOWN MENU
170: JPanel modulesPanel = new JPanel(new FlowLayout(
171: FlowLayout.LEFT, 20, 5));
172: JLabel nodesLabel = new JLabel(JMeterUtils
173: .getResString("module_controller_module_to_run")); // $NON-NLS-1$
174: modulesPanel.add(nodesLabel);
175: nodesModel = new DefaultComboBoxModel();
176: nodes = new JComboBox(nodesModel);
177: nodesLabel.setLabelFor(nodes);
178: reinitialize();
179: modulesPanel.add(nodes);
180: warningLabel = new JLabel(""); // $NON-NLS-1$
181: modulesPanel.add(warningLabel);
182: add(modulesPanel);
183: }
184:
185: private void reinitialize() {
186: TreeNodeWrapper current;
187: nodesModel.removeAllElements();
188: GuiPackage gp = GuiPackage.getInstance();
189: JMeterTreeNode root;
190: if (gp != null) {
191: root = (JMeterTreeNode) GuiPackage.getInstance()
192: .getTreeModel().getRoot();
193: buildNodesModel(root, "", 0); // $NON-NLS-1$
194: }
195: if (selected != null) {
196: for (int i = 0; i < nodesModel.getSize(); i++) {
197: current = (TreeNodeWrapper) nodesModel.getElementAt(i);
198: if ((current.getTreeNode() == null && selected == null)
199: || (current.getTreeNode() != null && current
200: .getTreeNode().equals(selected))) {
201: nodesModel.setSelectedItem(current);
202: break;
203: }
204: }
205: }
206: }
207:
208: private void buildNodesModel(JMeterTreeNode node,
209: String parent_name, int level) {
210: if (level == 0
211: && (parent_name == null || parent_name.length() == 0)) {
212: nodesModel.addElement(new TreeNodeWrapper(null, "")); // $NON-NLS-1$
213: }
214: String seperator = " > "; // $NON-NLS-1$
215: if (node != null) {
216: for (int i = 0; i < node.getChildCount(); i++) {
217: StringBuffer name = new StringBuffer();
218: JMeterTreeNode cur = (JMeterTreeNode) node
219: .getChildAt(i);
220: TestElement te = cur.getTestElement();
221: if (te instanceof ThreadGroup) {
222: name.append(parent_name);
223: name.append(cur.getName());
224: name.append(seperator);
225: buildNodesModel(cur, name.toString(), level);
226: } else if (te instanceof Controller
227: && !(te instanceof ModuleController)) {
228: name.append(spaces(level));
229: name.append(parent_name);
230: name.append(cur.getName());
231: TreeNodeWrapper tnw = new TreeNodeWrapper(cur, name
232: .toString());
233: nodesModel.addElement(tnw);
234: name = new StringBuffer();
235: name.append(cur.getName());
236: name.append(seperator);
237: buildNodesModel(cur, name.toString(), level + 1);
238: } else if (te instanceof TestPlan
239: || te instanceof WorkBench) {
240: name.append(cur.getName());
241: name.append(seperator);
242: buildNodesModel(cur, name.toString(), 0);
243: }
244: }
245: }
246: }
247:
248: private String spaces(int level) {
249: int multi = 4;
250: StringBuffer spaces = new StringBuffer(level * multi);
251: for (int i = 0; i < level * multi; i++) {
252: spaces.append(" "); // $NON-NLS-1$
253: }
254: return spaces.toString();
255: }
256: }
257:
258: class TreeNodeWrapper {
259:
260: private JMeterTreeNode tn;
261:
262: private String label;
263:
264: private TreeNodeWrapper() {
265: };
266:
267: public TreeNodeWrapper(JMeterTreeNode tn, String label) {
268: this .tn = tn;
269: this .label = label;
270: }
271:
272: public JMeterTreeNode getTreeNode() {
273: return tn;
274: }
275:
276: public String toString() {
277: return label;
278: }
279:
280: }
|