1 17 package org.apache.jmeter.testbeans.gui; 18 19 import java.awt.Component ; 20 import java.awt.event.ItemEvent ; 21 import java.awt.event.ItemListener ; 22 import java.beans.PropertyEditorSupport ; 23 24 import javax.swing.DefaultComboBoxModel ; 25 import javax.swing.JComboBox ; 26 import javax.swing.text.JTextComponent ; 27 28 import org.apache.jmeter.util.JMeterUtils; 29 import org.apache.jorphan.logging.LoggingManager; 30 import org.apache.log.Logger; 31 32 49 class ComboStringEditor extends PropertyEditorSupport 50 implements ItemListener 51 { 52 protected static Logger log= LoggingManager.getLoggerForClass(); 53 54 57 private String [] tags= new String [0]; 58 59 62 private boolean noUndefined= false; 63 64 68 private boolean noEdit= false; 69 70 73 private String initialEditValue; 74 75 private JComboBox combo; 76 private DefaultComboBoxModel model; 77 private boolean startingEdit= false; 78 82 83 private static final Object UNDEFINED= 84 new UniqueObject(JMeterUtils.getResString("property_undefined")); 85 private static final Object EDIT= 86 new UniqueObject(JMeterUtils.getResString("property_edit")); 87 88 ComboStringEditor() 89 { 90 92 model= new DefaultComboBoxModel (); 93 model.addElement(UNDEFINED); 94 model.addElement(EDIT); 95 96 combo= new JComboBox (model); 97 combo.addItemListener(this); 98 combo.setEditable(false); 99 } 100 101 104 public boolean supportsCustomEditor() 105 { 106 return true; 107 } 108 109 112 public Component getCustomEditor() 113 { 114 return combo; 115 } 116 117 120 public Object getValue() 121 { 122 return getAsText(); 123 } 124 125 128 public String getAsText() 129 { 130 Object value= combo.getSelectedItem(); 131 132 if (value == UNDEFINED) return null; 133 else return (String )value; 134 } 135 136 139 public void setValue(Object value) 140 { 141 setAsText((String )value); 142 } 143 144 147 public void setAsText(String value) 148 { 149 combo.setEditable(true); 150 151 if (value == null) combo.setSelectedItem(UNDEFINED); 152 else combo.setSelectedItem(value); 153 154 if (! startingEdit && combo.getSelectedIndex() >= 0) 155 { 156 combo.setEditable(false); 157 } 158 } 159 160 163 public void itemStateChanged(ItemEvent e) 164 { 165 if (e.getStateChange() == ItemEvent.SELECTED) 166 { 167 if (e.getItem() == EDIT) { 168 startingEdit= true; 169 startEditing(); 170 startingEdit= false; 171 } 172 else 173 { 174 if (!startingEdit && combo.getSelectedIndex() >= 0) 175 { 176 combo.setEditable(false); 177 } 178 179 firePropertyChange(); 180 } 181 } 182 } 183 184 private void startEditing() 185 { 186 JTextComponent textField= 187 (JTextComponent )combo.getEditor().getEditorComponent(); 188 189 combo.setEditable(true); 190 191 textField.requestFocus(); 192 193 String text= initialEditValue; 194 if (initialEditValue == null) text= ""; 196 combo.setSelectedItem(text); 197 198 int i= text.indexOf("${}"); 199 if (i != -1) textField.setCaretPosition(i+2); 200 else textField.selectAll(); 201 } 202 203 206 public String getInitialEditValue() 207 { 208 return initialEditValue; 209 } 210 211 214 public boolean getNoEdit() 215 { 216 return noEdit; 217 } 218 219 222 public boolean getNoUndefined() 223 { 224 return noUndefined; 225 } 226 227 230 public String [] getTags() 231 { 232 return tags; 233 } 234 235 238 public void setInitialEditValue(String object) 239 { 240 initialEditValue= object; 241 } 242 243 246 public void setNoEdit(boolean b) 247 { 248 if (noEdit == b) return; 249 noEdit= b; 250 251 if (noEdit) model.removeElement(EDIT); 252 else model.addElement(EDIT); 253 } 254 255 258 public void setNoUndefined(boolean b) 259 { 260 if (noUndefined == b) return; 261 noUndefined= b; 262 263 if (noUndefined) model.removeElement(UNDEFINED); 264 else model.insertElementAt(UNDEFINED, 0); 265 } 266 267 270 public void setTags(String [] strings) 271 { 272 if (tags.equals(strings)) return; 273 274 for (int i=0; i<tags.length; i++) model.removeElement(tags[i]); 275 276 tags= strings==null ? new String [0] : strings; 277 278 int b= noUndefined ? 0 : 1; for (int i=0; i<tags.length; i++) model.insertElementAt(tags[i], b+i); 280 } 281 282 289 private static class UniqueObject 290 { 291 private String s; 292 293 UniqueObject(String s) 294 { 295 this.s= s; 296 } 297 298 public String toString() 299 { 300 return s; 301 } 302 } 303 304 public static class Test extends junit.framework.TestCase 305 { 306 public Test(String name) 307 { 308 super(name); 309 } 310 311 private void testSetGet(ComboStringEditor e, Object value) throws Exception 312 { 313 e.setValue(value); 314 assertEquals(value, e.getValue()); 315 } 316 private void testSetGetAsText(ComboStringEditor e, String text) throws Exception 317 { 318 e.setAsText(text); 319 assertEquals(text, e.getAsText()); 320 } 321 public void testSetGet() throws Exception 322 { 323 ComboStringEditor e= new ComboStringEditor(); 324 325 testSetGet(e, "any string"); 326 testSetGet(e, ""); 327 testSetGet(e, null); 328 testSetGet(e, "${var}"); 329 } 330 public void testSetGetAsText() throws Exception 331 { 332 ComboStringEditor e= new ComboStringEditor(); 333 334 testSetGetAsText(e, "any string"); 335 testSetGetAsText(e, ""); 336 testSetGetAsText(e, null); 337 testSetGetAsText(e, "${var}"); 338 339 e.setAsText(UNDEFINED.toString()); 341 assertNotNull(e.getAsText()); 342 } 343 } 344 } | Popular Tags |