e.g. Calendar Search Help
You must enter a value before pressing Search
jmeter

Class: org.apache.jmeter.config.gui.ArgumentsPanel   ©

 OK to copy?
001 // $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 $
002 /*
003  * Copyright 2001-2004 The Apache Software Foundation.
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * 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.config.gui;
020 
021 import java.awt.BorderLayout;
022 import java.awt.Component;
023 import java.awt.FlowLayout;
024 import java.awt.event.ActionEvent;
025 import java.awt.event.ActionListener;
026 import java.util.Collection;
027 import java.util.Iterator;
028 
029 import javax.swing.BorderFactory;
030 import javax.swing.Box;
031 import javax.swing.JButton;
032 import javax.swing.JLabel;
033 import javax.swing.JPanel;
034 import javax.swing.JTable;
035 import javax.swing.ListSelectionModel;
036 import javax.swing.table.TableCellEditor;
037 
038 import junit.framework.TestCase;
039 
040 import org.apache.jmeter.config.Argument;
041 import org.apache.jmeter.config.Arguments;
042 import org.apache.jmeter.testelement.TestElement;
043 import org.apache.jmeter.testelement.property.PropertyIterator;
044 import org.apache.jmeter.util.JMeterUtils;
045 import org.apache.jorphan.gui.ObjectTableModel;
046 
047 /**
048  * A GUI panel allowing the user to enter name-value argument pairs.  These
049  * arguments (or parameters) are usually used to provide configuration values
050  * for some other component.
051  * 
052  * @version   $Revision: 1.24 $ on $Date: 2004/03/05 01:33:48 $
053  */
054 public class ArgumentsPanel
055     extends AbstractConfigGui
056     implements  ActionListener
057 {
058         
059     /** The title label for this component. */    
060     private JLabel tableLabel;
061 
062     /** The table containing the list of arguments. */
063     private transient JTable table;
064     
065     /** The model for the arguments table. */
066     protected transient ObjectTableModel tableModel;
067 
068     /** A button for adding new arguments to the table. */
069     private JButton add;
070     
071     /** A button for removing arguments from the table. */
072     private JButton delete;
073 
074     /**
075      * Boolean indicating whether this component is a standalong component or
076      * it is intended to be used as a subpanel for another component.
077      */
078     private boolean standalone = true;
079 
080     /** Command for adding a row to the table. */ 
081     private static final String ADD = "add";
082     
083     /** Command for removing a row from the table. */
084     private static final String DELETE = "delete";
085 
086     public static final String[] COLUMN_NAMES =
087         {
088             JMeterUtils.getResString("name"),
089             JMeterUtils.getResString("value"),
090             JMeterUtils.getResString("metadata")
091         };
092 
093     /**
094      * Create a new ArgumentsPanel as a standalone component. 
095      */
096     public ArgumentsPanel()
097     {
098         tableLabel= new JLabel(JMeterUtils.getResString("user_defined_variables"));
099         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 label)
110     {
111         tableLabel = new JLabel(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 getMenuCategories()
124     {
125         if (standalone)
126         {
127             return super.getMenuCategories();
128         }
129         else
130         {
131             return null;
132         } 
133     }    
134     
135     public String 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 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 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 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 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 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 e)
266     {
267         String 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 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 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 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[] {
373                     COLUMN_NAMES[0],
374                     COLUMN_NAMES[1] },
375                 new String[] { "name", "value" },
376                 new Class[] { String.class, String.class },
377                 new Class[] { String.class, String.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 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 makeMainPanel()
395     {
396         initializeTableModel();
397         table = new JTable(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 makeLabelPanel()
408     {
409         JPanel labelPanel = new JPanel(new FlowLayout(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 makeButtonPanel()
420     {
421         add = new JButton(JMeterUtils.getResString("add"));
422         add.setActionCommand(ADD);
423         add.setEnabled(true);
424         
425         delete = new JButton(JMeterUtils.getResString("delete"));
426         delete.setActionCommand(DELETE);
427         
428         checkDeleteStatus();
429         
430         JPanel buttonPanel = new JPanel();
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 p= this;
445         
446         if (standalone)
447         {
448             setLayout(new BorderLayout(0,5));
449             setBorder(makeBorder());
450             add(makeTitlePanel(), BorderLayout.NORTH);
451             p= new JPanel();
452         }
453 
454         p.setLayout(new BorderLayout());
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 
Rate467         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 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
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 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
467 0% of 0