KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > protocol > http > proxy > gui > ProxyControlGui


1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/http/org/apache/jmeter/protocol/http/proxy/gui/ProxyControlGui.java,v 1.41.2.1 2004/10/23 17:58:32 sebb Exp $
2
/*
3  * Copyright 2002-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.protocol.http.proxy.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.FocusEvent JavaDoc;
26 import java.awt.event.FocusListener JavaDoc;
27 import java.awt.event.KeyEvent JavaDoc;
28 import java.awt.event.KeyListener JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33
34 import javax.swing.BorderFactory JavaDoc;
35 import javax.swing.Box JavaDoc;
36 import javax.swing.DefaultComboBoxModel JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JCheckBox JavaDoc;
39 import javax.swing.JComboBox JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JOptionPane JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JScrollPane JavaDoc;
44 import javax.swing.JTable JavaDoc;
45 import javax.swing.JTextField JavaDoc;
46
47 import org.apache.jmeter.control.Controller;
48 import org.apache.jmeter.control.gui.LogicControllerGui;
49 import org.apache.jmeter.engine.util.ValueReplacer;
50 import org.apache.jmeter.functions.InvalidVariableException;
51 import org.apache.jmeter.gui.GuiPackage;
52 import org.apache.jmeter.gui.JMeterGUIComponent;
53 import org.apache.jmeter.gui.UnsharedComponent;
54 import org.apache.jmeter.gui.tree.JMeterTreeNode;
55 import org.apache.jmeter.gui.util.HorizontalPanel;
56 import org.apache.jmeter.gui.util.MenuFactory;
57 import org.apache.jmeter.gui.util.PowerTableModel;
58 import org.apache.jmeter.protocol.http.proxy.ProxyControl;
59 import org.apache.jmeter.testelement.TestElement;
60 import org.apache.jmeter.testelement.TestPlan;
61 import org.apache.jmeter.testelement.WorkBench;
62 import org.apache.jmeter.testelement.property.PropertyIterator;
63 import org.apache.jmeter.util.JMeterUtils;
64 import org.apache.jorphan.logging.LoggingManager;
65 import org.apache.log.Logger;
66
67 /**
68  * @version $Revision: 1.41.2.1 $ updated on $Date: 2004/10/23 17:58:32 $
69  */

