KickJava   Java API By Example, From Geeks To Geeks.

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


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.connection;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
29 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
30 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
31 import org.apache.directory.ldapstudio.browser.common.widgets.HistoryUtils;
32 import org.apache.directory.ldapstudio.browser.common.widgets.search.AliasesDereferencingWidget;
33 import org.apache.directory.ldapstudio.browser.common.widgets.search.LimitWidget;
34 import org.apache.directory.ldapstudio.browser.common.widgets.search.ReferralsHandlingWidget;
35 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
36 import org.apache.directory.ldapstudio.browser.core.internal.model.Connection;
37 import org.apache.directory.ldapstudio.browser.core.jobs.CheckBindJob;
38 import org.apache.directory.ldapstudio.browser.core.jobs.CheckNetworkParameterJob;
39 import org.apache.directory.ldapstudio.browser.core.jobs.FetchBaseDNsJob;
40 import org.apache.directory.ldapstudio.browser.core.model.DN;
41 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
42 import org.apache.directory.ldapstudio.browser.core.model.NameException;
43 import org.eclipse.jface.dialogs.MessageDialog;
44 import org.eclipse.jface.operation.IRunnableContext;
45 import org.eclipse.swt.SWT;
46 import org.eclipse.swt.events.ModifyEvent;
47 import org.eclipse.swt.events.ModifyListener;
48 import org.eclipse.swt.events.SelectionEvent;
49 import org.eclipse.swt.events.SelectionListener;
50 import org.eclipse.swt.events.VerifyEvent;
51 import org.eclipse.swt.events.VerifyListener;
52 import org.eclipse.swt.layout.GridData;
53 import org.eclipse.swt.widgets.Button;
54 import org.eclipse.swt.widgets.Combo;
55 import org.eclipse.swt.widgets.Composite;
56 import org.eclipse.swt.widgets.Display;
57 import org.eclipse.swt.widgets.Group;
58 import org.eclipse.swt.widgets.Text;
59
60
61 /**
62  * The ConnectionPageWrapper is a wrapper for all UI widgets needed for the
63  * connection configuration. It is used by the new connection wizard as well
64  * as the connection property page. So all widgets and functionality is
65  * implemented only once in this wrapper.
66  *
67  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
68  * @version $Rev$, $Date$
69  */

