1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets.entryeditor; 22 23 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 29 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 30 import org.apache.directory.ldapstudio.browser.common.actions.CopyAction; 31 import org.apache.directory.ldapstudio.browser.common.actions.DeleteAction; 32 import org.apache.directory.ldapstudio.browser.common.actions.NewValueAction; 33 import org.apache.directory.ldapstudio.browser.common.actions.PasteAction; 34 import org.apache.directory.ldapstudio.browser.common.actions.PropertiesAction; 35 import org.apache.directory.ldapstudio.browser.common.actions.SelectAllAction; 36 import org.apache.directory.ldapstudio.browser.common.actions.ShowRawValuesAction; 37 import org.apache.directory.ldapstudio.browser.common.actions.ValueEditorPreferencesAction; 38 import org.apache.directory.ldapstudio.browser.common.actions.proxy.BrowserActionProxy; 39 import org.apache.directory.ldapstudio.browser.common.actions.proxy.EntryEditorActionProxy; 40 import org.apache.directory.ldapstudio.valueeditors.IValueEditor; 41 import org.eclipse.jface.action.IAction; 42 import org.eclipse.jface.action.IMenuListener; 43 import org.eclipse.jface.action.IMenuManager; 44 import org.eclipse.jface.action.IToolBarManager; 45 import org.eclipse.jface.action.MenuManager; 46 import org.eclipse.jface.action.Separator; 47 import org.eclipse.jface.commands.ActionHandler; 48 import org.eclipse.jface.viewers.TreeViewer; 49 import org.eclipse.ui.IActionBars; 50 import org.eclipse.ui.PlatformUI; 51 import org.eclipse.ui.actions.ActionFactory; 52 import org.eclipse.ui.commands.ICommandService; 53 54 55 61 public class EntryEditorWidgetActionGroup 62 { 63 64 65 protected OpenSortDialogAction openSortDialogAction; 66 67 68 protected ShowRawValuesAction showRawValuesAction; 69 70 71 protected ShowQuickFilterAction showQuickFilterAction; 72 73 74 protected EntryEditorActionProxy openDefaultValueEditorActionProxy; 75 76 77 protected EntryEditorActionProxy openBestValueEditorActionProxy; 78 79 80 protected EntryEditorActionProxy[] openValueEditorActionProxies; 81 82 83 protected ValueEditorPreferencesAction openValueEditorPreferencesAction; 84 85 86 protected final static String newValueAction = "newValueAction"; 87 88 89 protected final static String copyAction = "copyAction"; 90 91 92 protected final static String pasteAction = "pasteAction"; 93 94 95 protected final static String deleteAction = "deleteAction"; 96 97 98 protected final static String selectAllAction = "selectAllAction"; 99 100 101 protected final static String propertyDialogAction = "propertyDialogAction"; 102 103 104 protected Map <String , EntryEditorActionProxy> entryEditorActionMap; 105 106 107 protected IActionBars actionBars; 108 109 110 private EntryEditorWidget mainWidget; 111 112 113 119 public EntryEditorWidgetActionGroup( EntryEditorWidget mainWidget, EntryEditorWidgetConfiguration configuration ) 120 { 121 this.mainWidget = mainWidget; 122 123 entryEditorActionMap = new HashMap <String , EntryEditorActionProxy>(); 124 TreeViewer viewer = mainWidget.getViewer(); 125 126 openSortDialogAction = new OpenSortDialogAction( configuration.getPreferences() ); 127 showRawValuesAction = new ShowRawValuesAction(); 128 showQuickFilterAction = new ShowQuickFilterAction( mainWidget.getQuickFilterWidget() ); 129 130 openBestValueEditorActionProxy = new EntryEditorActionProxy( viewer, new OpenBestEditorAction( viewer, this, 131 configuration.getValueEditorManager( viewer ) ) ); 132 openDefaultValueEditorActionProxy = new EntryEditorActionProxy( viewer, new OpenDefaultEditorAction( viewer, 133 openBestValueEditorActionProxy, false ) ); 134 IValueEditor[] valueEditors = configuration.getValueEditorManager( viewer ).getAllValueEditors(); 135 openValueEditorActionProxies = new EntryEditorActionProxy[valueEditors.length]; 136 for ( int i = 0; i < openValueEditorActionProxies.length; i++ ) 137 { 138 openValueEditorActionProxies[i] = new EntryEditorActionProxy( viewer, new OpenEditorAction( viewer, this, 139 configuration.getValueEditorManager( viewer ), valueEditors[i] ) ); 140 } 141 openValueEditorPreferencesAction = new ValueEditorPreferencesAction(); 142 143 entryEditorActionMap.put( newValueAction, new EntryEditorActionProxy( viewer, new NewValueAction() ) ); 144 145 entryEditorActionMap.put( pasteAction, new EntryEditorActionProxy( viewer, new PasteAction() ) ); 146 entryEditorActionMap.put( copyAction, new EntryEditorActionProxy( viewer, new CopyAction( 147 ( BrowserActionProxy ) entryEditorActionMap.get( pasteAction ) ) ) ); 148 entryEditorActionMap.put( deleteAction, new EntryEditorActionProxy( viewer, new DeleteAction() ) ); 149 entryEditorActionMap.put( selectAllAction, new EntryEditorActionProxy( viewer, new SelectAllAction( viewer ) ) ); 150 151 entryEditorActionMap.put( propertyDialogAction, new EntryEditorActionProxy( viewer, new PropertiesAction() ) ); 152 } 153 154 155 158 public void dispose() 159 { 160 if ( mainWidget != null ) 161 { 162 openSortDialogAction = null; 163 showQuickFilterAction.dispose(); 164 showQuickFilterAction = null; 165 showRawValuesAction = null; 166 167 openDefaultValueEditorActionProxy.dispose(); 168 openDefaultValueEditorActionProxy = null; 169 openBestValueEditorActionProxy.dispose(); 170 openBestValueEditorActionProxy = null; 171 for ( int i = 0; i < openValueEditorActionProxies.length; i++ ) 172 { 173 openValueEditorActionProxies[i].dispose(); 174 openValueEditorActionProxies[i] = null; 175 } 176 openValueEditorPreferencesAction = null; 177 178 for ( Iterator it = entryEditorActionMap.keySet().iterator(); it.hasNext(); ) 179 { 180 String key = ( String ) it.next(); 181 EntryEditorActionProxy action = ( EntryEditorActionProxy ) entryEditorActionMap.get( key ); 182 action.dispose(); 183 action = null; 184 it.remove(); 185 } 186 entryEditorActionMap.clear(); 187 entryEditorActionMap = null; 188 189 actionBars = null; 190 mainWidget = null; 191 } 192 } 193 194 195 200 public void enableGlobalActionHandlers( IActionBars actionBars ) 201 { 202 this.actionBars = actionBars; 203 activateGlobalActionHandlers(); 204 } 205 206 207 212 public void fillToolBar( IToolBarManager toolBarManager ) 213 { 214 215 toolBarManager.add( ( IAction ) entryEditorActionMap.get( newValueAction ) ); 216 toolBarManager.add( new Separator() ); 217 toolBarManager.add( ( IAction ) entryEditorActionMap.get( deleteAction ) ); 218 toolBarManager.add( new Separator() ); 219 toolBarManager.add( showQuickFilterAction ); 220 toolBarManager.update( true ); 221 222 } 223 224 225 230 public void fillMenu( IMenuManager menuManager ) 231 { 232 menuManager.add( openSortDialogAction ); 233 menuManager.add( showRawValuesAction ); 234 menuManager.addMenuListener( new IMenuListener() 235 { 236 public void menuAboutToShow( IMenuManager manager ) 237 { 238 showRawValuesAction.setChecked( BrowserCommonActivator.getDefault().getPreferenceStore().getBoolean( 239 BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES ) ); 240 } 241 } ); 242 } 243 244 245 251 public void fillContextMenu( IMenuManager menuManager ) 252 { 253 menuManager.setRemoveAllWhenShown( true ); 254 menuManager.addMenuListener( new IMenuListener() 255 { 256 public void menuAboutToShow( IMenuManager manager ) 257 { 258 contextMenuAboutToShow( manager ); 259 } 260 } ); 261 } 262 263 264 269 protected void contextMenuAboutToShow( IMenuManager menuManager ) 270 { 271 menuManager.add( ( IAction ) entryEditorActionMap.get( newValueAction ) ); 273 menuManager.add( new Separator() ); 274 275 menuManager.add( ( IAction ) entryEditorActionMap.get( copyAction ) ); 277 menuManager.add( ( IAction ) entryEditorActionMap.get( pasteAction ) ); 278 menuManager.add( ( IAction ) entryEditorActionMap.get( deleteAction ) ); 279 menuManager.add( ( IAction ) entryEditorActionMap.get( selectAllAction ) ); 280 menuManager.add( new Separator() ); 281 282 addEditMenu( menuManager ); 284 menuManager.add( new Separator() ); 285 286 menuManager.add( ( IAction ) entryEditorActionMap.get( propertyDialogAction ) ); 288 } 289 290 291 296 protected void addEditMenu( IMenuManager menuManager ) 297 { 298 menuManager.add( openDefaultValueEditorActionProxy ); 299 MenuManager editorMenuManager = new MenuManager( "Edit Value With" ); 300 if ( openBestValueEditorActionProxy.isEnabled() ) 301 { 302 editorMenuManager.add( openBestValueEditorActionProxy ); 303 editorMenuManager.add( new Separator() ); 304 } 305 for ( int i = 0; i < openValueEditorActionProxies.length; i++ ) 306 { 307 if ( openValueEditorActionProxies[i].isEnabled() 308 && ( ( OpenEditorAction ) openValueEditorActionProxies[i].getAction() ).getValueEditor().getClass() != ( ( OpenBestEditorAction ) openBestValueEditorActionProxy 309 .getAction() ).getBestValueEditor().getClass() ) 310 { 311 editorMenuManager.add( openValueEditorActionProxies[i] ); 312 } 313 } 314 editorMenuManager.add( new Separator() ); 315 editorMenuManager.add( openValueEditorPreferencesAction ); 316 menuManager.add( editorMenuManager ); 317 } 318 319 320 323 public void activateGlobalActionHandlers() 324 { 325 326 ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter( 327 ICommandService.class ); 328 329 if ( actionBars != null ) 330 { 331 actionBars.setGlobalActionHandler( ActionFactory.COPY.getId(), ( IAction ) entryEditorActionMap 332 .get( copyAction ) ); 333 actionBars.setGlobalActionHandler( ActionFactory.PASTE.getId(), ( IAction ) entryEditorActionMap 334 .get( pasteAction ) ); 335 actionBars.setGlobalActionHandler( ActionFactory.DELETE.getId(), ( IAction ) entryEditorActionMap 336 .get( deleteAction ) ); 337 actionBars.setGlobalActionHandler( ActionFactory.SELECT_ALL.getId(), ( IAction ) entryEditorActionMap 338 .get( selectAllAction ) ); 339 actionBars.setGlobalActionHandler( ActionFactory.PROPERTIES.getId(), ( IAction ) entryEditorActionMap 340 .get( propertyDialogAction ) ); 341 actionBars.setGlobalActionHandler( ActionFactory.FIND.getId(), showQuickFilterAction ); 343 actionBars.updateActionBars(); 344 } 345 else 346 { 347 if ( commandService != null ) 348 { 349 IAction da = ( IAction ) entryEditorActionMap.get( deleteAction ); 350 da.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.delete" ); 351 commandService.getCommand( da.getActionDefinitionId() ).setHandler( new ActionHandler( da ) ); 352 353 IAction ca = ( IAction ) entryEditorActionMap.get( copyAction ); 354 ca.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.copy" ); 355 commandService.getCommand( ca.getActionDefinitionId() ).setHandler( new ActionHandler( ca ) ); 356 357 IAction pa = ( IAction ) entryEditorActionMap.get( pasteAction ); 358 pa.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.paste" ); 359 commandService.getCommand( pa.getActionDefinitionId() ).setHandler( new ActionHandler( pa ) ); 360 361 showQuickFilterAction.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.find" ); 362 commandService.getCommand( showQuickFilterAction.getActionDefinitionId() ).setHandler( 363 new ActionHandler( showQuickFilterAction ) ); 364 365 IAction pda = ( IAction ) entryEditorActionMap.get( propertyDialogAction ); 366 pda.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.properties" ); 367 commandService.getCommand( pda.getActionDefinitionId() ).setHandler( new ActionHandler( pda ) ); 368 } 369 } 370 371 if ( commandService != null ) 372 { 373 IAction nva = ( IAction ) entryEditorActionMap.get( newValueAction ); 374 commandService.getCommand( nva.getActionDefinitionId() ).setHandler( new ActionHandler( nva ) ); 375 commandService.getCommand( openDefaultValueEditorActionProxy.getActionDefinitionId() ).setHandler( 376 new ActionHandler( openDefaultValueEditorActionProxy ) ); 377 } 378 379 } 380 381 382 385 public void deactivateGlobalActionHandlers() 386 { 387 388 ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter( 389 ICommandService.class ); 390 391 if ( actionBars != null ) 392 { 393 actionBars.setGlobalActionHandler( ActionFactory.COPY.getId(), null ); 394 actionBars.setGlobalActionHandler( ActionFactory.PASTE.getId(), null ); 395 actionBars.setGlobalActionHandler( ActionFactory.DELETE.getId(), null ); 396 actionBars.setGlobalActionHandler( ActionFactory.SELECT_ALL.getId(), null ); 397 actionBars.setGlobalActionHandler( ActionFactory.FIND.getId(), null ); 398 actionBars.setGlobalActionHandler( ActionFactory.PROPERTIES.getId(), null ); 399 400 actionBars.updateActionBars(); 401 } 402 else 403 { 404 if ( commandService != null ) 405 { 406 IAction da = ( IAction ) entryEditorActionMap.get( deleteAction ); 407 commandService.getCommand( da.getActionDefinitionId() ).setHandler( null ); 408 409 IAction ca = ( IAction ) entryEditorActionMap.get( copyAction ); 410 commandService.getCommand( ca.getActionDefinitionId() ).setHandler( null ); 411 412 IAction pa = ( IAction ) entryEditorActionMap.get( pasteAction ); 413 commandService.getCommand( pa.getActionDefinitionId() ).setHandler( null ); 414 415 commandService.getCommand( showQuickFilterAction.getActionDefinitionId() ).setHandler( null ); 416 417 IAction pda = ( IAction ) entryEditorActionMap.get( propertyDialogAction ); 418 commandService.getCommand( pda.getActionDefinitionId() ).setHandler( null ); 419 } 420 } 421 422 if ( commandService != null ) 423 { 424 IAction nva = ( IAction ) entryEditorActionMap.get( newValueAction ); 425 commandService.getCommand( nva.getActionDefinitionId() ).setHandler( null ); 426 commandService.getCommand( openDefaultValueEditorActionProxy.getActionDefinitionId() ).setHandler( null ); 427 } 428 429 } 430 431 432 437 public OpenDefaultEditorAction getOpenDefaultEditorAction() 438 { 439 return ( OpenDefaultEditorAction ) openDefaultValueEditorActionProxy.getAction(); 440 } 441 442 } 443 | Popular Tags |