1 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 48 public class EntryEditorUniversalListener extends EntryEditorWidgetUniversalListener 49 { 50 51 52 private EntryEditor entryEditor; 53 54 55 private IContextActivation contextActivation; 56 57 58 private INullSelectionListener entrySelectionListener = new INullSelectionListener() 59 { 60 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 [] 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 100 private IPartListener2 partListener = new IPartListener2() 101 { 102 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 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 140 entryEditor.getActionGroup().activateGlobalActionHandlers(); 141 } 142 } 143 144 145 148 public void partBroughtToTop( IWorkbenchPartReference partRef ) 149 { 150 } 151 152 153 156 public void partClosed( IWorkbenchPartReference partRef ) 157 { 158 } 159 160 161 164 public void partOpened( IWorkbenchPartReference partRef ) 165 { 166 } 167 168 169 172 public void partHidden( IWorkbenchPartReference partRef ) 173 { 174 } 175 176 177 180 public void partVisible( IWorkbenchPartReference partRef ) 181 { 182 } 183 184 185 188 public void partInputChanged( IWorkbenchPartReference partRef ) 189 { 190 } 191 }; 192 193 194 199 public EntryEditorUniversalListener( EntryEditor entryEditor ) 200 { 201 super( entryEditor.getMainWidget().getViewer(), entryEditor.getActionGroup().getOpenDefaultEditorAction() ); 202 this.entryEditor = entryEditor; 203 204 entryEditor.getSite().getPage().addPartListener( partListener ); 206 entryEditor.getSite().getWorkbenchWindow().getSelectionService().addPostSelectionListener( BrowserView.getId(), 207 entrySelectionListener ); 208 } 209 210 211 214 public void dispose() 215 { 216 if ( entryEditor != null ) 217 { 218 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 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 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 |