70 public class ConnectionPageWrapper implements ModifyListener, SelectionListener
71 {
72
73     /** The connection name text widget */
74     private Text nameText;
75
76     /** The host name combo with the history of recently used host names */
77     private Combo hostCombo;
78
79     /** The host name text widget */
80     private Text hostText;
81
82     /** The host combo with the history of recently used ports */
83     private Combo portCombo;
84
85     /** The port text widget */
86     private Text portText;
87
88     /** The combo to select the encryption method */
89     private Combo encryptionMethodCombo;
90
91     /** The button to check the connection parameters */
92     private Button checkConnectionButton;
93
94     /** The checkbox to fetch the base DN's from namingContexts whenever opening the connection */
95     private Button autoFetchBaseDnsButton;
96
97     /** The button to fetch the base DN's from namingContexts attribute */
98     private Button fetchBaseDnsButton;
99
100     /** The combo that displays the fetched base DN's */
101     private Combo baseDNCombo;
102
103     /** The widget with the count and time limits */
104     private LimitWidget limitWidget;
105
106     /** The widget to select the alias dereferencing method */
107     private AliasesDereferencingWidget aliasesDereferencingWidget;
108
109     /** The widget to select the referrals handling method */
110     private ReferralsHandlingWidget referralsHandlingWidget;
111
112     /** The checkbox to choose wether the connection should be opened when finishing the wizard */
113     private Button openConnectionButton;
114
115     /** The radio to select anonymous authentication */
116     private Button anonymousAuthButton;
117
118     /** The radio to select simple authentication */
119     private Button simpleAuthButton;
120
121     /** The bind user combo with the history of recently used bind users */
122     private Combo simpleAuthBindPrincipalCombo;
123
124     /** The text widget with the bind user */
125     private Text simpleAuthBindPrincipalText;
126
127     /** The text widget to input bind password */
128     private Text simpleAuthBindPasswordText;
129
130     /** The checkbox to choose if the bind password should be saved on disk */
131     private Button saveSimpleAuthBindPasswordButton;
132
133     /** The button to check the authentication parameters */
134     private Button checkSimpleAuthButton;
135
136     /** The list of listerns that are interested in modifications in this page */
137     private List JavaDoc<ConnectionPageModifyListener> listenerList;
138
139     /**
140      * This flag indicats if the connection is opened. It is used to determin wether to render
141      * the combos or just simple text widgets.
142      */

143     private boolean isConnectionOpened;
144
145     /** The runnable contxt that is used for long-running operations such as connection checks */
146     private IRunnableContext runnableContext;
147
148
149     /**
150      * Creates a new instance of ConnectionPageWrapper.
151      *
152      * @param listener the initial modify listener, may be null
153      * @param runnableContext the runnable context
154      */

155     public ConnectionPageWrapper( ConnectionPageModifyListener listener, IRunnableContext runnableContext )
156     {
157         this.listenerList = new ArrayList JavaDoc<ConnectionPageModifyListener>( 5 );
158         if ( listener != null )
159         {
160             this.listenerList.add( listener );
161             this.isConnectionOpened = listener.getRealConnection() != null && listener.getRealConnection().isOpened();
162         }
163         else
164         {
165             this.isConnectionOpened = false;
166         }
167         this.runnableContext = runnableContext;
168     }
169
170
171     /**
172      * Add the give listnere to the list of modify listeners.
173      *
174      * @param listener the modify listener
175      */

176     public void addConnectionPageModifyListener( ConnectionPageModifyListener listener )
177     {
178         listenerList.add( listener );
179     }
180
181
182     /**
183      * Gets the connection name.
184      *
185      * @return the connectio name
186      */

187     public String JavaDoc getName()
188     {
189         return nameText.getText();
190     }
191
192
193     /**
194      * Gets the host name.
195      *
196      * @return the host name
197      */

198     public String JavaDoc getHostName()
199     {
200         return hostCombo != null ? hostCombo.getText() : hostText.getText();
201     }
202
203
204     /**
205      * Gets the port.
206      *
207      * @return the port
208      */

209     public int getPort()
210     {
211         return Integer.parseInt( portCombo != null ? portCombo.getText() : portText.getText() );
212     }
213
214
215     /**
216      * Gets the encyrption method, one of IConnection.ENCYRPTION_NONE,
217      * IConnection.ENCYRPTION_LDAPS or IConnection.ENCYRPTION_STARTTLS.
218      *
219      * @return the encyrption method
220      */

221     public int getEncyrptionMethod()
222     {
223         if ( encryptionMethodCombo != null )
224         {
225             switch ( encryptionMethodCombo.getSelectionIndex() )
226             {
227                 case 1:
228                     return IConnection.ENCYRPTION_LDAPS;
229                 case 2:
230                     return IConnection.ENCYRPTION_STARTTLS;
231                 default:
232                     return IConnection.ENCYRPTION_NONE;
233             }
234         }
235         return IConnection.ENCYRPTION_NONE;
236     }
237
238
239     /**
240      * Returns true if base DN's should be fetched
241      * whenever opening the connection.
242      *
243      * @return true, if base DN's should be fetched
244      */

245     public boolean isAutoFetchBaseDns()
246     {
247         return autoFetchBaseDnsButton.getSelection();
248     }
249
250
251     /**
252      * Gets the base DN.
253      *
254      * @return the base DN
255      */

256     public String JavaDoc getBaseDN()
257     {
258         return baseDNCombo.getText();
259     }
260
261
262     /**
263      * Gets the count limit.
264      *
265      * @return the count limit
266      */

267     public int getCountLimit()
268     {
269         return limitWidget.getCountLimit();
270     }
271
272
273     /**
274      * Gets the time limit.
275      *
276      * @return the time limit
277      */

278     public int getTimeLimit()
279     {
280         return limitWidget.getTimeLimit();
281     }
282
283
284     /**
285      * Gets the aliases dereferencing method.
286      *
287      * @return the aliases dereferencing method
288      */

289     public int getAliasesDereferencingMethod()
290     {
291         return aliasesDereferencingWidget.getAliasesDereferencingMethod();
292     }
293
294
295     /**
296      * Gets the referrals handling method.
297      *
298      * @return the referrals handling method
299      */

300     public int getReferralsHandlingMethod()
301     {
302         return referralsHandlingWidget.getReferralsHandlingMethod();
303     }
304
305
306     /**
307      * Sets the open connection on finish flag.
308      *
309      * @param b the open connection on finish flag
310      */

311     public void setOpenConnectionOnFinish( boolean b )
312     {
313         if ( openConnectionButton != null )
314         {
315             openConnectionButton.setSelection( b );
316         }
317     }
318
319
320     /**
321      * Returns true if the connection should be opened
322      * when finishing the wizard.
323      *
324      * @return true, if the connection should be opened
325      */

326     public boolean isOpenConnectionOnFinish()
327     {
328         return openConnectionButton.getSelection();
329     }
330
331
332     /**
333      * Gets the authentication method, one of IConnection.AUTH_ANONYMOUS
334      * or IConnection.AUTH_SIMPLE.
335      *
336      * @return the authentication method
337      */

338     public int getAuthenticationMethod()
339     {
340         if ( anonymousAuthButton.getSelection() )
341         {
342             return IConnection.AUTH_ANONYMOUS;
343         }
344         else if ( simpleAuthButton.getSelection() )
345         {
346             return IConnection.AUTH_SIMPLE;
347         }
348
349         return IConnection.AUTH_ANONYMOUS;
350     }
351
352
353     /**
354      * Gets the simple auth bind principal.
355      *
356      * @return the simple auth bind principal
357      */

358     public String JavaDoc getSimpleAuthBindPrincipal()
359     {
360         return simpleAuthBindPrincipalCombo != null ? simpleAuthBindPrincipalCombo.getText()
361             : simpleAuthBindPrincipalText.getText();
362     }
363
364
365     /**
366      * Gets the simple auth bind password.
367      *
368      * @return the simple auth bind password
369      */

370     public String JavaDoc getSimpleAuthBindPassword()
371     {
372         return simpleAuthBindPasswordText.getText();
373     }
374
375
376     /**
377      * Returns true if the bind password should be saved on disk.
378      *
379      * @return true, if the bind password should be saved on disk
380      */

381     public boolean isSaveSimpleAuthBindPassword()
382     {
383         return saveSimpleAuthBindPasswordButton.getSelection();
384     }
385
386
387     /**
388      * Adds the main input widgets. In includes widgets for the connection name,
389      * host, port and encrypition method
390      *
391      * @param name the initial name
392      * @param host the initial host
393      * @param port the initial port
394      * @param encryptionMethod the initial encryption method
395      * @param parent the parent
396      */

397     public void addMainInput( String JavaDoc name, String JavaDoc host, int port, int encryptionMethod, Composite parent )
398     {
399
400         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 1, 1 );
401
402         Composite nameComposite = BaseWidgetUtils.createColumnContainer( composite, 2, 1 );
403         BaseWidgetUtils.createLabel( nameComposite, "Connection name:", 1 );
404         nameText = BaseWidgetUtils.createText( nameComposite, name, 1 );
405         nameText.addModifyListener( this );
406
407         BaseWidgetUtils.createSpacer( composite, 1 );
408
409         Group group = BaseWidgetUtils.createGroup( composite, "Network Parameter", 1 );
410
411         Composite groupComposite = BaseWidgetUtils.createColumnContainer( group, 3, 1 );
412         BaseWidgetUtils.createLabel( groupComposite, "Hostname:", 1 );
413         if ( isConnectionOpened )
414         {
415             hostText = BaseWidgetUtils.createReadonlyText( groupComposite, host, 2 );
416         }
417         else
418         {
419             String JavaDoc[] hostHistory = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_HOST_HISTORY );
420             hostCombo = BaseWidgetUtils.createCombo( groupComposite, hostHistory, -1, 2 );
421             hostCombo.setText( host );
422             hostCombo.addModifyListener( this );
423         }
424
425         BaseWidgetUtils.createLabel( groupComposite, "Port:", 1 );
426         if ( isConnectionOpened )
427         {
428             portText = BaseWidgetUtils.createReadonlyText( groupComposite, Integer.toString( port ), 2 );
429         }
430         else
431         {
432             String JavaDoc[] portHistory = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_PORT_HISTORY );
433             portCombo = BaseWidgetUtils.createCombo( groupComposite, portHistory, -1, 2 );
434             portCombo.setText( Integer.toString( port ) );
435             portCombo.addVerifyListener( new VerifyListener()
436             {
437                 public void verifyText( VerifyEvent e )
438                 {
439                     if ( !e.text.matches( "[0-9]*" ) )
440                     {
441                         e.doit = false;
442                     }
443                     if ( portCombo.getText().length() > 4 && e.text.length() > 0 )
444                     {
445                         e.doit = false;
446                     }
447                 }
448             } );
449             portCombo.addModifyListener( this );
450         }
451
452         String JavaDoc[] encMethods = new String JavaDoc[]
453             { "No encryption", "Use SSL encryption (ldaps://)", "Use StartTLS extension" };
454         BaseWidgetUtils.createLabel( groupComposite, "Encryption method:", 1 );
455         encryptionMethodCombo = BaseWidgetUtils.createReadonlyCombo( groupComposite, encMethods, encryptionMethod, 2 );
456         encryptionMethodCombo.addSelectionListener( this );
457         BaseWidgetUtils.createSpacer( groupComposite, 1 );
458         BaseWidgetUtils
459             .createLabel(
460                 groupComposite,
461                 "Warning: The current version doesn't support certificate validation, \nbe aware of invalid certificates or man-in-the-middle attacks!",
462                 2 );
463
464         BaseWidgetUtils.createSpacer( groupComposite, 2 );
465         checkConnectionButton = new Button( groupComposite, SWT.PUSH );
466         GridData gd = new GridData();
467         gd.horizontalAlignment = SWT.RIGHT;
468         gd.verticalAlignment = SWT.BOTTOM;
469         checkConnectionButton.setLayoutData( gd );
470         checkConnectionButton.setText( "Check Network Parameter" );
471         checkConnectionButton.setEnabled( !isConnectionOpened );
472         checkConnectionButton.addSelectionListener( new SelectionListener()
473         {
474             public void widgetSelected( SelectionEvent e )
475             {
476                 IConnection connection = getTestConnection();
477                 CheckNetworkParameterJob job = new CheckNetworkParameterJob( connection );
478                 RunnableContextJobAdapter.execute( job, runnableContext );
479                 if ( job.getExternalResult().isOK() )
480                 {
481                     MessageDialog.openInformation( Display.getDefault().getActiveShell(), "Check Network Parameter",
482                         "The connection was established successfully." );
483                 }
484             }
485
486
487             public void widgetDefaultSelected( SelectionEvent e )
488             {
489             }
490         } );
491
492         setEnabled();
493     }
494
495
496     /**
497      * Adds the base DN input.
498      *
499      * @param autoFetchBaseDNs the initial auto fetch base DN's flag
500      * @param baseDN the initial base DN
501      * @param parent the parent
502      */

