1 package org.apache.directory.ldapstudio.aciitemeditor.valueeditors; 2 3 import java.text.ParseException ; 4 import java.util.ArrayList ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.Set ; 8 9 import org.apache.directory.ldapstudio.aciitemeditor.Activator; 10 import org.apache.directory.ldapstudio.browser.common.dialogs.TextDialog; 11 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 12 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyEvent; 13 import org.apache.directory.ldapstudio.browser.common.widgets.WidgetModifyListener; 14 import org.apache.directory.ldapstudio.browser.common.widgets.search.EntryWidget; 15 import org.apache.directory.ldapstudio.browser.common.widgets.search.FilterWidget; 16 import org.apache.directory.ldapstudio.browser.core.model.DN; 17 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 18 import org.apache.directory.ldapstudio.browser.core.model.NameException; 19 import org.apache.directory.shared.ldap.name.LdapDN; 20 import org.apache.directory.shared.ldap.subtree.BaseSubtreeSpecification; 21 import org.apache.directory.shared.ldap.subtree.SubtreeSpecification; 22 import org.apache.directory.shared.ldap.subtree.SubtreeSpecificationParser; 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 import org.eclipse.jface.viewers.ArrayContentProvider; 26 import org.eclipse.jface.viewers.DoubleClickEvent; 27 import org.eclipse.jface.viewers.IDoubleClickListener; 28 import org.eclipse.jface.viewers.ISelectionChangedListener; 29 import org.eclipse.jface.viewers.IStructuredSelection; 30 import org.eclipse.jface.viewers.LabelProvider; 31 import org.eclipse.jface.viewers.SelectionChangedEvent; 32 import org.eclipse.jface.viewers.TableViewer; 33 import org.eclipse.swt.SWT; 34 import org.eclipse.swt.events.ModifyEvent; 35 import org.eclipse.swt.events.ModifyListener; 36 import org.eclipse.swt.events.SelectionAdapter; 37 import org.eclipse.swt.events.SelectionEvent; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.layout.GridLayout; 40 import org.eclipse.swt.widgets.Button; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Control; 43 import org.eclipse.swt.widgets.Shell; 44 import org.eclipse.swt.widgets.Spinner; 45 import org.eclipse.swt.widgets.Table; 46 import org.eclipse.swt.widgets.Text; 47 48 54 class SubtreeSpecificationDialog extends Dialog 55 { 56 57 58 private final SubtreeSpecificationParser parser = new SubtreeSpecificationParser( null ); 59 60 61 private IConnection connection; 62 63 64 private DN subentryDN; 65 66 67 private boolean refinementOrFilterVisible; 68 69 70 private SubtreeSpecification subtreeSpecification; 71 72 73 private List <String > exclusions; 74 75 76 private String returnValue; 77 78 private EntryWidget entryWidget; 80 private Spinner minimumSpinner; 81 private Spinner maximumSpinner; 82 private TableViewer exclusionsTableViewer; 83 private Button exclusionsTableAddButton; 84 private Button exclusionsTableEditButton; 85 private Button exclusionsTableDeleteButton; 86 private Button refinementButton; 87 private Text refinementText; 88 private Button filterButton; 89 private FilterWidget filterWidget; 90 91 92 106 SubtreeSpecificationDialog( Shell shell, IConnection connection, DN subentryDN, 107 String initialSubtreeSpecification, boolean refinementOrFilterVisible ) 108 { 109 super( shell ); 110 this.connection = connection; 111 this.subentryDN = subentryDN; 112 this.refinementOrFilterVisible = refinementOrFilterVisible; 113 114 try 116 { 117 subtreeSpecification = parser.parse( initialSubtreeSpecification ); 118 if ( subtreeSpecification == null ) 119 { 120 subtreeSpecification = new BaseSubtreeSpecification(); 121 } 122 } 123 catch ( ParseException pe ) 124 { 125 pe.printStackTrace(); 127 subtreeSpecification = new BaseSubtreeSpecification(); 128 } 129 130 exclusions = new ArrayList <String >(); 131 Set chopBeforeExclusions = subtreeSpecification.getChopBeforeExclusions(); 132 for ( Object chopBeforeExclusion : chopBeforeExclusions ) 133 { 134 LdapDN dn = ( LdapDN ) chopBeforeExclusion; 135 exclusions.add( "chopBefore: \"" + dn.toNormName() + "\"" ); } 137 138 Set chopAfterExclusions = subtreeSpecification.getChopAfterExclusions(); 139 for ( Object chopAfterExclusion : chopAfterExclusions ) 140 { 141 LdapDN dn = ( LdapDN ) chopAfterExclusion; 142 exclusions.add( "chopAfter: \"" + dn.toNormName() + "\"" ); } 144 145 returnValue = null; 146 } 147 148 149 152 protected void configureShell( Shell newShell ) 153 { 154 super.configureShell( newShell ); 155 newShell.setText( Messages.getString( "SubtreeValueEditor.title" ) ); newShell.setImage( Activator.getDefault().getImage( Messages.getString( "SubtreeValueEditor.icon" ) ) ); } 158 159 160 163 protected void okPressed() 164 { 165 StringBuffer sb = new StringBuffer (); 168 subtreeSpecification.printToBuffer( sb ); 169 returnValue = sb.toString(); 170 171 if ( refinementOrFilterVisible ) 173 { 174 filterWidget.saveDialogSettings(); 175 } 176 entryWidget.saveDialogSettings(); 177 178 super.okPressed(); 179 } 180 181 182 185 protected Control createDialogArea( Composite parent ) 186 { 187 Composite outer = ( Composite ) super.createDialogArea( parent ); 188 GridData gd = new GridData( GridData.FILL_BOTH ); 189 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 190 outer.setLayoutData( gd ); 191 192 Composite composite = BaseWidgetUtils.createColumnContainer( outer, 3, 1 ); 193 194 BaseWidgetUtils.createLabel( composite, Messages.getString( "SubtreeValueEditor.label.base" ), 1 ); 196 DN base = null; 197 DN suffix = null; 198 try 199 { 200 base = new DN( subtreeSpecification.getBase().toNormName() ); 201 suffix = subentryDN != null ? subentryDN.getParentDn() : null; 202 } 203 catch ( NameException e ) 204 { 205 } 206 entryWidget = new EntryWidget( connection, base, suffix ); 207 entryWidget.createWidget( composite ); 208 entryWidget.addWidgetModifyListener( new WidgetModifyListener() 209 { 210 public void widgetModified( WidgetModifyEvent event ) 211 { 212 validate(); 213 } 214 } ); 215 216 GridData spinnersGridData = new GridData(); 217 spinnersGridData.grabExcessHorizontalSpace = true; 218 spinnersGridData.verticalAlignment = GridData.CENTER; 219 spinnersGridData.horizontalSpan = 2; 220 spinnersGridData.horizontalAlignment = GridData.BEGINNING; 221 spinnersGridData.widthHint = 3 * 12; 222 223 BaseWidgetUtils.createLabel( composite, Messages.getString( "SubtreeValueEditor.label.minimum" ), 1 ); minimumSpinner = new Spinner( composite, SWT.BORDER ); 225 minimumSpinner.setMinimum( 0 ); 226 minimumSpinner.setMaximum( Integer.MAX_VALUE ); 227 minimumSpinner.setDigits( 0 ); 228 minimumSpinner.setIncrement( 1 ); 229 minimumSpinner.setPageIncrement( 100 ); 230 minimumSpinner.setSelection( subtreeSpecification.getMinBaseDistance() ); 231 minimumSpinner.setLayoutData( spinnersGridData ); 232 minimumSpinner.addModifyListener( new ModifyListener() 233 { 234 public void modifyText( ModifyEvent event ) 235 { 236 validate(); 237 } 238 } ); 239 240 BaseWidgetUtils.createLabel( composite, Messages.getString( "SubtreeValueEditor.label.maximum" ), 1 ); maximumSpinner = new Spinner( composite, SWT.BORDER ); 242 maximumSpinner.setMinimum( 0 ); 243 maximumSpinner.setMaximum( Integer.MAX_VALUE ); 244 maximumSpinner.setDigits( 0 ); 245 maximumSpinner.setIncrement( 1 ); 246 maximumSpinner.setPageIncrement( 100 ); 247 maximumSpinner.setSelection( subtreeSpecification.getMaxBaseDistance() ); 248 maximumSpinner.setLayoutData( spinnersGridData ); 249 maximumSpinner.addModifyListener( new ModifyListener() 250 { 251 public void modifyText( ModifyEvent event ) 252 { 253 validate(); 254 } 255 } ); 256 257 createExclusionsTable( composite ); 258 259 if ( refinementOrFilterVisible ) 260 { 261 BaseWidgetUtils.createSpacer( composite, 3 ); 262 createRefinementOrFilterWidgets( composite ); 263 } 264 265 applyDialogFont( outer ); 266 267 initFromInput(); 268 269 validate(); 270 271 return outer; 272 } 273 274 275 278 private void initFromInput() 279 { 280 281 } 282 283 284 290 private void createExclusionsTable( Composite composite ) 291 { 292 GridData tableGridData = new GridData(); 293 tableGridData.grabExcessHorizontalSpace = true; 294 tableGridData.verticalAlignment = GridData.FILL; 295 tableGridData.horizontalAlignment = GridData.FILL; 296 tableGridData.heightHint = 100; 297 298 BaseWidgetUtils.createLabel( composite, Messages.getString( "SubtreeValueEditor.label.exclusions" ), 1 ); Table exclusionsTable = new Table( composite, SWT.BORDER ); 300 exclusionsTable.setHeaderVisible( false ); 301 exclusionsTable.setLayoutData( tableGridData ); 302 exclusionsTable.setLinesVisible( false ); 303 exclusionsTableViewer = new TableViewer( exclusionsTable ); 304 exclusionsTableViewer.setContentProvider( new ArrayContentProvider() ); 305 exclusionsTableViewer.setLabelProvider( new LabelProvider() ); 306 exclusionsTableViewer.setInput( exclusions ); 307 exclusionsTableViewer.addSelectionChangedListener( new ISelectionChangedListener() 308 { 309 public void selectionChanged( SelectionChangedEvent event ) 310 { 311 valueSelectedExclusionsTable(); 312 } 313 } ); 314 exclusionsTableViewer.addDoubleClickListener( new IDoubleClickListener() 315 { 316 public void doubleClick( DoubleClickEvent event ) 317 { 318 editValueExclusionsTable(); 319 } 320 } ); 321 322 GridLayout gridLayout = new GridLayout(); 323 gridLayout.marginWidth = 0; 324 gridLayout.marginHeight = 0; 325 GridData gridData = new GridData(); 326 gridData.horizontalAlignment = GridData.CENTER; 327 gridData.grabExcessHorizontalSpace = false; 328 gridData.grabExcessVerticalSpace = false; 329 gridData.verticalAlignment = GridData.FILL; 330 331 Composite buttonComposite = new Composite( composite, SWT.NONE ); 332 buttonComposite.setLayoutData( gridData ); 333 buttonComposite.setLayout( gridLayout ); 334 335 GridData buttonGridData = new GridData(); 336 buttonGridData.horizontalAlignment = GridData.FILL; 337 buttonGridData.grabExcessHorizontalSpace = false; 338 buttonGridData.verticalAlignment = GridData.BEGINNING; 339 buttonGridData.widthHint = Activator.getButtonWidth( buttonComposite ); 340 341 exclusionsTableAddButton = new Button( buttonComposite, SWT.PUSH ); 342 exclusionsTableAddButton.setText( Messages.getString( "SubtreeValueEditor.button.add" ) ); exclusionsTableAddButton.setLayoutData( buttonGridData ); 344 exclusionsTableAddButton.addSelectionListener( new SelectionAdapter() 345 { 346 public void widgetSelected( SelectionEvent e ) 347 { 348 addValueExclusionsTable(); 349 } 350 } ); 351 352 exclusionsTableEditButton = new Button( buttonComposite, SWT.PUSH ); 353 exclusionsTableEditButton.setText( Messages.getString( "SubtreeValueEditor.button.edit" ) ); exclusionsTableEditButton.setLayoutData( buttonGridData ); 355 exclusionsTableEditButton.addSelectionListener( new SelectionAdapter() 356 { 357 public void widgetSelected( SelectionEvent e ) 358 { 359 editValueExclusionsTable(); 360 } 361 } ); 362 exclusionsTableEditButton.setEnabled( false ); 363 364 exclusionsTableDeleteButton = new Button( buttonComposite, SWT.PUSH ); 365 exclusionsTableDeleteButton.setText( Messages.getString( "SubtreeValueEditor.button.delete" ) ); exclusionsTableDeleteButton.setLayoutData( buttonGridData ); 367 exclusionsTableDeleteButton.addSelectionListener( new SelectionAdapter() 368 { 369 public void widgetSelected( SelectionEvent e ) 370 { 371 deleteValueExclusionsTable(); 372 } 373 } ); 374 exclusionsTableDeleteButton.setEnabled( false ); 375 } 376 377 378 384 private void createRefinementOrFilterWidgets( Composite parent ) 385 { 386 BaseWidgetUtils.createLabel( parent, Messages 388 .getString( "SubtreeValueEditor.SubtreeValueEditor.label.refinementOrFilter" ), 1 ); 390 Composite composite = BaseWidgetUtils.createColumnContainer( parent, 2, 2 ); 391 392 refinementButton = BaseWidgetUtils.createRadiobutton( composite, Messages 394 .getString( "SubtreeValueEditor.SubtreeValueEditor.label.refinement" ), 2 ); 396 refinementText = new Text( composite, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL ); 398 GridData gd = new GridData( GridData.FILL_BOTH ); 399 gd.horizontalSpan = 2; 400 gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH ); 401 gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH / 6 ); 402 refinementText.setLayoutData( gd ); 403 try 404 { 405 StringBuffer refinementBuffer = new StringBuffer (); 406 if ( subtreeSpecification.getRefinement() != null ) 407 { 408 subtreeSpecification.getRefinement().printRefinementToBuffer( refinementBuffer ); 409 } 410 refinementText.setText( refinementBuffer.toString().trim() ); 411 refinementText.setEnabled( true ); 412 refinementButton.setSelection( true ); 413 } 414 catch ( UnsupportedOperationException e ) 415 { 416 refinementText.setText( "" ); refinementText.setEnabled( false ); 419 refinementButton.setSelection( false ); 420 } 421 422 filterButton = BaseWidgetUtils.createRadiobutton( composite, Messages 424 .getString( "SubtreeValueEditor.SubtreeValueEditor.label.filter" ), 2 ); 426 StringBuffer filterBuffer = new StringBuffer (); 428 if ( subtreeSpecification.getRefinement() != null ) 429 { 430 subtreeSpecification.getRefinement().printToBuffer( filterBuffer ); 431 } 432 filterWidget = new FilterWidget( connection, filterBuffer.toString().trim() ); 433 filterWidget.createWidget( composite ); 434 filterButton.setSelection( !refinementButton.getSelection() ); 435 filterWidget.setEnabled( !refinementButton.getSelection() ); 436 437 refinementButton.addSelectionListener( new SelectionAdapter() 439 { 440 public void widgetSelected( SelectionEvent arg0 ) 441 { 442 refinementText.setEnabled( true ); 443 filterWidget.setEnabled( false ); 445 validate(); 446 } 447 } ); 448 refinementText.addModifyListener( new ModifyListener() 449 { 450 public void modifyText( ModifyEvent event ) 451 { 452 validate(); 453 } 454 } ); 455 filterButton.addSelectionListener( new SelectionAdapter() 456 { 457 public void widgetSelected( SelectionEvent arg0 ) 458 { 459 refinementText.setEnabled( false ); 461 filterWidget.setEnabled( true ); 462 validate(); 463 } 464 } ); 465 filterWidget.addWidgetModifyListener( new WidgetModifyListener() 466 { 467 public void widgetModified( WidgetModifyEvent event ) 468 { 469 validate(); 470 } 471 } ); 472 } 473 474 475 478 private void validate() 479 { 480 boolean valid = true; 481 482 DN base = entryWidget.getDn(); 483 valid &= base != null; 484 485 String ss = buildSubreeSpecification(); 486 487 try 488 { 489 subtreeSpecification = parser.parse( ss ); 490 valid &= true; 491 } 492 catch ( ParseException pe ) 493 { 494 subtreeSpecification = null; 495 valid &= false; 496 } 497 498 if ( getButton( IDialogConstants.OK_ID ) != null ) 499 { 500 getButton( IDialogConstants.OK_ID ).setEnabled( valid ); 501 } 502 503 } 508 509 510 private String buildSubreeSpecification() 511 { 512 StringBuffer sb = new StringBuffer (); 514 sb.append( "{" ); 516 DN base = entryWidget.getDn(); 518 if ( base != null && !SubtreeValueEditor.EMPTY.equals( base.toString() ) ) 519 { 520 sb.append( " base \"" + base.toString() + "\"," ); } 522 523 int minimum = minimumSpinner.getSelection(); 525 if ( minimum != 0 ) 526 { 527 sb.append( " minimum " + minimum + "," ); } 529 530 int maximum = maximumSpinner.getSelection(); 532 if ( maximum != 0 ) 533 { 534 sb.append( " maximum " + maximum + "," ); } 536 537 if ( !exclusions.isEmpty() ) 539 { 540 sb.append( " specificExclusions {" ); 542 for ( Iterator <String > it = exclusions.iterator(); it.hasNext(); ) 543 { 544 sb.append( " " + it.next() ); 546 if ( it.hasNext() ) 547 { 548 sb.append( "," ); } 550 } 551 552 sb.append( " }," ); } 554 555 String refinementOrFilter = ""; if ( refinementOrFilterVisible ) 558 { 559 if ( refinementButton.getSelection() ) 560 { 561 refinementOrFilter = refinementText.getText(); 562 } 563 else 564 { 565 refinementOrFilter = filterWidget.getFilter(); 566 } 567 } 568 else 569 { 570 refinementOrFilter = ""; } 572 if ( refinementOrFilter != null && !SubtreeValueEditor.EMPTY.equals( refinementOrFilter ) ) 573 { 574 sb.append( " specificationFilter " + refinementOrFilter + "," ); } 576 577 if ( sb.charAt( sb.length() - 1 ) == ',' ) 579 { 580 sb.deleteCharAt( sb.length() - 1 ); 581 } 582 583 sb.append( " }" ); 585 return sb.toString(); 586 } 587 588 589 593 private void valueSelectedExclusionsTable() 594 { 595 String value = getSelectedValueExclusionsTable(); 596 597 if ( value == null ) 598 { 599 exclusionsTableEditButton.setEnabled( false ); 600 exclusionsTableDeleteButton.setEnabled( false ); 601 } 602 else 603 { 604 exclusionsTableEditButton.setEnabled( true ); 605 exclusionsTableDeleteButton.setEnabled( true ); 606 } 607 } 608 609 610 616 private String getSelectedValueExclusionsTable() 617 { 618 String value = null; 619 620 IStructuredSelection selection = ( IStructuredSelection ) exclusionsTableViewer.getSelection(); 621 if ( !selection.isEmpty() ) 622 { 623 Object element = selection.getFirstElement(); 624 if ( element instanceof String ) 625 { 626 value = ( String ) element; 627 } 628 } 629 630 return value; 631 } 632 633 634 637 private void addValueExclusionsTable() 638 { 639 DN chopBase = null; 640 try 641 { 642 chopBase = new DN( subtreeSpecification.getBase().toNormName() ); 643 } 644 catch ( NameException e ) 645 { 646 } 647 if ( subentryDN != null && subentryDN.getParentDn() != null ) 648 { 649 DN suffix = subentryDN != null ? subentryDN.getParentDn() : null; 650 chopBase = new DN( chopBase, suffix ); 651 } 652 653 ExclusionDialog dialog = new ExclusionDialog( getShell(), connection, chopBase, "" ); if ( dialog.open() == TextDialog.OK && !SubtreeValueEditor.EMPTY.equals( dialog.getType() ) && !SubtreeValueEditor.EMPTY.equals( dialog.getDN() ) ) 655 { 656 String newValue = dialog.getType() + ": \"" + dialog.getDN() + "\""; exclusions.add( newValue ); 658 exclusionsTableViewer.refresh(); 659 validate(); 660 } 661 } 662 663 664 668 private void editValueExclusionsTable() 669 { 670 String oldValue = getSelectedValueExclusionsTable(); 671 if ( oldValue != null ) 672 { 673 DN chopBase = null; 674 try 675 { 676 chopBase = new DN( subtreeSpecification.getBase().toNormName() ); 677 } 678 catch ( NameException e ) 679 { 680 } 681 if ( subentryDN != null && subentryDN.getParentDn() != null ) 682 { 683 DN suffix = subentryDN != null ? subentryDN.getParentDn() : null; 684 chopBase = new DN( chopBase, suffix ); 685 } 686 687 ExclusionDialog dialog = new ExclusionDialog( getShell(), connection, chopBase, oldValue ); 688 if ( dialog.open() == TextDialog.OK && !SubtreeValueEditor.EMPTY.equals( dialog.getType() ) 689 && !SubtreeValueEditor.EMPTY.equals( dialog.getDN() ) ) 690 { 691 String newValue = dialog.getType() + ": \"" + dialog.getDN() + "\""; exclusions.remove( oldValue ); 693 exclusions.add( newValue ); 694 exclusionsTableViewer.refresh(); 695 validate(); 696 } 697 } 698 } 699 700 701 704 private void deleteValueExclusionsTable() 705 { 706 String value = getSelectedValueExclusionsTable(); 707 if ( value != null ) 708 { 709 exclusions.remove( value ); 710 exclusionsTableViewer.refresh(); 711 validate(); 712 } 713 } 714 715 716 721 public String getSubtreeSpecificationValue() 722 { 723 return returnValue; 724 } 725 } | Popular Tags |