KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > actions > LocateInDitAction


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.actions;
22
23
24 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction;
25 import org.apache.directory.ldapstudio.browser.common.jobs.RunnableContextJobAdapter;
26 import org.apache.directory.ldapstudio.browser.core.jobs.ReadEntryJob;
27 import org.apache.directory.ldapstudio.browser.core.model.DN;
28 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
29 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
30 import org.apache.directory.ldapstudio.browser.ui.views.browser.BrowserView;
31
32 import org.eclipse.ui.IViewPart;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.PartInitException;
35 import org.eclipse.ui.PlatformUI;
36
37
38 /**
39  * This action is used to locate and open an entry by its DN in DIT.
40  *
41  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
42  * @version $Rev$, $Date$
43  */

44 public abstract class LocateInDitAction extends BrowserAction
45 {
46     /**
47      * {@inheritDoc}
48      */

49     public final void run()
50     {
51         ConnectionAndDn connectionAndDn = getConnectionAndDn();
52         if ( connectionAndDn != null )
53         {
54             IConnection connection = connectionAndDn.connection;
55             DN dn = connectionAndDn.dn;
56
57             IEntry entry = connection.getEntryFromCache( dn );
58             if ( entry == null )
59             {
60                 ReadEntryJob job = new ReadEntryJob( connection, dn );
61                 RunnableContextJobAdapter.execute( job );
62                 entry = job.getReadEntry();
63             }
64
65             if ( entry != null )
66             {
67                 String JavaDoc targetId = BrowserView.getId();
68                 IViewPart targetView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().findView(
69                     targetId );
70                 if ( targetView == null )
71                 {
72                     try
73                     {
74                         targetView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(
75                             targetId, null, IWorkbenchPage.VIEW_ACTIVATE );
76                     }
77                     catch ( PartInitException e )
78                     {
79                     }
80                 }
81                 if ( targetView != null && targetView instanceof BrowserView )
82                 {
83                     ( ( BrowserView ) targetView ).select( entry );
84                     PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().activate( targetView );
85                 }
86             }
87         }
88     }
89
90
91     /**
92      * {@inheritDoc}
93      */

94     public String JavaDoc getCommandId()
95     {
96         return "org.apache.directory.ldapstudio.browser.action.locateInDit";
97     }
98
99
100     /**
101      * {@inheritDoc}
102      */

103     public final boolean isEnabled()
104     {
105         return getConnectionAndDn() != null;
106     }
107
108
109     /**
110      * Get the connection and DN to open.
111      *
112      * @return a ConnectionAndDn bean, or null.
113      */

114     protected abstract ConnectionAndDn getConnectionAndDn();
115
116     
117     /**
118      * Inner class to get connection and DN of the entry to locate.
119      *
120      * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
121      * @version $Rev$, $Date$
122      */

123     protected class ConnectionAndDn
124     {
125         /** The connection */
126         private IConnection connection;
127
128         /** The DN */
129         private DN dn;
130
131
132         /**
133          * Creates a new instance of ConnectionAndDn.
134          *
135          * @param connection the connection
136          * @param dn the DN
137          */

138         protected ConnectionAndDn( IConnection connection, DN dn )
139         {
140             this.connection = connection;
141             this.dn = dn;
142         }
143     }
144 }
145
Popular Tags