503     public void addBaseDNInput( boolean autoFetchBaseDNs, String JavaDoc baseDN, Composite parent )
504     {
505
506         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 1, 1 );
507
508         Group group = BaseWidgetUtils.createGroup( composite, "Base DN", 1 );
509         Composite groupComposite = BaseWidgetUtils.createColumnContainer( group, 3, 1 );
510         GridData gd;
511
512         autoFetchBaseDnsButton = BaseWidgetUtils.createCheckbox( groupComposite, "Get base DNs from Root DSE", 2 );
513         autoFetchBaseDnsButton.setSelection( autoFetchBaseDNs );
514         autoFetchBaseDnsButton.addSelectionListener( this );
515
516         fetchBaseDnsButton = new Button( groupComposite, SWT.PUSH );
517         fetchBaseDnsButton.setText( "Fetch Base DNs" );
518         fetchBaseDnsButton.setEnabled( true );
519         gd = new GridData();
520         gd.horizontalAlignment = SWT.RIGHT;
521         fetchBaseDnsButton.setLayoutData( gd );
522         fetchBaseDnsButton.addSelectionListener( new SelectionListener()
523         {
524
525             public void widgetSelected( SelectionEvent e )
526             {
527                 IConnection connection = getTestConnection();
528
529                 FetchBaseDNsJob job = new FetchBaseDNsJob( connection );
530                 RunnableContextJobAdapter.execute( job, runnableContext );
531                 if ( job.getExternalResult().isOK() )
532                 {
533                     if ( job.getBaseDNs().length > 0 )
534                     {
535                         String JavaDoc[] baseDNs = job.getBaseDNs();
536                         baseDNCombo.setItems( baseDNs );
537                         baseDNCombo.select( 0 );
538
539                         String JavaDoc msg = "The server returned the following base DNs:";
540                         for ( int i = 0; i < baseDNs.length; i++ )
541                         {
542                             msg += "\n - " + baseDNs[i];
543                         }
544                         MessageDialog.openInformation( Display.getDefault().getActiveShell(), "Fetch Base DNs", msg );
545                     }
546                     else
547                     {
548                         MessageDialog.openWarning( Display.getDefault().getActiveShell(), "Fetch Base DNs",
549                             "No base DN returned from server. Please enter the base DN manually." );
550                         autoFetchBaseDnsButton.setSelection( false );
551                     }
552                 }
553             }
554
555
556             public void widgetDefaultSelected( SelectionEvent e )
557             {
558             }
559         } );
560
561         BaseWidgetUtils.createLabel( groupComposite, "Base DN:", 1 );
562         baseDNCombo = BaseWidgetUtils.createCombo( groupComposite, new String JavaDoc[]
563             { baseDN.toString() }, 0, 2 );
564         baseDNCombo.setText( baseDN.toString() );
565         baseDNCombo.addModifyListener( this );
566
567         setEnabled();
568     }
569
570
571     /**
572      * Adds the limit input.
573      *
574      * @param countLimit the initial count limit
575      * @param timeLimit the initial time limit
576      * @param aliasesDereferencingMethod the initial aliases dereferencing method
577      * @param referralsHandlingMethod the initial referrals handling method
578      * @param parent the parent
579      */

