KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $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 $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * 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, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * 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.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25
26 import javax.swing.Box JavaDoc;
27 import javax.swing.JButton JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JTable JavaDoc;
30 import javax.swing.ListSelectionModel JavaDoc;
31 import javax.swing.table.TableCellEditor JavaDoc;
32
33 import org.apache.jmeter.config.ConfigTestElement;
34 import org.apache.jmeter.gui.util.PowerTableModel;
35 import org.apache.jmeter.testelement.TestElement;
36 import org.apache.jmeter.testelement.property.JMeterProperty;
37 import org.apache.jmeter.testelement.property.PropertyIterator;
38 import org.apache.jmeter.testelement.property.StringProperty;
39 import org.apache.jmeter.util.JMeterUtils;
40 import org.apache.jorphan.collections.Data;
41
42 /**
43  * Default config gui for Configuration Element.
44  *
45  * @version $Revision: 1.8.2.1 $ on $Date: 2004/06/12 16:44:34 $
46  */

47 public class SimpleConfigGui extends AbstractConfigGui implements
48       ActionListener JavaDoc
49 {
50    /* This class created for enhancement Bug ID 9101. */
51
52    // TODO: This class looks a lot like ArgumentsPanel. What exactly is the
53
// difference? Could they be combined?
54
/** The table of configuration parameters. */
55    private JTable JavaDoc table;
56
57    /** The model for the parameter table. */
58    private PowerTableModel tableModel;
59
60    /** A button for adding new parameters to the table. */
61    private JButton JavaDoc add;
62
63    /** A button for removing parameters from the table. */
64    private JButton JavaDoc delete;
65
66    /** Command for adding a row to the table. */
67    private static final String JavaDoc ADD = "add";
68
69    /** Command for removing a row from the table. */
70    private static final String JavaDoc DELETE = "delete";
71
72    /**
73     * Boolean indicating whether or not this component should display its name.
74     * If true, this is a standalone component. If false, this component is
75     * intended to be used as a subpanel for another component.
76     */

77    private boolean displayName = true;
78
79    /** The names of the columns in the table. */
80    public static final String JavaDoc[] COLUMN_NAMES = {
81          JMeterUtils.getResString("name"), JMeterUtils.getResString("value"),
82          JMeterUtils.getResString("metadata")};
83
84    /**
85     * Create a new standalone SimpleConfigGui.
86     */

87    public SimpleConfigGui()
88    {
89       this(true);
90    }
91
92    /**
93     * Create a new SimpleConfigGui as either a standalone or an embedded
94     * component.
95     *
96     * @param displayName
97     * indicates whether or not this component should display its
98     * name. If true, this is a standalone component. If false, this
99     * 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 JavaDoc 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 JavaDoc[] { 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 JavaDoc) model
165                .getColumnValue(COLUMN_NAMES[0]), (String JavaDoc) 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 JavaDoc(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 JavaDoc e)
198    {
199       String JavaDoc 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 JavaDoc createTablePanel()
216    {
217       tableModel = new PowerTableModel(new String JavaDoc[] { COLUMN_NAMES[0],
218             COLUMN_NAMES[1]}, new Class JavaDoc[] { String JavaDoc.class, String JavaDoc.class});
219
220       table = new JTable JavaDoc(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 JavaDoc createButtonPanel()
231    {
232       add = new JButton JavaDoc(JMeterUtils.getResString("add"));
233       add.setActionCommand(ADD);
234       add.addActionListener(this);
235       add.setEnabled(true);
236
237       delete = new JButton JavaDoc(JMeterUtils.getResString("delete"));
238       delete.setActionCommand(DELETE);
239       delete.addActionListener(this);
240
241       checkDeleteStatus();
242
243       JPanel JavaDoc buttonPanel = new JPanel JavaDoc();
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;
283       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 JavaDoc 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 JavaDoc 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
340             table.setRowSelectionInterval(rowToSelect, rowToSelect);
341          }
342       }
343    }
344 }
345
Popular Tags