KickJava   Java API By Example, From Geeks To Geeks.

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


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.views.browser;
22
23
24 import java.util.Arrays JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
29 import org.apache.directory.ldapstudio.browser.common.actions.SelectionUtils;
30 import org.apache.directory.ldapstudio.browser.common.widgets.browser.BrowserUniversalListener;
31 import org.apache.directory.ldapstudio.browser.core.events.AttributesInitializedEvent;
32 import org.apache.directory.ldapstudio.browser.core.events.BookmarkUpdateEvent;
33 import org.apache.directory.ldapstudio.browser.core.events.BookmarkUpdateListener;
34 import org.apache.directory.ldapstudio.browser.core.events.BulkModificationEvent;
35 import org.apache.directory.ldapstudio.browser.core.events.ConnectionUpdateEvent;
36 import org.apache.directory.ldapstudio.browser.core.events.EntryAddedEvent;
37 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent;
38 import org.apache.directory.ldapstudio.browser.core.events.EntryMovedEvent;
39 import org.apache.directory.ldapstudio.browser.core.events.EntryRenamedEvent;
40 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
41 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateEvent;
42 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateListener;
43 import org.apache.directory.ldapstudio.browser.core.model.IBookmark;
44 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
45 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
46 import org.apache.directory.ldapstudio.browser.core.model.IRootDSE;
47 import org.apache.directory.ldapstudio.browser.core.model.ISearch;
48 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult;
49 import org.apache.directory.ldapstudio.browser.ui.editors.entry.EntryEditor;
50 import org.apache.directory.ldapstudio.browser.ui.editors.entry.EntryEditorInput;
51 import org.apache.directory.ldapstudio.browser.ui.editors.searchresult.SearchResultEditor;
52 import org.apache.directory.ldapstudio.browser.ui.editors.searchresult.SearchResultEditorInput;
53 import org.apache.directory.ldapstudio.browser.ui.views.connection.ConnectionView;
54 import org.eclipse.jface.viewers.ISelection;
55 import org.eclipse.jface.viewers.ISelectionChangedListener;
56 import org.eclipse.jface.viewers.ITreeContentProvider;
57 import org.eclipse.jface.viewers.SelectionChangedEvent;
58 import org.eclipse.jface.viewers.StructuredSelection;
59 import org.eclipse.ui.INullSelectionListener;
60 import org.eclipse.ui.IPartListener2;
61 import org.eclipse.ui.IWorkbenchPart;
62 import org.eclipse.ui.IWorkbenchPartReference;
63 import org.eclipse.ui.PartInitException;
64 import org.eclipse.ui.PlatformUI;
65 import org.eclipse.ui.contexts.IContextActivation;
66 import org.eclipse.ui.contexts.IContextService;
67
68
69 /**
70  * The BrowserViewUniversalListener manages all events for the browser view.
71  *
72  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
73  * @version $Rev$, $Date$
74  */

