KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > wizards > AttributeOptionsWizardPage


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.wizards;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.SortedSet JavaDoc;
32 import java.util.TreeSet JavaDoc;
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 /**
53  * The AttributeOptionsWizardPageprovides input elements for various options
54  * and a preview field.
55  *
56  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
57  * @version $Rev$, $Date$
58  */

59 public class AttributeOptionsWizardPage extends WizardPage
60 {
61
62     /** The wizard. */
63     private AttributeWizard wizard;
64
65     /** The shell */
66     private Shell shell;
67
68     /** The possible languages. */
69     private String JavaDoc[] possibleLanguages;
70
71     /** The possible language to countries map. */
72     private Map JavaDoc<String JavaDoc, String JavaDoc[]> possibleLangToCountriesMap;
73
74     /** The parsed lang list. */
75     private List JavaDoc<String JavaDoc> parsedLangList;
76
77     /** The parsed option list. */
78     private List JavaDoc<String JavaDoc> parsedOptionList;
79
80     /** The parsed binary option. */
81     private boolean parsedBinary;
82
83     /** The language group. */
84     private Group langGroup;
85
86     /** The lang line list. */
87     private ArrayList JavaDoc<LangLine> langLineList;
88
89     /** The options group. */
90     private Group optionsGroup;
91
92     /** The option line list. */
93     private ArrayList JavaDoc<OptionLine> optionLineList;
94
95     /** The binary option button. */
96     private Button binaryOptionButton;
97
98     /** The preview text. */
99     private Text previewText;
100
101
102     /**
103      * Creates a new instance of AttributeOptionsWizardPage.
104      *
105      * @param pageName the page name
106      * @param initialAttributeDescription the initial attribute description
107      * @param wizard the wizard
108      */

109     public AttributeOptionsWizardPage( String JavaDoc pageName, String JavaDoc 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.setImageDescriptor(BrowserUIPlugin.getDefault().getImageDescriptor(BrowserUIConstants.IMG_ATTRIBUTE_WIZARD));
115
super.setPageComplete( false );
116
117         this.wizard = wizard;
118
119         // init possible languages and countries
120
SortedSet JavaDoc<String JavaDoc> languageSet = new TreeSet JavaDoc<String JavaDoc>();
121         Map JavaDoc<String JavaDoc, SortedSet JavaDoc<String JavaDoc>> languageToCountrySetMap = new HashMap JavaDoc<String JavaDoc, SortedSet JavaDoc<String JavaDoc>>();
122         Locale JavaDoc[] locales = Locale.getAvailableLocales();
123         for ( int i = 0; i < locales.length; i++ )
124         {
125             Locale JavaDoc locale = locales[i];
126             languageSet.add( locale.getLanguage() );
127             if ( !languageToCountrySetMap.containsKey( locale.getLanguage() ) )
128             {
129                 languageToCountrySetMap.put( locale.getLanguage(), new TreeSet JavaDoc<String JavaDoc>() );
130             }
131             SortedSet JavaDoc<String JavaDoc> countrySet = languageToCountrySetMap.get( locale.getLanguage() );
132             countrySet.add( locale.getCountry() );
133         }
134         possibleLanguages = languageSet.toArray( new String JavaDoc[languageSet.size()] );
135         possibleLangToCountriesMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc[]>();
136         for ( Iterator JavaDoc<String JavaDoc> it = languageToCountrySetMap.keySet().iterator(); it.hasNext(); )
137         {
138             String JavaDoc language = it.next();
139             SortedSet JavaDoc<String JavaDoc> countrySet = languageToCountrySetMap.get( language );
140             String JavaDoc[] countries = countrySet.toArray( new String JavaDoc[countrySet.size()] );
141             possibleLangToCountriesMap.put( language, countries );
142         }
143
144         // parse options
145
if ( initialAttributeDescription == null )
146         {
147             initialAttributeDescription = "";
148         }
149         String JavaDoc[] attributeDescriptionComponents = initialAttributeDescription.split( ";" );
150         parsedLangList = new ArrayList JavaDoc<String JavaDoc>();
151         parsedOptionList = new ArrayList JavaDoc<String JavaDoc>();
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     /**
172      * Validates the options.
173      */

174     private void validate()
175     {
176         previewText.setText( wizard.getAttributeDescription() );
177         setPageComplete( true );
178     }
179
180
181     /**
182      * {@inheritDoc}
183      */

184     public void setVisible( boolean visible )
185     {
186         super.setVisible( visible );
187         if ( visible )
188         {
189             validate();
190         }
191     }
192
193
194     /**
195      * {@inheritDoc}
196      */

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         // Lang group
207
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 JavaDoc<LangLine>();
213
214         BaseWidgetUtils.createSpacer( composite, 2 );
215
216         // Options group with binary option
217
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 JavaDoc<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         // Preview text
233
BaseWidgetUtils.createLabel( composite, "Preview:", 1 );
234         previewText = BaseWidgetUtils.createReadonlyText( composite, "", 1 );
235
236         // fill lang
237
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 JavaDoc l = parsedLangList.get( i );
247                 String JavaDoc[] 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         // fill options
260
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         // binary listener
274
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     /**
289      * Gets the attribute options.
290      *
291      * @return the attribute options
292      */

293     String JavaDoc getAttributeOptions()
294     {
295
296         if ( binaryOptionButton == null || binaryOptionButton.isDisposed() )
297         {
298             return "";
299         }
300
301         // attribute type
302
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
303
304         // options
305
// sort and unique options
306
Comparator JavaDoc<String JavaDoc> comparator = new Comparator JavaDoc<String JavaDoc>()
307         {
308             public int compare( String JavaDoc s1, String JavaDoc s2 )
309             {
310                 if ( s1 == null || s2 == null )
311                 {
312                     throw new ClassCastException JavaDoc( "Must not be null" );
313                 }
314                 return s1.compareToIgnoreCase( s2 );
315             }
316         };
317         SortedSet JavaDoc<String JavaDoc> options = new TreeSet JavaDoc<String JavaDoc>( 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 JavaDoc l = langLine.languageCombo.getText();
343             String JavaDoc c = langLine.countryCombo.getText();
344
345             if ( !"".equals( l ) )
346             {
347                 String JavaDoc 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         // append options
366
for ( Iterator JavaDoc<String JavaDoc> it = options.iterator(); it.hasNext(); )
367         {
368             String JavaDoc option = it.next();
369             sb.append( ';' );
370             sb.append( option );
371         }
372
373         return sb.toString();
374     }
375
376
377     /**
378      * Adds an option line at the given index.
379      *
380      * @param optionComposite the option composite
381      * @param index the index
382      */

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                 // remember values
394
String JavaDoc oldValue = oldOptionLine.optionText.getText();
395
396                 // delete old
397
oldOptionLine.optionText.dispose();
398                 oldOptionLine.optionAddButton.dispose();
399                 oldOptionLine.optionDeleteButton.dispose();
400                 optionLineList.remove( oldOptionLine );
401
402                 // add new
403
OptionLine newOptionLine = createOptionLine( optionComposite );
404                 optionLineList.add( newOptionLine );
405
406                 // restore value
407
newOptionLine.optionText.setText( oldValue );
408
409                 // check
410
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     /**
428      * Creates the option line.
429      *
430      * @param optionComposite the option composite
431      *
432      * @return the option line
433      */

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 " ); // \u2013
466
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     /**
499      * Deletes the option line at the given index.
500      *
501      * @param optionComposite the option composite
502      * @param index the index
503      */

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     /**
521      * The class OptionLine is a wrapper for all input elements of an option.
522      *
523      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
524      * @version $Rev$, $Date$
525      */

526     public class OptionLine
527     {
528         /** The option text. */
529         public Text optionText;
530
531         /** The option add button. */
532         public Button optionAddButton;
533
534         /** The option delete button. */
535         public Button optionDeleteButton;
536     }
537
538
539     /**
540      * Adds a language line at the given index.
541      *
542      * @param langComposite the language composite
543      * @param index the index
544      */

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                 // remember values
556
String JavaDoc oldLanguage = oldLangLine.languageCombo.getText();
557                 String JavaDoc oldCountry = oldLangLine.countryCombo.getText();
558
559                 // delete old
560
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                 // add new
569
LangLine newLangLine = createLangLine( langComposite );
570                 langLineList.add( newLangLine );
571
572                 // restore value
573
newLangLine.languageCombo.setText( oldLanguage );
574                 newLangLine.countryCombo.setText( oldCountry );
575
576                 // check
577
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     /**
595      * Creates a language line.
596      *
597      * @param langComposite the language composite
598      *
599      * @return the language line
600      */

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 JavaDoc[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 " ); // \u2013
638
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 JavaDoc 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 JavaDoc[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     /**
697      * Deletes the language line at the given index.
698      *
699      * @param langComposite the language composite
700      * @param index the index
701      */

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     /**
722      * The class LangLine is a wrapper for all input elements of a language tag.
723      *
724      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
725      * @version $Rev$, $Date$
726      */

727     public class LangLine
728     {
729
730         /** The lang label. */
731         public Label langLabel;
732
733         /** The language combo. */
734         public Combo languageCombo;
735
736         /** The minus label. */
737         public Label minusLabel;
738
739         /** The country combo. */
740         public Combo countryCombo;
741
742         /** The add button. */
743         public Button addButton;
744
745         /** The delete button. */
746         public Button deleteButton;
747     }
748
749 }
750
Popular Tags