1 17 package org.apache.jmeter.testbeans.gui; 18 19 import java.awt.Component ; 20 import java.awt.event.ActionEvent ; 21 import java.awt.event.ActionListener ; 22 import java.awt.event.FocusEvent ; 23 import java.awt.event.FocusListener ; 24 import java.beans.PropertyEditorSupport ; 25 26 import javax.swing.JTextField ; 27 28 import org.apache.jorphan.logging.LoggingManager; 29 import org.apache.log.Logger; 30 31 41 class FieldStringEditor extends PropertyEditorSupport 42 implements ActionListener , FocusListener 43 { 44 protected static Logger log= LoggingManager.getLoggerForClass(); 45 46 51 private JTextField textField; 52 53 57 private String initialValue= ""; 58 59 protected FieldStringEditor() 60 { 61 super(); 62 63 textField= new JTextField (); 64 textField.addActionListener(this); 65 textField.addFocusListener(this); 66 } 67 68 public String getAsText() 69 { 70 return textField.getText(); 71 } 72 73 public void setAsText(String value) 74 { 75 initialValue= value; 76 textField.setText(value); 77 } 78 79 public Object getValue() 80 { 81 return getAsText(); 82 } 83 84 public void setValue(Object value) 85 { 86 if (value instanceof String ) setAsText((String )value); 87 else throw new IllegalArgumentException (); 88 } 89 90 93 public Component getCustomEditor() 94 { 95 return textField; 96 } 97 98 101 public void firePropertyChange() 102 { 103 String newValue= getAsText(); 104 105 if (initialValue.equals(newValue)) return; 106 initialValue= newValue; 107 108 super.firePropertyChange(); 109 } 110 111 114 public void actionPerformed(ActionEvent e) 115 { 116 firePropertyChange(); 117 } 118 119 122 public void focusGained(FocusEvent e) 123 { 124 } 125 126 129 public void focusLost(FocusEvent e) 130 { 131 firePropertyChange(); 132 } 133 134 public static class Test extends junit.framework.TestCase 135 { 136 public Test(String name) 137 { 138 super(name); 139 } 140 141 private void testSetGet(ComboStringEditor e, Object value) throws Exception 142 { 143 e.setValue(value); 144 assertEquals(value, e.getValue()); 145 } 146 private void testSetGetAsText(ComboStringEditor e, String text) throws Exception 147 { 148 e.setAsText(text); 149 assertEquals(text, e.getAsText()); 150 } 151 public void testSetGet() throws Exception 152 { 153 ComboStringEditor e= new ComboStringEditor(); 154 155 testSetGet(e, "any string"); 156 testSetGet(e, ""); 157 testSetGet(e, "${var}"); 158 } 159 public void testSetGetAsText() throws Exception 160 { 161 ComboStringEditor e= new ComboStringEditor(); 162 163 testSetGetAsText(e, "any string"); 164 testSetGetAsText(e, ""); 165 testSetGetAsText(e, "${var}"); 166 } 167 } 168 } | Popular Tags |