KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > views > connection > LinkWithEditorAction


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 package org.apache.directory.ldapstudio.browser.ui.views.connection;
21
22
23 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
24 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
25 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
26 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
27 import org.apache.directory.ldapstudio.browser.ui.editors.entry.EntryEditor;
28 import org.apache.directory.ldapstudio.browser.ui.editors.entry.EntryEditorInput;
29 import org.apache.directory.ldapstudio.browser.ui.editors.searchresult.SearchResultEditor;
30 import org.apache.directory.ldapstudio.browser.ui.editors.searchresult.SearchResultEditorInput;
31 import org.eclipse.jface.action.Action;
32 import org.eclipse.jface.util.IPropertyChangeListener;
33 import org.eclipse.jface.util.PropertyChangeEvent;
34 import org.eclipse.jface.viewers.IStructuredSelection;
35 import org.eclipse.ui.IEditorInput;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IPartListener2;
38 import org.eclipse.ui.IWorkbenchPart;
39 import org.eclipse.ui.IWorkbenchPartReference;
40
41
42 /**
43  * This class implements the Link With Editor Action for the Connection View.
44  *
45  * This action is not visible to the user, but it listens to to the link
46  * with editor property of the browser view.
47  *
48  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
49  * @version $Rev$, $Date$
50  */

51 public class LinkWithEditorAction extends Action
52 {
53     /** The connection view */
54     private ConnectionView connectionView;
55
56     /** The listener listening on changes on editors */
57     private IPartListener2 editorListener = new IPartListener2()
58     {
59         /**
60          * {@inheritDoc}
61          */

62         public void partBroughtToTop( IWorkbenchPartReference partRef )
63         {
64         }
65
66
67         /**
68          * {@inheritDoc}
69          */

70         public void partActivated( IWorkbenchPartReference partRef )
71         {
72         }
73
74
75         /**
76          * {@inheritDoc}
77          */

78         public void partClosed( IWorkbenchPartReference partRef )
79         {
80         }
81
82
83         /**
84          * {@inheritDoc}
85          */

86         public void partDeactivated( IWorkbenchPartReference partRef )
87         {
88         }
89
90
91         /**
92          * {@inheritDoc}
93          */

94         public void partHidden( IWorkbenchPartReference partRef )
95         {
96         }
97
98
99         /**
100          * {@inheritDoc}
101          */

102         public void partInputChanged( IWorkbenchPartReference partRef )
103         {
104             linkViewWithEditor( partRef.getPart( false ) );
105         }
106
107
108         /**
109          * {@inheritDoc}
110          */

111         public void partOpened( IWorkbenchPartReference partRef )
112         {
113         }
114
115
116         /**
117          * {@inheritDoc}
118          */

119         public void partVisible( IWorkbenchPartReference partRef )
120         {
121         }
122     };
123
124     /** The listener listening on changes of the link with editor action of the browser view */
125     private IPropertyChangeListener propertyChangeListener = new IPropertyChangeListener()
126     {
127
128         /**
129          * {@inheritDoc}
130          */

131         public void propertyChange( PropertyChangeEvent event )
132         {
133             if ( BrowserUIConstants.PREFERENCE_BROWSER_LINK_WITH_EDITOR.equals( event.getProperty() ) )
134             {
135                 run();
136             }
137         }
138     };
139
140
141     /**
142      * Creates a new instance of LinkWithEditorAction.
143      *
144      * @param connectionView
145      * the associated view
146      */

147     public LinkWithEditorAction( ConnectionView connectionView )
148     {
149         super( "Link with editor", AS_CHECK_BOX );
150
151         super.setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor(
152             BrowserUIConstants.IMG_LINK_WITH_EDITOR ) );
153         super.setEnabled( true );
154         this.connectionView = connectionView;
155
156         super.setChecked( BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
157             BrowserUIConstants.PREFERENCE_BROWSER_LINK_WITH_EDITOR ) );
158
159         // enable the listeners
160
BrowserUIPlugin.getDefault().getPreferenceStore().addPropertyChangeListener( propertyChangeListener );
161         if ( isChecked() )
162         {
163             connectionView.getSite().getWorkbenchWindow().getPartService().addPartListener( editorListener );
164         }
165     }
166
167
168     /**
169      * {@inheritDoc}
170      */

171     public void run()
172     {
173         setChecked( BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
174             BrowserUIConstants.PREFERENCE_BROWSER_LINK_WITH_EDITOR ) );
175
176         if ( isChecked() )
177         {
178             // enable the listeners
179
connectionView.getSite().getWorkbenchWindow().getPartService().addPartListener( editorListener );
180
181             // link
182
IEditorPart activeEditor = connectionView.getSite().getWorkbenchWindow().getActivePage().getActiveEditor();
183             linkViewWithEditor( activeEditor );
184         }
185         else
186         {
187             // dsable the listeners
188
connectionView.getSite().getWorkbenchWindow().getPartService().removePartListener( editorListener );
189         }
190     }
191
192
193     /**
194      * Links the view with the right editor
195      *
196      * @param partRef the part
197      */

198     private void linkViewWithEditor( IWorkbenchPart part )
199     {
200         if ( part != null && connectionView != null
201             && part.getSite().getWorkbenchWindow() == connectionView.getSite().getWorkbenchWindow() )
202         {
203             Object JavaDoc objectToSelect = null;
204
205             if ( part instanceof EntryEditor )
206             {
207                 EntryEditor editor = ( EntryEditor ) part;
208                 IEditorInput input = editor.getEditorInput();
209                 if ( input != null && input instanceof EntryEditorInput )
210                 {
211                     EntryEditorInput eei = ( EntryEditorInput ) input;
212                     IEntry entry = eei.getResolvedEntry();
213                     if ( entry != null )
214                     {
215                         objectToSelect = entry.getConnection();
216                     }
217                 }
218             }
219             else if ( part instanceof SearchResultEditor )
220             {
221                 SearchResultEditor editor = ( SearchResultEditor ) part;
222                 IEditorInput input = editor.getEditorInput();
223                 if ( input != null && input instanceof SearchResultEditorInput )
224                 {
225                     SearchResultEditorInput srei = ( SearchResultEditorInput ) input;
226                     ISearch search = srei.getSearch();
227                     if ( search != null )
228                     {
229                         objectToSelect = search.getConnection();
230                     }
231                 }
232             }
233
234             if ( objectToSelect != null )
235             {
236                 // do not select if already selected!
237
// necessary to avoid infinite loops!
238
IStructuredSelection selection = ( IStructuredSelection ) connectionView.getMainWidget().getViewer()
239                     .getSelection();
240                 if ( selection.size() != 1 || !selection.getFirstElement().equals( objectToSelect ) )
241                 {
242                     connectionView.select( objectToSelect );
243                 }
244             }
245         }
246     }
247
248
249     /**
250      * Disposes this action.
251      */

252     public void dispose()
253     {
254         if ( editorListener != null )
255         {
256             connectionView.getSite().getWorkbenchWindow().getPartService().removePartListener( editorListener );
257             BrowserUIPlugin.getDefault().getPreferenceStore().removePropertyChangeListener( propertyChangeListener );
258             editorListener = null;
259         }
260
261         connectionView = null;
262     }
263
264 }
265
Popular Tags