001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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: public class UserParametersGui extends AbstractPreProcessorGui {
051: private static Logger log = LoggingManager.getLoggerForClass();
052:
053: private String THREAD_COLUMNS_PREFIX = JMeterUtils
054: .getResString("user")
055: + "_"; // $NON-NLS-1$ $NON-NLS-2$
056:
057: private JTable paramTable;
058:
059: private PowerTableModel tableModel;
060:
061: private int numUserColumns = 1;
062:
063: private JButton addParameterButton, addUserButton, deleteRowButton,
064: deleteColumnButton;
065:
066: private JCheckBox perIterationCheck;
067:
068: public UserParametersGui() {
069: super ();
070: init();
071: }
072:
073: public String getLabelResource() {
074: return "user_parameters_title"; // $NON-NLS-1$
075: }
076:
077: public void configure(TestElement el) {
078: initTableModel();
079: paramTable.setModel(tableModel);
080: UserParameters params = (UserParameters) el;
081: CollectionProperty names = params.getNames();
082: CollectionProperty threadValues = params.getThreadLists();
083: tableModel.setColumnData(0, (List) names.getObjectValue());
084: PropertyIterator iter = threadValues.iterator();
085: if (iter.hasNext()) {
086: tableModel.setColumnData(1, (List) iter.next()
087: .getObjectValue());
088: }
089: int count = 2;
090: while (iter.hasNext()) {
091: String colName = THREAD_COLUMNS_PREFIX + count;
092: tableModel.addNewColumn(colName, String.class);
093: tableModel.setColumnData(count, (List) iter.next()
094: .getObjectValue());
095: count++;
096: }
097: perIterationCheck.setSelected(params.isPerIteration());
098: super .configure(el);
099: }
100:
101: /**
102: * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
103: */
104: public TestElement createTestElement() {
105: UserParameters params = new UserParameters();
106: modifyTestElement(params);
107: return params;
108: }
109:
110: /**
111: * Modifies a given TestElement to mirror the data in the gui components.
112: *
113: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
114: */
115: public void modifyTestElement(TestElement params) {
116: if (paramTable.isEditing()) {
117: paramTable.getCellEditor().stopCellEditing();
118: }
119: ((UserParameters) params).setNames(new CollectionProperty(
120: UserParameters.NAMES,
121: tableModel.getColumnData(JMeterUtils
122: .getResString("name")))); // $NON-NLS-1$
123: CollectionProperty threadLists = new CollectionProperty(
124: UserParameters.THREAD_VALUES, new ArrayList());
125: log.debug("making threadlists from gui = " + threadLists);
126: for (int x = 1; x < tableModel.getColumnCount(); x++) {
127: threadLists.addItem(tableModel
128: .getColumnData(THREAD_COLUMNS_PREFIX + x));
129: log.debug("Adding column to threadlist: "
130: + tableModel.getColumnData(THREAD_COLUMNS_PREFIX
131: + x));
132: log.debug("Threadlists now = " + threadLists);
133: }
134: log.debug("In the end, threadlists = " + threadLists);
135: ((UserParameters) params).setThreadLists(threadLists);
136: ((UserParameters) params).setPerIteration(perIterationCheck
137: .isSelected());
138: super .configureTestElement(params);
139: }
140:
141: /**
142: * Implements JMeterGUIComponent.clearGui
143: */
144: public void clearGui() {
145: super .clearGui();
146:
147: initTableModel();
148: paramTable.setModel(tableModel);
149: perIterationCheck.setSelected(false);
150: }
151:
152: private void init() {
153: setBorder(makeBorder());
154: setLayout(new BorderLayout());
155: JPanel vertPanel = new VerticalPanel();
156: vertPanel.add(makeTitlePanel());
157:
158: perIterationCheck = new JCheckBox(JMeterUtils
159: .getResString("update_per_iter"), true); // $NON-NLS-1$
160: Box perIterationPanel = Box.createHorizontalBox();
161: perIterationPanel.add(perIterationCheck);
162: perIterationPanel.add(Box.createHorizontalGlue());
163: vertPanel.add(perIterationPanel);
164: add(vertPanel, BorderLayout.NORTH);
165:
166: add(makeParameterPanel(), BorderLayout.CENTER);
167: }
168:
169: private JPanel makeParameterPanel() {
170: JLabel tableLabel = new JLabel(JMeterUtils
171: .getResString("user_parameters_table")); // $NON-NLS-1$
172: initTableModel();
173: paramTable = new JTable(tableModel);
174: // paramTable.setRowSelectionAllowed(true);
175: // paramTable.setColumnSelectionAllowed(true);
176: paramTable
177: .setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
178: // paramTable.setCellSelectionEnabled(true);
179: // paramTable.setPreferredScrollableViewportSize(new Dimension(100,
180: // 70));
181:
182: JPanel paramPanel = new JPanel(new BorderLayout());
183: paramPanel.add(tableLabel, BorderLayout.NORTH);
184: JScrollPane scroll = new JScrollPane(paramTable);
185: scroll.setPreferredSize(scroll.getMinimumSize());
186: paramPanel.add(scroll, BorderLayout.CENTER);
187: paramPanel.add(makeButtonPanel(), BorderLayout.SOUTH);
188: return paramPanel;
189: }
190:
191: protected void initTableModel() {
192: tableModel = new PowerTableModel(new String[] {
193: JMeterUtils.getResString("name"), // $NON-NLS-1$
194: THREAD_COLUMNS_PREFIX + numUserColumns }, new Class[] {
195: String.class, String.class });
196: }
197:
198: private JPanel makeButtonPanel() {
199: JPanel buttonPanel = new JPanel();
200: buttonPanel.setLayout(new GridLayout(2, 2));
201: addParameterButton = new JButton(JMeterUtils
202: .getResString("add_parameter")); // $NON-NLS-1$
203: addUserButton = new JButton(JMeterUtils
204: .getResString("add_user")); // $NON-NLS-1$
205: deleteRowButton = new JButton(JMeterUtils
206: .getResString("delete_parameter")); // $NON-NLS-1$
207: deleteColumnButton = new JButton(JMeterUtils
208: .getResString("delete_user")); // $NON-NLS-1$
209: buttonPanel.add(addParameterButton);
210: buttonPanel.add(deleteRowButton);
211: buttonPanel.add(addUserButton);
212: buttonPanel.add(deleteColumnButton);
213: addParameterButton.addActionListener(new AddParamAction());
214: addUserButton.addActionListener(new AddUserAction());
215: deleteRowButton.addActionListener(new DeleteRowAction());
216: deleteColumnButton.addActionListener(new DeleteColumnAction());
217: return buttonPanel;
218: }
219:
220: private class AddParamAction implements ActionListener {
221: public void actionPerformed(ActionEvent e) {
222: if (paramTable.isEditing()) {
223: TableCellEditor cellEditor = paramTable.getCellEditor(
224: paramTable.getEditingRow(), paramTable
225: .getEditingColumn());
226: cellEditor.stopCellEditing();
227: }
228:
229: tableModel.addNewRow();
230: tableModel.fireTableDataChanged();
231:
232: // Enable DELETE (which may already be enabled, but it won't hurt)
233: deleteRowButton.setEnabled(true);
234:
235: // Highlight (select) the appropriate row.
236: int rowToSelect = tableModel.getRowCount() - 1;
237: paramTable
238: .setRowSelectionInterval(rowToSelect, rowToSelect);
239: }
240: }
241:
242: private class AddUserAction implements ActionListener {
243: public void actionPerformed(ActionEvent e) {
244:
245: if (paramTable.isEditing()) {
246: TableCellEditor cellEditor = paramTable.getCellEditor(
247: paramTable.getEditingRow(), paramTable
248: .getEditingColumn());
249: cellEditor.stopCellEditing();
250: }
251:
252: tableModel.addNewColumn(THREAD_COLUMNS_PREFIX
253: + tableModel.getColumnCount(), String.class);
254: tableModel.fireTableDataChanged();
255:
256: // Enable DELETE (which may already be enabled, but it won't hurt)
257: deleteColumnButton.setEnabled(true);
258:
259: // Highlight (select) the appropriate row.
260: int colToSelect = tableModel.getColumnCount() - 1;
261: paramTable.setColumnSelectionInterval(colToSelect,
262: colToSelect);
263: }
264: }
265:
266: private class DeleteRowAction implements ActionListener {
267: public void actionPerformed(ActionEvent e) {
268: if (paramTable.isEditing()) {
269: TableCellEditor cellEditor = paramTable.getCellEditor(
270: paramTable.getEditingRow(), paramTable
271: .getEditingColumn());
272: cellEditor.cancelCellEditing();
273: }
274:
275: int rowSelected = paramTable.getSelectedRow();
276: if (rowSelected >= 0) {
277: tableModel.removeRow(rowSelected);
278: tableModel.fireTableDataChanged();
279:
280: // Disable DELETE if there are no rows in the table to delete.
281: if (tableModel.getRowCount() == 0) {
282: deleteRowButton.setEnabled(false);
283: }
284:
285: // Table still contains one or more rows, so highlight (select)
286: // the appropriate one.
287: else {
288: int rowToSelect = rowSelected;
289:
290: if (rowSelected >= tableModel.getRowCount()) {
291: rowToSelect = rowSelected - 1;
292: }
293:
294: paramTable.setRowSelectionInterval(rowToSelect,
295: rowToSelect);
296: }
297: }
298: }
299: }
300:
301: private class DeleteColumnAction implements ActionListener {
302: public void actionPerformed(ActionEvent e) {
303: if (paramTable.isEditing()) {
304: TableCellEditor cellEditor = paramTable.getCellEditor(
305: paramTable.getEditingRow(), paramTable
306: .getEditingColumn());
307: cellEditor.cancelCellEditing();
308: }
309:
310: int colSelected = paramTable.getSelectedColumn();
311: if (colSelected == 0 || colSelected == 1) {
312: JOptionPane.showMessageDialog(null, JMeterUtils
313: .getResString("column_delete_disallowed"), // $NON-NLS-1$
314: "Error", JOptionPane.ERROR_MESSAGE);
315: return;
316: }
317: if (colSelected >= 0) {
318: tableModel.removeColumn(colSelected);
319: tableModel.fireTableDataChanged();
320:
321: // Disable DELETE if there are no rows in the table to delete.
322: if (tableModel.getColumnCount() == 0) {
323: deleteColumnButton.setEnabled(false);
324: }
325:
326: // Table still contains one or more rows, so highlight (select)
327: // the appropriate one.
328: else {
329:
330: if (colSelected >= tableModel.getColumnCount()) {
331: colSelected = colSelected - 1;
332: }
333:
334: paramTable.setColumnSelectionInterval(colSelected,
335: colSelected);
336: }
337: }
338: }
339: }
340: }
|