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, 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: /*
020: * Created on Sep 15, 2004
021: */
022: package org.apache.jmeter.gui;
023:
024: import java.awt.BorderLayout;
025: import java.awt.Frame;
026: import java.awt.GridLayout;
027: import java.awt.event.ActionEvent;
028: import java.awt.event.ActionListener;
029: import java.lang.reflect.Method;
030: import java.util.HashMap;
031: import java.util.Map;
032:
033: import javax.swing.JButton;
034: import javax.swing.JCheckBox;
035: import javax.swing.JDialog;
036: import javax.swing.JPanel;
037:
038: import org.apache.jmeter.samplers.SampleSaveConfiguration;
039: import org.apache.jmeter.util.JMeterUtils;
040: import org.apache.jorphan.logging.LoggingManager;
041: import org.apache.jorphan.reflect.Functor;
042: import org.apache.log.Logger;
043:
044: /**
045: * Generates Configure pop-up dialogue for Listeners from all methods in SampleSaveConfiguration
046: * with the signature "boolean saveXXX()".
047: * There must be a corresponding "void setXXX(boolean)" method, and a property save_XXX which is
048: * used to name the field on the dialogue.
049: *
050: * @author mstover
051: */
052: public class SavePropertyDialog extends JDialog implements
053: ActionListener {
054:
055: private static final Logger log = LoggingManager
056: .getLoggerForClass();
057:
058: private static Map functors = new HashMap();
059:
060: private static final long serialVersionUID = 1;
061:
062: private static final String NAME_SAVE_PFX = "save"; // $NON-NLS-1$ i.e. boolean saveXXX()
063: private static final String NAME_SET_PREFIX = "set"; // $NON-NLS-1$ i.e. void setXXX(boolean)
064: private static final String RESOURCE_PREFIX = "save_"; // $NON-NLS-1$ e.g. save_XXX property
065: private static final int NAME_SAVE_PFX_LEN = NAME_SAVE_PFX.length();
066:
067: private SampleSaveConfiguration saveConfig;
068:
069: public SavePropertyDialog() {
070: log.warn("Constructor only intended for use in testing"); // $NON-NLS-1$
071: }
072:
073: /**
074: * @param owner
075: * @param title
076: * @param modal
077: * @throws java.awt.HeadlessException
078: */
079: public SavePropertyDialog(Frame owner, String title, boolean modal,
080: SampleSaveConfiguration s)
081: // throws HeadlessException
082: {
083: super (owner, title, modal);
084: saveConfig = s;
085: log.debug("SampleSaveConfiguration = " + saveConfig);// $NON-NLS-1$
086: dialogInit();
087: }
088:
089: private int countMethods(Method[] m) {
090: int count = 0;
091: for (int i = 0; i < m.length; i++) {
092: if (m[i].getName().startsWith(NAME_SAVE_PFX)) {
093: count++;
094: }
095: }
096: return count;
097: }
098:
099: /*
100: * (non-Javadoc)
101: *
102: * @see javax.swing.JDialog#dialogInit()
103: */
104: protected void dialogInit() {
105: if (saveConfig != null) {
106: super .dialogInit();
107: this .getContentPane().setLayout(new BorderLayout());
108: Method[] methods = SampleSaveConfiguration.class
109: .getMethods();
110: int x = (countMethods(methods) / 3) + 1;
111: log.debug("grid panel is " + 3 + " by " + x);
112: JPanel checkPanel = new JPanel(new GridLayout(x, 3));
113: for (int i = 0; i < methods.length; i++) {
114: String name = methods[i].getName();
115: if (name.startsWith(NAME_SAVE_PFX)
116: && methods[i].getParameterTypes().length == 0) {
117: try {
118: name = name.substring(NAME_SAVE_PFX_LEN);
119: JCheckBox check = new JCheckBox(JMeterUtils
120: .getResString(RESOURCE_PREFIX + name)// $NON-NLS-1$
121: , ((Boolean) methods[i].invoke(
122: saveConfig, new Object[0]))
123: .booleanValue());
124: checkPanel.add(check, BorderLayout.NORTH);
125: check.addActionListener(this );
126: String actionCommand = NAME_SET_PREFIX + name; // $NON-NLS-1$
127: check.setActionCommand(actionCommand);
128: if (!functors.containsKey(actionCommand)) {
129: functors.put(actionCommand, new Functor(
130: actionCommand));
131: }
132: } catch (Exception e) {
133: log.warn("Problem creating save config dialog",
134: e);
135: }
136: }
137: }
138: getContentPane().add(checkPanel, BorderLayout.NORTH);
139: JButton exit = new JButton(JMeterUtils.getResString("done")); // $NON-NLS-1$
140: this .getContentPane().add(exit, BorderLayout.SOUTH);
141: exit.addActionListener(new ActionListener() {
142: public void actionPerformed(ActionEvent e) {
143: dispose();
144: }
145: });
146: }
147: }
148:
149: /*
150: * (non-Javadoc)
151: *
152: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
153: */
154: public void actionPerformed(ActionEvent e) {
155: String action = e.getActionCommand();
156: Functor f = (Functor) functors.get(action);
157: f.invoke(saveConfig, new Object[] { Boolean
158: .valueOf(((JCheckBox) e.getSource()).isSelected()) });
159: }
160:
161: /**
162: * @return Returns the saveConfig.
163: */
164: public SampleSaveConfiguration getSaveConfig() {
165: return saveConfig;
166: }
167:
168: /**
169: * @param saveConfig
170: * The saveConfig to set.
171: */
172: public void setSaveConfig(SampleSaveConfiguration saveConfig) {
173: this.saveConfig = saveConfig;
174: }
175: }
|