1 20 21 package org.apache.directory.ldapstudio.valueeditors; 22 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.LinkedHashSet ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 34 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 35 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 36 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 37 import org.apache.directory.ldapstudio.browser.core.model.IValue; 38 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription; 39 import org.apache.directory.ldapstudio.browser.core.model.schema.LdapSyntaxDescription; 40 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 41 import org.eclipse.core.runtime.IConfigurationElement; 42 import org.eclipse.core.runtime.IExtension; 43 import org.eclipse.core.runtime.IExtensionPoint; 44 import org.eclipse.core.runtime.IExtensionRegistry; 45 import org.eclipse.core.runtime.IStatus; 46 import org.eclipse.core.runtime.Platform; 47 import org.eclipse.core.runtime.Status; 48 import org.eclipse.jface.resource.ImageDescriptor; 49 import org.eclipse.swt.widgets.Composite; 50 import org.eclipse.ui.plugin.AbstractUIPlugin; 51 52 53 65 public class ValueEditorManager 66 { 67 68 private static final String EXTENSION_POINT = "org.apache.directory.ldapstudio.valueeditors"; 69 70 71 private Composite parent; 72 73 77 private IValueEditor userSelectedValueEditor; 78 79 80 private MultivaluedValueEditor multiValuedValueEditor; 81 82 83 private IValueEditor defaultStringSingleLineValueEditor; 84 85 86 private IValueEditor defaultStringMultiLineValueEditor; 87 88 89 private IValueEditor defaultBinaryValueEditor; 90 91 92 private Map <String , IValueEditor> class2ValueEditors; 93 94 95 100 public ValueEditorManager( Composite parent ) 101 { 102 this.parent = parent; 103 userSelectedValueEditor = null; 104 105 class2ValueEditors = new HashMap <String , IValueEditor>(); 107 Collection <IValueEditor> valueEditors = createValueEditors( parent ); 108 for ( IValueEditor valueEditor : valueEditors ) 109 { 110 class2ValueEditors.put( valueEditor.getClass().getName(), valueEditor ); 111 } 112 113 multiValuedValueEditor = new MultivaluedValueEditor( this.parent, this ); 115 multiValuedValueEditor.setValueEditorName( "Mulitvalued Editor" ); 116 multiValuedValueEditor.setValueEditorImageDescriptor( BrowserCommonActivator.getDefault().getImageDescriptor( 117 BrowserCommonConstants.IMG_MULTIVALUEDEDITOR ) ); 118 119 defaultStringSingleLineValueEditor = class2ValueEditors.get( InPlaceTextValueEditor.class.getName() ); 121 defaultStringMultiLineValueEditor = class2ValueEditors.get( TextValueEditor.class.getName() ); 122 defaultBinaryValueEditor = class2ValueEditors.get( HexValueEditor.class.getName() ); 123 } 124 125 126 129 public void dispose() 130 { 131 if ( this.parent != null ) 132 { 133 this.userSelectedValueEditor = null; 134 this.multiValuedValueEditor.dispose(); 135 this.defaultStringSingleLineValueEditor.dispose(); 136 this.defaultStringMultiLineValueEditor.dispose(); 137 this.defaultBinaryValueEditor.dispose(); 138 139 for ( Iterator it = this.class2ValueEditors.values().iterator(); it.hasNext(); ) 140 { 141 IValueEditor vp = ( IValueEditor ) it.next(); 142 vp.dispose(); 143 } 144 145 this.parent = null; 146 } 147 } 148 149 150 156 public void setUserSelectedValueEditor( IValueEditor userSelectedValueEditor ) 157 { 158 this.userSelectedValueEditor = userSelectedValueEditor; 159 } 160 161 162 180 public IValueEditor getCurrentValueEditor( Schema schema, String attributeType ) 181 { 182 183 if ( this.userSelectedValueEditor != null ) 185 { 186 return this.userSelectedValueEditor; 187 } 188 189 AttributeTypeDescription atd = schema.getAttributeTypeDescription( attributeType ); 191 Map attributeValueEditorMap = BrowserCommonActivator.getDefault().getValueEditorsPreferences().getAttributeValueEditorMap(); 192 if ( atd.getNumericOID() != null && attributeValueEditorMap.containsKey( atd.getNumericOID().toLowerCase() ) ) 193 { 194 return ( IValueEditor ) this.class2ValueEditors.get( attributeValueEditorMap.get( atd.getNumericOID() 195 .toLowerCase() ) ); 196 } 197 String [] names = atd.getNames(); 198 for ( int i = 0; i < names.length; i++ ) 199 { 200 if ( attributeValueEditorMap.containsKey( names[i].toLowerCase() ) ) 201 { 202 return ( IValueEditor ) this.class2ValueEditors.get( attributeValueEditorMap.get( names[i] 203 .toLowerCase() ) ); 204 } 205 } 206 207 LdapSyntaxDescription lsd = atd.getSyntaxDescription(); 209 Map syntaxValueEditorMap = BrowserCommonActivator.getDefault().getValueEditorsPreferences().getSyntaxValueEditorMap(); 210 if ( lsd.getNumericOID() != null && syntaxValueEditorMap.containsKey( lsd.getNumericOID().toLowerCase() ) ) 211 { 212 return ( IValueEditor ) this.class2ValueEditors.get( syntaxValueEditorMap.get( lsd.getNumericOID() 213 .toLowerCase() ) ); 214 } 215 216 if ( lsd.isBinary() ) 218 { 219 return this.defaultBinaryValueEditor; 220 } 221 else 222 { 223 return this.defaultStringSingleLineValueEditor; 224 } 225 226 } 227 228 229 237 public IValueEditor getCurrentValueEditor( IEntry entry, String attributeType ) 238 { 239 return getCurrentValueEditor( entry.getConnection().getSchema(), attributeType ); 240 } 241 242 243 250 public IValueEditor getCurrentValueEditor( IValue value ) 251 { 252 253 IValueEditor ve = this.getCurrentValueEditor( value.getAttribute().getEntry(), value.getAttribute() 254 .getDescription() ); 255 256 if ( ve == this.defaultStringSingleLineValueEditor ) 258 { 259 if ( value.getStringValue().indexOf( '\n' ) == -1 && value.getStringValue().indexOf( '\r' ) == -1 ) 260 { 261 ve = this.defaultStringSingleLineValueEditor; 262 } 263 else 264 { 265 ve = this.defaultStringMultiLineValueEditor; 266 } 267 } 268 269 return ve; 270 } 271 272 273 280 public IValueEditor getCurrentValueEditor( AttributeHierarchy attributeHierarchy ) 281 { 282 if ( attributeHierarchy == null ) 283 { 284 return null; 285 } 286 else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 0 ) 287 { 288 return this.getCurrentValueEditor( attributeHierarchy.getAttribute().getEntry(), attributeHierarchy 289 .getAttribute().getDescription() ); 290 } 291 else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 1 ) 292 { 293 if ( attributeHierarchy.getAttribute().isObjectClassAttribute() ) 296 { 297 return this.multiValuedValueEditor; 298 } 299 if ( attributeHierarchy.getAttribute().getValues()[0].isRdnPart() ) 300 { 301 return this.multiValuedValueEditor; 302 } 303 304 return this.getCurrentValueEditor( attributeHierarchy.getAttribute().getValues()[0] ); 305 } 306 else 307 { 308 return this.multiValuedValueEditor; 309 } 310 } 311 312 313 321 public IValueEditor[] getAlternativeValueEditors( IEntry entry, String attributeName ) 322 { 323 Schema schema = entry.getConnection().getSchema(); 324 return getAlternativeValueEditors( schema, attributeName ); 325 } 326 327 328 336 public IValueEditor[] getAlternativeValueEditors( Schema schema, String attributeName ) 337 { 338 List <IValueEditor> alternativeList = new ArrayList <IValueEditor>(); 339 340 AttributeTypeDescription atd = schema.getAttributeTypeDescription( attributeName ); 341 342 if ( atd.getSyntaxDescription().isBinary() ) 343 { 344 alternativeList.add( this.defaultBinaryValueEditor ); 345 alternativeList.add( this.defaultStringSingleLineValueEditor ); 346 alternativeList.add( this.defaultStringMultiLineValueEditor ); 347 } 348 else if ( atd.getSyntaxDescription().isString() ) 349 { 350 alternativeList.add( this.defaultStringSingleLineValueEditor ); 351 alternativeList.add( this.defaultStringMultiLineValueEditor ); 352 alternativeList.add( this.defaultBinaryValueEditor ); 353 } 354 355 alternativeList.add( this.multiValuedValueEditor ); 356 357 alternativeList.remove( getCurrentValueEditor( schema, attributeName ) ); 358 359 return (org.apache.directory.ldapstudio.valueeditors.IValueEditor[] ) alternativeList.toArray( new IValueEditor[alternativeList.size()] ); 360 } 361 362 363 370 public IValueEditor[] getAlternativeValueEditors( IValue value ) 371 { 372 List <IValueEditor> alternativeList = new ArrayList <IValueEditor>(); 373 374 if ( value.isBinary() ) 375 { 376 alternativeList.add( this.defaultBinaryValueEditor ); 377 alternativeList.add( this.defaultStringSingleLineValueEditor ); 378 alternativeList.add( this.defaultStringMultiLineValueEditor ); 379 } 380 else if ( value.isString() ) 381 { 382 alternativeList.add( this.defaultStringSingleLineValueEditor ); 383 alternativeList.add( this.defaultStringMultiLineValueEditor ); 384 alternativeList.add( this.defaultBinaryValueEditor ); 385 } 386 387 alternativeList.add( this.multiValuedValueEditor ); 388 389 alternativeList.remove( getCurrentValueEditor( value ) ); 390 391 return (org.apache.directory.ldapstudio.valueeditors.IValueEditor[] ) alternativeList.toArray( new IValueEditor[alternativeList.size()] ); 392 } 393 394 395 402 public IValueEditor[] getAlternativeValueEditors( AttributeHierarchy ah ) 403 { 404 if ( ah == null ) 405 { 406 return new IValueEditor[0]; 407 } 408 else if ( ah.size() == 1 && ah.getAttribute().getValueSize() == 0 ) 409 { 410 return this.getAlternativeValueEditors( ah.getAttribute().getEntry(), ah.getAttribute().getDescription() ); 411 } 412 else if ( ah.size() == 1 && ah.getAttribute().getValueSize() == 1 ) 413 { 414 415 if ( ah.getAttribute().isObjectClassAttribute() ) 418 { 419 return new IValueEditor[0]; 420 } 421 if ( ah.getAttribute().getValues()[0].isRdnPart() ) 422 { 423 return new IValueEditor[0]; 424 } 425 426 return this.getAlternativeValueEditors( ah.getAttribute().getValues()[0] ); 427 } 428 else 429 { 430 return new IValueEditor[0]; 431 } 432 } 433 434 435 440 public IValueEditor[] getAllValueEditors() 441 { 442 Set <IValueEditor> list = new LinkedHashSet <IValueEditor>(); 444 445 list.add( this.defaultStringSingleLineValueEditor ); 446 list.add( this.defaultStringMultiLineValueEditor ); 447 list.add( defaultBinaryValueEditor ); 448 449 list.addAll( this.class2ValueEditors.values() ); 450 451 list.add( this.multiValuedValueEditor ); 452 453 return list.toArray( new IValueEditor[list.size()] ); 454 } 455 456 457 462 public IValueEditor getDefaultBinaryValueEditor() 463 { 464 return defaultBinaryValueEditor; 465 } 466 467 468 473 public IValueEditor getDefaultStringValueEditor() 474 { 475 return defaultStringMultiLineValueEditor; 476 } 477 478 479 484 public MultivaluedValueEditor getMultiValuedValueEditor() 485 { 486 return multiValuedValueEditor; 487 } 488 489 490 496 private Collection <IValueEditor> createValueEditors( Composite parent ) 497 { 498 Collection <IValueEditor> valueEditors = new ArrayList <IValueEditor>(); 499 500 Collection <ValueEditorExtension> valueEditorProxys = getValueEditorProxys(); 501 for ( ValueEditorExtension proxy : valueEditorProxys ) 502 { 503 try 504 { 505 IValueEditor valueEditor = ( IValueEditor ) proxy.member.createExecutableExtension( "class" ); 506 valueEditor.create( parent ); 507 valueEditor.setValueEditorName( proxy.name ); 508 valueEditor.setValueEditorImageDescriptor( proxy.icon ); 509 valueEditors.add( valueEditor ); 510 } 511 catch ( Exception e ) 512 { 513 BrowserCommonActivator.getDefault().getLog().log( 514 new Status( IStatus.ERROR, BrowserCommonActivator.PLUGIN_ID, 1, "Unable to create ValueEditor " 515 + proxy.className, e ) ); 516 } 517 } 518 519 return valueEditors; 520 } 521 522 523 528 public static Collection <ValueEditorExtension> getValueEditorProxys() 529 { 530 Collection <ValueEditorExtension> valueEditorProxies = new ArrayList <ValueEditorExtension>(); 531 532 IExtensionRegistry registry = Platform.getExtensionRegistry(); 533 IExtensionPoint extensionPoint = registry.getExtensionPoint( EXTENSION_POINT ); 534 IConfigurationElement[] members = extensionPoint.getConfigurationElements(); 535 536 for ( int m = 0; m < members.length; m++ ) 538 { 539 ValueEditorExtension proxy = new ValueEditorExtension(); 540 valueEditorProxies.add( proxy ); 541 542 IConfigurationElement member = members[m]; 543 IExtension extension = member.getDeclaringExtension(); 544 String extendingPluginId = extension.getNamespaceIdentifier(); 545 546 proxy.member = member; 547 proxy.name = member.getAttribute( "name" ); 548 String iconPath = member.getAttribute( "icon" ); 549 proxy.icon = AbstractUIPlugin.imageDescriptorFromPlugin( extendingPluginId, iconPath ); 550 if ( proxy.icon == null ) 551 { 552 proxy.icon = ImageDescriptor.getMissingImageDescriptor(); 553 } 554 proxy.className = member.getAttribute( "class" ); 555 556 IConfigurationElement[] children = member.getChildren(); 557 for ( int c = 0; c < children.length; c++ ) 558 { 559 IConfigurationElement element = children[c]; 560 String type = element.getName(); 561 if ( "syntax".equals( type ) ) 562 { 563 String syntaxOID = element.getAttribute( "syntaxOID" ); 564 proxy.syntaxOids.add( syntaxOID ); 565 } 566 else if ( "attribute".equals( type ) ) 567 { 568 String attributeType = element.getAttribute( "attributeType" ); 569 proxy.attributeTypes.add( attributeType ); 570 } 571 } 572 } 573 574 return valueEditorProxies; 575 } 576 577 583 public static class ValueEditorExtension 584 { 585 586 587 public String name = null; 588 589 590 public ImageDescriptor icon = null; 591 592 593 public String className = null; 594 595 596 public Collection <String > syntaxOids = new ArrayList <String >( 3 ); 597 598 599 public Collection <String > attributeTypes = new ArrayList <String >( 3 ); 600 601 602 private IConfigurationElement member = null; 603 } 604 605 } 606 | Popular Tags |