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

Class: org.apache.jmeter.modifiers.gui.UserParametersGui   ©

 OK to copy?
001 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/modifiers/gui/UserParametersGui.java,v 1.17.2.1 2004/06/12 16:44:34 mstover1 Exp $
002 /*
003  * Copyright 2002-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.modifiers.gui;
020 
021 import java.awt.BorderLayout;
022 import java.awt.GridLayout;
023 import java.awt.event.ActionEvent;
024 import java.awt.event.ActionListener;
025 import java.util.ArrayList;
026 import java.util.List;
027 
028 import javax.swing.Box;
029 import javax.swing.JButton;
030 import javax.swing.JCheckBox;
031 import javax.swing.JLabel;
032 import javax.swing.JOptionPane;
033 import javax.swing.JPanel;
034 import javax.swing.JScrollPane;
035 import javax.swing.JTable;
036 import javax.swing.ListSelectionModel;
037 import javax.swing.table.TableCellEditor;
038 
039 import org.apache.jmeter.gui.util.PowerTableModel;
040 import org.apache.jmeter.gui.util.VerticalPanel;
041 import org.apache.jmeter.modifiers.UserParameters;
042 import org.apache.jmeter.processor.gui.AbstractPreProcessorGui;
043 import org.apache.jmeter.testelement.TestElement;
044 import org.apache.jmeter.testelement.property.CollectionProperty;
045 import org.apache.jmeter.testelement.property.PropertyIterator;
046 import org.apache.jmeter.util.JMeterUtils;
047 import org.apache.jorphan.logging.LoggingManager;
048 import org.apache.log.Logger;
049 
050 /**
051  * @version $Revision: 1.17.2.1 $
052  */
053 public class UserParametersGui extends AbstractPreProcessorGui
054 {
055     private static Logger log = LoggingManager.getLoggerForClass();
056     private String THREAD_COLUMNS = JMeterUtils.getResString("user");
057 
058     private JTable paramTable;
059     private PowerTableModel tableModel;
060     private int numUserColumns = 1;
061     private JButton addParameterButton,
062         addUserButton,
063         deleteRowButton,
064         deleteColumnButton;
065     private JCheckBox perIterationCheck;
066 
067     public UserParametersGui()
068     {
069         super();
070         init();
071     }
072 
073     public String getLabelResource()
074     {
075         return "user_parameters_title";
076     }
077 
078     public void configure(TestElement el)
079     {
080         initTableModel();
081         paramTable.setModel(tableModel);
082         UserParameters params = (UserParameters) el;
083         CollectionProperty names = params.getNames();
084         CollectionProperty threadValues = params.getThreadLists();
085         tableModel.setColumnData(0, (List) names.getObjectValue());
086         PropertyIterator iter = threadValues.iterator();
087         if (iter.hasNext())
088         {
089             tableModel.setColumnData(1, (List) iter.next().getObjectValue());
090         }
091         int count = 2;
092         while (iter.hasNext())
093         {
094             String colName = THREAD_COLUMNS + "_" + count;
095             tableModel.addNewColumn(colName, String.class);
096             tableModel.setColumnData(
097                 count,
098                 (List) iter.next().getObjectValue());
099             count++;
100         }
101         perIterationCheck.setSelected(params.isPerIteration());
102         super.configure(el);
103     }
104 
105     /**
106      * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
107      */
108     public TestElement createTestElement()
109     {
110         UserParameters params = new UserParameters();
111         modifyTestElement(params);
112         return params;
113     }
114 
115     /**
116      * Modifies a given TestElement to mirror the data in the gui components.
117      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
118      */
119     public void modifyTestElement(TestElement params)
120     {
121        if (paramTable.isEditing())
122        {
123           paramTable.getCellEditor().stopCellEditing();
124        }
125         ((UserParameters) params).setNames(
126             new CollectionProperty(
127                 UserParameters.NAMES,
128                 tableModel.getColumnData(JMeterUtils.getResString("name"))));
129         CollectionProperty threadLists =
130             new CollectionProperty(
131                 UserParameters.THREAD_VALUES,
132                 new ArrayList());
133         log.debug("making threadlists from gui = " + threadLists);
134         for (int x = 1; x < tableModel.getColumnCount(); x++)
135         {
136             threadLists.addItem(
137                 tableModel.getColumnData(THREAD_COLUMNS + "_" + x));
138             log.debug(
139                 "Adding column to threadlist: "
140                     + tableModel.getColumnData(THREAD_COLUMNS + "_" + x));
141             log.debug("Threadlists now = " + threadLists);
142         }
143         log.debug("In the end, threadlists = " + threadLists);
144         ((UserParameters) params).setThreadLists(threadLists);
145         ((UserParameters) params).setPerIteration(
146             perIterationCheck.isSelected());
147         super.configureTestElement(params);
148     }
149 
150     private void init()
151     {
152         setBorder(makeBorder());
153         setLayout(new BorderLayout());
154         JPanel vertPanel = new VerticalPanel();
155         vertPanel.add(makeTitlePanel());
156 
157         perIterationCheck =
158             new JCheckBox(JMeterUtils.getResString("update_per_iter"), true);
159         Box perIterationPanel = Box.createHorizontalBox();
160         perIterationPanel.add(perIterationCheck);
161         perIterationPanel.add(Box.createHorizontalGlue());
162         vertPanel.add(perIterationPanel);
163         add(vertPanel, BorderLayout.NORTH);
164 
165         add(makeParameterPanel(), BorderLayout.CENTER);
166     }
167 
168     private JPanel makeParameterPanel()
169     {
170         JLabel tableLabel =
171             new JLabel(JMeterUtils.getResString("user_parameters_table"));
172         initTableModel();
173         paramTable = new JTable(tableModel);
174         //paramTable.setRowSelectionAllowed(true);
175         //paramTable.setColumnSelectionAllowed(true);
176         paramTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
177         //paramTable.setCellSelectionEnabled(true);
178         //paramTable.setPreferredScrollableViewportSize(new Dimension(100, 70));
179 
180         JPanel paramPanel = new JPanel(new BorderLayout());
181         paramPanel.add(tableLabel, BorderLayout.NORTH);
182         JScrollPane scroll = new JScrollPane(paramTable);
183         scroll.setPreferredSize(scroll.getMinimumSize());
184         paramPanel.add(scroll, BorderLayout.CENTER);
185         paramPanel.add(makeButtonPanel(), BorderLayout.SOUTH);
186         return paramPanel;
187     }
188 
189     protected void initTableModel()
190     {
191         tableModel =
192             new PowerTableModel(
193                 new String[] {
194                     JMeterUtils.getResString("name"),
195                     THREAD_COLUMNS + "_" + numUserColumns },
196                 new Class[] { String.class, String.class });
197     }
198 
199     private JPanel makeButtonPanel()
200     {
201         JPanel buttonPanel = new JPanel();
202         buttonPanel.setLayout(new GridLayout(2, 2));
203         addParameterButton =
204             new JButton(JMeterUtils.getResString("add_parameter"));
205         addUserButton = new JButton(JMeterUtils.getResString("add_user"));
206         deleteRowButton =
207             new JButton(JMeterUtils.getResString("delete_parameter"));
208         deleteColumnButton =
209             new JButton(JMeterUtils.getResString("delete_user"));
210         buttonPanel.add(addParameterButton);
211         buttonPanel.add(deleteRowButton);
212         buttonPanel.add(addUserButton);
213         buttonPanel.add(deleteColumnButton);
214         addParameterButton.addActionListener(new AddParamAction());
215         addUserButton.addActionListener(new AddUserAction());
216         deleteRowButton.addActionListener(new DeleteRowAction());
217         deleteColumnButton.addActionListener(new DeleteColumnAction());
218         return buttonPanel;
219     }
220 
221     private class AddParamAction implements ActionListener
222     {
223         public void actionPerformed(ActionEvent e)
224         {
225             if (paramTable.isEditing())
226             {
227                 TableCellEditor cellEditor =
228                     paramTable.getCellEditor(
229                         paramTable.getEditingRow(),
230                         paramTable.getEditingColumn());
231                 cellEditor.stopCellEditing();
232             }
233 
234             tableModel.addNewRow();
235             tableModel.fireTableDataChanged();
236 
237             // Enable DELETE (which may already be enabled, but it won't hurt)
238             deleteRowButton.setEnabled(true);
239 
240             // Highlight (select) the appropriate row.
241             int rowToSelect = tableModel.getRowCount() - 1;
Rate242             paramTable.setRowSelectionInterval(rowToSelect, rowToSelect);
243         }
244     }
245 
246     private class AddUserAction implements ActionListener
247     {
248         public void actionPerformed(ActionEvent e)
249         {
250 
251             if (paramTable.isEditing())
252             {
253                 TableCellEditor cellEditor =
254                     paramTable.getCellEditor(
255                         paramTable.getEditingRow(),
256                         paramTable.getEditingColumn());
257                 cellEditor.stopCellEditing();
258             }
259 
260             tableModel.addNewColumn(
261                 THREAD_COLUMNS + "_" + tableModel.getColumnCount(),
262                 String.class);
263             tableModel.fireTableDataChanged();
264 
265             // Enable DELETE (which may already be enabled, but it won't hurt)
266             deleteColumnButton.setEnabled(true);
267 
268             // Highlight (select) the appropriate row.
269             int colToSelect = tableModel.getColumnCount() - 1;
270             paramTable.setColumnSelectionInterval(colToSelect, colToSelect);
271         }
272     }
273 
274     private class DeleteRowAction implements ActionListener
275     {
276         public void actionPerformed(ActionEvent e)
277         {
278             if (paramTable.isEditing())
279             {
280                 TableCellEditor cellEditor =
281                     paramTable.getCellEditor(
282                         paramTable.getEditingRow(),
283                         paramTable.getEditingColumn());
284                 cellEditor.cancelCellEditing();
285             }
286 
287             int rowSelected = paramTable.getSelectedRow();
288             if (rowSelected >= 0)
289             {
290                 tableModel.removeRow(rowSelected);
291                 tableModel.fireTableDataChanged();
292 
293                 // Disable DELETE if there are no rows in the table to delete.
294                 if (tableModel.getRowCount() == 0)
295                 {
296                     deleteRowButton.setEnabled(false);
297                 }
298 
299                 // Table still contains one or more rows, so highlight (select)
300                 // the appropriate one.
301                 else
302                 {
303                     int rowToSelect = rowSelected;
304 
305                     if (rowSelected >= tableModel.getRowCount())
306                     {
307                         rowToSelect = rowSelected - 1;
308                     }
309 
Rate310                     paramTable.setRowSelectionInterval(
311                         rowToSelect,
312                         rowToSelect);
313                 }
314             }
315         }
316     }
317 
318     private class DeleteColumnAction implements ActionListener
319     {
320         public void actionPerformed(ActionEvent e)
321         {
322             if (paramTable.isEditing())
323             {
324                 TableCellEditor cellEditor =
325                     paramTable.getCellEditor(
326                         paramTable.getEditingRow(),
327                         paramTable.getEditingColumn());
328                 cellEditor.cancelCellEditing();
329             }
330 
331             int colSelected = paramTable.getSelectedColumn();
332             if (colSelected == 0 || colSelected == 1)
333             {
334                 JOptionPane.showMessageDialog(
335                     null,
336                     JMeterUtils.getResString("column_delete_disallowed"),
337                     "Error",
338                     JOptionPane.ERROR_MESSAGE);
339                 return;
340             }
341             if (colSelected >= 0)
342             {
343                 tableModel.removeColumn(colSelected);
344                 tableModel.fireTableDataChanged();
345 
346                 // Disable DELETE if there are no rows in the table to delete.
347                 if (tableModel.getColumnCount() == 0)
348                 {
349                     deleteColumnButton.setEnabled(false);
350                 }
351 
352                 // Table still contains one or more rows, so highlight (select)
353                 // the appropriate one.
354                 else
355                 {
356 
357                     if (colSelected >= tableModel.getColumnCount())
358                     {
359                         colSelected = colSelected - 1;
360                     }
361 
362                     paramTable.setColumnSelectionInterval(
363                         colSelected,
364                         colSelected);
365                 }
366             }
367         }
368     }
369 }

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