1 20 21 package org.apache.directory.ldapstudio.browser.common.dialogs.preferences; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 import java.util.List ; 28 import java.util.SortedMap ; 29 import java.util.TreeMap ; 30 31 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 32 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 33 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 34 import org.apache.directory.ldapstudio.browser.core.ConnectionManager; 35 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 36 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription; 37 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeValueProviderRelation; 38 import org.apache.directory.ldapstudio.browser.core.model.schema.LdapSyntaxDescription; 39 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 40 import org.apache.directory.ldapstudio.browser.core.model.schema.SyntaxValueProviderRelation; 41 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager; 42 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager.ValueEditorExtension; 43 import org.eclipse.jface.preference.PreferencePage; 44 import org.eclipse.jface.viewers.ArrayContentProvider; 45 import org.eclipse.jface.viewers.DoubleClickEvent; 46 import org.eclipse.jface.viewers.IDoubleClickListener; 47 import org.eclipse.jface.viewers.ITableLabelProvider; 48 import org.eclipse.jface.viewers.LabelProvider; 49 import org.eclipse.jface.viewers.StructuredSelection; 50 import org.eclipse.jface.viewers.TableViewer; 51 import org.eclipse.swt.SWT; 52 import org.eclipse.swt.events.SelectionAdapter; 53 import org.eclipse.swt.events.SelectionEvent; 54 import org.eclipse.swt.graphics.Image; 55 import org.eclipse.swt.layout.GridData; 56 import org.eclipse.swt.widgets.Button; 57 import org.eclipse.swt.widgets.Composite; 58 import org.eclipse.swt.widgets.Control; 59 import org.eclipse.swt.widgets.Table; 60 import org.eclipse.swt.widgets.TableColumn; 61 import org.eclipse.ui.IWorkbench; 62 import org.eclipse.ui.IWorkbenchPreferencePage; 63 64 65 public class ValueEditorsPreferencePage extends PreferencePage implements IWorkbenchPreferencePage 66 { 67 68 private SortedMap <String , ValueEditorExtension> class2ValueEditorProxyMap; 69 70 private SortedMap attributeOid2AtdMap; 71 72 private SortedMap attributeTypes2AtdMap; 73 74 private String [] attributeTypesAndOids; 75 76 private SortedMap syntaxOid2LsdMap; 77 78 private SortedMap syntaxDesc2LsdMap; 79 80 private String [] syntaxDescsAndOids; 81 82 private List attributeList; 83 84 private TableViewer attributeViewer; 85 86 private Button attributeAddButton; 87 88 private Button attributeEditButton; 89 90 private Button attributeRemoveButton; 91 92 private List syntaxList; 93 94 private TableViewer syntaxViewer; 95 96 private Button syntaxAddButton; 97 98 private Button syntaxEditButton; 99 100 private Button syntaxRemoveButton; 101 102 103 public ValueEditorsPreferencePage() 104 { 105 super(); 106 super.setDescription( "Specify value editors:" ); 107 } 108 109 110 public void init( IWorkbench workbench ) 111 { 112 } 113 114 115 protected Control createContents( Composite parent ) 116 { 117 118 Composite composite = BaseWidgetUtils.createColumnContainer( parent, 1, 1 ); 119 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 120 121 this.class2ValueEditorProxyMap = new TreeMap <String , ValueEditorExtension>(); 123 Composite dummyComposite = new Composite( composite, SWT.NONE ); 124 dummyComposite.setLayoutData( new GridData( 1, 1 ) ); 125 126 Collection <ValueEditorExtension> valueEditorProxys = ValueEditorManager.getValueEditorProxys(); 127 for ( ValueEditorExtension proxy : valueEditorProxys ) 128 { 129 this.class2ValueEditorProxyMap.put( proxy.className, proxy ); 130 } 131 132 this.attributeTypes2AtdMap = new TreeMap (); 134 this.attributeOid2AtdMap = new TreeMap (); 135 ConnectionManager cm = BrowserCorePlugin.getDefault().getConnectionManager(); 136 IConnection[] connections = cm.getConnections(); 137 for ( int i = 0; i < connections.length; i++ ) 138 { 139 Schema schema = connections[i].getSchema(); 140 if ( schema != null ) 141 { 142 createAttributeMaps( schema ); 143 } 144 } 145 createAttributeMaps( Schema.DEFAULT_SCHEMA ); 146 this.attributeTypesAndOids = new String [this.attributeTypes2AtdMap.size() + this.attributeOid2AtdMap.size()]; 147 System.arraycopy( this.attributeTypes2AtdMap.keySet().toArray(), 0, this.attributeTypesAndOids, 0, 148 this.attributeTypes2AtdMap.size() ); 149 System.arraycopy( this.attributeOid2AtdMap.keySet().toArray(), 0, this.attributeTypesAndOids, 150 this.attributeTypes2AtdMap.size(), this.attributeOid2AtdMap.size() ); 151 152 this.syntaxOid2LsdMap = new TreeMap (); 154 this.syntaxDesc2LsdMap = new TreeMap (); 155 for ( int i = 0; i < connections.length; i++ ) 156 { 157 Schema schema = connections[i].getSchema(); 158 if ( schema != null ) 159 { 160 createSyntaxMaps( schema ); 161 } 162 } 163 createSyntaxMaps( Schema.DEFAULT_SCHEMA ); 164 this.syntaxDescsAndOids = new String [this.syntaxOid2LsdMap.size()]; 165 System.arraycopy( this.syntaxOid2LsdMap.keySet().toArray(), 0, this.syntaxDescsAndOids, 0, 166 this.syntaxOid2LsdMap.size() ); 167 168 BaseWidgetUtils.createSpacer( composite, 1 ); 171 this.createAttributeContents( composite ); 172 this.attributeList = new ArrayList ( Arrays.asList( BrowserCommonActivator.getDefault().getValueEditorsPreferences() 173 .getAttributeValueProviderRelations() ) ); 174 attributeViewer.setInput( this.attributeList ); 175 attributeViewer.getTable().getColumn( 0 ).pack(); 176 attributeViewer.getTable().getColumn( 2 ).pack(); 178 attributeViewer.getTable().pack(); 179 180 BaseWidgetUtils.createSpacer( composite, 1 ); 182 BaseWidgetUtils.createSpacer( composite, 1 ); 183 this.createSyntaxContents( composite ); 184 this.syntaxList = new ArrayList ( Arrays.asList( BrowserCommonActivator.getDefault().getValueEditorsPreferences() 185 .getSyntaxValueProviderRelations() ) ); 186 syntaxViewer.setInput( this.syntaxList ); 187 syntaxViewer.getTable().getColumn( 0 ).pack(); 188 syntaxViewer.getTable().getColumn( 2 ).pack(); 190 syntaxViewer.getTable().pack(); 191 192 return composite; 193 } 194 195 196 private void createAttributeMaps( Schema schema ) 197 { 198 AttributeTypeDescription[] atds = schema.getAttributeTypeDescriptions(); 199 for ( int i = 0; i < atds.length; i++ ) 200 { 201 202 attributeOid2AtdMap.put( atds[i].getNumericOID(), atds[i] ); 203 204 String [] names = atds[i].getNames(); 205 for ( int j = 0; j < names.length; j++ ) 206 { 207 attributeTypes2AtdMap.put( names[j], atds[i] ); 208 } 209 210 } 211 } 212 213 214 private void createSyntaxMaps( Schema schema ) 215 { 216 LdapSyntaxDescription[] lsds = schema.getLdapSyntaxDescriptions(); 217 for ( int i = 0; i < lsds.length; i++ ) 218 { 219 220 syntaxOid2LsdMap.put( lsds[i].getNumericOID(), lsds[i] ); 221 222 if ( lsds[i].getDesc() != null ) 223 { 224 syntaxDesc2LsdMap.put( lsds[i].getDesc(), lsds[i] ); 225 } 226 227 } 228 } 229 230 231 private void createAttributeContents( Composite parent ) 232 { 233 234 BaseWidgetUtils.createLabel( parent, "Value Editors by Attribute Types", 1 ); 235 236 Composite composite = BaseWidgetUtils.createColumnContainer( parent, 2, 1 ); 237 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 238 Composite listComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 ); 239 listComposite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 240 Composite buttonComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 ); 241 buttonComposite.setLayoutData( new GridData( GridData.VERTICAL_ALIGN_BEGINNING ) ); 242 243 Table table = new Table( listComposite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION ); 244 GridData data = new GridData( GridData.FILL_BOTH ); 245 data.widthHint = 360; 246 data.heightHint = convertHeightInCharsToPixels( 10 ); 247 table.setLayoutData( data ); 248 table.setHeaderVisible( true ); 249 table.setLinesVisible( true ); 250 attributeViewer = new TableViewer( table ); 251 252 TableColumn c1 = new TableColumn( table, SWT.NONE ); 253 c1.setText( "Attribute" ); 254 c1.setWidth( 80 ); 255 TableColumn c2 = new TableColumn( table, SWT.NONE ); 256 c2.setText( "Alias" ); 257 c2.setWidth( 80 ); 258 TableColumn c3 = new TableColumn( table, SWT.NONE ); 259 c3.setText( "Value Editor" ); 260 c3.setWidth( 200 ); 261 262 attributeViewer.setColumnProperties( new String [] 263 { "Attribute", "Value Editor" } ); 264 attributeViewer.setContentProvider( new ArrayContentProvider() ); 265 attributeViewer.setLabelProvider( new AttributeLabelProvider() ); 266 267 attributeViewer.addDoubleClickListener( new IDoubleClickListener() 268 { 269 public void doubleClick( DoubleClickEvent event ) 270 { 271 editAttribute(); 272 } 273 } ); 274 275 attributeAddButton = BaseWidgetUtils.createButton( buttonComposite, "Add...", 1 ); 276 attributeAddButton.addSelectionListener( new SelectionAdapter() 277 { 278 public void widgetSelected( SelectionEvent e ) 279 { 280 addAttribute(); 281 } 282 } ); 283 attributeEditButton = BaseWidgetUtils.createButton( buttonComposite, "Edit...", 1 ); 284 attributeEditButton.addSelectionListener( new SelectionAdapter() 285 { 286 public void widgetSelected( SelectionEvent e ) 287 { 288 editAttribute(); 289 } 290 } ); 291 attributeRemoveButton = BaseWidgetUtils.createButton( buttonComposite, "Remove", 1 ); 292 attributeRemoveButton.addSelectionListener( new SelectionAdapter() 293 { 294 public void widgetSelected( SelectionEvent e ) 295 { 296 removeAttribute(); 297 } 298 } ); 299 300 } 304 305 306 private void createSyntaxContents( Composite parent ) 307 { 308 309 BaseWidgetUtils.createLabel( parent, "Value Editors by Syntax", 1 ); 310 311 Composite composite = BaseWidgetUtils.createColumnContainer( parent, 2, 1 ); 312 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 313 Composite listComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 ); 314 listComposite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 315 Composite buttonComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 1 ); 316 buttonComposite.setLayoutData( new GridData( GridData.VERTICAL_ALIGN_BEGINNING ) ); 317 318 Table table = new Table( listComposite, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.FULL_SELECTION ); 319 GridData data = new GridData( GridData.FILL_BOTH ); 320 data.widthHint = 360; 321 data.heightHint = convertHeightInCharsToPixels( 10 ); 322 table.setLayoutData( data ); 323 table.setHeaderVisible( true ); 324 table.setLinesVisible( true ); 325 syntaxViewer = new TableViewer( table ); 326 327 TableColumn c1 = new TableColumn( table, SWT.NONE ); 328 c1.setText( "Syntax" ); 329 c1.setWidth( 80 ); 330 TableColumn c2 = new TableColumn( table, SWT.NONE ); 331 c2.setText( "Desc" ); 332 c2.setWidth( 80 ); 333 TableColumn c3 = new TableColumn( table, SWT.NONE ); 334 c3.setText( "Value Editor" ); 335 c3.setWidth( 200 ); 336 337 syntaxViewer.setColumnProperties( new String [] 338 { "Syntax", "Value Editor" } ); 339 syntaxViewer.setContentProvider( new ArrayContentProvider() ); 340 syntaxViewer.setLabelProvider( new SyntaxLabelProvider() ); 341 342 syntaxViewer.addDoubleClickListener( new IDoubleClickListener() 343 { 344 public void doubleClick( DoubleClickEvent event ) 345 { 346 editSyntax(); 347 } 348 } ); 349 350 syntaxAddButton = BaseWidgetUtils.createButton( buttonComposite, "Add...", 1 ); 351 syntaxAddButton.addSelectionListener( new SelectionAdapter() 352 { 353 public void widgetSelected( SelectionEvent e ) 354 { 355 addSyntax(); 356 } 357 } ); 358 syntaxEditButton = BaseWidgetUtils.createButton( buttonComposite, "Edit...", 1 ); 359 syntaxEditButton.addSelectionListener( new SelectionAdapter() 360 { 361 public void widgetSelected( SelectionEvent e ) 362 { 363 editSyntax(); 364 } 365 } ); 366 syntaxRemoveButton = BaseWidgetUtils.createButton( buttonComposite, "Remove", 1 ); 367 syntaxRemoveButton.addSelectionListener( new SelectionAdapter() 368 { 369 public void widgetSelected( SelectionEvent e ) 370 { 371 removeSyntax(); 372 } 373 } ); 374 375 } 379 380 381 protected void addAttribute() 382 { 383 AttributeValueEditorDialog dialog = new AttributeValueEditorDialog( getShell(), null, 384 this.class2ValueEditorProxyMap, this.attributeTypesAndOids ); 385 if ( dialog.open() == AttributeValueEditorDialog.OK ) 386 { 387 this.attributeList.add( dialog.getRelation() ); 388 this.attributeViewer.refresh(); 389 } 390 } 391 392 393 protected void removeAttribute() 394 { 395 Object o = ( ( StructuredSelection ) this.attributeViewer.getSelection() ).getFirstElement(); 396 this.attributeList.remove( o ); 397 this.attributeViewer.refresh(); 398 } 399 400 401 protected void editAttribute() 402 { 403 StructuredSelection sel = ( StructuredSelection ) this.attributeViewer.getSelection(); 404 if ( !sel.isEmpty() ) 405 { 406 AttributeValueProviderRelation relation = ( AttributeValueProviderRelation ) sel.getFirstElement(); 407 AttributeValueEditorDialog dialog = new AttributeValueEditorDialog( getShell(), relation, 408 this.class2ValueEditorProxyMap, this.attributeTypesAndOids ); 409 if ( dialog.open() == AttributeValueEditorDialog.OK ) 410 { 411 int index = this.attributeList.indexOf( relation ); 412 this.attributeList.set( index, dialog.getRelation() ); 413 this.attributeViewer.refresh(); 414 } 415 } 416 } 417 418 419 protected void addSyntax() 420 { 421 SyntaxValueEditorDialog dialog = new SyntaxValueEditorDialog( getShell(), null, 422 this.class2ValueEditorProxyMap, this.syntaxDescsAndOids ); 423 if ( dialog.open() == SyntaxValueEditorDialog.OK ) 424 { 425 this.syntaxList.add( dialog.getRelation() ); 426 this.syntaxViewer.refresh(); 427 } 428 } 429 430 431 protected void removeSyntax() 432 { 433 Object o = ( ( StructuredSelection ) this.syntaxViewer.getSelection() ).getFirstElement(); 434 this.syntaxList.remove( o ); 435 this.syntaxViewer.refresh(); 436 } 437 438 439 protected void editSyntax() 440 { 441 StructuredSelection sel = ( StructuredSelection ) this.syntaxViewer.getSelection(); 442 if ( !sel.isEmpty() ) 443 { 444 SyntaxValueProviderRelation relation = ( SyntaxValueProviderRelation ) sel.getFirstElement(); 445 SyntaxValueEditorDialog dialog = new SyntaxValueEditorDialog( getShell(), relation, 446 this.class2ValueEditorProxyMap, this.syntaxDescsAndOids ); 447 if ( dialog.open() == SyntaxValueEditorDialog.OK ) 448 { 449 int index = this.syntaxList.indexOf( relation ); 450 this.syntaxList.set( index, dialog.getRelation() ); 451 this.syntaxViewer.refresh(); 452 } 453 } 454 } 455 456 457 public boolean performOk() 458 { 459 AttributeValueProviderRelation[] aRelations = ( AttributeValueProviderRelation[] ) this.attributeList 460 .toArray( new AttributeValueProviderRelation[this.attributeList.size()] ); 461 BrowserCommonActivator.getDefault().getValueEditorsPreferences().setAttributeValueProviderRelations( aRelations ); 462 463 SyntaxValueProviderRelation[] sRelations = ( SyntaxValueProviderRelation[] ) this.syntaxList 464 .toArray( new SyntaxValueProviderRelation[this.syntaxList.size()] ); 465 BrowserCommonActivator.getDefault().getValueEditorsPreferences().setSyntaxValueProviderRelations( sRelations ); 466 467 return true; 468 } 469 470 471 protected void performDefaults() 472 { 473 this.attributeList.clear(); 474 this.attributeList.addAll( Arrays.asList( BrowserCommonActivator.getDefault().getValueEditorsPreferences() 475 .getDefaultAttributeValueProviderRelations() ) ); 476 this.attributeViewer.refresh(); 477 478 this.syntaxList.clear(); 479 this.syntaxList.addAll( Arrays.asList( BrowserCommonActivator.getDefault().getValueEditorsPreferences() 480 .getDefaultSyntaxValueProviderRelations() ) ); 481 this.syntaxViewer.refresh(); 482 483 super.performDefaults(); 484 } 485 486 class AttributeLabelProvider extends LabelProvider implements ITableLabelProvider 487 { 488 public String getColumnText( Object obj, int index ) 489 { 490 if ( obj instanceof AttributeValueProviderRelation ) 491 { 492 AttributeValueProviderRelation relation = ( AttributeValueProviderRelation ) obj; 493 if ( index == 0 ) 494 { 495 return relation.getAttributeNumericOidOrType(); 496 } 497 else if ( index == 1 ) 498 { 499 if ( relation.getAttributeNumericOidOrType() != null ) 500 { 501 if ( attributeTypes2AtdMap.containsKey( relation.getAttributeNumericOidOrType() ) ) 502 { 503 AttributeTypeDescription atd = ( AttributeTypeDescription ) attributeTypes2AtdMap 504 .get( relation.getAttributeNumericOidOrType() ); 505 String s = atd.getNumericOID(); 506 for ( int i = 0; i < atd.getNames().length; i++ ) 507 { 508 if ( !relation.getAttributeNumericOidOrType().equals( atd.getNames()[i] ) ) 509 { 510 s += ", " + atd.getNames()[i]; 511 } 512 } 513 return s; 514 } 515 else if ( attributeOid2AtdMap.containsKey( relation.getAttributeNumericOidOrType() ) ) 516 { 517 AttributeTypeDescription atd = ( AttributeTypeDescription ) attributeOid2AtdMap 518 .get( relation.getAttributeNumericOidOrType() ); 519 return atd.toString(); 520 } 521 } 522 } 523 else if ( index == 2 ) 524 { 525 ValueEditorExtension vp = class2ValueEditorProxyMap.get( relation.getValueProviderClassname() ); 526 return vp != null ? vp.name : null; 527 } 528 } 529 return null; 530 } 531 532 533 public Image getColumnImage( Object obj, int index ) 534 { 535 if ( obj instanceof AttributeValueProviderRelation ) 536 { 537 AttributeValueProviderRelation relation = ( AttributeValueProviderRelation ) obj; 538 if ( index == 2 ) 539 { 540 ValueEditorExtension vp = class2ValueEditorProxyMap.get( relation.getValueProviderClassname() ); 541 return vp != null ? vp.icon.createImage() : null; 542 } 543 } 544 545 return null; 546 } 547 } 548 549 class SyntaxLabelProvider extends LabelProvider implements ITableLabelProvider 550 { 551 public String getColumnText( Object obj, int index ) 552 { 553 if ( obj instanceof SyntaxValueProviderRelation ) 554 { 555 SyntaxValueProviderRelation relation = ( SyntaxValueProviderRelation ) obj; 556 if ( index == 0 ) 557 { 558 return relation.getSyntaxOID(); 559 } 560 else if ( index == 1 ) 561 { 562 if ( relation.getSyntaxOID() != null ) 563 { 564 if ( syntaxOid2LsdMap.containsKey( relation.getSyntaxOID() ) ) 565 { 566 LdapSyntaxDescription lsd = ( LdapSyntaxDescription ) syntaxOid2LsdMap.get( relation 567 .getSyntaxOID() ); 568 return lsd.toString(); 569 } 570 } 571 } 572 else if ( index == 2 ) 573 { 574 ValueEditorExtension vp = class2ValueEditorProxyMap.get( relation.getValueProviderClassname() ); 575 return vp != null ? vp.name : null; 576 } 577 } 578 return null; 579 } 580 581 582 public Image getColumnImage( Object obj, int index ) 583 { 584 if ( obj instanceof SyntaxValueProviderRelation ) 585 { 586 SyntaxValueProviderRelation relation = ( SyntaxValueProviderRelation ) obj; 587 if ( index == 2 ) 588 { 589 ValueEditorExtension vp = class2ValueEditorProxyMap.get( relation.getValueProviderClassname() ); 590 return vp != null ? vp.icon.createImage() : null; 591 } 592 } 593 594 return null; 595 } 596 } 597 598 } 599 | Popular Tags |