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.functions.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.FlowLayout;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.io.IOException;
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Map;
030:
031: import javax.swing.JButton;
032: import javax.swing.JDialog;
033: import javax.swing.JFrame;
034: import javax.swing.JPanel;
035: import javax.swing.event.ChangeEvent;
036: import javax.swing.event.ChangeListener;
037:
038: import org.apache.jmeter.config.Argument;
039: import org.apache.jmeter.config.Arguments;
040: import org.apache.jmeter.config.gui.ArgumentsPanel;
041: import org.apache.jmeter.functions.Function;
042: import org.apache.jmeter.gui.action.ActionRouter;
043: import org.apache.jmeter.gui.action.Help;
044: import org.apache.jmeter.testelement.property.PropertyIterator;
045: import org.apache.jmeter.util.JMeterUtils;
046: import org.apache.jorphan.gui.ComponentUtil;
047: import org.apache.jorphan.gui.JLabeledChoice;
048: import org.apache.jorphan.gui.JLabeledTextField;
049: import org.apache.jorphan.reflect.ClassFinder;
050:
051: public class FunctionHelper extends JDialog implements ActionListener,
052: ChangeListener {
053: JLabeledChoice functionList;
054:
055: ArgumentsPanel parameterPanel;
056:
057: JLabeledTextField cutPasteFunction;
058:
059: private Map functionMap = new HashMap();
060:
061: JButton generateButton;
062:
063: public FunctionHelper() {
064: super ((JFrame) null, JMeterUtils
065: .getResString("function_helper_title"), false); //$NON-NLS-1$
066: init();
067: }
068:
069: private void init() {
070: parameterPanel = new ArgumentsPanel(JMeterUtils
071: .getResString("function_params")); //$NON-NLS-1$
072: initializeFunctionList();
073: this .getContentPane().setLayout(new BorderLayout(10, 10));
074: JPanel comboPanel = new JPanel(
075: new FlowLayout(FlowLayout.CENTER));
076: comboPanel.add(functionList);
077: JButton helpButton = new JButton(JMeterUtils
078: .getResString("help")); //$NON-NLS-1$
079: helpButton.addActionListener(new HelpListener());
080: comboPanel.add(helpButton);
081: this .getContentPane().add(comboPanel, BorderLayout.NORTH);
082: this .getContentPane().add(parameterPanel, BorderLayout.CENTER);
083: JPanel resultsPanel = new JPanel(new FlowLayout(
084: FlowLayout.CENTER));
085: cutPasteFunction = new JLabeledTextField(JMeterUtils
086: .getResString("cut_paste_function"), 35); //$NON-NLS-1$
087: resultsPanel.add(cutPasteFunction);
088: generateButton = new JButton(JMeterUtils
089: .getResString("generate")); //$NON-NLS-1$
090: generateButton.addActionListener(this );
091: resultsPanel.add(generateButton);
092: this .getContentPane().add(resultsPanel, BorderLayout.SOUTH);
093: this .pack();
094: ComponentUtil.centerComponentInWindow(this );
095: }
096:
097: private void initializeFunctionList() {
098: try {
099: List functionClasses = ClassFinder.findClassesThatExtend(
100: JMeterUtils.getSearchPaths(),
101: new Class[] { Function.class }, true);
102: Iterator iter = functionClasses.iterator();
103: String[] functionNames = new String[functionClasses.size()];
104: int count = 0;
105: while (iter.hasNext()) {
106: Class cl = Class.forName((String) iter.next());
107: functionNames[count] = ((Function) cl.newInstance())
108: .getReferenceKey();
109: functionMap.put(functionNames[count], cl);
110: count++;
111: }
112: functionList = new JLabeledChoice(JMeterUtils
113: .getResString("choose_function"), functionNames); //$NON-NLS-1$
114: functionList.addChangeListener(this );
115: } catch (IOException e) {
116: } catch (ClassNotFoundException e) {
117: } catch (InstantiationException e) {
118: } catch (IllegalAccessException e) {
119: }
120: }
121:
122: public void stateChanged(ChangeEvent event) {
123: try {
124: Arguments args = new Arguments();
125: Function function = (Function) ((Class) functionMap
126: .get(functionList.getText())).newInstance();
127: List argumentDesc = function.getArgumentDesc();
128: Iterator iter = argumentDesc.iterator();
129: while (iter.hasNext()) {
130: String help = (String) iter.next();
131: args.addArgument(help, ""); //$NON-NLS-1$
132: }
133: parameterPanel.configure(args);
134: parameterPanel.revalidate();
135: getContentPane().remove(parameterPanel);
136: this .pack();
137: getContentPane().add(parameterPanel, BorderLayout.CENTER);
138: this .pack();
139: this .validate();
140: this .repaint();
141: } catch (InstantiationException e) {
142: } catch (IllegalAccessException e) {
143: }
144: }
145:
146: public void actionPerformed(ActionEvent e) {
147: StringBuffer functionCall = new StringBuffer("${");
148: functionCall.append(functionList.getText());
149: Arguments args = (Arguments) parameterPanel.createTestElement();
150: if (args.getArguments().size() > 0) {
151: functionCall.append("(");
152: PropertyIterator iter = args.iterator();
153: boolean first = true;
154: while (iter.hasNext()) {
155: Argument arg = (Argument) iter.next().getObjectValue();
156: if (!first) {
157: functionCall.append(",");
158: }
159: functionCall.append(arg.getValue());
160: first = false;
161: }
162: functionCall.append(")");
163: }
164: functionCall.append("}");
165: cutPasteFunction.setText(functionCall.toString());
166: }
167:
168: private class HelpListener implements ActionListener {
169: public void actionPerformed(ActionEvent e) {
170: String[] source = new String[] { Help.HELP_FUNCTIONS,
171: functionList.getText() };
172: ActionEvent helpEvent = new ActionEvent(source, e.getID(),
173: "help"); //$NON-NLS-1$
174: ActionRouter.getInstance().actionPerformed(helpEvent);
175: }
176: }
177: }
|