KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > config > gui > ArgumentsPanel


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/config/gui/ArgumentsPanel.java,v 1.24 2004/03/05 01:33:48 sebb Exp $
2
/*
3  * Copyright 2001-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.config.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.Component JavaDoc;
23 import java.awt.FlowLayout JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.ActionListener JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import javax.swing.BorderFactory JavaDoc;
30 import javax.swing.Box JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JLabel JavaDoc;
33 import javax.swing.JPanel JavaDoc;
34 import javax.swing.JTable JavaDoc;
35 import javax.swing.ListSelectionModel JavaDoc;
36 import javax.swing.table.TableCellEditor JavaDoc;
37
38 import junit.framework.TestCase;
39
40 import org.apache.jmeter.config.Argument;
41 import org.apache.jmeter.config.Arguments;
42 import org.apache.jmeter.testelement.TestElement;
43 import org.apache.jmeter.testelement.property.PropertyIterator;
44 import org.apache.jmeter.util.JMeterUtils;
45 import org.apache.jorphan.gui.ObjectTableModel;
46
47 /**
48  * A GUI panel allowing the user to enter name-value argument pairs. These
49  * arguments (or parameters) are usually used to provide configuration values
50  * for some other component.
51  *
52  * @version $Revision: 1.24 $ on $Date: 2004/03/05 01:33:48 $
53  */

