1 20 21 package org.apache.directory.ldapstudio.browser.common.widgets.connection; 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.actions.CloseConnectionAction; 29 import org.apache.directory.ldapstudio.browser.common.actions.CopyAction; 30 import org.apache.directory.ldapstudio.browser.common.actions.DeleteAction; 31 import org.apache.directory.ldapstudio.browser.common.actions.NewConnectionAction; 32 import org.apache.directory.ldapstudio.browser.common.actions.OpenConnectionAction; 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.RenameAction; 36 import org.apache.directory.ldapstudio.browser.common.actions.proxy.BrowserActionProxy; 37 import org.apache.directory.ldapstudio.browser.common.actions.proxy.ConnectionViewActionProxy; 38 import org.eclipse.jface.action.IAction; 39 import org.eclipse.jface.action.IMenuListener; 40 import org.eclipse.jface.action.IMenuManager; 41 import org.eclipse.jface.action.IToolBarManager; 42 import org.eclipse.jface.action.Separator; 43 import org.eclipse.jface.commands.ActionHandler; 44 import org.eclipse.jface.viewers.TableViewer; 45 import org.eclipse.ui.IActionBars; 46 import org.eclipse.ui.IWorkbenchActionConstants; 47 import org.eclipse.ui.PlatformUI; 48 import org.eclipse.ui.actions.ActionFactory; 49 import org.eclipse.ui.commands.ICommandService; 50 51 52 58 public class ConnectionActionGroup implements IMenuListener 59 { 60 61 62 protected static final String newConnectionAction = "newConnectionAction"; 63 64 65 protected static final String openConnectionAction = "openConnectionAction"; 66 67 68 protected static final String closeConnectionAction = "closeConnectionAction"; 69 70 71 protected static final String copyConnectionAction = "copyConnectionAction"; 72 73 74 protected static final String pasteConnectionAction = "pasteConnectionAction"; 75 76 77 protected static final String deleteConnectionAction = "deleteConnectionAction"; 78 79 80 protected static final String renameConnectionAction = "renameConnectionAction"; 81 82 83 protected static final String propertyDialogAction = "propertyDialogAction"; 84 85 86 protected Map <String , ConnectionViewActionProxy> connectionActionMap; 87 88 89 protected IActionBars actionBars; 90 91 92 protected ConnectionWidget mainWidget; 93 94 95 101 public ConnectionActionGroup( ConnectionWidget mainWidget, ConnectionConfiguration configuration ) 102 { 103 this.mainWidget = mainWidget; 104 this.connectionActionMap = new HashMap <String , ConnectionViewActionProxy>(); 105 106 TableViewer viewer = mainWidget.getViewer(); 107 connectionActionMap 108 .put( newConnectionAction, new ConnectionViewActionProxy( viewer, new NewConnectionAction() ) ); 109 connectionActionMap.put( openConnectionAction, new ConnectionViewActionProxy( viewer, 110 new OpenConnectionAction() ) ); 111 connectionActionMap.put( closeConnectionAction, new ConnectionViewActionProxy( viewer, 112 new CloseConnectionAction() ) ); 113 connectionActionMap.put( pasteConnectionAction, new ConnectionViewActionProxy( viewer, new PasteAction() ) ); 114 connectionActionMap.put( copyConnectionAction, new ConnectionViewActionProxy( viewer, new CopyAction( 115 ( BrowserActionProxy ) connectionActionMap.get( pasteConnectionAction ) ) ) ); 116 connectionActionMap.put( deleteConnectionAction, new ConnectionViewActionProxy( viewer, new DeleteAction() ) ); 117 connectionActionMap.put( renameConnectionAction, new ConnectionViewActionProxy( viewer, new RenameAction() ) ); 118 connectionActionMap.put( propertyDialogAction, new ConnectionViewActionProxy( viewer, new PropertiesAction() ) ); 119 } 120 121 122 125 public void dispose() 126 { 127 if ( mainWidget != null ) 128 { 129 for ( Iterator it = connectionActionMap.keySet().iterator(); it.hasNext(); ) 130 { 131 String key = ( String ) it.next(); 132 ConnectionViewActionProxy action = ( ConnectionViewActionProxy ) this.connectionActionMap.get( key ); 133 action.dispose(); 134 action = null; 135 it.remove(); 136 } 137 connectionActionMap.clear(); 138 connectionActionMap = null; 139 140 actionBars = null; 141 mainWidget = null; 142 } 143 } 144 145 146 151 public void enableGlobalActionHandlers( IActionBars actionBars ) 152 { 153 this.actionBars = actionBars; 154 activateGlobalActionHandlers(); 155 } 156 157 158 163 public void fillToolBar( IToolBarManager toolBarManager ) 164 { 165 toolBarManager.add( ( IAction ) this.connectionActionMap.get( newConnectionAction ) ); 166 toolBarManager.add( new Separator() ); 167 toolBarManager.add( ( IAction ) this.connectionActionMap.get( openConnectionAction ) ); 168 toolBarManager.add( ( IAction ) this.connectionActionMap.get( closeConnectionAction ) ); 169 170 toolBarManager.update( true ); 171 } 172 173 174 179 public void fillMenu( IMenuManager menuManager ) 180 { 181 } 185 186 187 192 public void fillContextMenu( IMenuManager menuManager ) 193 { 194 menuManager.setRemoveAllWhenShown( true ); 195 menuManager.addMenuListener( this ); 196 } 197 198 199 204 public void menuAboutToShow( IMenuManager menuManager ) 205 { 206 menuManager.add( ( IAction ) connectionActionMap.get( newConnectionAction ) ); 208 menuManager.add( new Separator() ); 209 210 if ( ( ( IAction ) connectionActionMap.get( closeConnectionAction ) ).isEnabled() ) 212 { 213 menuManager.add( ( IAction ) connectionActionMap.get( closeConnectionAction ) ); 214 } 215 else if ( ( ( IAction ) connectionActionMap.get( openConnectionAction ) ).isEnabled() ) 216 { 217 menuManager.add( ( IAction ) connectionActionMap.get( openConnectionAction ) ); 218 } 219 menuManager.add( new Separator() ); 220 221 menuManager.add( ( IAction ) connectionActionMap.get( copyConnectionAction ) ); 223 menuManager.add( ( IAction ) connectionActionMap.get( pasteConnectionAction ) ); 224 menuManager.add( ( IAction ) connectionActionMap.get( deleteConnectionAction ) ); 225 menuManager.add( ( IAction ) connectionActionMap.get( renameConnectionAction ) ); 226 menuManager.add( new Separator() ); 227 228 menuManager.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) ); 230 231 menuManager.add( ( IAction ) connectionActionMap.get( propertyDialogAction ) ); 233 } 234 235 236 239 public void activateGlobalActionHandlers() 240 { 241 ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter( 242 ICommandService.class ); 243 244 if ( actionBars != null ) 245 { 246 actionBars.setGlobalActionHandler( ActionFactory.COPY.getId(), ( IAction ) connectionActionMap 247 .get( copyConnectionAction ) ); 248 actionBars.setGlobalActionHandler( ActionFactory.PASTE.getId(), ( IAction ) connectionActionMap 249 .get( pasteConnectionAction ) ); 250 actionBars.setGlobalActionHandler( ActionFactory.DELETE.getId(), ( IAction ) connectionActionMap 251 .get( deleteConnectionAction ) ); 252 actionBars.setGlobalActionHandler( ActionFactory.RENAME.getId(), ( IAction ) connectionActionMap 253 .get( renameConnectionAction ) ); 254 actionBars.setGlobalActionHandler( ActionFactory.PROPERTIES.getId(), ( IAction ) connectionActionMap 255 .get( propertyDialogAction ) ); 256 actionBars.updateActionBars(); 257 } 258 else 259 { 260 if ( commandService != null ) 261 { 262 IAction ca = ( IAction ) connectionActionMap.get( copyConnectionAction ); 263 ca.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.copy" ); 264 commandService.getCommand( ca.getActionDefinitionId() ).setHandler( new ActionHandler( ca ) ); 265 266 IAction pa = ( IAction ) connectionActionMap.get( pasteConnectionAction ); 267 pa.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.paste" ); 268 commandService.getCommand( pa.getActionDefinitionId() ).setHandler( new ActionHandler( pa ) ); 269 270 IAction da = ( IAction ) connectionActionMap.get( deleteConnectionAction ); 271 da.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.delete" ); 272 commandService.getCommand( da.getActionDefinitionId() ).setHandler( new ActionHandler( da ) ); 273 274 IAction pda = ( IAction ) connectionActionMap.get( propertyDialogAction ); 275 pda.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.properties" ); 276 commandService.getCommand( pda.getActionDefinitionId() ).setHandler( new ActionHandler( pda ) ); 277 278 } 279 } 280 } 281 282 283 286 public void deactivateGlobalActionHandlers() 287 { 288 ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter( 289 ICommandService.class ); 290 291 if ( actionBars != null ) 292 { 293 actionBars.setGlobalActionHandler( ActionFactory.COPY.getId(), null ); 294 actionBars.setGlobalActionHandler( ActionFactory.PASTE.getId(), null ); 295 actionBars.setGlobalActionHandler( ActionFactory.DELETE.getId(), null ); 296 actionBars.setGlobalActionHandler( ActionFactory.RENAME.getId(), null ); 297 actionBars.setGlobalActionHandler( ActionFactory.PROPERTIES.getId(), null ); 298 actionBars.updateActionBars(); 299 } 300 else 301 { 302 if ( commandService != null ) 303 { 304 IAction ca = ( IAction ) connectionActionMap.get( copyConnectionAction ); 305 commandService.getCommand( ca.getActionDefinitionId() ).setHandler( null ); 306 307 IAction pa = ( IAction ) connectionActionMap.get( pasteConnectionAction ); 308 commandService.getCommand( pa.getActionDefinitionId() ).setHandler( null ); 309 310 IAction da = ( IAction ) connectionActionMap.get( deleteConnectionAction ); 311 commandService.getCommand( da.getActionDefinitionId() ).setHandler( null ); 312 313 IAction pda = ( IAction ) connectionActionMap.get( propertyDialogAction ); 314 commandService.getCommand( pda.getActionDefinitionId() ).setHandler( null ); 315 316 } 317 } 318 } 319 320 } 321 | Popular Tags |