KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > ui > editors > entry > EntryEditorUniversalListener


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.editors.entry;
22
23
24 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils;
25 import org.apache.directory.ldapstudio.browser.common.widgets.entryeditor.EntryEditorWidgetUniversalListener;
26 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
27 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
28 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
29 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
30 import org.apache.directory.ldapstudio.browser.ui.views.browser.BrowserView;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.ui.INullSelectionListener;
33 import org.eclipse.ui.IPartListener2;
34 import org.eclipse.ui.IWorkbenchPart;
35 import org.eclipse.ui.IWorkbenchPartReference;
36 import org.eclipse.ui.PlatformUI;
37 import org.eclipse.ui.contexts.IContextActivation;
38 import org.eclipse.ui.contexts.IContextService;
39 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
40
41
42 /**
43  * The EntryEditorUniversalListener manages all events for the entry editor.
44  *
45  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
46  * @version $Rev$, $Date$
47  */

48 public class EntryEditorUniversalListener extends EntryEditorWidgetUniversalListener
49 {
50
51     /** The entry editor */
52     private EntryEditor entryEditor;
53
54     /** Token used to activate and deactivate shortcuts in the editor */
55     private IContextActivation contextActivation;
56
57     /** Listener that listens for selections of IEntry, ISeachResult and IBookmark objects. */
58     private INullSelectionListener entrySelectionListener = new INullSelectionListener()
59     {
60         /**
61          * {@inheritDoc}
62          *
63          * This implementation sets the editor's input when a entry, search result or bookmark is selected.
64          */

65         public void selectionChanged( IWorkbenchPart part, ISelection selection )
66         {
67             if ( entryEditor != null && part != null )
68             {
69                 if ( entryEditor.getSite().getWorkbenchWindow() == part.getSite().getWorkbenchWindow() )
70                 {
71                     IEntry[] entries = SelectionUtils.getEntries( selection );
72                     ISearchResult[] searchResults = SelectionUtils.getSearchResults( selection );
73                     IBookmark[] bookmarks = SelectionUtils.getBookmarks( selection );
74                     Object JavaDoc[] objects = SelectionUtils.getObjects( selection );
75                     if ( entries.length + searchResults.length + bookmarks.length == 1 && objects.length == 1 )
76                     {
77                         if ( entries.length == 1 )
78                         {
79                             entryEditor.setInput( new EntryEditorInput( entries[0] ) );
80                         }
81                         else if ( searchResults.length == 1 )
82                         {
83                             entryEditor.setInput( new EntryEditorInput( searchResults[0] ) );
84                         }
85                         else if ( bookmarks.length == 1 )
86                         {
87                             entryEditor.setInput( new EntryEditorInput( bookmarks[0] ) );
88                         }
89                     }
90                     else
91                     {
92                         entryEditor.setInput( new EntryEditorInput( ( IEntry ) null ) );
93                     }
94                 }
95             }
96         }
97     };
98
99     /** The part listener used to activate and deactivate the shortcuts */
100     private IPartListener2 partListener = new IPartListener2()
101     {
102         /**
103          * {@inheritDoc}
104          *
105          * This implementation deactivates the shortcuts when the part is deactivated.
106          */

107         public void partDeactivated( IWorkbenchPartReference partRef )
108         {
109             if ( partRef.getPart( false ) == entryEditor && contextActivation != null )
110             {
111
112                 entryEditor.getActionGroup().deactivateGlobalActionHandlers();
113
114                 IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
115                     IContextService.class );
116                 contextService.deactivateContext( contextActivation );
117                 contextActivation = null;
118             }
119         }
120
121
122         /**
123          * {@inheritDoc}
124          *
125          * This implementation activates the shortcuts when the part is activated.
126          */

127         public void partActivated( IWorkbenchPartReference partRef )
128         {
129             if ( partRef.getPart( false ) == entryEditor )
130             {
131
132                 IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
133                     IContextService.class );
134                 contextActivation = contextService
135                     .activateContext( "org.apache.directory.ldapstudio.browser.action.context" );
136                 // org.eclipse.ui.contexts.dialogAndWindow
137
// org.eclipse.ui.contexts.window
138
// org.eclipse.ui.text_editor_context
139

140                 entryEditor.getActionGroup().activateGlobalActionHandlers();
141             }
142         }
143
144
145         /**
146          * {@inheritDoc}
147          */

148         public void partBroughtToTop( IWorkbenchPartReference partRef )
149         {
150         }
151
152
153         /**
154          * {@inheritDoc}
155          */

156         public void partClosed( IWorkbenchPartReference partRef )
157         {
158         }
159
160
161         /**
162          * {@inheritDoc}
163          */

164         public void partOpened( IWorkbenchPartReference partRef )
165         {
166         }
167
168
169         /**
170          * {@inheritDoc}
171          */

172         public void partHidden( IWorkbenchPartReference partRef )
173         {
174         }
175
176
177         /**
178          * {@inheritDoc}
179          */

180         public void partVisible( IWorkbenchPartReference partRef )
181         {
182         }
183
184
185         /**
186          * {@inheritDoc}
187          */

188         public void partInputChanged( IWorkbenchPartReference partRef )
189         {
190         }
191     };
192
193
194     /**
195      * Creates a new instance of EntryEditorUniversalListener.
196      *
197      * @param entryEditor the entry editor
198      */

199     public EntryEditorUniversalListener( EntryEditor entryEditor )
200     {
201         super( entryEditor.getMainWidget().getViewer(), entryEditor.getActionGroup().getOpenDefaultEditorAction() );
202         this.entryEditor = entryEditor;
203
204         // register listeners
205
entryEditor.getSite().getPage().addPartListener( partListener );
206         entryEditor.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener( BrowserView.getId(),
207             entrySelectionListener );
208     }
209
210
211     /**
212      * {@inheritDoc}
213      */

214     public void dispose()
215     {
216         if ( entryEditor != null )
217         {
218             // deregister listneners
219
entryEditor.getSite().getPage().removePartListener( partListener );
220             entryEditor.getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(
221                 BrowserView.getId(), entrySelectionListener );
222             entryEditor = null;
223         }
224
225         super.dispose();
226     }
227
228
229     /**
230      * Sets the input to the viewer.
231      *
232      * @param entry the entry input
233      */

234     void setInput( IEntry entry )
235     {
236         if ( entry != viewer.getInput() )
237         {
238             viewer.setInput( entry );
239             entryEditor.getActionGroup().setInput( entry );
240         }
241
242     }
243
244
245     /**
246      * {@inheritDoc}
247      *
248      * This implementation updates the outline page when the entry is updated.
249      */

250     public void entryUpdated( EntryModificationEvent event )
251     {
252         super.entryUpdated( event );
253
254         EntryEditorOutlinePage outlinePage = ( EntryEditorOutlinePage ) entryEditor.getAdapter( IContentOutlinePage.class );
255         if ( outlinePage != null )
256         {
257             outlinePage.refresh();
258         }
259     }
260
261 }
262
Popular Tags