70 public class ProxyControlGui
71     extends LogicControllerGui
72     implements
73         JMeterGUIComponent,
74         ActionListener JavaDoc,
75         KeyListener JavaDoc,
76         FocusListener JavaDoc,
77         UnsharedComponent
78 {
79     private static transient Logger log = LoggingManager.getLoggerForClass();
80     private JTextField JavaDoc portField;
81
82    /**
83     * Used to indicate that HTTP request headers should be captured.
84     * The default is to capture the HTTP request headers,
85     * which are specific to particular browser settings.
86     */

87     private JCheckBox JavaDoc httpHeaders;
88
89     /**
90      * Whether to group requests together based on inactivity separation
91      * periods -- and how to handle such grouping afterwards.
92      */

93     private JComboBox JavaDoc groupingMode;
94
95     /**
96      * Add an Assertion to the first sample of each set
97      */

98     private JCheckBox JavaDoc addAssertions;
99     
100     /**
101      * Set/clear the Use Keep-Alive box on the samplers
102      * (default is true)
103      */

104     private JCheckBox JavaDoc useKeepAlive;
105
106     /**
107      * List of available target controllers
108      */

109     private JComboBox JavaDoc targetNodes;
110     private DefaultComboBoxModel JavaDoc targetNodesModel;
111     
112     private ProxyControl model;
113
114     private JTable JavaDoc excludeTable;
115     private PowerTableModel excludeModel;
116     private JTable JavaDoc includeTable;
117     private PowerTableModel includeModel;
118     private static final String JavaDoc CHANGE_TARGET = "change_target";
119         
120     private JButton JavaDoc stop, start, restart;
121     private static final String JavaDoc STOP = "stop";
122     private static final String JavaDoc START = "start";
123     private static final String JavaDoc RESTART = "restart";
124     private static final String JavaDoc ADD_INCLUDE = "add_include";
125     private static final String JavaDoc ADD_EXCLUDE = "add_exclude";
126     private static final String JavaDoc DELETE_INCLUDE = "delete_include";
127     private static final String JavaDoc DELETE_EXCLUDE = "delete_exclude";
128
129     private static final String JavaDoc INCLUDE_COL =
130         JMeterUtils.getResString("patterns_to_include");
131     private static final String JavaDoc EXCLUDE_COL =
132         JMeterUtils.getResString("patterns_to_exclude");
133
134     public ProxyControlGui()
135     {
136         super();
137         log.debug("Creating ProxyControlGui");
138         init();
139     }
140
141     public TestElement createTestElement()
142     {
143         model = makeProxyControl();
144         log.debug("creating/configuring model = " + model);
145         modifyTestElement(model);
146         return model;
147     }
148
149     protected ProxyControl makeProxyControl()
150         {
151             ProxyControl local = new ProxyControl();
152             return local;
153         }
154
155     /**
156      * Modifies a given TestElement to mirror the data in the gui components.
157      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
158      */

159     public void modifyTestElement(TestElement el)
160     {
161         configureTestElement(el);
162         if(el instanceof ProxyControl)
163         {
164             model = (ProxyControl)el;
165             model.setPort(portField.getText());
166             setIncludeListInProxyControl(model);
167             setExcludeListInProxyControl(model);
168             model.setCaptureHttpHeaders(httpHeaders.isSelected());
169             model.setGroupingMode(groupingMode.getSelectedIndex());
170             model.setAssertions(addAssertions.isSelected());
171             model.setUseKeepAlive(useKeepAlive.isSelected());
172             TreeNodeWrapper nw= (TreeNodeWrapper)targetNodes.getSelectedItem();
173             if (nw == null)
174             {
175                 model.setTarget(null);
176             }
177             else
178             {
179                 model.setTarget(nw.getTreeNode());
180             }
181         }
182     }
183
184     protected void setIncludeListInProxyControl(ProxyControl element)
185     {
186         List JavaDoc includeList = getDataList(includeModel, INCLUDE_COL);
187         element.setIncludeList(includeList);
188     }
189
190     protected void setExcludeListInProxyControl(ProxyControl element)
191     {
192         List JavaDoc excludeList = getDataList(excludeModel, EXCLUDE_COL);
193         element.setExcludeList(excludeList);
194     }
195
196     private List JavaDoc getDataList(PowerTableModel model, String JavaDoc colName)
197     {
198         String JavaDoc[] dataArray = model.getData().getColumn(colName);
199         List JavaDoc list = new LinkedList JavaDoc();
200         for (int i = 0; i < dataArray.length; i++)
201         {
202             list.add(dataArray[i]);
203         }
204         return list;
205     }
206
207     public String JavaDoc getLabelResource()
208     {
209         return "proxy_title";
210     }
211
212     public Collection JavaDoc getMenuCategories()
213     {
214         return Arrays.asList(new String JavaDoc[] { MenuFactory.NON_TEST_ELEMENTS });
215     }
216
217     public void configure(TestElement element)
218     {
219         log.debug("Configuring gui with " + element);
220         super.configure(element);
221         model = (ProxyControl)element;
222         portField.setText(model.getPropertyAsString(ProxyControl.PORT));
223         httpHeaders.setSelected(model.getPropertyAsBoolean(ProxyControl.CAPTURE_HTTP_HEADERS));
224         groupingMode.setSelectedIndex(model.getPropertyAsInt(ProxyControl.GROUPING_MODE));
225         addAssertions.setSelected(model.getPropertyAsBoolean(ProxyControl.ADD_ASSERTIONS));
226         useKeepAlive.setSelected(model.getPropertyAsBoolean(ProxyControl.USE_KEEPALIVE,true));
227         
228         reinitializeTargetCombo();
229
230         populateTable(includeModel, model.getIncludePatterns().iterator());
231         populateTable(excludeModel, model.getExcludePatterns().iterator());
232         repaint();
233     }
234
235     private void populateTable(PowerTableModel model, PropertyIterator iter)
236     {
237         model.clearData();
238         while (iter.hasNext())
239         {
240             model.addRow(new Object JavaDoc[] { iter.next().getStringValue()});
241         }
242         model.fireTableDataChanged();
243     }
244
245     public void focusLost(FocusEvent JavaDoc e)
246     {
247         try
248         {
249             ((JTable JavaDoc) e.getSource()).getCellEditor().stopCellEditing();
250         }
251         catch (Exception JavaDoc err)
252         {}
253     }
254
255     public void focusGained(FocusEvent JavaDoc e)
256     {}
257
258     /****************************************
259      * !ToDo (Method description)
260      *
261      *@param action !ToDo (Parameter description)
262      ***************************************/

263     public void actionPerformed(ActionEvent JavaDoc action)
264     {
265         String JavaDoc command = action.getActionCommand();
266
267         if (command.equals(STOP))
268         {
269             model.stopProxy();
270             stop.setEnabled(false);
271             start.setEnabled(true);
272             restart.setEnabled(false);
273         }
274         else if (command.equals(START))
275         {
276             startProxy();
277         }
278         else if (command.equals(RESTART))
279         {
280             model.stopProxy();
281             startProxy();
282         }
283         else if ( command.equals(ProxyControl.CAPTURE_HTTP_HEADERS)
284                 || command.equals(ProxyControl.ADD_ASSERTIONS)
285                 || command.equals(ProxyControl.GROUPING_MODE)
286                 || command.equals(ProxyControl.USE_KEEPALIVE)
287                  )
288         {
289             enableRestart();
290         }
291         else if (command.equals(ADD_EXCLUDE))
292         {
293             excludeModel.addNewRow();
294             excludeModel.fireTableDataChanged();
295             enableRestart();
296         }
297         else if (command.equals(ADD_INCLUDE))
298         {
299             includeModel.addNewRow();
300             includeModel.fireTableDataChanged();
301             enableRestart();
302         }
303         else if (command.equals(DELETE_EXCLUDE))
304         {
305             excludeModel.removeRow(excludeTable.getSelectedRow());
306             excludeModel.fireTableDataChanged();
307             enableRestart();
308         }
309         else if (command.equals(DELETE_INCLUDE))
310         {
311             includeModel.removeRow(includeTable.getSelectedRow());
312             includeModel.fireTableDataChanged();
313             enableRestart();
314         }
315         else if (command.equals(CHANGE_TARGET))
316         {
317             log.debug("Change target "+targetNodes.getSelectedItem());
318             log.debug("In model "+model);
319             TreeNodeWrapper nw= (TreeNodeWrapper)targetNodes.getSelectedItem();
320             model.setTarget(nw.getTreeNode());
321         }
322     }
323
324     private void startProxy()
325     {
326         ValueReplacer replacer = GuiPackage.getInstance().getReplacer();
327         modifyTestElement(model);
328         try
329         {
330             replacer.replaceValues(model);
331             model.startProxy();
332             start.setEnabled(false);
333             stop.setEnabled(true);
334             restart.setEnabled(false);
335         }
336         catch (InvalidVariableException e)
337         {
338             JOptionPane.showMessageDialog(
339                 this,
340                 JMeterUtils.getResString("invalid_variables"),
341                 "Error",
342                 JOptionPane.ERROR_MESSAGE);
343         }
344     }
345
346     private void enableRestart()
347     {
348         if (stop.isEnabled())
349         {
350             restart.setEnabled(true);
351         }
352     }
353
354     /****************************************
355      * !ToDo (Method description)
356      *
357      *@param e !ToDo (Parameter description)
358      ***************************************/

359     public void keyPressed(KeyEvent JavaDoc e)
360     {}
361
362     /****************************************
363      * !ToDo (Method description)
364      *
365      *@param e !ToDo (Parameter description)
366      ***************************************/

367     public void keyTyped(KeyEvent JavaDoc e)
368     {}
369
370     /****************************************
371      * !ToDo (Method description)
372      *
373      *@param e !ToDo (Parameter description)
374      ***************************************/

375     public void keyReleased(KeyEvent JavaDoc e)
376     {
377         String JavaDoc fieldName = e.getComponent().getName();
378
379         if (fieldName.equals(ProxyControl.PORT))
380         {
381             try
382             {
383                 Integer.parseInt(portField.getText());
384             }
385             catch (NumberFormatException JavaDoc nfe)
386             {
387                 if (portField.getText().length() > 0)
388                 {
389                     JOptionPane.showMessageDialog(
390                         this,
391                         "You must enter a valid number",
392                         "Invalid data",
393                         JOptionPane.WARNING_MESSAGE);
394
395                     // Right now, the cleanest thing to do is simply clear the
396
// entire text field. We do not want to set the text to
397
// the default because that would be confusing to the user.
398
// For example, the user typed "5t" instead of "56". After
399
// the user closes the error dialog, the text would change
400
// from "5t" to "1". A litle confusing. If anything, it
401
// should display just "5". Future enhancement...
402
portField.setText("");
403                 }
404             }
405             enableRestart();
406         }
407     }
408
409     private void init()
410     {
411         setLayout(new BorderLayout JavaDoc(0, 5));
412         setBorder(makeBorder());
413
414         add(makeTitlePanel(), BorderLayout.NORTH);
415
416         JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
417         
418         Box JavaDoc myBox = Box.createVerticalBox();
419         myBox.add(createPortPanel());
420         myBox.add(Box.createVerticalStrut(5));
421         myBox.add(createTargetPanel());
422         myBox.add(Box.createVerticalStrut(5));
423         myBox.add(createGroupingPanel());
424         myBox.add(Box.createVerticalStrut(5));
425         mainPanel.add(myBox, BorderLayout.NORTH);
426         
427         Box JavaDoc includeExcludePanel = Box.createVerticalBox();
428         includeExcludePanel.add(createIncludePanel());
429         includeExcludePanel.add(createExcludePanel());
430         mainPanel.add(includeExcludePanel, BorderLayout.CENTER);
431
432         mainPanel.add(createControls(), BorderLayout.SOUTH);
433
434         add(mainPanel, BorderLayout.CENTER);
435     }
436
437     private JPanel JavaDoc createControls()
438     {
439         start = new JButton JavaDoc(JMeterUtils.getResString("start"));
440         start.addActionListener(this);
441         start.setActionCommand(START);
442         start.setEnabled(true);
443
444         stop = new JButton JavaDoc(JMeterUtils.getResString("stop"));
445         stop.addActionListener(this);
446         stop.setActionCommand(STOP);
447         stop.setEnabled(false);
448
449         restart = new JButton JavaDoc(JMeterUtils.getResString("restart"));
450         restart.addActionListener(this);
451         restart.setActionCommand(RESTART);
452         restart.setEnabled(false);
453
454         JPanel JavaDoc panel = new JPanel JavaDoc();
455         panel.add(start);
456         panel.add(stop);
457         panel.add(restart);
458         return panel;
459     }
460
461     private JPanel JavaDoc createPortPanel()
462     {
463         portField = new JTextField JavaDoc(ProxyControl.DEFAULT_PORT_S, 8);
464         portField.setName(ProxyControl.PORT);
465         portField.addKeyListener(this);
466
467         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("port"));
468         label.setLabelFor(portField);
469
470         httpHeaders = new JCheckBox JavaDoc(JMeterUtils.getResString("proxy_headers"));
471         httpHeaders.setName(ProxyControl.CAPTURE_HTTP_HEADERS);
472         httpHeaders.setSelected(true); //maintain original default
473
httpHeaders.addActionListener(this);
474         httpHeaders.setActionCommand(ProxyControl.CAPTURE_HTTP_HEADERS);
475
476         addAssertions = new JCheckBox JavaDoc(JMeterUtils.getResString("proxy_assertions"));
477         addAssertions.setName(ProxyControl.ADD_ASSERTIONS);
478         addAssertions.setSelected(false);
479         addAssertions.addActionListener(this);
480         addAssertions.setActionCommand(ProxyControl.ADD_ASSERTIONS);
481
482         useKeepAlive = new JCheckBox JavaDoc(JMeterUtils.getResString("proxy_usekeepalive"));
483         useKeepAlive.setName(ProxyControl.USE_KEEPALIVE);
484         useKeepAlive.setSelected(true);
485         useKeepAlive.addActionListener(this);
486         useKeepAlive.setActionCommand(ProxyControl.USE_KEEPALIVE);
487
488         HorizontalPanel panel = new HorizontalPanel();
489         panel.add(label);
490         panel.add(portField);
491
492         panel.add(Box.createHorizontalStrut(10));
493         panel.add(httpHeaders);
494
495         panel.add(useKeepAlive);
496         panel.add(addAssertions);
497
498         return panel;
499     }
500
501     private JPanel JavaDoc createTargetPanel()
502     {
503         targetNodesModel= new DefaultComboBoxModel JavaDoc();
504         targetNodes = new JComboBox JavaDoc(targetNodesModel);
505         targetNodes.setActionCommand(CHANGE_TARGET);
506         // Action listener will be added later
507

508         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("proxy_target"));
509         label.setLabelFor(targetNodes);
510         
511         HorizontalPanel panel = new HorizontalPanel();
512         panel.add(label);
513         panel.add(targetNodes);
514
515         /* This listener subscription prevents freeing up the GUI when it's no longer in use
516          * (e.g. on locale change)... plus causes some anoying NPEs in the GUI instance
517          * created by the menu manager just to find out our name and which menus we
518          * want to be in...
519          * ... plus I don't think it's really necessary: configure(TestElement) already takes
520          * care of reinitializing the target combo when we come back to it. And I can't see how
521          * the tree can change in a relevant way without we leaving this GUI (since it is very
522          * unlikely that we will want to record into one of the controllers created by the proxy).
523          * I'll comment it out for the time being:
524          * TODO: remove once we're convinced it's really unnecessary.
525          */