54 public class ArgumentsPanel
55     extends AbstractConfigGui
56     implements ActionListener JavaDoc
57 {
58         
59     /** The title label for this component. */
60     private JLabel JavaDoc tableLabel;
61
62     /** The table containing the list of arguments. */
63     private transient JTable JavaDoc table;
64     
65     /** The model for the arguments table. */
66     protected transient ObjectTableModel tableModel;
67
68     /** A button for adding new arguments to the table. */
69     private JButton JavaDoc add;
70     
71     /** A button for removing arguments from the table. */
72     private JButton JavaDoc delete;
73
74     /**
75      * Boolean indicating whether this component is a standalong component or
76      * it is intended to be used as a subpanel for another component.
77      */

78     private boolean standalone = true;
79
80     /** Command for adding a row to the table. */
81     private static final String JavaDoc ADD = "add";
82     
83     /** Command for removing a row from the table. */
84     private static final String JavaDoc DELETE = "delete";
85
86     public static final String JavaDoc[] COLUMN_NAMES =
87         {
88             JMeterUtils.getResString("name"),
89             JMeterUtils.getResString("value"),
90             JMeterUtils.getResString("metadata")
91         };
92
93     /**
94      * Create a new ArgumentsPanel as a standalone component.
95      */

96     public ArgumentsPanel()
97     {
98         tableLabel= new JLabel JavaDoc(JMeterUtils.getResString("user_defined_variables"));
99         standalone = true;
100         init();
101     }
102
103     /**
104      * Create a new ArgumentsPanel as an embedded component, using the specified
105      * title.
106      *
107      * @param label the title for the component.
108      */

109     public ArgumentsPanel(String JavaDoc label)
110     {
111         tableLabel = new JLabel JavaDoc(label);
112         standalone = false;
113         init();
114     }
115
116     /**
117      * This is the list of menu categories this gui component will be available
118      * under.
119      *
120      * @return a Collection of Strings, where each element is one of the
121      * constants defined in MenuFactory
122      */

123     public Collection JavaDoc getMenuCategories()
124     {
125         if (standalone)
126         {
127             return super.getMenuCategories();
128         }
129         else
130         {
131             return null;
132         }
133     }
134     
135     public String JavaDoc getLabelResource()
136     {
137         return "user_defined_variables";
138     }
139
140     /* Implements JMeterGUIComponent.createTestElement() */
141     public TestElement createTestElement()
142     {
143         Arguments args = new Arguments();
144         modifyTestElement(args);
145         return args;
146     }
147
148     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
149     public void modifyTestElement(TestElement args)
150     {
151         stopTableEditing();
152         Iterator JavaDoc modelData = tableModel.iterator();
153         Arguments arguments = null;
154         if (args instanceof Arguments)
155         {
156             arguments = (Arguments) args;
157             arguments.clear();
158             while (modelData.hasNext())
159             {
160                 Argument arg = (Argument) modelData.next();
161                 arg.setMetaData("=");
162                 arguments.addArgument(arg);
163             }
164         }
165         this.configureTestElement(args);
166     }
167
168     /**
169      * A newly created component can be initialized with the contents of
170      * a Test Element object by calling this method. The component is
171      * responsible for querying the Test Element object for the
172      * relevant information to display in its GUI.
173      *
174      * @param el the TestElement to configure
175      */

176      public void configure(TestElement el)
177     {
178         super.configure(el);
179         if (el instanceof Arguments)
180         {
181             tableModel.clearData();
182             PropertyIterator iter = ((Arguments) el).iterator();
183             while (iter.hasNext())
184             {
185                 Argument arg = (Argument) iter.next().getObjectValue();
186                 tableModel.addRow(arg);
187             }
188         }
189         checkDeleteStatus();
190     }
191
192     /**
193      * Get the table used to enter arguments.
194      *
195      * @return the table used to enter arguments
196      */

197     protected JTable JavaDoc getTable()
198     {
199         return table;
200     }
201
202     /**
203      * Get the title label for this component.
204      *
205      * @return the title label displayed with the table
206      */

207     protected JLabel JavaDoc getTableLabel()
208     {
209         return tableLabel;
210     }
211
212     /**
213      * Get the button used to delete rows from the table.
214      *
215      * @return the button used to delete rows from the table
216      */

217     protected JButton JavaDoc getDeleteButton()
218     {
219         return delete;
220     }
221
222     /**
223      * Get the button used to add rows to the table.
224      *
225      * @return the button used to add rows to the table
226      */

227     protected JButton JavaDoc getAddButton()
228     {
229         return add;
230     }
231
232     /**
233      * Enable or disable the delete button depending on whether or not there
234      * is a row to be deleted.
235      */

236     protected void checkDeleteStatus()
237     {
238         // Disable DELETE if there are no rows in the table to delete.
239
if (tableModel.getRowCount() == 0)
240         {
241             delete.setEnabled(false);
242         }
243         else
244         {
245             delete.setEnabled(true);
246         }
247     }
248
249     /**
250      * Clear all rows from the table.
251      * T.Elanjchezhiyan(chezhiyan@siptech.co.in)
252      */

253     public void clear()
254     {
255         stopTableEditing();
256         tableModel.clearData();
257     }
258
259     /**
260      * Invoked when an action occurs. This implementation supports the add
261      * and delete buttons.
262      *
263      * @param e the event that has occurred
264      */

265     public void actionPerformed(ActionEvent JavaDoc e)
266     {
267         String JavaDoc action = e.getActionCommand();
268         if (action.equals(DELETE))
269         {
270             deleteArgument();
271         }
272         else if (action.equals(ADD))
273         {
274             addArgument();
275         }
276     }
277
278     /**
279      * Remove the currently selected argument from the table.
280      */

281     protected void deleteArgument()
282     {
283         // If a table cell is being edited, we must cancel the editing before
284
// deleting the row
285
if (table.isEditing())
286         {
287             TableCellEditor JavaDoc cellEditor = table.getCellEditor(
288                     table.getEditingRow(),
289                     table.getEditingColumn());
290             cellEditor.cancelCellEditing();
291         }
292
293         int rowSelected = table.getSelectedRow();
294         if (rowSelected >= 0)
295         {
296             tableModel.removeRow(rowSelected);
297             tableModel.fireTableDataChanged();
298
299             // Disable DELETE if there are no rows in the table to delete.
300
if (tableModel.getRowCount() == 0)
301             {
302                 delete.setEnabled(false);
303             }
304
305             // Table still contains one or more rows, so highlight (select)
306
// the appropriate one.
307
else
308             {
309                 int rowToSelect = rowSelected;
310
311                 if (rowSelected >= tableModel.getRowCount())
312                 {
313                     rowToSelect = rowSelected - 1;
314                 }
315
316                 table.setRowSelectionInterval(rowToSelect, rowToSelect);
317             }
318         }
319     }
320
321     /**
322      * Add a new argument row to the table.
323      */

324     protected void addArgument()
325     {
326         // If a table cell is being edited, we should accept the current value
327
// and stop the editing before adding a new row.
328
stopTableEditing();
329
330         tableModel.addRow(makeNewArgument());
331
332         // Enable DELETE (which may already be enabled, but it won't hurt)
333
delete.setEnabled(true);
334
335         // Highlight (select) the appropriate row.
336
int rowToSelect = tableModel.getRowCount() - 1;
337         table.setRowSelectionInterval(rowToSelect, rowToSelect);
338     }
339
340     /**
341      * Create a new Argument object.
342      * @return a new Argument object
343      */

344     protected Object JavaDoc makeNewArgument()
345     {
346         return new Argument("", "");
347     }
348
349     /**
350      * Stop any editing that is currently being done on the table. This will
351      * save any changes that have already been made.
352      */

353     protected void stopTableEditing()
354     {
355         if (table.isEditing())
356         {
357             TableCellEditor JavaDoc cellEditor =
358                 table.getCellEditor(
359                     table.getEditingRow(),
360                     table.getEditingColumn());
361             cellEditor.stopCellEditing();
362         }
363     }
364
365     /**
366      * Initialize the table model used for the arguments table.
367      */

368     protected void initializeTableModel()
369     {
370         tableModel =
371             new ObjectTableModel(
372                 new String JavaDoc[] {
373                     COLUMN_NAMES[0],
374                     COLUMN_NAMES[1] },
375                 new String JavaDoc[] { "name", "value" },
376                 new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class },
377                 new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class },
378                 new Argument());
379     }
380
381     /**
382      * Resize the table columns to appropriate widths.
383      * @param table the table to resize columns for
384      */

