KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > widgets > connection > ConnectionActionGroup


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.common.widgets.connection;
22
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
53  * This class manages all the actions of the connection widget.
54  *
55  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
56  * @version $Rev$, $Date$
57  */

58 public class ConnectionActionGroup implements IMenuListener
59 {
60
61     /** The Constant newConnectionAction. */
62     protected static final String JavaDoc newConnectionAction = "newConnectionAction";
63
64     /** The Constant openConnectionAction. */
65     protected static final String JavaDoc openConnectionAction = "openConnectionAction";
66
67     /** The Constant closeConnectionAction. */
68     protected static final String JavaDoc closeConnectionAction = "closeConnectionAction";
69
70     /** The Constant copyConnectionAction. */
71     protected static final String JavaDoc copyConnectionAction = "copyConnectionAction";
72
73     /** The Constant pasteConnectionAction. */
74     protected static final String JavaDoc pasteConnectionAction = "pasteConnectionAction";
75
76     /** The Constant deleteConnectionAction. */
77     protected static final String JavaDoc deleteConnectionAction = "deleteConnectionAction";
78
79     /** The Constant renameConnectionAction. */
80     protected static final String JavaDoc renameConnectionAction = "renameConnectionAction";
81
82     /** The Constant propertyDialogAction. */
83     protected static final String JavaDoc propertyDialogAction = "propertyDialogAction";
84
85     /** The action map. */
86     protected Map JavaDoc<String JavaDoc, ConnectionViewActionProxy> connectionActionMap;
87
88     /** The action bars. */
89     protected IActionBars actionBars;
90
91     /** The connection main widget. */
92     protected ConnectionWidget mainWidget;
93
94
95     /**
96      * Creates a new instance of ConnectionActionGroup.
97      *
98      * @param mainWidget the connection main widget
99      * @param configuration the connection widget configuration
100      */

101     public ConnectionActionGroup( ConnectionWidget mainWidget, ConnectionConfiguration configuration )
102     {
103         this.mainWidget = mainWidget;
104         this.connectionActionMap = new HashMap JavaDoc<String JavaDoc, 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     /**
123      * Disposes this action group.
124      */

125     public void dispose()
126     {
127         if ( mainWidget != null )
128         {
129             for ( Iterator JavaDoc it = connectionActionMap.keySet().iterator(); it.hasNext(); )
130             {
131                 String JavaDoc key = ( String JavaDoc ) 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     /**
147      * Enables the action handlers.
148      *
149      * @param actionBars the action bars
150      */

151     public void enableGlobalActionHandlers( IActionBars actionBars )
152     {
153         this.actionBars = actionBars;
154         activateGlobalActionHandlers();
155     }
156
157
158     /**
159      * Fills the tool bar.
160      *
161      * @param toolBarManager the tool bar manager
162      */

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     /**
175      * Fills the local menu.
176      *
177      * @param menuManager the local menu manager
178      */

179     public void fillMenu( IMenuManager menuManager )
180     {
181         // menuManager.add(this.openSortDialogAction);
182
// menuManager.add(new Separator());
183
// menuManager.update(true);
184
}
185
186
187     /**
188      * Fills the context menu.
189      *
190      * @param menuManager the context menu manager
191      */

192     public void fillContextMenu( IMenuManager menuManager )
193     {
194         menuManager.setRemoveAllWhenShown( true );
195         menuManager.addMenuListener( this );
196     }
197
198
199     /**
200      * {@inheritDoc}
201      *
202      * This implementation fills the context menu.
203      */

204     public void menuAboutToShow( IMenuManager menuManager )
205     {
206         // add
207
menuManager.add( ( IAction ) connectionActionMap.get( newConnectionAction ) );
208         menuManager.add( new Separator() );
209
210         // open/close
211
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         // copy/paste/...
222
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         // additions
229
menuManager.add( new Separator( IWorkbenchActionConstants.MB_ADDITIONS ) );
230
231         // properties
232
menuManager.add( ( IAction ) connectionActionMap.get( propertyDialogAction ) );
233     }
234
235
236     /**
237      * Activates the action handlers.
238      */

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     /**
284      * Deactivates the action handlers.
285      */

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