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

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

 OK to copy?
001 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/config/gui/SimpleConfigGui.java,v 1.8.2.1 2004/06/12 16:44:34 mstover1 Exp $
002 /*
003  * Copyright 2001-2004 The Apache Software Foundation.
004  * 
005  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006  * use this file except in compliance with the License. You may obtain a copy
007  * 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, WITHOUT
013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014  * License for the specific language governing permissions and limitations
015  * 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.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 
026 import javax.swing.Box;
027 import javax.swing.JButton;
028 import javax.swing.JPanel;
029 import javax.swing.JTable;
030 import javax.swing.ListSelectionModel;
031 import javax.swing.table.TableCellEditor;
032 
033 import org.apache.jmeter.config.ConfigTestElement;
034 import org.apache.jmeter.gui.util.PowerTableModel;
035 import org.apache.jmeter.testelement.TestElement;
036 import org.apache.jmeter.testelement.property.JMeterProperty;
037 import org.apache.jmeter.testelement.property.PropertyIterator;
038 import org.apache.jmeter.testelement.property.StringProperty;
039 import org.apache.jmeter.util.JMeterUtils;
040 import org.apache.jorphan.collections.Data;
041 
042 /**
043  * Default config gui for Configuration Element.
044  * 
045  * @version $Revision: 1.8.2.1 $ on $Date: 2004/06/12 16:44:34 $
046  */
047 public class SimpleConfigGui extends AbstractConfigGui implements
048       ActionListener
049 {
050    /* This class created for enhancement Bug ID 9101. */
051 
052    // TODO: This class looks a lot like ArgumentsPanel. What exactly is the
053    // difference? Could they be combined?
054    /** The table of configuration parameters. */
055    private JTable table;
056 
057    /** The model for the parameter table. */
058    private PowerTableModel tableModel;
059 
060    /** A button for adding new parameters to the table. */
061    private JButton add;
062 
063    /** A button for removing parameters from the table. */
064    private JButton delete;
065 
066    /** Command for adding a row to the table. */
067    private static final String ADD = "add";
068 
069    /** Command for removing a row from the table. */
070    private static final String DELETE = "delete";
071 
072    /**
073     * Boolean indicating whether or not this component should display its name.
074     * If true, this is a standalone component. If false, this component is
075     * intended to be used as a subpanel for another component.
076     */
077    private boolean displayName = true;
078 
079    /** The names of the columns in the table. */
080    public static final String[] COLUMN_NAMES = {
081          JMeterUtils.getResString("name"), JMeterUtils.getResString("value"),
082          JMeterUtils.getResString("metadata")};
083 
084    /**
085     * Create a new standalone SimpleConfigGui.
086     */
087    public SimpleConfigGui()
088    {
089       this(true);
090    }
091 
092    /**
093     * Create a new SimpleConfigGui as either a standalone or an embedded
094     * component.
095     * 
096     * @param displayName
097     *            indicates whether or not this component should display its
098     *            name. If true, this is a standalone component. If false, this
099     *            component is intended to be used as a subpanel for another
100     *            component.
101     */
102    public SimpleConfigGui(boolean displayName)
103    {
104       this.displayName = displayName;
105       init();
106    }
107 
108    public String getLabelResource()
109    {
110       return "simple_config_element";
111    }
112 
113    /**
114     * A newly created component can be initialized with the contents of a Test
115     * Element object by calling this method. The component is responsible for
116     * querying the Test Element object for the relevant information to display
117     * in its GUI.
118     * <p>
119     * This implementation retrieves all key/value pairs from the TestElement
120     * object and sets these values in the GUI.
121     * 
122     * @param el
123     *            the TestElement to configure
124     */
125    public void configure(TestElement el)
126    {
127       super.configure(el);
128       tableModel.clearData();
129       PropertyIterator iter = el.propertyIterator();
130       while (iter.hasNext())
131       {
132          JMeterProperty prop = iter.next();
133          tableModel
134                .addRow(new Object[] { prop.getName(), prop.getStringValue()});
135       }
136       checkDeleteStatus();
137    }
138 
139    /* Implements JMeterGUIComponent.createTestElement() */
140    public TestElement createTestElement()
141    {
142       TestElement el = new ConfigTestElement();
143       modifyTestElement(el);
144       return el;
145    }
146 
147    /**
148     * Get all of the values from the GUI component and set them in the
149     * TestElement.
150     * 
151     * @param el
152     *            the TestElement to modify
153     */
154    public void modifyTestElement(TestElement el)
155    {
156       if (table.isEditing())
157       {
158          table.getCellEditor().stopCellEditing();
159       }
160       Data model = tableModel.getData();
161       model.reset();
162       while (model.next())
163       {
164          el.setProperty(new StringProperty((String) model
165                .getColumnValue(COLUMN_NAMES[0]), (String) model
166                .getColumnValue(COLUMN_NAMES[1])));
167       }
168       super.configureTestElement(el);
169    }
170 
171    /**
172     * Initialize the components and layout of this component.
173     */
174    private void init()
175    {
176       setLayout(new BorderLayout(0, 10));
177 
178       if (displayName)
179       {
180          setBorder(makeBorder());
181          add(makeTitlePanel(), BorderLayout.NORTH);
182       }
183 
184       add(createTablePanel(), BorderLayout.CENTER);
185       // Force the table to be at least 70 pixels high
186       add(Box.createVerticalStrut(70), BorderLayout.WEST);
187       add(createButtonPanel(), BorderLayout.SOUTH);
188    }
189 
190    /**
191     * Invoked when an action occurs. This implementation supports the add and
192     * delete buttons.
193     * 
194     * @param e
195     *            the event that has occurred
196     */
197    public void actionPerformed(ActionEvent e)
198    {
199       String action = e.getActionCommand();
200       if (action.equals(DELETE))
201       {
202          deleteArgument();
203       }
204       else if (action.equals(ADD))
205       {
206          addArgument();
207       }
208    }
209 
210    /**
211     * Create a GUI panel containing the table of configuration parameters.
212     * 
213     * @return a GUI panel containing the parameter table
214     */
215    private Component createTablePanel()
216    {
217       tableModel = new PowerTableModel(new String[] { COLUMN_NAMES[0],
218             COLUMN_NAMES[1]}, new Class[] { String.class, String.class});
219 
220       table = new JTable(tableModel);
221       table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
222       return makeScrollPane(table);
223    }
224 
225    /**
226     * Create a panel containing the add and delete buttons.
227     * 
228     * @return a GUI panel containing the buttons
229     */
230    private JPanel createButtonPanel()
231    {
232       add = new JButton(JMeterUtils.getResString("add"));
233       add.setActionCommand(ADD);
234       add.addActionListener(this);
235       add.setEnabled(true);
236 
237       delete = new JButton(JMeterUtils.getResString("delete"));
238       delete.setActionCommand(DELETE);
239       delete.addActionListener(this);
240 
241       checkDeleteStatus();
242 
243       JPanel buttonPanel = new JPanel();
244       buttonPanel.add(add);
245       buttonPanel.add(delete);
246       return buttonPanel;
247    }
248 
249    /**
250     * Enable or disable the delete button depending on whether or not there is
251     * a row to be deleted.
252     */
253    protected void checkDeleteStatus()
254    {
255       // Disable DELETE if there are no rows in the table to delete.
256       if (tableModel.getRowCount() == 0)
257       {
258          delete.setEnabled(false);
259       }
260       else
261       {
262          delete.setEnabled(true);
263       }
264    }
265 
266    /**
267     * Add a new argument row to the table.
268     */
269    protected void addArgument()
270    {
271       // If a table cell is being edited, we should accept the current value
272       // and stop the editing before adding a new row.
273       stopTableEditing();
274 
275       tableModel.addNewRow();
276       tableModel.fireTableDataChanged();
277 
278       // Enable DELETE (which may already be enabled, but it won't hurt)
279       delete.setEnabled(true);
280 
281       // Highlight (select) the appropriate row.
282       int rowToSelect = tableModel.getRowCount() - 1;
Rate283       table.setRowSelectionInterval(rowToSelect, rowToSelect);
284    }
285 
286    /**
287     * Stop any editing that is currently being done on the table. This will
288     * save any changes that have already been made.
289     */
290    protected void stopTableEditing()
291    {
292       if (table.isEditing())
293       {
294          TableCellEditor cellEditor = table.getCellEditor(
295                table.getEditingRow(), table.getEditingColumn());
296          cellEditor.stopCellEditing();
297       }
298    }
299 
300    /**
301     * Remove the currently selected argument from the table.
302     */
303    protected void deleteArgument()
304    {
305       // If a table cell is being edited, we must cancel the editing before
306       // deleting the row
307       if (table.isEditing())
308       {
309          TableCellEditor cellEditor = table.getCellEditor(
310                table.getEditingRow(), table.getEditingColumn());
311          cellEditor.cancelCellEditing();
312       }
313 
314       int rowSelected = table.getSelectedRow();
315 
316       if (rowSelected >= 0)
317       {
318 
319          //removeProperty(tableModel.getValueAt (
320          //    table.getSelectedRow(),0).toString());
321          tableModel.removeRow(rowSelected);
322          tableModel.fireTableDataChanged();
323 
324          // Disable DELETE if there are no rows in the table to delete.
325          if (tableModel.getRowCount() == 0)
326          {
327             delete.setEnabled(false);
328          }
329          else
330          {
331             // Table still contains one or more rows, so highlight (select)
332             // the appropriate one.
333             int rowToSelect = rowSelected;
334 
335             if (rowSelected >= tableModel.getRowCount())
336             {
337                rowToSelect = rowSelected - 1;
338             }
339 
Rate340             table.setRowSelectionInterval(rowToSelect, rowToSelect);
341          }
342       }
343    }
344 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
283 100% of 1
340 0% of 0