580     public void addLimitInput( int countLimit, int timeLimit, int aliasesDereferencingMethod,
581         int referralsHandlingMethod, Composite parent )
582     {
583
584         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 3, 1 );
585
586         limitWidget = new LimitWidget( countLimit, timeLimit );
587         limitWidget.createWidget( composite );
588
589         aliasesDereferencingWidget = new AliasesDereferencingWidget( aliasesDereferencingMethod );
590         aliasesDereferencingWidget.createWidget( composite );
591
592         referralsHandlingWidget = new ReferralsHandlingWidget( referralsHandlingMethod );
593         referralsHandlingWidget.createWidget( composite );
594
595         setEnabled();
596     }
597
598
599     /**
600      * Adds the open connection on finish input.
601      *
602      * @param openConnectionOnFinish the initial value
603      * @param parent the parent
604      */

605     public void addOpenConnectionInput( boolean openConnectionOnFinish, Composite parent )
606     {
607         openConnectionButton = BaseWidgetUtils.createCheckbox( parent, "Open connection on finish", 1 );
608         openConnectionButton.setSelection( openConnectionOnFinish );
609         openConnectionButton.addSelectionListener( this );
610     }
611
612
613     /**
614      * Adds the authentication method input.
615      *
616      * @param authMethod the initial auth method
617      * @param parent the parent
618      */

