KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > dialogs > properties > ConnectionPropertyPage


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.ui.dialogs.properties;
22
23
24 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionPageModifyListener;
25 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionPageWrapper;
26 import org.apache.directory.ldapstudio.browser.core.model.DN;
27 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
28 import org.apache.directory.ldapstudio.browser.core.model.NameException;
29 import org.apache.directory.ldapstudio.browser.core.utils.Utils;
30
31 import org.eclipse.core.runtime.IAdaptable;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.layout.GridLayout;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.TabFolder;
37 import org.eclipse.swt.widgets.TabItem;
38 import org.eclipse.ui.dialogs.PropertyPage;
39
40
41 public class ConnectionPropertyPage extends PropertyPage implements ConnectionPageModifyListener
42 {
43
44     private TabFolder tabFolder;
45
46     private TabItem networkTab;
47
48     private TabItem authTab;
49
50     private TabItem optionsTab;
51
52     private ConnectionPageWrapper cpw;
53
54
55     public ConnectionPropertyPage()
56     {
57         super();
58         super.noDefaultAndApplyButton();
59     }
60
61
62     public void connectionPageModified()
63     {
64         validate();
65     }
66
67
68     public void setMessage( String JavaDoc message )
69     {
70         super.setMessage( message, PropertyPage.WARNING );
71         getContainer().updateTitle();
72         getContainer().updateMessage();
73         validate();
74     }
75
76
77     public void setErrorMessage( String JavaDoc errorMessage )
78     {
79         super.setErrorMessage( errorMessage );
80         getContainer().updateTitle();
81         getContainer().updateMessage();
82         validate();
83     }
84
85
86     public IConnection getRealConnection()
87     {
88         return getConnection( getElement() );
89     }
90
91
92     private void validate()
93     {
94         setValid( getMessage() == null && getErrorMessage() == null );
95     }
96
97
98     static IConnection getConnection( Object JavaDoc element )
99     {
100         IConnection connection = null;
101         if ( element instanceof IAdaptable )
102         {
103             connection = ( IConnection ) ( ( IAdaptable ) element ).getAdapter( IConnection.class );
104         }
105         return connection;
106     }
107
108
109     protected Control createContents( Composite parent )
110     {
111
112         IConnection connection = ( IConnection ) getConnection( getElement() );
113         if ( connection != null )
114         {
115             super.setMessage( "Connection " + Utils.shorten( connection.getName(), 30 ) );
116         }
117
118         this.tabFolder = new TabFolder( parent, SWT.TOP );
119
120         this.cpw = new ConnectionPageWrapper( this, null );
121
122         Composite networkComposite = new Composite( this.tabFolder, SWT.NONE );
123         GridLayout gl = new GridLayout( 1, false );
124         networkComposite.setLayout( gl );
125         cpw.addMainInput( connection.getName(), connection.getHost(), connection.getPort(), connection
126             .getEncryptionMethod(), networkComposite );
127         this.networkTab = new TabItem( this.tabFolder, SWT.NONE );
128         this.networkTab.setText( "Network Parameter" );
129         this.networkTab.setControl( networkComposite );
130
131         Composite authComposite = new Composite( this.tabFolder, SWT.NONE );
132         gl = new GridLayout( 1, false );
133         authComposite.setLayout( gl );
134         cpw.addAuthenticationMethodInput( connection.getAuthMethod(), authComposite );
135         cpw.addSimpleAuthInput( ( connection.getBindPassword() != null ) || ( connection.getBindPrincipal() == null && connection.getBindPassword() == null ),
136             connection.getBindPrincipal() != null ? connection.getBindPrincipal().toString() : "", connection
137                 .getBindPassword() != null ? connection.getBindPassword() : "", authComposite );
138         this.authTab = new TabItem( this.tabFolder, SWT.NONE );
139         this.authTab.setText( "Authentification" );
140         this.authTab.setControl( authComposite );
141
142         Composite optionsComposite = new Composite( this.tabFolder, SWT.NONE );
143         gl = new GridLayout( 1, false );
144         optionsComposite.setLayout( gl );
145         cpw.addBaseDNInput( connection.isFetchBaseDNs(), connection.getBaseDN().toString(), optionsComposite );
146         cpw.addLimitInput( connection.getCountLimit(), connection.getTimeLimit(), connection
147             .getAliasesDereferencingMethod(), connection.getReferralsHandlingMethod(), optionsComposite );
148
149         this.optionsTab = new TabItem( this.tabFolder, SWT.NONE );
150         this.optionsTab.setText( "Options" );
151         this.optionsTab.setControl( optionsComposite );
152
153         return tabFolder;
154     }
155
156
157     public boolean performOk()
158     {
159
160         IConnection connection = ( IConnection ) getConnection( getElement() );
161
162         if ( connection instanceof IConnection )
163         {
164             connection.setName( cpw.getName() );
165             connection.setHost( cpw.getHostName() );
166             connection.setPort( cpw.getPort() );
167             connection.setEncryptionMethod( cpw.getEncyrptionMethod() );
168
169             connection.setAuthMethod( cpw.getAuthenticationMethod() );
170
171             connection.setFetchBaseDNs( cpw.isAutoFetchBaseDns() );
172             try
173             {
174                 connection.setBaseDN( new DN( cpw.getBaseDN() ) );
175             }
176             catch ( NameException e )
177             {
178             }
179             connection.setCountLimit( cpw.getCountLimit() );
180             connection.setTimeLimit( cpw.getTimeLimit() );
181             connection.setAliasesDereferencingMethod( cpw.getAliasesDereferencingMethod() );
182             connection.setReferralsHandlingMethod( cpw.getReferralsHandlingMethod() );
183         }
184
185         if ( connection.getAuthMethod() == IConnection.AUTH_ANONYMOUS )
186         {
187             connection.setBindPrincipal( null );
188             connection.setBindPassword( null );
189         }
190         if ( connection.getAuthMethod() == IConnection.AUTH_SIMPLE )
191         {
192             try
193             {
194                 connection.setBindPrincipal( cpw.getSimpleAuthBindPrincipal() );
195                 connection
196                     .setBindPassword( cpw.isSaveSimpleAuthBindPassword() ? cpw.getSimpleAuthBindPassword() : null );
197             }
198             catch ( Exception JavaDoc e )
199             {
200             }
201         }
202
203         cpw.saveDialogSettings();
204         return true;
205     }
206
207 }
208
Popular Tags