1 // $Header: /home/cvs/jakarta-jmeter/src/protocol/jndi/config/gui/MethodConfigDialog.java,v 1.3 2004/02/13 02:40:53 sebb Exp $ 2 /* 3 * Copyright 2002-2004 The Apache Software Foundation. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy 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, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 package org.apache.jmeter.ejb.jndi.config.gui; 20 21 import java.awt; 22 import java.awt.event; 23 import java.beans; 24 import javax.swing; 25 import javax.swing.event; 26 27 import org.apache.jmeter.ejb.jndi.config.MethodConfigUserObject; 28 import org.apache.jmeter.ejb.jndi.config.MethodConfigUserObjectException; 29 import org.apache.log4j.Category; 30 31 /** 32 * Dialog to allow user to key in value for their method parameters 33 * 34 * @author Khor Soon Hin 35 * @version $Revision: 1.3 $ Last Updated: $Date: 2004/02/13 02:40:53 $ 36 * Created 2002 Jan 08 37 */ 38 public class MethodConfigDialog extends JDialog 39 { 40 private static Category catClass = Category.getInstance( 41 MethodConfigDialog.class.getName()); 42 43 protected JOptionPane optionPane; 44 protected JTextField textField; 45 protected JLabel classLabel; 46 protected MethodConfigUserObject userObject; 47 48 public MethodConfigDialog(Frame aFrame, final Class type) 49 { 50 super(aFrame, true); 51 classLabel = new JLabel(type.getName()); 52 textField = new JTextField(10); 53 Object[] array = {classLabel, textField}; 54 55 final String btnString1 = "Ok"; 56 final String btnString2 = "Cancel"; 57 Object[] options = {btnString1, btnString2}; 58 59 optionPane = new JOptionPane( 60 array, 61 JOptionPane.QUESTION_MESSAGE, 62 JOptionPane.OK_CANCEL_OPTION, 63 null, 64 options, 65 options[0]); 66 setContentPane(optionPane); 67 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 68 addWindowListener(new WindowAdapter() 69 { 70 } 71 ); 72 textField.addActionListener(new ActionListener() 73 { 74 public void actionPerformed(ActionEvent e) 75 { 76 optionPane.setValue(btnString1); 77 } 78 } 79 ); 80 optionPane.addPropertyChangeListener(new PropertyChangeListener() 81 { 82 public void propertyChange(PropertyChangeEvent e) 83 { 84 catClass.debug("Start : propertyChange1"); 85 String prop = e.getPropertyName(); 86 if(catClass.isDebugEnabled()) 87 { 88 catClass.debug("propertyChange1 : property name - " + prop); 89 } 90 catClass.debug("JOptionPane.INPUT_VALUE_PROPERTY - " + JOptionPane.INPUT_VALUE_PROPERTY); 91 catClass.debug("optionPane.getValue() - " + optionPane.getValue()); 92 catClass.debug("optionPane.getInputValue() - " + optionPane.getInputValue()); 93 Object value = null; 94 if(isVisible() 95 && (e.getSource() == optionPane) 96 && prop.equals(JOptionPane.VALUE_PROPERTY)) 97 { 98 value = optionPane.getValue(); 99 if(catClass.isDebugEnabled()) 100 { 101 catClass.debug("propertyChange1 : optionPane value - " + value); 102 } 103 104 // reset the JOptionPane's value 105 // If you don't don this then the next time the user 106 // presses ths same button no event will be fired 107 optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE); 108 109 String input = null; 110 if(value.equals(btnString1)) 111 { 112 try 113 { 114 input = textField.getText(); 115 if(catClass.isDebugEnabled()) 116 { 117 catClass.debug("MethodConfigDialog1 : input - " + input); 118 } 119 userObject = new MethodConfigUserObject(type, input); 120 setVisible(false); 121 } 122 catch(MethodConfigUserObjectException ex) 123 { 124 // the input is not compatible with the class 125 catClass.debug( 126 "propertyChange1 : input incompatible with class"); 127 textField.selectAll(); 128 JOptionPane.showMessageDialog( 129 MethodConfigDialog.this, 130 "Sorry, \"" + input + "\" " 131 + "is not valid for Class " 132 + type, 133 "Try again", 134 JOptionPane.ERROR_MESSAGE); 135 input = null; 136 userObject = null; 137 } 138 } 139 else 140 { 141 setVisible(false); 142 } 143 } 144 catClass.debug("End - propertyChange1"); 145 } 146 } 147 ); 148 } 149 150 public MethodConfigUserObject getValidatedInput() 151 { 152 return userObject; 153 } 154 }