KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > dialogs > SelectReferralConnectionDialog


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.dialogs;
22
23
24 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
25 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionActionGroup;
26 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionConfiguration;
27 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionUniversalListener;
28 import org.apache.directory.ldapstudio.browser.common.widgets.connection.ConnectionWidget;
29 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
30 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
31 import org.apache.directory.ldapstudio.browser.core.model.URL;
32
33 import org.eclipse.jface.dialogs.Dialog;
34 import org.eclipse.jface.dialogs.IDialogConstants;
35 import org.eclipse.jface.viewers.DoubleClickEvent;
36 import org.eclipse.jface.viewers.IDoubleClickListener;
37 import org.eclipse.jface.viewers.ISelectionChangedListener;
38 import org.eclipse.jface.viewers.IStructuredSelection;
39 import org.eclipse.jface.viewers.SelectionChangedEvent;
40 import org.eclipse.jface.viewers.StructuredSelection;
41 import org.eclipse.swt.SWT;
42 import org.eclipse.swt.layout.GridData;
43 import org.eclipse.swt.layout.GridLayout;
44 import org.eclipse.swt.widgets.Composite;
45 import org.eclipse.swt.widgets.Control;
46 import org.eclipse.swt.widgets.Shell;
47
48
49 public class SelectReferralConnectionDialog extends Dialog
50 {
51
52     private String JavaDoc title;
53
54     private URL referralUrl;
55
56     private IConnection selectedConnection;
57
58     private ConnectionConfiguration configuration;
59
60     private ConnectionUniversalListener universalListener;
61
62     private ConnectionActionGroup actionGroup;
63
64     private ConnectionWidget mainWidget;
65
66
67     public SelectReferralConnectionDialog( Shell parentShell, URL referralUrl )
68     {
69         super( parentShell );
70         super.setShellStyle( super.getShellStyle() | SWT.RESIZE );
71         this.title = "Select Referral Connection";
72         this.referralUrl = referralUrl;
73         this.selectedConnection = null;
74     }
75
76
77     protected void configureShell( Shell shell )
78     {
79         super.configureShell( shell );
80         shell.setText( title );
81     }
82
83
84     public boolean close()
85     {
86         if ( this.mainWidget != null )
87         {
88             this.configuration.dispose();
89             this.configuration = null;
90             this.actionGroup.deactivateGlobalActionHandlers();
91             this.actionGroup.dispose();
92             this.actionGroup = null;
93             this.universalListener.dispose();
94             this.universalListener = null;
95             this.mainWidget.dispose();
96             this.mainWidget = null;
97         }
98         return super.close();
99     }
100
101
102     protected void okPressed()
103     {
104         super.okPressed();
105     }
106
107
108     protected void cancelPressed()
109     {
110         this.selectedConnection = null;
111         super.cancelPressed();
112     }
113
114
115     protected void createButtonsForButtonBar( Composite parent )
116     {
117         createButton( parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, false );
118         createButton( parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false );
119     }
120
121
122     protected Control createDialogArea( Composite parent )
123     {
124
125         Composite composite = ( Composite ) super.createDialogArea( parent );
126         GridLayout gl = new GridLayout();
127         composite.setLayout( gl );
128         GridData gd = new GridData( GridData.FILL_BOTH );
129         gd.widthHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH );
130         gd.heightHint = convertHorizontalDLUsToPixels( IDialogConstants.MINIMUM_MESSAGE_AREA_WIDTH / 2 );
131         composite.setLayoutData( gd );
132
133         BaseWidgetUtils.createWrappedLabeledText( composite, "Please select a connection to handle referral "
134             + referralUrl, 1 );
135
136         // create configuration
137
this.configuration = new ConnectionConfiguration();
138
139         // create main widget
140
this.mainWidget = new ConnectionWidget( this.configuration, null );
141         this.mainWidget.createWidget( composite );
142         this.mainWidget.setInput( BrowserCorePlugin.getDefault().getConnectionManager() );
143
144         // create actions and context menu (and register global actions)
145
this.actionGroup = new ConnectionActionGroup( this.mainWidget, this.configuration );
146         this.actionGroup.fillToolBar( this.mainWidget.getToolBarManager() );
147         this.actionGroup.fillMenu( this.mainWidget.getMenuManager() );
148         this.actionGroup.fillContextMenu( this.mainWidget.getContextMenuManager() );
149         this.actionGroup.activateGlobalActionHandlers();
150
151         // create the listener
152
this.universalListener = new ConnectionUniversalListener( this.mainWidget.getViewer() );
153
154         this.mainWidget.getViewer().addSelectionChangedListener( new ISelectionChangedListener()
155         {
156             public void selectionChanged( SelectionChangedEvent event )
157             {
158                 if ( !event.getSelection().isEmpty() )
159                 {
160                     Object JavaDoc o = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement();
161                     if ( o instanceof IConnection )
162                     {
163                         selectedConnection = ( IConnection ) o;
164                     }
165                 }
166             }
167         } );
168
169         this.mainWidget.getViewer().addDoubleClickListener( new IDoubleClickListener()
170         {
171             public void doubleClick( DoubleClickEvent event )
172             {
173                 if ( !event.getSelection().isEmpty() )
174                 {
175                     Object JavaDoc o = ( ( IStructuredSelection ) event.getSelection() ).getFirstElement();
176                     if ( o instanceof IConnection )
177                     {
178                         selectedConnection = ( IConnection ) o;
179                         okPressed();
180                     }
181                 }
182             }
183         } );
184
185         if ( this.referralUrl != null )
186         {
187             IConnection[] connections = BrowserCorePlugin.getDefault().getConnectionManager().getConnections();
188             for ( int i = 0; i < connections.length; i++ )
189             {
190                 IConnection connection = connections[i];
191                 URL connectionUrl = connection.getUrl();
192                 if ( connectionUrl != null && referralUrl.toString().startsWith( connectionUrl.toString() ) )
193                 {
194                     this.mainWidget.getViewer().reveal( connection );
195                     this.mainWidget.getViewer().setSelection( new StructuredSelection( connection ), true );
196                 }
197             }
198         }
199
200         applyDialogFont( composite );
201
202         this.mainWidget.setFocus();
203
204         return composite;
205
206     }
207
208
209     public IConnection getReferralConnection()
210     {
211         return this.selectedConnection;
212     }
213
214 }
215
Popular Tags