1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 27 import org.apache.directory.ldapstudio.browser.common.widgets.search.EntryWidget; 28 import org.apache.directory.ldapstudio.browser.core.model.DN; 29 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 30 import org.apache.directory.ldapstudio.browser.core.model.RDN; 31 import org.apache.directory.ldapstudio.browser.core.model.RDNPart; 32 import org.eclipse.jface.fieldassist.ComboContentAdapter; 33 import org.eclipse.jface.fieldassist.ContentProposalAdapter; 34 import org.eclipse.jface.fieldassist.DecoratedField; 35 import org.eclipse.jface.fieldassist.FieldDecoration; 36 import org.eclipse.jface.fieldassist.FieldDecorationRegistry; 37 import org.eclipse.jface.fieldassist.IControlCreator; 38 import org.eclipse.swt.SWT; 39 import org.eclipse.swt.events.ModifyEvent; 40 import org.eclipse.swt.events.ModifyListener; 41 import org.eclipse.swt.events.SelectionAdapter; 42 import org.eclipse.swt.events.SelectionEvent; 43 import org.eclipse.swt.layout.GridData; 44 import org.eclipse.swt.widgets.Button; 45 import org.eclipse.swt.widgets.Combo; 46 import org.eclipse.swt.widgets.Composite; 47 import org.eclipse.swt.widgets.Control; 48 import org.eclipse.swt.widgets.Label; 49 import org.eclipse.swt.widgets.Shell; 50 import org.eclipse.swt.widgets.Text; 51 52 53 60 public class DnBuilderWidget extends BrowserWidget implements ModifyListener 61 { 62 63 64 private String [] attributeNames; 65 66 67 private RDN currentRdn; 68 69 70 private DN currentParentDn; 71 72 73 private boolean showRDN; 74 75 76 private boolean showParent; 77 78 79 private Shell shell; 80 81 82 private DN parentDn; 83 84 85 private EntryWidget parentEntryWidget; 86 87 88 private Composite rdnComposite; 89 90 91 private RDN rdn; 92 93 94 private ArrayList <RdnLine> rdnLineList; 95 96 97 private Text previewText; 98 99 100 106 public DnBuilderWidget( boolean showRDN, boolean showParent ) 107 { 108 this.showRDN = showRDN; 109 this.showParent = showParent; 110 } 111 112 113 116 public void dispose() 117 { 118 } 119 120 121 129 public void setInput( IConnection connection, String [] attributeNames, RDN rdn, DN parentDn ) 130 { 131 this.attributeNames = attributeNames; 132 this.currentRdn = rdn; 133 this.currentParentDn = parentDn; 134 135 if ( showRDN ) 136 { 137 for ( int i = 0; i < rdnLineList.size(); i++ ) 138 { 139 RdnLine rdnLine = rdnLineList.get( i ); 140 String oldName = rdnLine.rdnNameCombo.getText(); 141 rdnLine.rdnNameCombo.setItems( attributeNames ); 142 rdnLine.rdnNameCPA.setContentProposalProvider( new ListContentProposalProvider( attributeNames ) ); 143 if ( Arrays.asList( rdnLine.rdnNameCombo.getItems() ).contains( oldName ) ) 144 { 145 rdnLine.rdnNameCombo.setText( oldName ); 146 } 147 } 148 } 149 150 if ( showRDN ) 151 { 152 while ( !rdnLineList.isEmpty() ) 153 { 154 deleteRdnLine( rdnComposite, 0 ); 155 } 156 if ( currentRdn == null || currentRdn.getParts().length == 0 ) 157 { 158 addRdnLine( rdnComposite, 0 ); 159 rdnLineList.get( 0 ).rdnNameCombo.setFocus(); 160 } 161 else 162 { 163 RDNPart[] parts = currentRdn.getParts(); 164 for ( int i = 0; i < parts.length; i++ ) 165 { 166 addRdnLine( rdnComposite, i ); 167 rdnLineList.get( i ).rdnNameCombo.setText( parts[i].getType() ); 168 rdnLineList.get( i ).rdnValueText.setText( parts[i].getUnencodedValue() ); 169 if ( i == 0 ) 170 { 171 if ( "".equals( rdnLineList.get( i ).rdnNameCombo ) ) 172 { 173 rdnLineList.get( i ).rdnNameCombo.setFocus(); 174 } 175 else 176 { 177 rdnLineList.get( i ).rdnValueText.selectAll(); 178 rdnLineList.get( i ).rdnValueText.setFocus(); 179 } 180 } 181 } 182 } 183 } 184 185 if ( showParent ) 186 { 187 parentEntryWidget.setInput( connection, currentParentDn ); 188 } 189 190 validate(); 191 } 192 193 194 199 public RDN getRdn() 200 { 201 return rdn; 202 } 203 204 205 210 public DN getParentDn() 211 { 212 return parentDn; 213 } 214 215 216 223 public Composite createContents( Composite parent ) 224 { 225 this.shell = parent.getShell(); 226 227 Composite composite = BaseWidgetUtils.createColumnContainer( parent, 3, 1 ); 228 229 if ( showParent ) 231 { 232 BaseWidgetUtils.createLabel( composite, "Parent:", 1 ); 233 parentEntryWidget = new EntryWidget(); 234 parentEntryWidget.createWidget( composite ); 235 parentEntryWidget.addWidgetModifyListener( new WidgetModifyListener() 236 { 237 public void widgetModified( WidgetModifyEvent event ) 238 { 239 validate(); 240 } 241 } ); 242 243 BaseWidgetUtils.createSpacer( composite, 3 ); 244 } 245 246 if ( showRDN ) 248 { 249 BaseWidgetUtils.createLabel( composite, "RDN:", 1 ); 250 rdnComposite = BaseWidgetUtils.createColumnContainer( composite, 5, 2 ); 251 rdnLineList = new ArrayList <RdnLine>(); 252 BaseWidgetUtils.createSpacer( composite, 3 ); 253 } 254 255 if ( showRDN ) 257 { 258 BaseWidgetUtils.createLabel( composite, showParent ? "DN Preview: " : "RDN Preview: ", 1 ); 259 previewText = BaseWidgetUtils.createReadonlyText( composite, "", 2 ); 260 BaseWidgetUtils.createSpacer( composite, 3 ); 261 } 262 263 return composite; 264 } 265 266 267 270 public void modifyText( ModifyEvent e ) 271 { 272 validate(); 273 } 274 275 276 279 public void saveDialogSettings() 280 { 281 if ( parentEntryWidget != null ) 282 { 283 parentEntryWidget.saveDialogSettings(); 284 } 285 } 286 287 288 291 public void validate() 292 { 293 294 Exception rdnE = null; 295 if ( showRDN ) 296 { 297 try 298 { 299 String [] rdnNames = new String [rdnLineList.size()]; 301 String [] rdnValues = new String [rdnLineList.size()]; 302 for ( int i = 0; i < rdnLineList.size(); i++ ) 303 { 304 RdnLine rdnLine = ( RdnLine ) rdnLineList.get( i ); 305 rdnNames[i] = rdnLine.rdnNameCombo.getText(); 306 rdnValues[i] = rdnLine.rdnValueText.getText(); 307 308 if ( rdnLineList.size() > 1 ) 309 { 310 rdnLine.rdnDeleteButton.setEnabled( true ); 311 } 312 else 313 { 314 rdnLine.rdnDeleteButton.setEnabled( false ); 315 } 316 } 317 rdn = new RDN( rdnNames, rdnValues, false ); 318 } 319 catch ( Exception e ) 320 { 321 rdnE = e; 322 rdn = null; 323 } 324 } 325 326 Exception parentE = null; 327 if ( showParent ) 328 { 329 try 330 { 331 parentDn = new DN( parentEntryWidget.getDn() ); 333 } 334 catch ( Exception e ) 335 { 336 parentE = e; 337 parentDn = null; 338 } 339 } 340 341 String s = ""; 342 if ( rdnE != null ) 343 { 344 s += rdnE.getMessage() != null ? rdnE.getMessage() : "Error in RDN "; 345 } 346 if ( parentE != null ) 347 { 348 s += ", " + parentE.getMessage() != null ? parentE.getMessage() : "Error in Parent DN "; 349 } 350 351 if ( previewText != null ) 352 { 353 if ( s.length() > 0 ) 354 { 355 previewText.setText( s ); 356 } 357 else 358 { 359 DN dn; 360 if ( showParent && showRDN ) 361 { 362 dn = new DN( rdn, parentDn ); 363 } 364 else if ( showParent ) 365 { 366 dn = new DN( parentDn ); 367 } 368 else if ( showRDN ) 369 { 370 dn = new DN( rdn ); 371 } 372 else 373 { 374 dn = new DN(); 375 } 376 previewText.setText( dn.toString() ); 377 } 378 } 379 380 notifyListeners(); 381 } 382 383 384 390 private void addRdnLine( Composite rdnComposite, int index ) 391 { 392 RdnLine[] rdnLines = ( RdnLine[] ) rdnLineList.toArray( new RdnLine[rdnLineList.size()] ); 393 394 if ( rdnLines.length > 0 ) 395 { 396 for ( int i = 0; i < rdnLines.length; i++ ) 397 { 398 RdnLine oldRdnLine = rdnLines[i]; 399 400 String oldName = oldRdnLine.rdnNameCombo.getText(); 402 String oldValue = oldRdnLine.rdnValueText.getText(); 403 404 oldRdnLine.rdnNameComboField.getLayoutControl().dispose(); 406 oldRdnLine.rdnEqualsLabel.dispose(); 407 oldRdnLine.rdnValueText.dispose(); 408 oldRdnLine.rdnAddButton.dispose(); 409 oldRdnLine.rdnDeleteButton.dispose(); 410 rdnLineList.remove( oldRdnLine ); 411 412 RdnLine newRdnLine = createRdnLine( rdnComposite ); 414 rdnLineList.add( newRdnLine ); 415 416 newRdnLine.rdnNameCombo.setText( oldName ); 418 newRdnLine.rdnValueText.setText( oldValue ); 419 420 if ( index == i + 1 ) 422 { 423 RdnLine rdnLine = createRdnLine( rdnComposite ); 424 rdnLineList.add( rdnLine ); 425 } 426 } 427 } 428 else 429 { 430 RdnLine rdnLine = createRdnLine( rdnComposite ); 431 rdnLineList.add( rdnLine ); 432 } 433 434 shell.layout( true, true ); 435 } 436 437 438 445 private RdnLine createRdnLine( final Composite rdnComposite ) 446 { 447 final RdnLine rdnLine = new RdnLine(); 448 449 final FieldDecoration fieldDecoration = FieldDecorationRegistry.getDefault().getFieldDecoration( 450 FieldDecorationRegistry.DEC_CONTENT_PROPOSAL ); 451 rdnLine.rdnNameComboField = new DecoratedField( rdnComposite, SWT.NONE, new IControlCreator() 452 { 453 public Control createControl( Composite parent, int style ) 454 { 455 Combo combo = new Combo( parent, SWT.DROP_DOWN | SWT.BORDER ); 456 GridData gd = new GridData(); 457 gd.widthHint = 180; 458 combo.setLayoutData( gd ); 459 combo.setVisibleItemCount( 20 ); 460 return combo; 461 } 462 } ); 463 rdnLine.rdnNameComboField.addFieldDecoration( fieldDecoration, SWT.TOP | SWT.LEFT, true ); 464 GridData gd = new GridData(); 465 gd.widthHint = 180; 466 rdnLine.rdnNameComboField.getLayoutControl().setLayoutData( gd ); 467 rdnLine.rdnNameCombo = ( Combo ) rdnLine.rdnNameComboField.getControl(); 468 469 rdnLine.rdnNameCPA = new ContentProposalAdapter( rdnLine.rdnNameCombo, new ComboContentAdapter(), 470 new ListContentProposalProvider( attributeNames ), null, null ); 471 rdnLine.rdnNameCPA.setFilterStyle( ContentProposalAdapter.FILTER_NONE ); 472 rdnLine.rdnNameCPA.setProposalAcceptanceStyle( ContentProposalAdapter.PROPOSAL_REPLACE ); 473 474 rdnLine.rdnEqualsLabel = new Label( rdnComposite, SWT.NONE ); 475 rdnLine.rdnEqualsLabel.setText( "=" ); 476 477 rdnLine.rdnValueText = new Text( rdnComposite, SWT.BORDER ); 478 gd = new GridData( GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL ); 479 rdnLine.rdnValueText.setLayoutData( gd ); 480 481 rdnLine.rdnAddButton = new Button( rdnComposite, SWT.PUSH ); 482 rdnLine.rdnAddButton.setText( " + " ); 483 rdnLine.rdnAddButton.addSelectionListener( new SelectionAdapter() 484 { 485 public void widgetSelected( SelectionEvent e ) 486 { 487 int index = rdnLineList.size(); 488 for ( int i = 0; i < rdnLineList.size(); i++ ) 489 { 490 RdnLine rdnLine = ( RdnLine ) rdnLineList.get( i ); 491 if ( rdnLine.rdnAddButton == e.widget ) 492 { 493 index = i + 1; 494 } 495 } 496 addRdnLine( rdnComposite, index ); 497 498 validate(); 499 } 500 } ); 501 502 rdnLine.rdnDeleteButton = new Button( rdnComposite, SWT.PUSH ); 503 rdnLine.rdnDeleteButton.setText( " \u2212 " ); rdnLine.rdnDeleteButton.addSelectionListener( new SelectionAdapter() 505 { 506 public void widgetSelected( SelectionEvent e ) 507 { 508 int index = 0; 509 for ( int i = 0; i < rdnLineList.size(); i++ ) 510 { 511 RdnLine rdnLine = ( RdnLine ) rdnLineList.get( i ); 512 if ( rdnLine.rdnDeleteButton == e.widget ) 513 { 514 index = i; 515 } 516 } 517 deleteRdnLine( rdnComposite, index ); 518 519 validate(); 520 } 521 } ); 522 523 if ( attributeNames != null ) 524 { 525 rdnLine.rdnNameCombo.setItems( attributeNames ); 526 } 527 528 rdnLine.rdnNameCombo.addModifyListener( this ); 529 rdnLine.rdnValueText.addModifyListener( this ); 530 531 return rdnLine; 532 } 533 534 535 541 private void deleteRdnLine( Composite rdnComposite, int index ) 542 { 543 RdnLine rdnLine = ( RdnLine ) rdnLineList.remove( index ); 544 if ( rdnLine != null ) 545 { 546 rdnLine.rdnNameComboField.getLayoutControl().dispose(); 547 rdnLine.rdnEqualsLabel.dispose(); 548 rdnLine.rdnValueText.dispose(); 549 rdnLine.rdnAddButton.dispose(); 550 rdnLine.rdnDeleteButton.dispose(); 551 552 if ( !rdnComposite.isDisposed() ) 553 { 554 shell.layout( true, true ); 555 } 556 } 557 } 558 559 570 private class RdnLine 571 { 572 573 574 private DecoratedField rdnNameComboField; 575 576 577 private Combo rdnNameCombo; 578 579 580 private ContentProposalAdapter rdnNameCPA; 581 582 583 private Text rdnValueText; 584 585 586 private Label rdnEqualsLabel; 587 588 589 private Button rdnAddButton; 590 591 592 private Button rdnDeleteButton; 593 } 594 595 } 596 | Popular Tags |