KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > views > browser > 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.browser;
21
22
23 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants;
24 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin;
25 import org.apache.directory.ldapstudio.browser.ui.editors.entry.EntryEditor;
26 import org.apache.directory.ldapstudio.browser.ui.editors.entry.EntryEditorInput;
27 import org.apache.directory.ldapstudio.browser.ui.editors.searchresult.SearchResultEditor;
28 import org.apache.directory.ldapstudio.browser.ui.editors.searchresult.SearchResultEditorInput;
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.jface.viewers.IStructuredSelection;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IPartListener2;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.IWorkbenchPartReference;
36
37
38 /**
39  * This class implements the Link With Editor Action for the Browser View.
40  *
41  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
42  * @version $Rev$, $Date$
43  */

44 public class LinkWithEditorAction extends Action
45 {
46     /** The browser view */
47     private BrowserView browserView;
48
49     /** The listener listening on changes on editors */
50     private IPartListener2 editorListener = new IPartListener2()
51     {
52         /**
53          * {@inheritDoc}
54          */

55         public void partBroughtToTop( IWorkbenchPartReference partRef )
56         {
57         }
58
59
60         /**
61          * {@inheritDoc}
62          */

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

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

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

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

95         public void partInputChanged( IWorkbenchPartReference partRef )
96         {
97             linkViewWithEditor( partRef.getPart( false ) );
98         }
99
100
101         /**
102          * {@inheritDoc}
103          */

104         public void partOpened( IWorkbenchPartReference partRef )
105         {
106         }
107
108
109         /**
110          * {@inheritDoc}
111          */

112         public void partVisible( IWorkbenchPartReference partRef )
113         {
114         }
115     };
116
117
118     /**
119      * Creates a new instance of LinkWithEditorAction.
120      *
121      * @param browserView
122      * the associated view
123      */

124     public LinkWithEditorAction( BrowserView browserView )
125     {
126         super( "Link with editor", AS_CHECK_BOX );
127         setImageDescriptor( BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_LINK_WITH_EDITOR ) );
128         setEnabled( true );
129         setChecked( BrowserUIPlugin.getDefault().getPreferenceStore().getBoolean(
130             BrowserUIConstants.PREFERENCE_BROWSER_LINK_WITH_EDITOR ) );
131         this.browserView = browserView;
132
133         // Enable the listeners
134
if ( isChecked() )
135         {
136             browserView.getSite().getWorkbenchWindow().getPartService().addPartListener( editorListener );
137         }
138     }
139
140
141     /**
142      * {@inheritDoc}
143      */

144     public void run()
145     {
146         setChecked( isChecked() );
147         BrowserUIPlugin.getDefault().getPreferenceStore().setValue(
148             BrowserUIConstants.PREFERENCE_BROWSER_LINK_WITH_EDITOR, isChecked() );
149
150         if ( isChecked() )
151         {
152             // Enable the listener
153
browserView.getSite().getWorkbenchWindow().getPartService().addPartListener( editorListener );
154
155             // link
156
IEditorPart activeEditor = browserView.getSite().getWorkbenchWindow().getActivePage().getActiveEditor();
157             linkViewWithEditor( activeEditor );
158         }
159         else
160         {
161             // Disable the listener
162
browserView.getSite().getWorkbenchWindow().getPartService().removePartListener( editorListener );
163         }
164     }
165
166
167     /**
168      * Links the view with the right editor
169      *
170      * @param partRef the part
171      */

172     private void linkViewWithEditor( IWorkbenchPart part )
173     {
174         if ( part != null && browserView != null
175             && part.getSite().getWorkbenchWindow() == browserView.getSite().getWorkbenchWindow() )
176         {
177             Object JavaDoc objectToSelect = null;
178
179             if ( part instanceof EntryEditor )
180             {
181                 EntryEditor editor = ( EntryEditor ) part;
182                 IEditorInput input = editor.getEditorInput();
183                 if ( input != null && input instanceof EntryEditorInput )
184                 {
185                     EntryEditorInput eei = ( EntryEditorInput ) input;
186                     objectToSelect = eei.getInput();
187                 }
188             }
189             else if ( part instanceof SearchResultEditor )
190             {
191                 SearchResultEditor editor = ( SearchResultEditor ) part;
192                 IEditorInput input = editor.getEditorInput();
193                 if ( input != null && input instanceof SearchResultEditorInput )
194                 {
195                     SearchResultEditorInput srei = ( SearchResultEditorInput ) input;
196                     objectToSelect = srei.getSearch();
197                 }
198             }
199
200             if ( objectToSelect != null )
201             {
202                 // do not select if already selected!
203
// necessary to avoid infinite loops!
204
IStructuredSelection selection = ( IStructuredSelection ) browserView.getMainWidget().getViewer()
205                     .getSelection();
206                 if ( selection.size() != 1 || !selection.getFirstElement().equals( objectToSelect ) )
207                 {
208                     browserView.select( objectToSelect );
209                 }
210             }
211         }
212     }
213
214
215     /**
216      * Disposes this action.
217      */

218     public void dispose()
219     {
220         if ( editorListener != null )
221         {
222             browserView.getSite().getWorkbenchWindow().getPartService().removePartListener( editorListener );
223             editorListener = null;
224         }
225
226         browserView = null;
227     }
228
229 }
230
Popular Tags