1 17 package org.apache.jmeter.testbeans.gui; 18 19 import java.awt.Component ; 20 import java.beans.PropertyChangeEvent ; 21 import java.beans.PropertyChangeListener ; 22 import java.beans.PropertyEditor ; 23 import java.beans.PropertyEditorSupport ; 24 25 import javax.swing.JOptionPane ; 26 27 import org.apache.jmeter.util.JMeterUtils; 28 import org.apache.jorphan.logging.LoggingManager; 29 import org.apache.log.Logger; 30 31 55 class WrapperEditor extends PropertyEditorSupport 56 implements PropertyChangeListener 57 { 58 protected static Logger log= LoggingManager.getLoggerForClass(); 59 60 63 PropertyEditor typeEditor; 64 65 68 PropertyEditor guiEditor; 69 70 73 boolean acceptsNull; 74 75 78 boolean acceptsExpressions; 79 80 83 boolean acceptsOther; 84 85 89 private String lastValidValue= null; 90 91 94 WrapperEditor( 95 Object source, 96 PropertyEditor typeEditor, 97 PropertyEditor guiEditor, 98 boolean acceptsNull, 99 boolean acceptsExpressions, 100 boolean acceptsOther, 101 Object defaultValue) 102 { 103 super(source); 104 initialize(typeEditor, guiEditor, acceptsNull, acceptsExpressions, 105 acceptsOther, defaultValue); 106 } 107 108 111 WrapperEditor( 112 PropertyEditor typeEditor, 113 PropertyEditor guiEditor, 114 boolean acceptsNull, 115 boolean acceptsExpressions, 116 boolean acceptsOther, 117 Object defaultValue) 118 { 119 super(); 120 initialize(typeEditor, guiEditor, acceptsNull, acceptsExpressions, 121 acceptsOther, defaultValue); 122 } 123 124 private void initialize( 125 PropertyEditor typeEditor, 126 PropertyEditor guiEditor, 127 boolean acceptsNull, 128 boolean acceptsExpressions, 129 boolean acceptsOther, 130 Object defaultValue) 131 { 132 this.typeEditor= typeEditor; 133 this.guiEditor= guiEditor; 134 this.acceptsNull= acceptsNull; 135 this.acceptsExpressions= acceptsExpressions; 136 this.acceptsOther= acceptsOther; 137 138 setValue(defaultValue); 139 lastValidValue= getAsText(); 140 141 if (guiEditor instanceof ComboStringEditor) 142 { 143 String [] tags= ((ComboStringEditor)guiEditor).getTags(); 144 145 149 String v; 150 if (! acceptsOther) v="${}"; 151 else if (isValidValue("")) v= ""; 152 else if (acceptsExpressions) v= "${}"; 153 else if (tags != null && tags.length>0) v= tags[0]; 154 else v= getAsText(); 155 156 ((ComboStringEditor)guiEditor).setInitialEditValue(v); 157 } 158 159 guiEditor.addPropertyChangeListener(this); 160 } 161 162 public boolean supportsCustomEditor() 163 { 164 return true; 165 } 166 167 public Component getCustomEditor() 168 { 169 return guiEditor.getCustomEditor(); 170 } 171 172 public String [] getTags() 173 { 174 return guiEditor.getTags(); 175 } 176 177 183 private boolean isATag(String text) 184 { 185 String [] tags= getTags(); 186 if (tags == null) return false; 187 for (int i=0; i<tags.length; i++) 188 { 189 if (tags[i].equals(text)) return true; 190 } 191 return false; 192 } 193 194 200 private boolean isValidValue(String text) 201 { 202 if (text == null) return acceptsNull; 203 204 if (acceptsExpressions && isExpression(text)) return true; 205 206 208 if (isATag(text)) return true; 210 211 if (! acceptsOther) return false; 213 214 try 216 { 217 typeEditor.setAsText(text); 218 } 219 catch (IllegalArgumentException e1) 220 { 221 return false; 223 } 224 return true; 226 } 227 228 236 private final void shouldNeverHappen() throws Error 237 { 238 throw new Error (); } 240 241 247 private final void shouldNeverHappen(Exception e) throws Error 248 { 249 throw new Error (e.toString()); } 251 252 259 private final boolean isExpression(String text) 260 { 261 return text.indexOf("${") != -1; 262 } 263 264 270 private final boolean isExpression(Object text) 271 { 272 return text instanceof String && isExpression((String )text); 273 } 274 275 279 public Object getValue() 280 { 281 String text= (String )guiEditor.getValue(); 282 283 Object value; 284 285 if (text == null) 286 { 287 if (!acceptsNull) shouldNeverHappen(); 288 value= null; 289 } 290 else 291 { 292 if (acceptsExpressions && isExpression(text)) 293 { 294 value= text; 295 } 296 else 297 { 298 300 if (! acceptsOther && ! isATag(text)) shouldNeverHappen(); 302 303 try 304 { 305 typeEditor.setAsText(text); 306 } 307 catch (IllegalArgumentException e) 308 { 309 shouldNeverHappen(e); 310 } 311 value= typeEditor.getValue(); 312 } 313 } 314 315 if (log.isDebugEnabled()) 316 { 317 log.debug( 318 "->" 319 + (value != null ? value.getClass().getName() : "NULL") 320 + ":" 321 + value); 322 } 323 return value; 324 } 325 326 public void setValue(Object value) 327 { 328 String text; 329 330 if (log.isDebugEnabled()) 331 { 332 log.debug( 333 "<-" 334 + (value != null ? value.getClass().getName() : "NULL") 335 + ":" 336 + value); 337 } 338 339 if (value == null) 340 { 341 if (!acceptsNull) throw new IllegalArgumentException (); 342 text= null; 343 } 344 else if (acceptsExpressions && isExpression(value)) 345 { 346 text= (String )value; 347 } 348 else 349 { 350 typeEditor.setValue(value); text= typeEditor.getAsText(); 353 354 if (! acceptsOther && ! isATag(text)) throw new IllegalArgumentException (); 355 } 356 357 guiEditor.setValue(text); 358 } 359 360 public String getAsText() 361 { 362 String text= guiEditor.getAsText(); 363 364 if (text == null) 365 { 366 if (!acceptsNull) shouldNeverHappen(); 367 } 368 else if (!acceptsExpressions || !isExpression(text)) 369 { 370 try 372 { 373 typeEditor.setAsText(text); 374 } 375 catch (IllegalArgumentException e) 376 { 377 shouldNeverHappen(e); 378 } 379 text= typeEditor.getAsText(); 380 381 if (! acceptsOther && ! isATag(text)) shouldNeverHappen(); 383 } 384 385 if (log.isDebugEnabled()) 386 { 387 log.debug("->\"" + text + "\""); 388 } 389 return text; 390 } 391 392 public void setAsText(String text) throws IllegalArgumentException 393 { 394 if (log.isDebugEnabled()) 395 { 396 log.debug(text == null ? "<-null" : "<-\"" + text + "\""); 397 } 398 399 String value; 400 401 if (text == null) 402 { 403 if (! acceptsNull) throw new IllegalArgumentException (); 404 value= null; 405 } 406 else 407 { 408 if (acceptsExpressions && isExpression(text)) 409 { 410 value= text; 411 } 412 else 413 { 414 typeEditor.setAsText(text); value= typeEditor.getAsText(); 417 418 if (! acceptsOther && ! isATag(text)) throw new IllegalArgumentException (); 419 } 420 } 421 422 guiEditor.setValue(value); 423 } 424 425 public void propertyChange(PropertyChangeEvent event) 426 { 427 String text= guiEditor.getAsText(); 428 if (isValidValue(text)) 429 { 430 lastValidValue= text; 431 firePropertyChange(); 432 } 433 else 434 { 435 JOptionPane.showMessageDialog( 437 guiEditor.getCustomEditor().getParent(), 438 JMeterUtils.getResString("property_editor.value_is_invalid_message"), 439 JMeterUtils.getResString("property_editor.value_is_invalid_title"), 440 JOptionPane.WARNING_MESSAGE); 441 442 guiEditor.setAsText(lastValidValue); 444 } 445 } 446 } | Popular Tags |