75 public class BrowserViewUniversalListener extends BrowserUniversalListener implements SearchUpdateListener,
76     BookmarkUpdateListener
77 {
78
79     /** This map contains all expanded elements for a particular connection */
80     private Map JavaDoc<IConnection, Object JavaDoc[]> connectionToExpandedElementsMap;
81
82     /** This map contains all selected elements for a particular connection */
83     private Map JavaDoc<IConnection, ISelection> connectionToSelectedElementMap;
84
85     /** The browser view */
86     private BrowserView view;
87
88     /** Token used to activate and deactivate shortcuts in the view */
89     private IContextActivation contextActivation;
90
91     /** Listener that listens for selections of connections */
92     private INullSelectionListener connectionSelectionListener = new INullSelectionListener()
93     {
94         /**
95          * {@inheritDoc}
96          *
97          * This implementation sets the input when another connection was selected.
98          */

99         public void selectionChanged( IWorkbenchPart part, ISelection selection )
100         {
101             if ( view != null && part != null )
102             {
103                 if ( view.getSite().getWorkbenchWindow() == part.getSite().getWorkbenchWindow() )
104                 {
105                     IConnection[] connections = SelectionUtils.getConnections( selection );
106                     if ( connections.length == 1 )
107                     {
108                         setInput( connections[0] );
109                     }
110                     else
111                     {
112                         setInput( null );
113                     }
114                 }
115             }
116         }
117     };
118
119     /** The part listener used to activate and deactivate the shortcuts */
120     private IPartListener2 partListener = new IPartListener2()
121     {
122         /**
123          * {@inheritDoc}
124          *
125          * This implementation deactivates the shortcuts when the part is deactivated.
126          */

127         public void partDeactivated( IWorkbenchPartReference partRef )
128         {
129             if ( partRef.getPart( false ) == view && contextActivation != null )
130             {
131
132                 view.getActionGroup().deactivateGlobalActionHandlers();
133
134                 IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
135                     IContextService.class );
136                 contextService.deactivateContext( contextActivation );
137                 contextActivation = null;
138             }
139         }
140
141
142         /**
143          * {@inheritDoc}
144          *
145          * This implementation activates the shortcuts when the part is activated.
146          */

147         public void partActivated( IWorkbenchPartReference partRef )
148         {
149             if ( partRef.getPart( false ) == view )
150             {
151
152                 IContextService contextService = ( IContextService ) PlatformUI.getWorkbench().getAdapter(
153                     IContextService.class );
154                 contextActivation = contextService
155                     .activateContext( "org.apache.directory.ldapstudio.browser.action.context" );
156                 // org.eclipse.ui.contexts.dialogAndWindow
157
// org.eclipse.ui.contexts.window
158
// org.eclipse.ui.text_editor_context
159

160                 view.getActionGroup().activateGlobalActionHandlers();
161             }
162         }
163
164
165         /**
166          * {@inheritDoc}
167          */

168         public void partBroughtToTop( IWorkbenchPartReference partRef )
169         {
170         }
171
172
173         /**
174          * {@inheritDoc}
175          */

176         public void partClosed( IWorkbenchPartReference partRef )
177         {
178         }
179
180
181         /**
182          * {@inheritDoc}
183          */

184         public void partOpened( IWorkbenchPartReference partRef )
185         {
186         }
187
188
189         /**
190          * {@inheritDoc}
191          */

192         public void partHidden( IWorkbenchPartReference partRef )
193         {
194         }
195
196
197         /**
198          * {@inheritDoc}
199          */

200         public void partVisible( IWorkbenchPartReference partRef )
201         {
202         }
203
204
205         /**
206          * {@inheritDoc}
207          */

208         public void partInputChanged( IWorkbenchPartReference partRef )
209         {
210         }
211     };
212
213     /** This listener is used to ensure that the entry editor and search result editor are opened
214      when an object in the browser view is selected */

215     private ISelectionChangedListener viewerSelectionListener = new ISelectionChangedListener()
216     {
217         /**
218          * {@inheritDoc}
219          */

220         public void selectionChanged( SelectionChangedEvent event )
221         {
222             ensureEditorsVisible( event.getSelection() );
223         }
224     };
225
226
227     /**
228      * Creates a new instance of BrowserViewUniversalListener.
229      *
230      * @param view the browser view
231      */

232     public BrowserViewUniversalListener( BrowserView view )
233     {
234         super( view.getMainWidget().getViewer() );
235         this.view = view;
236
237         // create maps
238
connectionToExpandedElementsMap = new HashMap JavaDoc<IConnection, Object JavaDoc[]>();
239         connectionToSelectedElementMap = new HashMap JavaDoc<IConnection, ISelection>();
240
241         // register listeners
242
EventRegistry.addSearchUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
243         EventRegistry.addBookmarkUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
244         EventRegistry.addEntryUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
245         EventRegistry.addConnectionUpdateListener( this, BrowserCommonActivator.getDefault().getEventRunner() );
246
247         view.getSite().getPage().addPartListener( partListener );
248         view.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener( ConnectionView.getId(),
249             connectionSelectionListener );
250
251         viewer.addSelectionChangedListener( viewerSelectionListener );
252     }
253
254
255     /**
256      * Ensures that the entry editor or the search result editor are
257      * opended and ready to show the given selection.
258      *
259      * @param selection the browser's selection.
260      */