385     protected void sizeColumns(JTable JavaDoc table)
386     {
387     }
388
389     /**
390      * Create the main GUI panel which contains the argument table.
391      *
392      * @return the main GUI panel
393      */

394     private Component JavaDoc makeMainPanel()
395     {
396         initializeTableModel();
397         table = new JTable JavaDoc(tableModel);
398         table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
399         return makeScrollPane(table);
400     }
401
402     /**
403      * Create a panel containing the title label for the table.
404      *
405      * @return a panel containing the title label
406      */

407     protected Component JavaDoc makeLabelPanel()
408     {
409         JPanel JavaDoc labelPanel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.CENTER));
410         labelPanel.add(tableLabel);
411         return labelPanel;
412     }
413
414     /**
415      * Create a panel containing the add and delete buttons.
416      *
417      * @return a GUI panel containing the buttons
418      */

419     private JPanel JavaDoc makeButtonPanel()
420     {
421         add = new JButton JavaDoc(JMeterUtils.getResString("add"));
422         add.setActionCommand(ADD);
423         add.setEnabled(true);
424         
425         delete = new JButton JavaDoc(JMeterUtils.getResString("delete"));
426         delete.setActionCommand(DELETE);
427         
428         checkDeleteStatus();
429         
430         JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
431         buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0, 10));
432         add.addActionListener(this);
433         delete.addActionListener(this);
434         buttonPanel.add(add);
435         buttonPanel.add(delete);
436         return buttonPanel;
437     }
438
439     /**
440      * Initialize the components and layout of this component.
441      */

442     private void init()
443     {
444         JPanel JavaDoc p= this;
445         
446         if (standalone)
447         {
448             setLayout(new BorderLayout JavaDoc(0,5));
449             setBorder(makeBorder());
450             add(makeTitlePanel(), BorderLayout.NORTH);
451             p= new JPanel JavaDoc();
452         }
453
454         p.setLayout(new BorderLayout JavaDoc());
455
456         p.add(makeLabelPanel(), BorderLayout.NORTH);
457         p.add(makeMainPanel(), BorderLayout.CENTER);
458         // Force a minimum table height of 70 pixels
459
p.add(Box.createVerticalStrut(70), BorderLayout.WEST);
460         p.add(makeButtonPanel(), BorderLayout.SOUTH);
461
462         if (standalone)
463         {
464             add(p, BorderLayout.CENTER);
465         }
466
467         table.revalidate();
468         sizeColumns(table);
469     }
470
471
472     /**
473      * Tests for the ArgumentsPanel component.
474      */

475     public static class Test extends TestCase
476     {
477         /**
478          * Create a new test.
479          * @param name the name of the test
480          */

481         public Test(String JavaDoc name)
482         {
483             super(name);
484         }
485
486         /**
487          * Test that adding an argument to the table results in an appropriate
488          * TestElement being created.
489          *
490          * @throws Exception if an exception occurred during the test
491          */

492         public void testArgumentCreation() throws Exception JavaDoc
493         {
494             ArgumentsPanel gui = new ArgumentsPanel();
495             gui.tableModel.addRow(new Argument());
496             gui.tableModel.setValueAt("howdy", 0, 0);
497             gui.tableModel.addRow(new Argument());
498             gui.tableModel.setValueAt("doody", 0, 1);
499
500             assertEquals(
501                 "=",
502                 ((Argument) ((Arguments) gui.createTestElement())
503                     .getArguments()
504                     .get(0)
505                     .getObjectValue())
506                     .getMetaData());
507         }
508     }
509 }
510
Popular Tags