619     public void addAuthenticationMethodInput( int authMethod, Composite parent )
620     {
621
622         Composite composite = BaseWidgetUtils.createColumnContainer( parent, 1, 1 );
623
624         Group group = BaseWidgetUtils.createGroup( composite, "Authentication Method", 1 );
625         Composite groupComposite = BaseWidgetUtils.createColumnContainer( group, 1, 1 );
626
627         anonymousAuthButton = BaseWidgetUtils.createRadiobutton( groupComposite, "Anonymous Authentication", 1 );
628         anonymousAuthButton.setSelection( authMethod == IConnection.AUTH_ANONYMOUS );
629         anonymousAuthButton.addSelectionListener( this );
630
631         simpleAuthButton = BaseWidgetUtils.createRadiobutton( groupComposite, "Simple Authentication", 1 );
632         simpleAuthButton.setSelection( authMethod == IConnection.AUTH_SIMPLE );
633         simpleAuthButton.addSelectionListener( this );
634
635         // saslAuthButton = new Button(authenticationMethodGroup, SWT.RADIO);
636
// saslAuthButton.setText("SASL Authentication");
637
// saslAuthButton.setSelection(authMethod ==
638
// ConnectionParameter.AUTH_SASL);
639
// saslAuthButton.addSelectionListener(this);
640
}
641
642
643     /**
644      * Adds the simple auth input.
645      *
646      * @param saveBindPassword the initial save bind password flag
647      * @param bindPrincipal the initial bind principal
648      * @param bindPassword the initial bind password
649      * @param parent the parent
650      */