261     private void ensureEditorsVisible( ISelection selection )
262     {
263         if ( view != null )
264         {
265             IEntry[] entries = SelectionUtils.getEntries( selection );
266             ISearchResult[] searchResults = SelectionUtils.getSearchResults( selection );
267             IBookmark[] bookmarks = SelectionUtils.getBookmarks( selection );
268             ISearch[] searches = SelectionUtils.getSearches( selection );
269
270             if ( entries.length + searchResults.length + bookmarks.length + searches.length == 1 )
271             {
272                 if ( entries.length == 1 )
273                 {
274                     try
275                     {
276                         EntryEditorInput input = new EntryEditorInput( entries[0] );
277                         view.getSite().getPage().openEditor( input, EntryEditor.getId(), false );
278                     }
279                     catch ( PartInitException e )
280                     {
281                     }
282                 }
283                 else if ( searchResults.length == 1 )
284                 {
285                     try
286                     {
287                         EntryEditorInput input = new EntryEditorInput( searchResults[0] );
288                         view.getSite().getPage().openEditor( input, EntryEditor.getId(), false );
289                     }
290                     catch ( PartInitException e )
291                     {
292                     }
293                 }
294                 else if ( bookmarks.length == 1 )
295                 {
296                     try
297                     {
298                         EntryEditorInput input = new EntryEditorInput( bookmarks[0] );
299                         view.getSite().getPage().openEditor( input, EntryEditor.getId(), false );
300                     }
301                     catch ( PartInitException e )
302                     {
303                     }
304                 }
305                 else if ( searches.length == 1 )
306                 {
307                     try
308                     {
309                         SearchResultEditorInput input = new SearchResultEditorInput( searches[0] );
310                         view.getSite().getPage().openEditor( input, SearchResultEditor.getId(), false );
311                     }
312                     catch ( PartInitException e )
313                     {
314                     }
315                 }
316             }
317         }
318     }
319
320
321     /**
322      * {@inheritDoc}
323      */

324     public void dispose()
325     {
326         if ( view != null )
327         {
328             EventRegistry.removeSearchUpdateListener( this );
329             EventRegistry.removeBookmarkUpdateListener( this );
330             EventRegistry.removeEntryUpdateListener( this );
331             EventRegistry.removeConnectionUpdateListener( this );
332
333             view.getSite().getPage().removePartListener( partListener );
334             view.getSite().getWorkbenchWindow().getSelectionService().removePostSelectionListener(
335                 ConnectionView.getId(), connectionSelectionListener );
336
337             view = null;
338             connectionToExpandedElementsMap.clear();
339             connectionToExpandedElementsMap = null;
340             connectionToSelectedElementMap.clear();
341             connectionToSelectedElementMap = null;
342         }
343
344         super.dispose();
345     }
346
347
348     /**
349      * Sets the input to the viewer and saves/restores the expanded and selected elements.
350      *
351      * @param connection the connection input
352      */

353     void setInput( IConnection connection )
354     {
355         // only if another connection is selected
356
if ( connection != viewer.getInput() )
357         {
358
359             IConnection currentConnection = viewer.getInput() instanceof IConnection ? ( IConnection ) viewer
360                 .getInput() : null;
361
362             // save expanded elements and selection
363
if ( currentConnection != null )
364             {
365                 connectionToExpandedElementsMap.put( currentConnection, viewer.getExpandedElements() );
366                 if ( !viewer.getSelection().isEmpty() )
367                 {
368                     connectionToSelectedElementMap.put( currentConnection, viewer.getSelection() );
369                 }
370             }
371
372             // change input
373
viewer.setInput( connection );
374             view.getActionGroup().setInput( connection );
375
376             // restore expanded elements and selection
377
if ( view != null && connection != null )
378             {
379                 if ( connectionToExpandedElementsMap.containsKey( connection ) )
380                 {
381                     viewer.setExpandedElements( ( Object JavaDoc[] ) connectionToExpandedElementsMap.get( connection ) );
382                 }
383                 if ( connectionToSelectedElementMap.containsKey( connection )
384                     && this.view.getSite().getPage().isPartVisible( view ) )
385                 {
386                     viewer.setSelection( ( ISelection ) connectionToSelectedElementMap.get( connection ), true );
387                 }
388             }
389         }
390     }
391
392
393     /**
394      * {@inheritDoc}
395      *
396      * This implementation refreshes the tree and expands/collapses the
397      * tree when the connection is opened/closed.
398      */

