KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > BrowserCommonReferralHandler


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;
22
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.directory.ldapstudio.browser.common.dialogs.SelectReferralConnectionDialog;
28 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin;
29 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
30 import org.apache.directory.ldapstudio.browser.core.model.IReferralHandler;
31 import org.apache.directory.ldapstudio.browser.core.model.URL;
32
33 import org.eclipse.ui.PlatformUI;
34
35
36 public class BrowserCommonReferralHandler implements IReferralHandler
37 {
38
39     private Map JavaDoc referralUrlToReferralConnectionCache = new HashMap JavaDoc();
40
41
42     public IConnection getReferralConnection( final URL referralUrl )
43     {
44
45         // check cache
46
if ( referralUrlToReferralConnectionCache.containsKey( referralUrl ) )
47         {
48             IConnection referralConnection = ( IConnection ) referralUrlToReferralConnectionCache.get( referralUrl );
49             if ( referralConnection != null )
50             {
51                 IConnection[] connections = BrowserCorePlugin.getDefault().getConnectionManager().getConnections();
52                 for ( int i = 0; i < connections.length; i++ )
53                 {
54                     IConnection connection = connections[i];
55                     if ( referralConnection == connection )
56                     {
57                         return referralConnection;
58                     }
59                 }
60             }
61         }
62
63         referralUrlToReferralConnectionCache.remove( referralUrl );
64
65         // open dialog
66
final IConnection[] referralConnection = new IConnection[1];
67         PlatformUI.getWorkbench().getDisplay().syncExec( new Runnable JavaDoc()
68         {
69             public void run()
70             {
71                 SelectReferralConnectionDialog dialog = new SelectReferralConnectionDialog( PlatformUI.getWorkbench()
72                     .getDisplay().getActiveShell(), referralUrl );
73                 if ( dialog.open() == SelectReferralConnectionDialog.OK )
74                 {
75                     IConnection connection = dialog.getReferralConnection();
76                     referralUrlToReferralConnectionCache.put( referralUrl, connection );
77                     referralConnection[0] = connection;
78                 }
79             }
80         } );
81
82         return referralConnection[0];
83     }
84
85 }
86
Popular Tags