526         /*try
527         {
528             Class addToTree =
529                 Class.forName("org.apache.jmeter.gui.action.AddToTree");
530             Class remove = Class.forName("org.apache.jmeter.gui.action.Remove");
531             ActionListener listener = new ActionListener()
532             {
533                 public void actionPerformed(ActionEvent e)
534                 {
535                     reinitializeTargetCombo();
536                 }
537             };
538             ActionRouter ar = ActionRouter.getInstance();
539             ar.addPostActionListener(addToTree, listener);
540             ar.addPostActionListener(remove, listener);
541         }
542         catch (ClassNotFoundException e)
543         {
544             // This should never happen -- throw an Error:
545             throw new Error(e.toString());//JDK1.4: remove .toString()
546         }*/

547         
548         return panel;
549     }
550
551     private JPanel JavaDoc createGroupingPanel()
552     {
553         DefaultComboBoxModel JavaDoc m= new DefaultComboBoxModel JavaDoc();
554         // Note: position of these elements in the menu *must* match the
555
// corresponding ProxyControl.GROUPING_* values.
556
m.addElement(JMeterUtils.getResString("grouping_no_groups"));
557         m.addElement(JMeterUtils.getResString("grouping_add_separators"));
558         m.addElement(JMeterUtils.getResString("grouping_in_controllers"));
559         m.addElement(JMeterUtils.getResString("grouping_store_first_only"));
560         groupingMode = new JComboBox JavaDoc(m);
561         groupingMode.setName(ProxyControl.GROUPING_MODE);
562         groupingMode.setSelectedIndex(0);
563         groupingMode.addActionListener(this);
564         groupingMode.setActionCommand(ProxyControl.GROUPING_MODE);
565
566         JLabel JavaDoc label2 = new JLabel JavaDoc(JMeterUtils.getResString("grouping_mode"));
567         label2.setLabelFor(groupingMode);
568         
569         HorizontalPanel panel = new HorizontalPanel();
570         panel.add(label2);
571         panel.add(groupingMode);
572         
573         return panel;
574     }
575
576     private JPanel JavaDoc createIncludePanel()
577     {
578         includeModel = new PowerTableModel(
579                 new String JavaDoc[] { INCLUDE_COL },
580                 new Class JavaDoc[] { String JavaDoc.class });
581         includeTable = new JTable JavaDoc(includeModel);
582         includeTable.setPreferredScrollableViewportSize(new Dimension JavaDoc(100, 50));
583         includeTable.addFocusListener(this);
584
585         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
586         panel.setBorder(BorderFactory.createTitledBorder(
587                 BorderFactory.createEtchedBorder(),
588                 JMeterUtils.getResString("patterns_to_include")));
589
590         panel.add(new JScrollPane JavaDoc(includeTable), BorderLayout.CENTER);
591         panel.add(createTableButtonPanel(ADD_INCLUDE, DELETE_INCLUDE),
592             BorderLayout.SOUTH);
593
594         return panel;
595     }
596
597     private JPanel JavaDoc createExcludePanel()
598     {
599         excludeModel = new PowerTableModel(
600             new String JavaDoc[] { EXCLUDE_COL },
601             new Class JavaDoc[] { String JavaDoc.class });
602         excludeTable = new JTable JavaDoc(excludeModel);
603         excludeTable.setPreferredScrollableViewportSize(new Dimension JavaDoc(100, 50));
604         excludeTable.addFocusListener(this);
605
606         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
607         panel.setBorder(
608             BorderFactory.createTitledBorder(
609                 BorderFactory.createEtchedBorder(),
610                 JMeterUtils.getResString("patterns_to_exclude")));
611
612         panel.add(new JScrollPane JavaDoc(excludeTable), BorderLayout.CENTER);
613         panel.add(createTableButtonPanel(ADD_EXCLUDE, DELETE_EXCLUDE),
614             BorderLayout.SOUTH);
615
616         return panel;
617     }
618
619     private JPanel JavaDoc createTableButtonPanel(
620         String JavaDoc addCommand,
621         String JavaDoc deleteCommand)
622     {
623         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
624
625         JButton JavaDoc addButton = new JButton JavaDoc(JMeterUtils.getResString("add"));
626         addButton.setActionCommand(addCommand);
627         addButton.addActionListener(this);
628         buttonPanel.add(addButton);
629
630         JButton JavaDoc deleteButton = new JButton JavaDoc(JMeterUtils.getResString("delete"));
631         deleteButton.setActionCommand(deleteCommand);
632         deleteButton.addActionListener(this);
633         buttonPanel.add(deleteButton);
634
635         return buttonPanel;
636     }
637
638     private void reinitializeTargetCombo() {
639         log.debug("Reinitializing target combo");
640
641         // Stop action notifications while we shuffle this around:
642
targetNodes.removeActionListener(this);
643         
644         targetNodesModel.removeAllElements();
645         GuiPackage gp = GuiPackage.getInstance();
646         JMeterTreeNode root;
647         if (gp != null)
648         {
649             root =
650                 (JMeterTreeNode) GuiPackage
651                     .getInstance()
652                     .getTreeModel()
653                     .getRoot();
654             targetNodesModel.addElement(
655                 new TreeNodeWrapper(
656                     null,
657                     JMeterUtils.getResString("use_recording_controller")));
658             buildNodesModel(root, "", 0);
659         }
660         TreeNodeWrapper choice = null;
661         for (int i = 0; i < targetNodesModel.getSize(); i++)
662         {
663             choice = (TreeNodeWrapper) targetNodesModel.getElementAt(i);
664             log.debug("Selecting item "+choice+" for model "+model+" in "+this);
665             if (choice.getTreeNode() == model.getTarget()) // .equals caused NPE
666
{
667                 break;
668             }
669         }
670         // Reinstate action notifications:
671
targetNodes.addActionListener(this);
672         // Set the current value:
673
targetNodesModel.setSelectedItem(choice);
674         
675         log.debug("Reinitialization complete");
676     }
677     
678     private void buildNodesModel(
679         JMeterTreeNode node,
680         String JavaDoc parent_name,
681         int level)
682     {
683         String JavaDoc seperator = " > ";
684         if (node != null)
685         {
686             for (int i = 0; i < node.getChildCount(); i++)
687             {
688                 StringBuffer JavaDoc name = new StringBuffer JavaDoc();
689                 JMeterTreeNode cur = (JMeterTreeNode) node.getChildAt(i);
690                 TestElement te = cur.getTestElement();
691 /*
692  * Will never be true.
693  * Probably intended to use org.apache.jmeter.threads.ThreadGroup rather than java.lang.ThreadGroup
694  * However, that does not work correctly; whereas treating it as a Controller does.
695                 if (te instanceof ThreadGroup)
696                 {
697                     name.append(parent_name);
698                     name.append(cur.getName());
699                     name.append(seperator);
700                     buildNodesModel(cur, name.toString(), level);
701                 }
702                 else
703 */

704                 if (te instanceof Controller)
705                 {
706                     name.append(spaces(level));
707                     name.append(parent_name);
708                     name.append(cur.getName());
709                     TreeNodeWrapper tnw =
710                         new TreeNodeWrapper(cur, name.toString());
711                     targetNodesModel.addElement(tnw);
712                     name = new StringBuffer JavaDoc();
713                     name.append(cur.getName());
714                     name.append(seperator);
715                     buildNodesModel(cur, name.toString(), level + 1);
716                 }
717                 else if (te instanceof TestPlan || te instanceof WorkBench)
718                 {
719                     name.append(cur.getName());
720                     name.append(seperator);
721                     buildNodesModel(cur, name.toString(), 0);
722                 }
723                 else {
724                     log.error("Cannot process "+te);
725                 }
726             }
727         }
728     }
729
730     private String JavaDoc spaces(int level)
731     {
732         int multi = 4;
733         StringBuffer JavaDoc spaces = new StringBuffer JavaDoc(level * multi);
734         for (int i = 0; i < level * multi; i++)
735         {
736             spaces.append(" ");
737         }
738         return spaces.toString();
739     }
740     
741     public void setNode(JMeterTreeNode node)
742     {
743         getNamePanel().setNode(node);
744     }
745 }
746
747 class TreeNodeWrapper
748 {
749     private JMeterTreeNode tn;
750     private String JavaDoc label;
751
752     private TreeNodeWrapper()
753     {
754     };
755
756     public TreeNodeWrapper(JMeterTreeNode tn, String JavaDoc label)
757     {
758         this.tn = tn;
759         this.label = label;
760     }
761
762     public JMeterTreeNode getTreeNode()
763     {
764         return tn;
765     }
766
767     public String JavaDoc toString()
768     {
769         return label;
770     }
771 }
772
Popular Tags