399     public void connectionUpdated( ConnectionUpdateEvent connectionUpdateEvent )
400     {
401         if ( connectionUpdateEvent.getDetail() == ConnectionUpdateEvent.EventDetail.CONNECTION_OPENED )
402         {
403             // expand viewer
404
viewer.refresh( connectionUpdateEvent.getConnection() );
405             viewer.expandToLevel( 2 );
406
407             // expand root DSE to show base entries
408
IRootDSE rootDSE = connectionUpdateEvent.getConnection().getRootDSE();
409             viewer.expandToLevel( rootDSE, 1 );
410
411             // expand base entries, if requested
412
if ( view.getConfiguration().getPreferences().isExpandBaseEntries() )
413             {
414                 viewer.expandToLevel( rootDSE, 2 );
415             }
416         }
417         else if ( connectionUpdateEvent.getDetail() == ConnectionUpdateEvent.EventDetail.CONNECTION_CLOSED )
418         {
419             viewer.collapseAll();
420             connectionToExpandedElementsMap.remove( connectionUpdateEvent.getConnection() );
421             connectionToSelectedElementMap.remove( connectionUpdateEvent.getConnection() );
422             viewer.refresh( connectionUpdateEvent.getConnection() );
423         }
424         else
425         {
426             viewer.refresh( connectionUpdateEvent.getConnection() );
427         }
428     }
429
430
431     /**
432      * {@inheritDoc}
433      *
434      * This viewer selects the updated search.
435      */

436     public void searchUpdated( SearchUpdateEvent searchUpdateEvent )
437     {
438         ISearch search = searchUpdateEvent.getSearch();
439         viewer.refresh();
440
441         if ( Arrays.asList( search.getConnection().getSearchManager().getSearches() ).contains( search ) )
442         {
443             viewer.setSelection( new StructuredSelection( search ), true );
444         }
445         else
446         {
447             Object JavaDoc searchCategory = ( ( ITreeContentProvider ) viewer.getContentProvider() ).getParent( search );
448             viewer.setSelection( new StructuredSelection( searchCategory ), true );
449         }
450     }
451
452
453     /**
454      * {@inheritDoc}
455      */

456     public void bookmarkUpdated( BookmarkUpdateEvent bookmarkUpdateEvent )
457     {
458         viewer.refresh();
459     }
460
461
462     /**
463      * {@inheritDoc}
464      *
465      * This implementation refreshes the tree and
466      * selects an entry depending on the event type.
467      */

468     public void entryUpdated( EntryModificationEvent event )
469     {
470         // Don't handle attribute initalization, could cause double
471
// retrieval of children.
472
//
473
// When double-clicking an entry two Jobs/Threads are started:
474
// - InitializeAttributesJob and
475
// - InitializeChildrenJob
476
// If the InitializeAttributesJob is finished first the
477
// AttributesInitializedEvent is fired. If this causes
478
// a refresh of the tree before the children are initialized
479
// another InitializeChildrenJob is executed.
480
if ( event instanceof AttributesInitializedEvent )
481         {
482             return;
483         }
484
485         if ( event instanceof EntryAddedEvent )
486         {
487             viewer.refresh( event.getModifiedEntry(), true );
488             viewer.refresh( event.getModifiedEntry().getParententry(), true );
489             viewer.setSelection( new StructuredSelection( event.getModifiedEntry() ), true );
490         }
491         else if ( event instanceof EntryRenamedEvent )
492         {
493             EntryRenamedEvent ere = ( EntryRenamedEvent ) event;
494             viewer.refresh( ere.getNewEntry().getParententry(), true );
495             viewer.refresh( ere.getNewEntry(), true );
496             viewer.setSelection( new StructuredSelection( ere.getNewEntry() ), true );
497         }
498         else if ( event instanceof EntryMovedEvent )
499         {
500             EntryMovedEvent eme = ( EntryMovedEvent ) event;
501             viewer.refresh( eme.getOldEntry().getParententry(), true );
502             viewer.refresh( eme.getNewEntry().getParententry(), true );
503             viewer.refresh( eme.getNewEntry(), true );
504             viewer.setSelection( new StructuredSelection( eme.getNewEntry() ), true );
505         }
506         else if ( event instanceof BulkModificationEvent )
507         {
508             viewer.refresh();
509         }
510
511         viewer.refresh( event.getModifiedEntry(), true );
512     }
513
514 }
515
Popular Tags