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.protocol.http.proxy.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.Dimension;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.awt.event.ItemEvent;
026: import java.awt.event.ItemListener;
027: import java.awt.event.KeyEvent;
028: import java.awt.event.KeyListener;
029: import java.util.Arrays;
030: import java.util.Collection;
031: import java.util.LinkedList;
032: import java.util.List;
033:
034: import javax.swing.BorderFactory;
035: import javax.swing.Box;
036: import javax.swing.DefaultComboBoxModel;
037: import javax.swing.JButton;
038: import javax.swing.JCheckBox;
039: import javax.swing.JComboBox;
040: import javax.swing.JLabel;
041: import javax.swing.JOptionPane;
042: import javax.swing.JPanel;
043: import javax.swing.JScrollPane;
044: import javax.swing.JTable;
045: import javax.swing.JTextField;
046:
047: import org.apache.jmeter.control.Controller;
048: import org.apache.jmeter.control.gui.LogicControllerGui;
049: import org.apache.jmeter.engine.util.ValueReplacer;
050: import org.apache.jmeter.functions.InvalidVariableException;
051: import org.apache.jmeter.gui.GuiPackage;
052: import org.apache.jmeter.gui.JMeterGUIComponent;
053: import org.apache.jmeter.gui.UnsharedComponent;
054: import org.apache.jmeter.gui.tree.JMeterTreeNode;
055: import org.apache.jmeter.gui.util.HorizontalPanel;
056: import org.apache.jmeter.gui.util.MenuFactory;
057: import org.apache.jmeter.gui.util.PowerTableModel;
058: import org.apache.jmeter.gui.util.VerticalPanel;
059: import org.apache.jmeter.protocol.http.proxy.ProxyControl;
060: import org.apache.jmeter.testelement.TestElement;
061: import org.apache.jmeter.testelement.TestPlan;
062: import org.apache.jmeter.testelement.WorkBench;
063: import org.apache.jmeter.testelement.property.PropertyIterator;
064: import org.apache.jmeter.util.JMeterUtils;
065: import org.apache.jorphan.logging.LoggingManager;
066: import org.apache.log.Logger;
067:
068: public class ProxyControlGui extends LogicControllerGui implements
069: JMeterGUIComponent, ActionListener, ItemListener, KeyListener,
070: UnsharedComponent {
071: private static transient Logger log = LoggingManager
072: .getLoggerForClass();
073:
074: private static final long serialVersionUID = 1L;
075:
076: private JTextField portField;
077:
078: /**
079: * Used to indicate that HTTP request headers should be captured. The
080: * default is to capture the HTTP request headers, which are specific to
081: * particular browser settings.
082: */
083: private JCheckBox httpHeaders;
084:
085: /**
086: * Whether to group requests together based on inactivity separation periods --
087: * and how to handle such grouping afterwards.
088: */
089: private JComboBox groupingMode;
090:
091: /**
092: * Add an Assertion to the first sample of each set
093: */
094: private JCheckBox addAssertions;
095:
096: /**
097: * Set/clear the Use Keep-Alive box on the samplers (default is true)
098: */
099: private JCheckBox useKeepAlive;
100:
101: /*
102: * Use regexes to match the source data
103: */
104: private JCheckBox regexMatch;
105:
106: /**
107: * The list of sampler type names to choose from
108: */
109: private JComboBox samplerTypeName;
110:
111: /**
112: * Set/clear the Redirect automatically box on the samplers (default is false)
113: */
114: private JCheckBox samplerRedirectAutomatically;
115:
116: /**
117: * Set/clear the Follow-redirects box on the samplers (default is true)
118: */
119: private JCheckBox samplerFollowRedirects;
120:
121: /**
122: * Set/clear the Download images box on the samplers (default is false)
123: */
124: private JCheckBox samplerDownloadImages;
125:
126: /*
127: * Spoof the client into thinking that it is communicating with http
128: * even if it is really https.
129: */
130: private JCheckBox httpsSpoof;
131:
132: private JTextField httpsMatch;
133:
134: /**
135: * Regular expression to include results based on content type
136: */
137: private JTextField contentTypeInclude;
138:
139: /**
140: * Regular expression to exclude results based on content type
141: */
142: private JTextField contentTypeExclude;
143:
144: /**
145: * List of available target controllers
146: */
147: private JComboBox targetNodes;
148:
149: private DefaultComboBoxModel targetNodesModel;
150:
151: private ProxyControl model;
152:
153: private JTable excludeTable;
154:
155: private PowerTableModel excludeModel;
156:
157: private JTable includeTable;
158:
159: private PowerTableModel includeModel;
160:
161: private static final String CHANGE_TARGET = "change_target"; // $NON-NLS-1$
162:
163: private JButton stop, start, restart;
164:
165: //+ action names
166: private static final String STOP = "stop"; // $NON-NLS-1$
167:
168: private static final String START = "start"; // $NON-NLS-1$
169:
170: private static final String RESTART = "restart"; // $NON-NLS-1$
171:
172: private static final String ENABLE_RESTART = "enable_restart"; // $NON-NLS-1$
173:
174: private static final String ADD_INCLUDE = "add_include"; // $NON-NLS-1$
175:
176: private static final String ADD_EXCLUDE = "add_exclude"; // $NON-NLS-1$
177:
178: private static final String DELETE_INCLUDE = "delete_include"; // $NON-NLS-1$
179:
180: private static final String DELETE_EXCLUDE = "delete_exclude"; // $NON-NLS-1$
181: //- action names
182:
183: private static final String INCLUDE_COL = JMeterUtils
184: .getResString("patterns_to_include"); // $NON-NLS-1$
185:
186: private static final String EXCLUDE_COL = JMeterUtils
187: .getResString("patterns_to_exclude"); // $NON-NLS-1$
188:
189: // Used by itemListener
190: private static final String PORTFIELD = "portField"; // $NON-NLS-1$
191:
192: public ProxyControlGui() {
193: super ();
194: log.debug("Creating ProxyControlGui");
195: init();
196: }
197:
198: public TestElement createTestElement() {
199: model = makeProxyControl();
200: log.debug("creating/configuring model = " + model);
201: modifyTestElement(model);
202: return model;
203: }
204:
205: protected ProxyControl makeProxyControl() {
206: ProxyControl local = new ProxyControl();
207: return local;
208: }
209:
210: /**
211: * Modifies a given TestElement to mirror the data in the gui components.
212: *
213: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
214: */
215: public void modifyTestElement(TestElement el) {
216: if (excludeTable.isEditing()) {// Bug 42948
217: excludeTable.getCellEditor().stopCellEditing();
218: }
219: if (includeTable.isEditing()) {// Bug 42948
220: includeTable.getCellEditor().stopCellEditing();
221: }
222: configureTestElement(el);
223: if (el instanceof ProxyControl) {
224: model = (ProxyControl) el;
225: model.setPort(portField.getText());
226: setIncludeListInProxyControl(model);
227: setExcludeListInProxyControl(model);
228: model.setCaptureHttpHeaders(httpHeaders.isSelected());
229: model.setGroupingMode(groupingMode.getSelectedIndex());
230: model.setAssertions(addAssertions.isSelected());
231: model
232: .setSamplerTypeName(samplerTypeName
233: .getSelectedIndex());
234: model
235: .setSamplerRedirectAutomatically(samplerRedirectAutomatically
236: .isSelected());
237: model.setSamplerFollowRedirects(samplerFollowRedirects
238: .isSelected());
239: model.setUseKeepAlive(useKeepAlive.isSelected());
240: model.setSamplerDownloadImages(samplerDownloadImages
241: .isSelected());
242: model.setRegexMatch(regexMatch.isSelected());
243: model.setHttpsSpoof(httpsSpoof.isSelected());
244: model.setHttpsSpoofMatch(httpsMatch.getText());
245: model.setContentTypeInclude(contentTypeInclude.getText());
246: model.setContentTypeExclude(contentTypeExclude.getText());
247: TreeNodeWrapper nw = (TreeNodeWrapper) targetNodes
248: .getSelectedItem();
249: if (nw == null) {
250: model.setTarget(null);
251: } else {
252: model.setTarget(nw.getTreeNode());
253: }
254: }
255: }
256:
257: protected void setIncludeListInProxyControl(ProxyControl element) {
258: List includeList = getDataList(includeModel, INCLUDE_COL);
259: element.setIncludeList(includeList);
260: }
261:
262: protected void setExcludeListInProxyControl(ProxyControl element) {
263: List excludeList = getDataList(excludeModel, EXCLUDE_COL);
264: element.setExcludeList(excludeList);
265: }
266:
267: private List getDataList(PowerTableModel p_model, String colName) {
268: String[] dataArray = p_model.getData().getColumn(colName);
269: List list = new LinkedList();
270: for (int i = 0; i < dataArray.length; i++) {
271: list.add(dataArray[i]);
272: }
273: return list;
274: }
275:
276: public String getLabelResource() {
277: return "proxy_title"; // $NON-NLS-1$
278: }
279:
280: public Collection getMenuCategories() {
281: return Arrays
282: .asList(new String[] { MenuFactory.NON_TEST_ELEMENTS });
283: }
284:
285: public void configure(TestElement element) {
286: log.debug("Configuring gui with " + element);
287: super .configure(element);
288: model = (ProxyControl) element;
289: portField.setText(model.getPortString());
290: httpHeaders.setSelected(model.getCaptureHttpHeaders());
291: groupingMode.setSelectedIndex(model.getGroupingMode());
292: addAssertions.setSelected(model.getAssertions());
293: samplerTypeName.setSelectedIndex(model.getSamplerTypeName());
294: samplerRedirectAutomatically.setSelected(model
295: .getSamplerRedirectAutomatically());
296: samplerFollowRedirects.setSelected(model
297: .getSamplerFollowRedirects());
298: useKeepAlive.setSelected(model.getUseKeepalive());
299: samplerDownloadImages.setSelected(model
300: .getSamplerDownloadImages());
301: regexMatch.setSelected(model.getRegexMatch());
302: httpsSpoof.setSelected(model.getHttpsSpoof());
303: httpsMatch.setText(model.getHttpsSpoofMatch());
304: contentTypeInclude.setText(model.getContentTypeInclude());
305: contentTypeExclude.setText(model.getContentTypeExclude());
306:
307: reinitializeTargetCombo();// Set up list of potential targets and
308: // enable listener
309:
310: populateTable(includeModel, model.getIncludePatterns()
311: .iterator());
312: populateTable(excludeModel, model.getExcludePatterns()
313: .iterator());
314: repaint();
315: }
316:
317: private void populateTable(PowerTableModel p_model,
318: PropertyIterator iter) {
319: p_model.clearData();
320: while (iter.hasNext()) {
321: p_model
322: .addRow(new Object[] { iter.next().getStringValue() });
323: }
324: p_model.fireTableDataChanged();
325: }
326:
327: /*
328: * Handles groupingMode. actionPerfomed is not suitable, as that seems to be
329: * activated whenever the Proxy is selected in the Test Plan
330: * Also handles samplerTypeName
331: */
332: public void itemStateChanged(ItemEvent e) {
333: // System.err.println(e.paramString());
334: enableRestart();
335: }
336:
337: /***************************************************************************
338: * !ToDo (Method description)
339: *
340: * @param action
341: * !ToDo (Parameter description)
342: **************************************************************************/
343: public void actionPerformed(ActionEvent action) {
344: String command = action.getActionCommand();
345:
346: // System.err.println(action.paramString()+" "+command+ "
347: // "+action.getModifiers());
348:
349: if (command.equals(STOP)) {
350: model.stopProxy();
351: stop.setEnabled(false);
352: start.setEnabled(true);
353: restart.setEnabled(false);
354: } else if (command.equals(START)) {
355: startProxy();
356: } else if (command.equals(RESTART)) {
357: model.stopProxy();
358: startProxy();
359: } else if (command.equals(ENABLE_RESTART)) {
360: enableRestart();
361: } else if (command.equals(ADD_EXCLUDE)) {
362: excludeModel.addNewRow();
363: excludeModel.fireTableDataChanged();
364: enableRestart();
365: } else if (command.equals(ADD_INCLUDE)) {
366: includeModel.addNewRow();
367: includeModel.fireTableDataChanged();
368: enableRestart();
369: } else if (command.equals(DELETE_EXCLUDE)) {
370: excludeModel.removeRow(excludeTable.getSelectedRow());
371: excludeModel.fireTableDataChanged();
372: enableRestart();
373: } else if (command.equals(DELETE_INCLUDE)) {
374: includeModel.removeRow(includeTable.getSelectedRow());
375: includeModel.fireTableDataChanged();
376: enableRestart();
377: } else if (command.equals(CHANGE_TARGET)) {
378: log.debug("Change target " + targetNodes.getSelectedItem());
379: log.debug("In model " + model);
380: TreeNodeWrapper nw = (TreeNodeWrapper) targetNodes
381: .getSelectedItem();
382: model.setTarget(nw.getTreeNode());
383: enableRestart();
384: }
385: }
386:
387: private void startProxy() {
388: ValueReplacer replacer = GuiPackage.getInstance().getReplacer();
389: modifyTestElement(model);
390: try {
391: replacer.replaceValues(model);
392: model.startProxy();
393: start.setEnabled(false);
394: stop.setEnabled(true);
395: restart.setEnabled(false);
396: } catch (InvalidVariableException e) {
397: JOptionPane.showMessageDialog(this , JMeterUtils
398: .getResString("invalid_variables"), // $NON-NLS-1$
399: "Error", JOptionPane.ERROR_MESSAGE);
400: }
401: }
402:
403: private void enableRestart() {
404: if (stop.isEnabled()) {
405: // System.err.println("Enable Restart");
406: restart.setEnabled(true);
407: }
408: }
409:
410: /***************************************************************************
411: * !ToDo (Method description)
412: *
413: * @param e
414: * !ToDo (Parameter description)
415: **************************************************************************/
416: public void keyPressed(KeyEvent e) {
417: }
418:
419: /***************************************************************************
420: * !ToDo (Method description)
421: *
422: * @param e
423: * !ToDo (Parameter description)
424: **************************************************************************/
425: public void keyTyped(KeyEvent e) {
426: }
427:
428: /***************************************************************************
429: * !ToDo (Method description)
430: *
431: * @param e
432: * !ToDo (Parameter description)
433: **************************************************************************/
434: public void keyReleased(KeyEvent e) {
435: String fieldName = e.getComponent().getName();
436:
437: if (fieldName.equals(PORTFIELD)) {
438: try {
439: Integer.parseInt(portField.getText());
440: } catch (NumberFormatException nfe) {
441: int length = portField.getText().length();
442: if (length > 0) {
443: JOptionPane.showMessageDialog(this ,
444: "Only digits allowed", "Invalid data",
445: JOptionPane.WARNING_MESSAGE);
446: // Drop the last character:
447: portField.setText(portField.getText().substring(0,
448: length - 1));
449: }
450: }
451: enableRestart();
452: } else if (fieldName.equals(ENABLE_RESTART)) {
453: enableRestart();
454: }
455: }
456:
457: private void init() {
458: setLayout(new BorderLayout(0, 5));
459: setBorder(makeBorder());
460:
461: add(makeTitlePanel(), BorderLayout.NORTH);
462:
463: JPanel mainPanel = new JPanel(new BorderLayout());
464:
465: Box myBox = Box.createVerticalBox();
466: myBox.add(createPortPanel());
467: myBox.add(Box.createVerticalStrut(5));
468: myBox.add(createTestPlanContentPanel());
469: myBox.add(Box.createVerticalStrut(5));
470: myBox.add(createHTTPSamplerPanel());
471: myBox.add(Box.createVerticalStrut(5));
472: myBox.add(createContentTypePanel());
473: myBox.add(Box.createVerticalStrut(5));
474: mainPanel.add(myBox, BorderLayout.NORTH);
475:
476: Box includeExcludePanel = Box.createVerticalBox();
477: includeExcludePanel.add(createIncludePanel());
478: includeExcludePanel.add(createExcludePanel());
479: mainPanel.add(includeExcludePanel, BorderLayout.CENTER);
480:
481: mainPanel.add(createControls(), BorderLayout.SOUTH);
482:
483: add(mainPanel, BorderLayout.CENTER);
484: }
485:
486: private JPanel createControls() {
487: start = new JButton(JMeterUtils.getResString("start")); // $NON-NLS-1$
488: start.addActionListener(this );
489: start.setActionCommand(START);
490: start.setEnabled(true);
491:
492: stop = new JButton(JMeterUtils.getResString("stop")); // $NON-NLS-1$
493: stop.addActionListener(this );
494: stop.setActionCommand(STOP);
495: stop.setEnabled(false);
496:
497: restart = new JButton(JMeterUtils.getResString("restart")); // $NON-NLS-1$
498: restart.addActionListener(this );
499: restart.setActionCommand(RESTART);
500: restart.setEnabled(false);
501:
502: JPanel panel = new JPanel();
503: panel.add(start);
504: panel.add(stop);
505: panel.add(restart);
506: return panel;
507: }
508:
509: private JPanel createPortPanel() {
510: portField = new JTextField(ProxyControl.DEFAULT_PORT_S, 5);
511: portField.setName(PORTFIELD);
512: portField.addKeyListener(this );
513:
514: JLabel label = new JLabel(JMeterUtils.getResString("port")); // $NON-NLS-1$
515: label.setLabelFor(portField);
516:
517: httpsSpoof = new JCheckBox(JMeterUtils
518: .getResString("proxy_httpsspoofing")); // $NON-NLS-1$
519: httpsSpoof.setSelected(false);
520: httpsSpoof.addActionListener(this );
521: httpsSpoof.setActionCommand(ENABLE_RESTART);
522:
523: httpsMatch = new JTextField(40);
524: httpsMatch.addKeyListener(this );
525: httpsMatch.setName(ENABLE_RESTART);
526:
527: JLabel matchlabel = new JLabel(JMeterUtils
528: .getResString("proxy_httpsspoofing_match")); // $NON-NLS-1$
529: matchlabel.setLabelFor(httpsMatch);
530:
531: HorizontalPanel panel = new HorizontalPanel();
532: panel.add(label);
533: panel.add(portField);
534:
535: panel.add(Box.createHorizontalStrut(10));
536: panel.add(httpsSpoof);
537:
538: panel.add(matchlabel);
539: panel.add(httpsMatch);
540:
541: return panel;
542: }
543:
544: private JPanel createTestPlanContentPanel() {
545: httpHeaders = new JCheckBox(JMeterUtils
546: .getResString("proxy_headers")); // $NON-NLS-1$
547: httpHeaders.setSelected(true); // maintain original default
548: httpHeaders.addActionListener(this );
549: httpHeaders.setActionCommand(ENABLE_RESTART);
550:
551: addAssertions = new JCheckBox(JMeterUtils
552: .getResString("proxy_assertions")); // $NON-NLS-1$
553: addAssertions.setSelected(false);
554: addAssertions.addActionListener(this );
555: addAssertions.setActionCommand(ENABLE_RESTART);
556:
557: regexMatch = new JCheckBox(JMeterUtils
558: .getResString("proxy_regex")); // $NON-NLS-1$
559: regexMatch.setSelected(false);
560: regexMatch.addActionListener(this );
561: regexMatch.setActionCommand(ENABLE_RESTART);
562:
563: VerticalPanel mainPanel = new VerticalPanel();
564: mainPanel.setBorder(BorderFactory.createTitledBorder(
565: BorderFactory.createEtchedBorder(), JMeterUtils
566: .getResString("proxy_test_plan_content"))); // $NON-NLS-1$
567:
568: HorizontalPanel nodeCreationPanel = new HorizontalPanel();
569: nodeCreationPanel.add(httpHeaders);
570: nodeCreationPanel.add(addAssertions);
571: nodeCreationPanel.add(regexMatch);
572:
573: mainPanel.add(createTargetPanel());
574: mainPanel.add(createGroupingPanel());
575: mainPanel.add(nodeCreationPanel);
576:
577: return mainPanel;
578: }
579:
580: private JPanel createHTTPSamplerPanel() {
581: DefaultComboBoxModel m = new DefaultComboBoxModel();
582: // Note: position of these elements in the menu *must* match the
583: // corresponding ProxyControl.SAMPLER_TYPE_* values.
584: m.addElement(JMeterUtils.getResString("web_testing_title")); // $NON-NLS-1$
585: m.addElement(JMeterUtils.getResString("web_testing2_title")); // $NON-NLS-1$
586: samplerTypeName = new JComboBox(m);
587: samplerTypeName.setSelectedIndex(0);
588: samplerTypeName.addItemListener(this );
589: JLabel label2 = new JLabel(JMeterUtils
590: .getResString("proxy_sampler_type")); // $NON-NLS-1$
591: label2.setLabelFor(samplerTypeName);
592:
593: samplerRedirectAutomatically = new JCheckBox(JMeterUtils
594: .getResString("follow_redirects_auto")); // $NON-NLS-1$
595: samplerRedirectAutomatically.setSelected(false);
596: samplerRedirectAutomatically.addActionListener(this );
597: samplerRedirectAutomatically.setActionCommand(ENABLE_RESTART);
598:
599: samplerFollowRedirects = new JCheckBox(JMeterUtils
600: .getResString("follow_redirects")); // $NON-NLS-1$
601: samplerFollowRedirects.setSelected(true);
602: samplerFollowRedirects.addActionListener(this );
603: samplerFollowRedirects.setActionCommand(ENABLE_RESTART);
604:
605: useKeepAlive = new JCheckBox(JMeterUtils
606: .getResString("use_keepalive")); // $NON-NLS-1$
607: useKeepAlive.setSelected(true);
608: useKeepAlive.addActionListener(this );
609: useKeepAlive.setActionCommand(ENABLE_RESTART);
610:
611: samplerDownloadImages = new JCheckBox(JMeterUtils
612: .getResString("web_testing_retrieve_images")); // $NON-NLS-1$
613: samplerDownloadImages.setSelected(false);
614: samplerDownloadImages.addActionListener(this );
615: samplerDownloadImages.setActionCommand(ENABLE_RESTART);
616:
617: HorizontalPanel panel = new HorizontalPanel();
618: panel.setBorder(BorderFactory.createTitledBorder(BorderFactory
619: .createEtchedBorder(), JMeterUtils
620: .getResString("proxy_sampler_settings"))); // $NON-NLS-1$
621: panel.add(label2);
622: panel.add(samplerTypeName);
623: panel.add(samplerRedirectAutomatically);
624: panel.add(samplerFollowRedirects);
625: panel.add(useKeepAlive);
626: panel.add(samplerDownloadImages);
627:
628: return panel;
629: }
630:
631: private JPanel createTargetPanel() {
632: targetNodesModel = new DefaultComboBoxModel();
633: targetNodes = new JComboBox(targetNodesModel);
634: targetNodes.setActionCommand(CHANGE_TARGET);
635: // Action listener will be added later
636:
637: JLabel label = new JLabel(JMeterUtils
638: .getResString("proxy_target")); // $NON-NLS-1$
639: label.setLabelFor(targetNodes);
640:
641: HorizontalPanel panel = new HorizontalPanel();
642: panel.add(label);
643: panel.add(targetNodes);
644:
645: /*
646: * This listener subscription prevents freeing up the GUI when it's no
647: * longer in use (e.g. on locale change)... plus causes some anoying
648: * NPEs in the GUI instance created by the menu manager just to find out
649: * our name and which menus we want to be in... ... plus I don't think
650: * it's really necessary: configure(TestElement) already takes care of
651: * reinitializing the target combo when we come back to it. And I can't
652: * see how the tree can change in a relevant way without we leaving this
653: * GUI (since it is very unlikely that we will want to record into one
654: * of the controllers created by the proxy). I'll comment it out for the
655: * time being: TODO: remove once we're convinced it's really
656: * unnecessary.
657: */
658: /*
659: * try { Class addToTree =
660: * Class.forName("org.apache.jmeter.gui.action.AddToTree"); Class remove =
661: * Class.forName("org.apache.jmeter.gui.action.Remove"); ActionListener
662: * listener = new ActionListener() { public void
663: * actionPerformed(ActionEvent e) { reinitializeTargetCombo(); } };
664: * ActionRouter ar = ActionRouter.getInstance();
665: * ar.addPostActionListener(addToTree, listener);
666: * ar.addPostActionListener(remove, listener); } catch
667: * (ClassNotFoundException e) { // This should never happen -- throw an
668: * Error: throw new Error(e.toString());//JDK1.4: remove .toString() }
669: */
670:
671: return panel;
672: }
673:
674: private JPanel createGroupingPanel() {
675: DefaultComboBoxModel m = new DefaultComboBoxModel();
676: // Note: position of these elements in the menu *must* match the
677: // corresponding ProxyControl.GROUPING_* values.
678: m.addElement(JMeterUtils.getResString("grouping_no_groups")); // $NON-NLS-1$
679: m.addElement(JMeterUtils
680: .getResString("grouping_add_separators")); // $NON-NLS-1$
681: m.addElement(JMeterUtils
682: .getResString("grouping_in_controllers")); // $NON-NLS-1$
683: m.addElement(JMeterUtils
684: .getResString("grouping_store_first_only")); // $NON-NLS-1$
685: groupingMode = new JComboBox(m);
686: groupingMode.setSelectedIndex(0);
687: groupingMode.addItemListener(this );
688:
689: JLabel label2 = new JLabel(JMeterUtils
690: .getResString("grouping_mode")); // $NON-NLS-1$
691: label2.setLabelFor(groupingMode);
692:
693: HorizontalPanel panel = new HorizontalPanel();
694: panel.add(label2);
695: panel.add(groupingMode);
696:
697: return panel;
698: }
699:
700: private JPanel createContentTypePanel() {
701: contentTypeInclude = new JTextField(30);
702: contentTypeInclude.addKeyListener(this );
703: contentTypeInclude.setName(ENABLE_RESTART);
704: JLabel labelInclude = new JLabel(JMeterUtils
705: .getResString("proxy_content_type_include")); // $NON-NLS-1$
706: labelInclude.setLabelFor(contentTypeInclude);
707: // Default value
708: contentTypeInclude.setText(JMeterUtils
709: .getProperty("proxy.content_type_include")); // $NON-NLS-1$
710:
711: contentTypeExclude = new JTextField(30);
712: contentTypeExclude.addKeyListener(this );
713: contentTypeExclude.setName(ENABLE_RESTART);
714: JLabel labelExclude = new JLabel(JMeterUtils
715: .getResString("proxy_content_type_exclude")); // $NON-NLS-1$
716: labelExclude.setLabelFor(contentTypeExclude);
717: // Default value
718: contentTypeExclude.setText(JMeterUtils
719: .getProperty("proxy.content_type_exclude")); // $NON-NLS-1$
720:
721: HorizontalPanel panel = new HorizontalPanel();
722: panel.setBorder(BorderFactory.createTitledBorder(BorderFactory
723: .createEtchedBorder(), JMeterUtils
724: .getResString("proxy_content_type_filter"))); // $NON-NLS-1$
725: panel.add(labelInclude);
726: panel.add(contentTypeInclude);
727: panel.add(labelExclude);
728: panel.add(contentTypeExclude);
729:
730: return panel;
731: }
732:
733: private JPanel createIncludePanel() {
734: includeModel = new PowerTableModel(
735: new String[] { INCLUDE_COL },
736: new Class[] { String.class });
737: includeTable = new JTable(includeModel);
738: includeTable.setPreferredScrollableViewportSize(new Dimension(
739: 100, 30));
740:
741: JPanel panel = new JPanel(new BorderLayout());
742: panel.setBorder(BorderFactory.createTitledBorder(BorderFactory
743: .createEtchedBorder(), JMeterUtils
744: .getResString("patterns_to_include"))); // $NON-NLS-1$
745:
746: panel.add(new JScrollPane(includeTable), BorderLayout.CENTER);
747: panel.add(createTableButtonPanel(ADD_INCLUDE, DELETE_INCLUDE),
748: BorderLayout.SOUTH);
749:
750: return panel;
751: }
752:
753: private JPanel createExcludePanel() {
754: excludeModel = new PowerTableModel(
755: new String[] { EXCLUDE_COL },
756: new Class[] { String.class });
757: excludeTable = new JTable(excludeModel);
758: excludeTable.setPreferredScrollableViewportSize(new Dimension(
759: 100, 30));
760:
761: JPanel panel = new JPanel(new BorderLayout());
762: panel.setBorder(BorderFactory.createTitledBorder(BorderFactory
763: .createEtchedBorder(), JMeterUtils
764: .getResString("patterns_to_exclude"))); // $NON-NLS-1$
765:
766: panel.add(new JScrollPane(excludeTable), BorderLayout.CENTER);
767: panel.add(createTableButtonPanel(ADD_EXCLUDE, DELETE_EXCLUDE),
768: BorderLayout.SOUTH);
769:
770: return panel;
771: }
772:
773: private JPanel createTableButtonPanel(String addCommand,
774: String deleteCommand) {
775: JPanel buttonPanel = new JPanel();
776:
777: JButton addButton = new JButton(JMeterUtils.getResString("add")); // $NON-NLS-1$
778: addButton.setActionCommand(addCommand);
779: addButton.addActionListener(this );
780: buttonPanel.add(addButton);
781:
782: JButton deleteButton = new JButton(JMeterUtils
783: .getResString("delete")); // $NON-NLS-1$
784: deleteButton.setActionCommand(deleteCommand);
785: deleteButton.addActionListener(this );
786: buttonPanel.add(deleteButton);
787:
788: return buttonPanel;
789: }
790:
791: private void reinitializeTargetCombo() {
792: log.debug("Reinitializing target combo");
793:
794: // Stop action notifications while we shuffle this around:
795: targetNodes.removeActionListener(this );
796:
797: targetNodesModel.removeAllElements();
798: GuiPackage gp = GuiPackage.getInstance();
799: JMeterTreeNode root;
800: if (gp != null) {
801: root = (JMeterTreeNode) GuiPackage.getInstance()
802: .getTreeModel().getRoot();
803: targetNodesModel.addElement(new TreeNodeWrapper(null,
804: JMeterUtils
805: .getResString("use_recording_controller"))); // $NON-NLS-1$
806: buildNodesModel(root, "", 0);
807: }
808: TreeNodeWrapper choice = null;
809: for (int i = 0; i < targetNodesModel.getSize(); i++) {
810: choice = (TreeNodeWrapper) targetNodesModel.getElementAt(i);
811: log.debug("Selecting item " + choice + " for model "
812: + model + " in " + this );
813: if (choice.getTreeNode() == model.getTarget()) // .equals caused
814: // NPE
815: {
816: break;
817: }
818: }
819: // Reinstate action notifications:
820: targetNodes.addActionListener(this );
821: // Set the current value:
822: targetNodesModel.setSelectedItem(choice);
823:
824: log.debug("Reinitialization complete");
825: }
826:
827: private void buildNodesModel(JMeterTreeNode node,
828: String parent_name, int level) {
829: String seperator = " > ";
830: if (node != null) {
831: for (int i = 0; i < node.getChildCount(); i++) {
832: StringBuffer name = new StringBuffer();
833: JMeterTreeNode cur = (JMeterTreeNode) node
834: .getChildAt(i);
835: TestElement te = cur.getTestElement();
836: /*
837: * Will never be true. Probably intended to use
838: * org.apache.jmeter.threads.ThreadGroup rather than
839: * java.lang.ThreadGroup However, that does not work correctly;
840: * whereas treating it as a Controller does. if (te instanceof
841: * ThreadGroup) { name.append(parent_name);
842: * name.append(cur.getName()); name.append(seperator);
843: * buildNodesModel(cur, name.toString(), level); } else
844: */
845: if (te instanceof Controller) {
846: name.append(spaces(level));
847: name.append(parent_name);
848: name.append(cur.getName());
849: TreeNodeWrapper tnw = new TreeNodeWrapper(cur, name
850: .toString());
851: targetNodesModel.addElement(tnw);
852: name = new StringBuffer();
853: name.append(cur.getName());
854: name.append(seperator);
855: buildNodesModel(cur, name.toString(), level + 1);
856: } else if (te instanceof TestPlan
857: || te instanceof WorkBench) {
858: name.append(cur.getName());
859: name.append(seperator);
860: buildNodesModel(cur, name.toString(), 0);
861: }
862: // Ignore everything else
863: }
864: }
865: }
866:
867: private String spaces(int level) {
868: int multi = 4;
869: StringBuffer spaces = new StringBuffer(level * multi);
870: for (int i = 0; i < level * multi; i++) {
871: spaces.append(" "); // $NON-NLS-1$
872: }
873: return spaces.toString();
874: }
875:
876: public void setNode(JMeterTreeNode node) {
877: getNamePanel().setNode(node);
878: }
879: }
880:
881: class TreeNodeWrapper {
882: private JMeterTreeNode tn;
883:
884: private String label;
885:
886: private TreeNodeWrapper() {
887: };
888:
889: public TreeNodeWrapper(JMeterTreeNode tn, String label) {
890: this .tn = tn;
891: this .label = label;
892: }
893:
894: public JMeterTreeNode getTreeNode() {
895: return tn;
896: }
897:
898: public String toString() {
899: return label;
900: }
901: }
|