1 20 21 package org.apache.directory.ldapstudio.browser.common.wizards; 22 23 24 import java.util.ArrayList ; 25 import java.util.Comparator ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Locale ; 30 import java.util.Map ; 31 import java.util.SortedSet ; 32 import java.util.TreeSet ; 33 34 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils; 35 import org.eclipse.jface.wizard.WizardPage; 36 import org.eclipse.swt.SWT; 37 import org.eclipse.swt.events.ModifyEvent; 38 import org.eclipse.swt.events.ModifyListener; 39 import org.eclipse.swt.events.SelectionAdapter; 40 import org.eclipse.swt.events.SelectionEvent; 41 import org.eclipse.swt.layout.GridData; 42 import org.eclipse.swt.layout.GridLayout; 43 import org.eclipse.swt.widgets.Button; 44 import org.eclipse.swt.widgets.Combo; 45 import org.eclipse.swt.widgets.Composite; 46 import org.eclipse.swt.widgets.Group; 47 import org.eclipse.swt.widgets.Label; 48 import org.eclipse.swt.widgets.Shell; 49 import org.eclipse.swt.widgets.Text; 50 51 52 59 public class AttributeOptionsWizardPage extends WizardPage 60 { 61 62 63 private AttributeWizard wizard; 64 65 66 private Shell shell; 67 68 69 private String [] possibleLanguages; 70 71 72 private Map <String , String []> possibleLangToCountriesMap; 73 74 75 private List <String > parsedLangList; 76 77 78 private List <String > parsedOptionList; 79 80 81 private boolean parsedBinary; 82 83 84 private Group langGroup; 85 86 87 private ArrayList <LangLine> langLineList; 88 89 90 private Group optionsGroup; 91 92 93 private ArrayList <OptionLine> optionLineList; 94 95 96 private Button binaryOptionButton; 97 98 99 private Text previewText; 100 101 102 109 public AttributeOptionsWizardPage( String pageName, String initialAttributeDescription, AttributeWizard wizard ) 110 { 111 super( pageName ); 112 super.setTitle( "Options" ); 113 super.setDescription( "Optionally you may specify options (e.g. language tags)." ); 114 super.setPageComplete( false ); 116 117 this.wizard = wizard; 118 119 SortedSet <String > languageSet = new TreeSet <String >(); 121 Map <String , SortedSet <String >> languageToCountrySetMap = new HashMap <String , SortedSet <String >>(); 122 Locale [] locales = Locale.getAvailableLocales(); 123 for ( int i = 0; i < locales.length; i++ ) 124 { 125 Locale locale = locales[i]; 126 languageSet.add( locale.getLanguage() ); 127 if ( !languageToCountrySetMap.containsKey( locale.getLanguage() ) ) 128 { 129 languageToCountrySetMap.put( locale.getLanguage(), new TreeSet <String >() ); 130 } 131 SortedSet <String > countrySet = languageToCountrySetMap.get( locale.getLanguage() ); 132 countrySet.add( locale.getCountry() ); 133 } 134 possibleLanguages = languageSet.toArray( new String [languageSet.size()] ); 135 possibleLangToCountriesMap = new HashMap <String , String []>(); 136 for ( Iterator <String > it = languageToCountrySetMap.keySet().iterator(); it.hasNext(); ) 137 { 138 String language = it.next(); 139 SortedSet <String > countrySet = languageToCountrySetMap.get( language ); 140 String [] countries = countrySet.toArray( new String [countrySet.size()] ); 141 possibleLangToCountriesMap.put( language, countries ); 142 } 143 144 if ( initialAttributeDescription == null ) 146 { 147 initialAttributeDescription = ""; 148 } 149 String [] attributeDescriptionComponents = initialAttributeDescription.split( ";" ); 150 parsedLangList = new ArrayList <String >(); 151 parsedOptionList = new ArrayList <String >(); 152 parsedBinary = false; 153 for ( int i = 1; i < attributeDescriptionComponents.length; i++ ) 154 { 155 if ( attributeDescriptionComponents[i].startsWith( "lang-" ) ) 156 { 157 parsedLangList.add( attributeDescriptionComponents[i] ); 158 } 159 else if ( attributeDescriptionComponents[i].equals( "binary" ) ) 160 { 161 parsedBinary = true; 162 } 163 else 164 { 165 parsedOptionList.add( attributeDescriptionComponents[i] ); 166 } 167 } 168 } 169 170 171 174 private void validate() 175 { 176 previewText.setText( wizard.getAttributeDescription() ); 177 setPageComplete( true ); 178 } 179 180 181 184 public void setVisible( boolean visible ) 185 { 186 super.setVisible( visible ); 187 if ( visible ) 188 { 189 validate(); 190 } 191 } 192 193 194 197 public void createControl( Composite parent ) 198 { 199 shell = parent.getShell(); 200 201 Composite composite = new Composite( parent, SWT.NONE ); 202 GridLayout gl = new GridLayout( 2, false ); 203 composite.setLayout( gl ); 204 composite.setLayoutData( new GridData( GridData.FILL_BOTH ) ); 205 206 langGroup = BaseWidgetUtils.createGroup( composite, "Language tags", 2 ); 208 GridData gd = new GridData( GridData.FILL_HORIZONTAL ); 209 gd.horizontalSpan = 2; 210 langGroup.setLayoutData( gd ); 211 Composite langComposite = BaseWidgetUtils.createColumnContainer( langGroup, 6, 1 ); 212 langLineList = new ArrayList <LangLine>(); 213 214 BaseWidgetUtils.createSpacer( composite, 2 ); 215 216 optionsGroup = BaseWidgetUtils.createGroup( composite, "Other options", 2 ); 218 gd = new GridData( GridData.FILL_HORIZONTAL ); 219 gd.horizontalSpan = 2; 220 optionsGroup.setLayoutData( gd ); 221 Composite optionsComposite = BaseWidgetUtils.createColumnContainer( optionsGroup, 3, 1 ); 222 optionLineList = new ArrayList <OptionLine>(); 223 Composite binaryComposite = BaseWidgetUtils.createColumnContainer( optionsGroup, 1, 1 ); 224 binaryOptionButton = BaseWidgetUtils.createCheckbox( binaryComposite, "binary option", 1 ); 225 binaryOptionButton.setSelection( parsedBinary ); 226 227 Label la = new Label( composite, SWT.NONE ); 228 gd = new GridData( GridData.GRAB_VERTICAL ); 229 gd.horizontalSpan = 2; 230 la.setLayoutData( gd ); 231 232 BaseWidgetUtils.createLabel( composite, "Preview:", 1 ); 234 previewText = BaseWidgetUtils.createReadonlyText( composite, "", 1 ); 235 236 if ( parsedLangList.isEmpty() ) 238 { 239 addLangLine( langComposite, 0 ); 240 } 241 else 242 { 243 for ( int i = 0; i < parsedLangList.size(); i++ ) 244 { 245 addLangLine( langComposite, i ); 246 String l = parsedLangList.get( i ); 247 String [] ls = l.split( "-", 3 ); 248 if ( ls.length > 1 ) 249 { 250 langLineList.get( i ).languageCombo.setText( ls[1] ); 251 } 252 if ( ls.length > 2 ) 253 { 254 langLineList.get( i ).countryCombo.setText( ls[2] ); 255 } 256 } 257 } 258 259 if ( parsedOptionList.isEmpty() ) 261 { 262 addOptionLine( optionsComposite, 0 ); 263 } 264 else 265 { 266 for ( int i = 0; i < parsedOptionList.size(); i++ ) 267 { 268 addOptionLine( optionsComposite, i ); 269 optionLineList.get( i ).optionText.setText( parsedOptionList.get( i ) ); 270 } 271 } 272 273 binaryOptionButton.addSelectionListener( new SelectionAdapter() 275 { 276 public void widgetSelected( SelectionEvent e ) 277 { 278 validate(); 279 } 280 } ); 281 282 validate(); 283 284 setControl( composite ); 285 } 286 287 288 293 String getAttributeOptions() 294 { 295 296 if ( binaryOptionButton == null || binaryOptionButton.isDisposed() ) 297 { 298 return ""; 299 } 300 301 StringBuffer sb = new StringBuffer (); 303 304 Comparator <String > comparator = new Comparator <String >() 307 { 308 public int compare( String s1, String s2 ) 309 { 310 if ( s1 == null || s2 == null ) 311 { 312 throw new ClassCastException ( "Must not be null" ); 313 } 314 return s1.compareToIgnoreCase( s2 ); 315 } 316 }; 317 SortedSet <String > options = new TreeSet <String >( comparator ); 318 if ( binaryOptionButton.getSelection() ) 319 { 320 options.add( "binary" ); 321 } 322 for ( int i = 0; i < optionLineList.size(); i++ ) 323 { 324 OptionLine optionLine = optionLineList.get( i ); 325 if ( !"".equals( optionLine.optionText.getText() ) ) 326 { 327 options.add( optionLine.optionText.getText() ); 328 } 329 330 if ( optionLineList.size() > 1 ) 331 { 332 optionLine.optionDeleteButton.setEnabled( true ); 333 } 334 else 335 { 336 optionLine.optionDeleteButton.setEnabled( false ); 337 } 338 } 339 for ( int i = 0; i < langLineList.size(); i++ ) 340 { 341 LangLine langLine = langLineList.get( i ); 342 String l = langLine.languageCombo.getText(); 343 String c = langLine.countryCombo.getText(); 344 345 if ( !"".equals( l ) ) 346 { 347 String s = "lang-" + l; 348 if ( !"".equals( c ) ) 349 { 350 s += "-" + c; 351 } 352 options.add( s ); 353 } 354 355 if ( langLineList.size() > 1 ) 356 { 357 langLine.deleteButton.setEnabled( true ); 358 } 359 else 360 { 361 langLine.deleteButton.setEnabled( false ); 362 } 363 } 364 365 for ( Iterator <String > it = options.iterator(); it.hasNext(); ) 367 { 368 String option = it.next(); 369 sb.append( ';' ); 370 sb.append( option ); 371 } 372 373 return sb.toString(); 374 } 375 376 377 383 private void addOptionLine( Composite optionComposite, int index ) 384 { 385 OptionLine[] optionLines = optionLineList.toArray( new OptionLine[optionLineList.size()] ); 386 387 if ( optionLines.length > 0 ) 388 { 389 for ( int i = 0; i < optionLines.length; i++ ) 390 { 391 OptionLine oldOptionLine = optionLines[i]; 392 393 String oldValue = oldOptionLine.optionText.getText(); 395 396 oldOptionLine.optionText.dispose(); 398 oldOptionLine.optionAddButton.dispose(); 399 oldOptionLine.optionDeleteButton.dispose(); 400 optionLineList.remove( oldOptionLine ); 401 402 OptionLine newOptionLine = createOptionLine( optionComposite ); 404 optionLineList.add( newOptionLine ); 405 406 newOptionLine.optionText.setText( oldValue ); 408 409 if ( index == i + 1 ) 411 { 412 OptionLine optionLine = createOptionLine( optionComposite ); 413 optionLineList.add( optionLine ); 414 } 415 } 416 } 417 else 418 { 419 OptionLine optionLine = createOptionLine( optionComposite ); 420 optionLineList.add( optionLine ); 421 } 422 423 shell.layout( true, true ); 424 } 425 426 427 434 private OptionLine createOptionLine( final Composite optionComposite ) 435 { 436 OptionLine optionLine = new OptionLine(); 437 438 optionLine.optionText = new Text( optionComposite, SWT.BORDER ); 439 GridData gd = new GridData( GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL ); 440 optionLine.optionText.setLayoutData( gd ); 441 442 optionLine.optionAddButton = new Button( optionComposite, SWT.PUSH ); 443 optionLine.optionAddButton.setText( " + " ); 444 optionLine.optionAddButton.addSelectionListener( new SelectionAdapter() 445 { 446 public void widgetSelected( SelectionEvent e ) 447 { 448 int index = optionLineList.size(); 449 for ( int i = 0; i < optionLineList.size(); i++ ) 450 { 451 OptionLine optionLine = optionLineList.get( i ); 452 if ( optionLine.optionAddButton == e.widget ) 453 { 454 index = i + 1; 455 } 456 } 457 458 addOptionLine( optionComposite, index ); 459 460 validate(); 461 } 462 } ); 463 464 optionLine.optionDeleteButton = new Button( optionComposite, SWT.PUSH ); 465 optionLine.optionDeleteButton.setText( " \u2212 " ); optionLine.optionDeleteButton.addSelectionListener( new SelectionAdapter() 467 { 468 public void widgetSelected( SelectionEvent e ) 469 { 470 int index = 0; 471 for ( int i = 0; i < optionLineList.size(); i++ ) 472 { 473 OptionLine optionLine = optionLineList.get( i ); 474 if ( optionLine.optionDeleteButton == e.widget ) 475 { 476 index = i; 477 } 478 } 479 480 deleteOptionLine( optionComposite, index ); 481 482 validate(); 483 } 484 } ); 485 486 optionLine.optionText.addModifyListener( new ModifyListener() 487 { 488 public void modifyText( ModifyEvent e ) 489 { 490 validate(); 491 } 492 } ); 493 494 return optionLine; 495 } 496 497 498 504 private void deleteOptionLine( Composite optionComposite, int index ) 505 { 506 OptionLine optionLine = optionLineList.remove( index ); 507 if ( optionLine != null ) 508 { 509 optionLine.optionText.dispose(); 510 optionLine.optionAddButton.dispose(); 511 optionLine.optionDeleteButton.dispose(); 512 513 if ( !optionComposite.isDisposed() ) 514 { 515 shell.layout( true, true ); 516 } 517 } 518 } 519 520 526 public class OptionLine 527 { 528 529 public Text optionText; 530 531 532 public Button optionAddButton; 533 534 535 public Button optionDeleteButton; 536 } 537 538 539 545 private void addLangLine( Composite langComposite, int index ) 546 { 547 LangLine[] langLines = langLineList.toArray( new LangLine[langLineList.size()] ); 548 549 if ( langLines.length > 0 ) 550 { 551 for ( int i = 0; i < langLines.length; i++ ) 552 { 553 LangLine oldLangLine = langLines[i]; 554 555 String oldLanguage = oldLangLine.languageCombo.getText(); 557 String oldCountry = oldLangLine.countryCombo.getText(); 558 559 oldLangLine.langLabel.dispose(); 561 oldLangLine.languageCombo.dispose(); 562 oldLangLine.minusLabel.dispose(); 563 oldLangLine.countryCombo.dispose(); 564 oldLangLine.addButton.dispose(); 565 oldLangLine.deleteButton.dispose(); 566 langLineList.remove( oldLangLine ); 567 568 LangLine newLangLine = createLangLine( langComposite ); 570 langLineList.add( newLangLine ); 571 572 newLangLine.languageCombo.setText( oldLanguage ); 574 newLangLine.countryCombo.setText( oldCountry ); 575 576 if ( index == i + 1 ) 578 { 579 LangLine langLine = createLangLine( langComposite ); 580 langLineList.add( langLine ); 581 } 582 } 583 } 584 else 585 { 586 LangLine langLine = createLangLine( langComposite ); 587 langLineList.add( langLine ); 588 } 589 590 shell.layout( true, true ); 591 } 592 593 594 601 private LangLine createLangLine( final Composite langComposite ) 602 { 603 final LangLine langLine = new LangLine(); 604 605 langLine.langLabel = BaseWidgetUtils.createLabel( langComposite, "lang-", 1 ); 606 607 langLine.languageCombo = BaseWidgetUtils.createCombo( langComposite, possibleLanguages, -1, 1 ); 608 609 langLine.minusLabel = BaseWidgetUtils.createLabel( langComposite, "-", 1 ); 610 611 langLine.countryCombo = BaseWidgetUtils.createCombo( langComposite, new String [0], -1, 1 ); 612 langLine.countryCombo.setEnabled( false ); 613 614 langLine.addButton = new Button( langComposite, SWT.PUSH ); 615 langLine.addButton.setText( " + " ); 616 langLine.addButton.addSelectionListener( new SelectionAdapter() 617 { 618 public void widgetSelected( SelectionEvent e ) 619 { 620 int index = langLineList.size(); 621 for ( int i = 0; i < langLineList.size(); i++ ) 622 { 623 LangLine langLine = langLineList.get( i ); 624 if ( langLine.addButton == e.widget ) 625 { 626 index = i + 1; 627 } 628 } 629 630 addLangLine( langComposite, index ); 631 632 validate(); 633 } 634 } ); 635 636 langLine.deleteButton = new Button( langComposite, SWT.PUSH ); 637 langLine.deleteButton.setText( " \u2212 " ); langLine.deleteButton.addSelectionListener( new SelectionAdapter() 639 { 640 public void widgetSelected( SelectionEvent e ) 641 { 642 int index = 0; 643 for ( int i = 0; i < langLineList.size(); i++ ) 644 { 645 LangLine langLine = langLineList.get( i ); 646 if ( langLine.deleteButton == e.widget ) 647 { 648 index = i; 649 } 650 } 651 652 deleteLangLine( langComposite, index ); 653 654 validate(); 655 } 656 } ); 657 658 langLine.languageCombo.addModifyListener( new ModifyListener() 659 { 660 public void modifyText( ModifyEvent e ) 661 { 662 if ( "".equals( langLine.languageCombo.getText() ) ) 663 { 664 langLine.countryCombo.setEnabled( false ); 665 } 666 else 667 { 668 langLine.countryCombo.setEnabled( true ); 669 String oldValue = langLine.countryCombo.getText(); 670 if ( possibleLangToCountriesMap.containsKey( langLine.languageCombo.getText() ) ) 671 { 672 langLine.countryCombo.setItems( possibleLangToCountriesMap.get( langLine.languageCombo 673 .getText() ) ); 674 } 675 else 676 { 677 langLine.countryCombo.setItems( new String [0] ); 678 } 679 langLine.countryCombo.setText( oldValue ); 680 } 681 validate(); 682 } 683 } ); 684 langLine.countryCombo.addModifyListener( new ModifyListener() 685 { 686 public void modifyText( ModifyEvent e ) 687 { 688 validate(); 689 } 690 } ); 691 692 return langLine; 693 } 694 695 696 702 private void deleteLangLine( Composite langComposite, int index ) 703 { 704 LangLine langLine = langLineList.remove( index ); 705 if ( langLine != null ) 706 { 707 langLine.langLabel.dispose(); 708 langLine.languageCombo.dispose(); 709 langLine.minusLabel.dispose(); 710 langLine.countryCombo.dispose(); 711 langLine.addButton.dispose(); 712 langLine.deleteButton.dispose(); 713 714 if ( !langComposite.isDisposed() ) 715 { 716 shell.layout( true, true ); 717 } 718 } 719 } 720 721 727 public class LangLine 728 { 729 730 731 public Label langLabel; 732 733 734 public Combo languageCombo; 735 736 737 public Label minusLabel; 738 739 740 public Combo countryCombo; 741 742 743 public Button addButton; 744 745 746 public Button deleteButton; 747 } 748 749 } 750 | Popular Tags |