651     public void addSimpleAuthInput( boolean saveBindPassword, String JavaDoc bindPrincipal, String JavaDoc bindPassword,
652         Composite parent )
653     {
654
655         Composite composite2 = BaseWidgetUtils.createColumnContainer( parent, 1, 1 );
656
657         Group group = BaseWidgetUtils.createGroup( composite2, "Authentication Parameter", 1 );
658         Composite composite = BaseWidgetUtils.createColumnContainer( group, 3, 1 );
659
660         BaseWidgetUtils.createLabel( composite, "Bind DN or user:", 1 );
661         if ( isConnectionOpened )
662         {
663             simpleAuthBindPrincipalText = BaseWidgetUtils.createReadonlyText( composite, bindPrincipal, 2 );
664         }
665         else
666         {
667             String JavaDoc[] dnHistory = HistoryUtils.load( BrowserCommonConstants.DIALOGSETTING_KEY_DN_HISTORY );
668             simpleAuthBindPrincipalCombo = BaseWidgetUtils.createCombo( composite, dnHistory, -1, 2 );
669             simpleAuthBindPrincipalCombo.setText( bindPrincipal );
670             simpleAuthBindPrincipalCombo.addModifyListener( this );
671         }
672
673         BaseWidgetUtils.createLabel( composite, "Bind password:", 1 );
674         if ( isConnectionOpened )
675         {
676             simpleAuthBindPasswordText = BaseWidgetUtils.createReadonlyPasswordText( composite, bindPassword, 2 );
677         }
678         else
679         {
680             simpleAuthBindPasswordText = BaseWidgetUtils.createPasswordText( composite, bindPassword, 2 );
681         }
682         simpleAuthBindPasswordText.addModifyListener( this );
683
684         BaseWidgetUtils.createSpacer( composite, 1 );
685         saveSimpleAuthBindPasswordButton = BaseWidgetUtils.createCheckbox( composite, "Save password", 1 );
686         saveSimpleAuthBindPasswordButton.setSelection( saveBindPassword );
687         saveSimpleAuthBindPasswordButton.addSelectionListener( this );
688
689         checkSimpleAuthButton = new Button( composite, SWT.PUSH );
690         GridData gd = new GridData( GridData.FILL_HORIZONTAL );
691         gd.horizontalAlignment = SWT.RIGHT;
692         checkSimpleAuthButton.setLayoutData( gd );
693         checkSimpleAuthButton.setText( "Check Authentication" );
694         checkSimpleAuthButton.setEnabled( false );
695         checkSimpleAuthButton.addSelectionListener( new SelectionListener()
696         {
697             public void widgetSelected( SelectionEvent e )
698             {
699                 IConnection connection = getTestConnection();
700                 CheckBindJob job = new CheckBindJob( connection );
701                 RunnableContextJobAdapter.execute( job, runnableContext );
702                 if ( job.getExternalResult().isOK() )
703                 {
704                     MessageDialog.openInformation( Display.getDefault().getActiveShell(), "Check Authentication",
705                         "The authentication was successful." );
706                 }
707             }
708
709
710             public void widgetDefaultSelected( SelectionEvent e )
711             {
712             }
713         } );
714         setEnabled();
715     }
716
717
718     /**
719      * Fires a connection page modified event when then page was modified.
720      */

721     private void fireConnectionPageModified()
722     {
723         for ( Iterator JavaDoc<ConnectionPageModifyListener> it = listenerList.iterator(); it.hasNext(); )
724         {
725             it.next().connectionPageModified();
726         }
727     }
728
729
730     /**
731      * Sets the enabled/disabled state of all widgets depending on the connection state.
732      */

