KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > DnBuilderWidget


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.widgets;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
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 /**
54  * The DnBuilderWidget provides input elements to select a parent DN
55  * and to build a (multivalued) RDN.
56  *
57  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
58  * @version $Rev$, $Date$
59  */

60 public class DnBuilderWidget extends BrowserWidget implements ModifyListener
61 {
62
63     /** The attribute names that could be selected from drop-down list. */
64     private String JavaDoc[] attributeNames;
65
66     /** The initial RDN. */
67     private RDN currentRdn;
68
69     /** The initial parent DN. */
70     private DN currentParentDn;
71
72     /** True if the RDN input elements should be shown. */
73     private boolean showRDN;
74
75     /** True if the parent DN input elements should be shown. */
76     private boolean showParent;
77
78     /** The shell. */
79     private Shell shell;
80
81     /** The selected parent DN. */
82     private DN parentDn;
83
84     /** The entry widget to enter/select the parent DN. */
85     private EntryWidget parentEntryWidget;
86
87     /** The composite that contains the RdnLines. */
88     private Composite rdnComposite;
89
90     /** The resulting RDN. */
91     private RDN rdn;
92
93     /** The list of RdnLines. */
94     private ArrayList JavaDoc<RdnLine> rdnLineList;
95
96     /** The preview text. */
97     private Text previewText;
98
99
100     /**
101      * Creates a new instance of DnBuilderWidget.
102      *
103      * @param showParent true if the parent DN input elements should be shown
104      * @param showRDN true if the RDN input elements should be shown
105      */

106     public DnBuilderWidget( boolean showRDN, boolean showParent )
107     {
108         this.showRDN = showRDN;
109         this.showParent = showParent;
110     }
111
112
113     /**
114      * Disposes this widget.
115      */

116     public void dispose()
117     {
118     }
119
120
121     /**
122      * Sets the input.
123      *
124      * @param rdn the initial RDN
125      * @param attributeNames the attribute names that could be selected from drop-down list
126      * @param connection the connection
127      * @param parentDn the initial parent DN
128      */

129     public void setInput( IConnection connection, String JavaDoc[] 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 JavaDoc 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     /**
195      * Gets the RDN.
196      *
197      * @return the RDN
198      */

199     public RDN getRdn()
200     {
201         return rdn;
202     }
203
204
205     /**
206      * Gets the parent DN.
207      *
208      * @return the parent DN
209      */

210     public DN getParentDn()
211     {
212         return parentDn;
213     }
214
215
216     /**
217      * Creates the contents.
218      *
219      * @param parent the parent composite
220      *
221      * @return the created composite
222      */

223     public Composite createContents( Composite parent )
224     {
225         this.shell = parent.getShell();
226
227         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 3, 1 );
228
229         // draw parent
230
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         // draw RDN group
247
if ( showRDN )
248         {
249             BaseWidgetUtils.createLabel( composite, "RDN:", 1 );
250             rdnComposite = BaseWidgetUtils.createColumnContainer( composite, 5, 2 );
251             rdnLineList = new ArrayList JavaDoc<RdnLine>();
252             BaseWidgetUtils.createSpacer( composite, 3 );
253         }
254
255         // draw dn/rdn preview
256
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     /**
268      * {@inheritDoc}
269      */

270     public void modifyText( ModifyEvent e )
271     {
272         validate();
273     }
274
275
276     /**
277      * Saves the dialog settings.
278      */

279     public void saveDialogSettings()
280     {
281         if ( parentEntryWidget != null )
282         {
283             parentEntryWidget.saveDialogSettings();
284         }
285     }
286
287
288     /**
289      * Validates the input elements.
290      */

291     public void validate()
292     {
293
294         Exception JavaDoc rdnE = null;
295         if ( showRDN )
296         {
297             try
298             {
299                 // calculate RDN
300
String JavaDoc[] rdnNames = new String JavaDoc[rdnLineList.size()];
301                 String JavaDoc[] rdnValues = new String JavaDoc[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 JavaDoc e )
320             {
321                 rdnE = e;
322                 rdn = null;
323             }
324         }
325
326         Exception JavaDoc parentE = null;
327         if ( showParent )
328         {
329             try
330             {
331                 // calculate DN
332
parentDn = new DN( parentEntryWidget.getDn() );
333             }
334             catch ( Exception JavaDoc e )
335             {
336                 parentE = e;
337                 parentDn = null;
338             }
339         }
340
341         String JavaDoc 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     /**
385      * Adds an RDN line at the given index.
386      *
387      * @param rdnComposite the RDN composite
388      * @param index the index
389      */

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                 // remember values
401
String JavaDoc oldName = oldRdnLine.rdnNameCombo.getText();
402                 String JavaDoc oldValue = oldRdnLine.rdnValueText.getText();
403
404                 // delete old
405
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                 // add new
413
RdnLine newRdnLine = createRdnLine( rdnComposite );
414                 rdnLineList.add( newRdnLine );
415
416                 // restore value
417
newRdnLine.rdnNameCombo.setText( oldName );
418                 newRdnLine.rdnValueText.setText( oldValue );
419
420                 // check
421
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     /**
439      * Creates and returns an RDN line.
440      *
441      * @param rdnComposite the RDN composite
442      *
443      * @return the created RDN line
444      */

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 " ); // \u2013
504
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     /**
536      * Delete thd RDN line on the given index.
537      *
538      * @param rdnComposite the RDN composite
539      * @param index the index
540      */

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     /**
560      * The Class RdnLine is a wrapper for all input elements
561      * of an RDN line. It contains a combo for the RDN attribute,
562      * an input field for the RDN value and + and - buttons
563      * to add and remove other RDN lines. It looks like this:
564      * <pre>
565      * --------------------------------------------------
566      * | attribute type v | = | attribute value | + | - |
567      * --------------------------------------------------
568      * </pre>
569      */

570     private class RdnLine
571     {
572
573         /** The rdn name combo. */
574         private DecoratedField rdnNameComboField;
575
576         /** The rdn name combo. */
577         private Combo rdnNameCombo;
578
579         /** The content proposal adapter */
580         private ContentProposalAdapter rdnNameCPA;
581
582         /** The rdn value text. */
583         private Text rdnValueText;
584
585         /** The rdn equals label. */
586         private Label rdnEqualsLabel;
587
588         /** The rdn add button. */
589         private Button rdnAddButton;
590
591         /** The rdn delete button. */
592         private Button rdnDeleteButton;
593     }
594
595 }
596
Popular Tags