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.visualizers;
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.ArrayList;
027: import java.util.Arrays;
028: import java.util.Collection;
029: import java.util.Collections;
030: import java.util.Comparator;
031: import java.util.Iterator;
032: import java.util.Map;
033: import java.util.Properties;
034: import java.util.Set;
035:
036: import javax.swing.BorderFactory;
037: import javax.swing.Box;
038: import javax.swing.ButtonGroup;
039: import javax.swing.JButton;
040: import javax.swing.JCheckBox;
041: import javax.swing.JLabel;
042: import javax.swing.JPanel;
043: import javax.swing.JTable;
044: import javax.swing.ListSelectionModel;
045:
046: import org.apache.jmeter.config.ConfigTestElement;
047: import org.apache.jmeter.config.gui.AbstractConfigGui;
048: import org.apache.jmeter.gui.UnsharedComponent;
049: import org.apache.jmeter.gui.util.MenuFactory;
050: import org.apache.jmeter.samplers.SampleResult;
051: import org.apache.jmeter.testelement.TestElement;
052: import org.apache.jmeter.util.JMeterUtils;
053: import org.apache.jorphan.gui.ObjectTableModel;
054: import org.apache.jorphan.reflect.Functor;
055:
056: public class PropertyControlGui extends AbstractConfigGui implements
057: ActionListener, UnsharedComponent {
058:
059: private static final long serialVersionUID = 1L;
060:
061: private static final String COLUMN_NAMES_0 = JMeterUtils
062: .getResString("name"); // $NON-NLS-1$
063:
064: private static final String COLUMN_NAMES_1 = JMeterUtils
065: .getResString("value"); // $NON-NLS-1$
066:
067: // TODO: add and delete not currently supported
068: private static final String ADD = "add"; // $NON-NLS-1$
069:
070: private static final String DELETE = "delete"; // $NON-NLS-1$
071:
072: private static final String SYSTEM = "system"; // $NON-NLS-1$
073:
074: private static final String JMETER = "jmeter"; // $NON-NLS-1$
075:
076: private JCheckBox systemButton = new JCheckBox("System");
077: private JCheckBox jmeterButton = new JCheckBox("JMeter");
078:
079: private JLabel tableLabel = new JLabel("Properties");
080:
081: /** The table containing the list of arguments. */
082: private transient JTable table;
083:
084: /** The model for the arguments table. */
085: protected transient ObjectTableModel tableModel;
086:
087: /** A button for adding new arguments to the table. */
088: private JButton add;
089:
090: /** A button for removing arguments from the table. */
091: private JButton delete;
092:
093: public PropertyControlGui() {
094: super ();
095: init();
096: }
097:
098: public String getLabelResource() {
099: return "property_visualiser_title"; // $NON-NLS-1$
100: }
101:
102: public Collection getMenuCategories() {
103: return Arrays
104: .asList(new String[] { MenuFactory.NON_TEST_ELEMENTS });
105: }
106:
107: public void actionPerformed(ActionEvent action) {
108: String command = action.getActionCommand();
109: if (ADD.equals(command)) {
110: return;
111: }
112: if (DELETE.equals(command)) {
113: return;
114: }
115: if (SYSTEM.equals(command)) {
116: setUpData();
117: return;
118: }
119: if (JMETER.equals(command)) {
120: setUpData();
121: return;
122: }
123:
124: }
125:
126: public void add(SampleResult sample) {
127: }
128:
129: public TestElement createTestElement() {
130: TestElement el = new ConfigTestElement();// TODO replace with simpler version?
131: modifyTestElement(el);
132: return el;
133: }
134:
135: public void configure(TestElement element) {
136: super .configure(element);
137: setUpData();
138: }
139:
140: private void setUpData() {
141: tableModel.clearData();
142: Properties p = null;
143: if (systemButton.isSelected()) {
144: p = System.getProperties();
145: }
146: if (jmeterButton.isSelected()) {
147: p = JMeterUtils.getJMeterProperties();
148: }
149: if (p == null)
150: return;
151: Set s = p.entrySet();
152: ArrayList al = new ArrayList(s);
153: Collections.sort(al, new Comparator() {
154: public int compare(Object o1, Object o2) {
155: String m1, m2;
156: m1 = (String) ((Map.Entry) o1).getKey();
157: m2 = (String) ((Map.Entry) o2).getKey();
158: return m1.compareTo(m2);
159: }
160: });
161: Iterator i = al.iterator();
162: while (i.hasNext()) {
163: tableModel.addRow(i.next());
164: }
165:
166: }
167:
168: public void modifyTestElement(TestElement element) {
169: configureTestElement(element);
170: }
171:
172: private Component makeMainPanel() {
173: initializeTableModel();
174: table = new JTable(tableModel);
175: table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
176: return makeScrollPane(table);
177: }
178:
179: /**
180: * Create a panel containing the title label for the table.
181: *
182: * @return a panel containing the title label
183: */
184: private Component makeLabelPanel() {
185: JPanel labelPanel = new JPanel(
186: new FlowLayout(FlowLayout.CENTER));
187: ButtonGroup bg = new ButtonGroup();
188: bg.add(systemButton);
189: bg.add(jmeterButton);
190: jmeterButton.setSelected(true);
191: systemButton.setActionCommand(SYSTEM);
192: jmeterButton.setActionCommand(JMETER);
193: systemButton.addActionListener(this );
194: jmeterButton.addActionListener(this );
195:
196: labelPanel.add(systemButton);
197: labelPanel.add(jmeterButton);
198: labelPanel.add(tableLabel);
199: return labelPanel;
200: }
201:
202: /**
203: * Create a panel containing the add and delete buttons.
204: *
205: * @return a GUI panel containing the buttons
206: */
207: private JPanel makeButtonPanel() {
208: add = new JButton(JMeterUtils.getResString("add")); // $NON-NLS-1$
209: add.setActionCommand(ADD);
210: add.setEnabled(true);
211:
212: delete = new JButton(JMeterUtils.getResString("delete")); // $NON-NLS-1$
213: delete.setActionCommand(DELETE);
214:
215: JPanel buttonPanel = new JPanel();
216: buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 0,
217: 10));
218: add.addActionListener(this );
219: delete.addActionListener(this );
220: buttonPanel.add(add);
221: buttonPanel.add(delete);
222: return buttonPanel;
223: }
224:
225: /**
226: * Initialize the components and layout of this component.
227: */
228: private void init() {
229: JPanel p = this ;
230:
231: setLayout(new BorderLayout(0, 5));
232: setBorder(makeBorder());
233: add(makeTitlePanel(), BorderLayout.NORTH);
234: p = new JPanel();
235:
236: p.setLayout(new BorderLayout());
237:
238: p.add(makeLabelPanel(), BorderLayout.NORTH);
239: p.add(makeMainPanel(), BorderLayout.CENTER);
240: // Force a minimum table height of 70 pixels
241: p.add(Box.createVerticalStrut(70), BorderLayout.WEST);
242: //p.add(makeButtonPanel(), BorderLayout.SOUTH);
243:
244: add(p, BorderLayout.CENTER);
245: table.revalidate();
246: }
247:
248: private void initializeTableModel() {
249: tableModel = new ObjectTableModel(new String[] {
250: COLUMN_NAMES_0, COLUMN_NAMES_1 }, new Functor[] {
251: new Functor(Map.Entry.class, "getKey"), // $NON-NLS-1$
252: new Functor(Map.Entry.class, "getValue") }, // $NON-NLS-1$
253: new Functor[] {
254: null, //new Functor("setName"), // $NON-NLS-1$
255: new Functor(Map.Entry.class, "setValue",
256: new Class[] { Object.class }) // $NON-NLS-1$
257: }, new Class[] { String.class, String.class });
258: }
259: }
|