733     private void setEnabled()
734     {
735
736         if ( isConnectionOpened )
737         {
738             if ( encryptionMethodCombo != null && checkConnectionButton != null )
739             {
740                 encryptionMethodCombo.setEnabled( false );
741                 checkConnectionButton.setEnabled( false );
742             }
743
744             if ( baseDNCombo != null && autoFetchBaseDnsButton != null )
745             {
746                 autoFetchBaseDnsButton.setEnabled( false );
747                 baseDNCombo.setEnabled( false );
748                 fetchBaseDnsButton.setEnabled( false );
749             }
750
751             if ( anonymousAuthButton != null && simpleAuthButton != null )
752             {
753                 anonymousAuthButton.setEnabled( false );
754                 simpleAuthButton.setEnabled( false );
755             }
756             if ( saveSimpleAuthBindPasswordButton != null && saveSimpleAuthBindPasswordButton != null )
757             {
758                 saveSimpleAuthBindPasswordButton.setEnabled( false );
759                 checkSimpleAuthButton.setEnabled( false );
760             }
761         }
762         else
763         {
764             if ( hostCombo != null && portCombo != null && checkConnectionButton != null )
765             {
766                 if ( !hostCombo.getText().equals( "" ) && !hostCombo.getText().equals( "" ) )
767                 {
768                     checkConnectionButton.setEnabled( true );
769                 }
770                 else
771                 {
772                     checkConnectionButton.setEnabled( false );
773                 }
774             }
775
776             if ( baseDNCombo != null && autoFetchBaseDnsButton != null )
777             {
778                 if ( autoFetchBaseDnsButton.getSelection() )
779                 {
780                     baseDNCombo.setEnabled( false );
781                 }
782                 else
783                 {
784                     baseDNCombo.setEnabled( true );
785                 }
786             }
787             if ( simpleAuthBindPrincipalCombo != null && simpleAuthBindPasswordText != null
788                 && saveSimpleAuthBindPasswordButton != null )
789             {
790                 boolean simpleAuthSelected = simpleAuthButton == null || simpleAuthButton.getSelection();
791                 simpleAuthBindPrincipalCombo.setEnabled( simpleAuthSelected );
792                 simpleAuthBindPasswordText.setEnabled( saveSimpleAuthBindPasswordButton.getSelection()
793                     && simpleAuthSelected );
794                 saveSimpleAuthBindPasswordButton.setEnabled( simpleAuthSelected );
795                 checkSimpleAuthButton.setEnabled( saveSimpleAuthBindPasswordButton.getSelection()
796                     && !simpleAuthBindPrincipalCombo.getText().equals( "" )
797                     && !simpleAuthBindPasswordText.getText().equals( "" ) && simpleAuthSelected );
798             }
799         }
800     }
801
802
803     /**
804      * Validates the connection parameters after each modification.
805      */

806     private void validate()
807     {
808         String JavaDoc message = null;
809         String JavaDoc errorMessage = null;
810
811         boolean simpleAuthSelected = simpleAuthButton == null || simpleAuthButton.getSelection();
812
813         if ( baseDNCombo != null && baseDNCombo.isVisible() )
814         {
815             try
816             {
817                 new DN( baseDNCombo.getText() );
818             }
819             catch ( NameException e )
820             {
821                 message = "Please enter a valid base DN.";
822             }
823         }
824         if ( simpleAuthBindPasswordText != null && simpleAuthSelected && simpleAuthBindPasswordText.isVisible() )
825         {
826             if ( saveSimpleAuthBindPasswordButton.getSelection() && "".equals( simpleAuthBindPasswordText.getText() ) )
827             {
828                 message = "Please enter a bind password.";
829             }
830         }
831         if ( simpleAuthBindPrincipalCombo != null && simpleAuthSelected && simpleAuthBindPrincipalCombo.isVisible() )
832         {
833             if ( "".equals( simpleAuthBindPrincipalCombo.getText() ) )
834             {
835                 message = "Please enter a bind DN or user.";
836             }
837             else
838             {
839                 // every bind principal is accepted
840
}
841         }
842         if ( portCombo != null && portCombo.isVisible() )
843         {
844             if ( "".equals( portCombo.getText() ) )
845             {
846                 message = "Please enter a port. The default LDAP port is 389.";
847             }
848         }
849         if ( hostCombo != null && hostCombo.isVisible() )
850         {
851             if ( "".equals( hostCombo.getText() ) )
852             {
853                 message = "Please enter a hostname.";
854             }
855         }
856         if ( nameText != null && nameText.isVisible() )
857         {
858             if ( "".equals( nameText.getText() ) )
859             {
860                 message = "Please enter a connection name.";
861             }
862             if ( BrowserCorePlugin.getDefault().getConnectionManager().getConnection( nameText.getText() ) != null
863                 && BrowserCorePlugin.getDefault().getConnectionManager().getConnection( nameText.getText() ) != listenerList
864                     .get( 0 ).getRealConnection() )
865             {
866                 errorMessage = "A connection named '" + nameText.getText() + "' already exists.";
867             }
868         }
869
870         for ( Iterator JavaDoc<ConnectionPageModifyListener> it = listenerList.iterator(); it.hasNext(); )
871         {
872             ConnectionPageModifyListener listener = it.next();
873             listener.setMessage( message );
874             listener.setErrorMessage( errorMessage );
875         }
876     }
877
878
879     /**
880      * {@inheritDoc}
881      */

882     public void modifyText( ModifyEvent e )
883     {
884         setEnabled();
885         validate();
886         fireConnectionPageModified();
887     }
888
889
890     /**
891      * {@inheritDoc}
892      */

893     public void widgetSelected( SelectionEvent e )
894     {
895         setEnabled();
896         validate();
897         fireConnectionPageModified();
898     }
899
900
901     /**
902      * {@inheritDoc}
903      */

904     public void widgetDefaultSelected( SelectionEvent e )
905     {
906         setEnabled();
907         validate();
908         fireConnectionPageModified();
909     }
910
911
912     /**
913      * Gets a temporary connection with all conection parameter
914      * entered in this page.
915      *
916      * @return a test connection
917      */

918     public IConnection getTestConnection()
919     {
920         if ( getAuthenticationMethod() == IConnection.AUTH_ANONYMOUS )
921         {
922             Connection conn;
923             try
924             {
925                 conn = new Connection( null, getHostName(), getPort(), getEncyrptionMethod(), isAutoFetchBaseDns(),
926                     new DN( getBaseDN() ), getCountLimit(), getTimeLimit(), getAliasesDereferencingMethod(),
927                     getReferralsHandlingMethod(), IConnection.AUTH_ANONYMOUS, null, null );
928             }
929             catch ( NameException e )
930             {
931                 conn = null;
932             }
933             return conn;
934         }
935         else if ( getAuthenticationMethod() == IConnection.AUTH_SIMPLE )
936         {
937             Connection conn;
938             try
939             {
940                 conn = new Connection( null, getHostName(), getPort(), getEncyrptionMethod(), isAutoFetchBaseDns(),
941                     new DN( getBaseDN() ), getCountLimit(), getTimeLimit(), getAliasesDereferencingMethod(),
942                     getReferralsHandlingMethod(), IConnection.AUTH_SIMPLE, getSimpleAuthBindPrincipal(),
943                     getSimpleAuthBindPassword() );
944             }
945             catch ( NameException e )
946             {
947                 conn = null;
948             }
949             return conn;
950         }
951         else
952         {
953             return null;
954         }
955     }
956
957
958     /**
959      * Saved the dialog settings. The curren values of host, port and bind principal are added
960      * to the history.
961      */

962     public void saveDialogSettings()
963     {
964         if ( !isConnectionOpened )
965         {
966             if ( hostCombo != null )
967             {
968                 HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_HOST_HISTORY, hostCombo.getText() );
969             }
970             if ( portCombo != null )
971             {
972                 HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_PORT_HISTORY, portCombo.getText() );
973             }
974             if ( simpleAuthBindPrincipalCombo != null )
975             {
976                 HistoryUtils.save( BrowserCommonConstants.DIALOGSETTING_KEY_DN_HISTORY, simpleAuthBindPrincipalCombo
977                     .getText() );
978             }
979         }
980     }
981
982 }
983
Popular Tags