KickJava   Java API By Example, From Geeks To Geeks.

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


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.browser;
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.CollapseAllAction;
29 import org.apache.directory.ldapstudio.browser.common.actions.FilterChildrenAction;
30 import org.apache.directory.ldapstudio.browser.common.actions.PropertiesAction;
31 import org.apache.directory.ldapstudio.browser.common.actions.RefreshAction;
32 import org.apache.directory.ldapstudio.browser.common.actions.UnfilterChildrenAction;
33 import org.apache.directory.ldapstudio.browser.common.actions.UpAction;
34 import org.apache.directory.ldapstudio.browser.common.actions.proxy.BrowserViewActionProxy;
35 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
36 import org.eclipse.jface.action.IAction;
37 import org.eclipse.jface.action.IMenuListener;
38 import org.eclipse.jface.action.IMenuManager;
39 import org.eclipse.jface.action.IToolBarManager;
40 import org.eclipse.jface.action.Separator;
41 import org.eclipse.jface.commands.ActionHandler;
42 import org.eclipse.jface.viewers.TreeViewer;
43 import org.eclipse.ui.IActionBars;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.actions.ActionFactory;
46 import org.eclipse.ui.commands.ICommandService;
47
48
49 /**
50  * This class manages all the actions of the browser widget.
51  *
52  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
53  * @version $Rev$, $Date$
54  */

55 public class BrowserActionGroup implements IMenuListener
56 {
57
58     /** The open sort dialog action. */
59     protected OpenSortDialogAction openSortDialogAction;
60
61     /** The collapse all action. */
62     protected CollapseAllAction collapseAllAction;
63
64     /** The Constant upAction. */
65     protected static final String JavaDoc upAction = "upAction";
66
67     /** The Constant refreshAction. */
68     protected static final String JavaDoc refreshAction = "refreshAction";
69
70     /** The Constant filterChildrenAction. */
71     protected static final String JavaDoc filterChildrenAction = "filterChildrenAction";
72
73     /** The Constant unfilterChildrenAction. */
74     protected static final String JavaDoc unfilterChildrenAction = "unfilterChildrenAction";
75
76     /** The Constant propertyDialogAction. */
77     protected static final String JavaDoc propertyDialogAction = "propertyDialogAction";
78
79     /** The browser action map. */
80     protected Map JavaDoc<String JavaDoc, BrowserViewActionProxy> browserActionMap;
81
82     /** The action bars. */
83     protected IActionBars actionBars;
84
85     /** The browser's main widget. */
86     protected BrowserWidget mainWidget;
87
88
89     /**
90      * Creates a new instance of BrowserActionGroup.
91      *
92      * @param mainWidget the browser's main widget
93      * @param configuration the browser's configuration
94      */

95     public BrowserActionGroup( BrowserWidget mainWidget, BrowserConfiguration configuration )
96     {
97         this.mainWidget = mainWidget;
98         this.browserActionMap = new HashMap JavaDoc<String JavaDoc, BrowserViewActionProxy>();
99
100         TreeViewer viewer = mainWidget.getViewer();
101         openSortDialogAction = new OpenSortDialogAction( ( BrowserPreferences ) configuration.getPreferences() );
102         collapseAllAction = new CollapseAllAction( viewer );
103
104         browserActionMap.put( upAction, new BrowserViewActionProxy( viewer, new UpAction( viewer ) ) );
105         browserActionMap.put( refreshAction, new BrowserViewActionProxy( viewer, new RefreshAction() ) );
106         browserActionMap.put( filterChildrenAction, new BrowserViewActionProxy( viewer, new FilterChildrenAction() ) );
107         browserActionMap
108             .put( unfilterChildrenAction, new BrowserViewActionProxy( viewer, new UnfilterChildrenAction() ) );
109         browserActionMap.put( propertyDialogAction, new BrowserViewActionProxy( viewer, new PropertiesAction() ) );
110     }
111
112
113     /**
114      * Disposes this action group.
115      */

116     public void dispose()
117     {
118         if ( mainWidget != null )
119         {
120
121             openSortDialogAction.dispose();
122             openSortDialogAction = null;
123             collapseAllAction.dispose();
124             collapseAllAction = null;
125
126             for ( Iterator JavaDoc it = browserActionMap.keySet().iterator(); it.hasNext(); )
127             {
128                 String JavaDoc key = ( String JavaDoc ) it.next();
129                 BrowserViewActionProxy action = ( BrowserViewActionProxy ) browserActionMap.get( key );
130                 action.dispose();
131                 action = null;
132                 it.remove();
133             }
134             browserActionMap.clear();
135             browserActionMap = null;
136
137             actionBars = null;
138             mainWidget = null;
139         }
140     }
141
142
143     /**
144      * Enables the action handlers.
145      *
146      * @param actionBars the action bars
147      */

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

160     public void fillToolBar( IToolBarManager toolBarManager )
161     {
162         toolBarManager.add( ( IAction ) browserActionMap.get( upAction ) );
163         toolBarManager.add( new Separator() );
164         toolBarManager.add( collapseAllAction );
165         toolBarManager.add( ( IAction ) browserActionMap.get( refreshAction ) );
166         toolBarManager.update( true );
167     }
168
169
170     /**
171      * Fills the local menu.
172      *
173      * @param menuManager the local menu manager
174      */

175     public void fillMenu( IMenuManager menuManager )
176     {
177         menuManager.add( openSortDialogAction );
178         menuManager.add( new Separator() );
179         menuManager.update( true );
180     }
181
182
183     /**
184      * Fills the context menu.
185      *
186      * @param menuManager the context menu manager
187      */

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

200     public void menuAboutToShow( IMenuManager menuManager )
201     {
202         // up
203
menuManager.add( ( IAction ) browserActionMap.get( upAction ) );
204         menuManager.add( new Separator() );
205
206         // filter
207
menuManager.add( ( IAction ) browserActionMap.get( filterChildrenAction ) );
208         if ( ( ( IAction ) browserActionMap.get( unfilterChildrenAction ) ).isEnabled() )
209         {
210             menuManager.add( ( IAction ) browserActionMap.get( unfilterChildrenAction ) );
211         }
212         menuManager.add( new Separator() );
213
214         // refresh
215
menuManager.add( ( IAction ) browserActionMap.get( refreshAction ) );
216         menuManager.add( new Separator() );
217
218         // properties
219
menuManager.add( ( IAction ) browserActionMap.get( propertyDialogAction ) );
220     }
221
222
223     /**
224      * Activates the action handlers.
225      */

226     public void activateGlobalActionHandlers()
227     {
228         ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter(
229             ICommandService.class );
230
231         if ( actionBars != null )
232         {
233             actionBars.setGlobalActionHandler( ActionFactory.REFRESH.getId(), ( IAction ) browserActionMap
234                 .get( refreshAction ) );
235             actionBars.setGlobalActionHandler( ActionFactory.PROPERTIES.getId(), ( IAction ) browserActionMap
236                 .get( propertyDialogAction ) );
237             actionBars.updateActionBars();
238         }
239         else
240         {
241             if ( commandService != null )
242             {
243                 IAction pda = ( IAction ) browserActionMap.get( propertyDialogAction );
244                 pda.setActionDefinitionId( "org.apache.directory.ldapstudio.browser.action.properties" );
245                 commandService.getCommand( pda.getActionDefinitionId() ).setHandler( new ActionHandler( pda ) );
246
247                 IAction ra = ( IAction ) browserActionMap.get( refreshAction );
248                 commandService.getCommand( ra.getActionDefinitionId() ).setHandler( new ActionHandler( ra ) );
249             }
250         }
251
252         if ( commandService != null )
253         {
254             IAction ua = ( IAction ) browserActionMap.get( upAction );
255             commandService.getCommand( ua.getActionDefinitionId() ).setHandler( new ActionHandler( ua ) );
256         }
257     }
258
259
260     /**
261      * Deactivates the action handlers.
262      */

263     public void deactivateGlobalActionHandlers()
264     {
265
266         ICommandService commandService = ( ICommandService ) PlatformUI.getWorkbench().getAdapter(
267             ICommandService.class );
268
269         if ( actionBars != null )
270         {
271             actionBars.setGlobalActionHandler( ActionFactory.REFRESH.getId(), null );
272             actionBars.setGlobalActionHandler( ActionFactory.PROPERTIES.getId(), null );
273             actionBars.updateActionBars();
274         }
275         else
276         {
277             if ( commandService != null )
278             {
279                 IAction pda = ( IAction ) browserActionMap.get( propertyDialogAction );
280                 commandService.getCommand( pda.getActionDefinitionId() ).setHandler( null );
281
282                 IAction ra = ( IAction ) browserActionMap.get( refreshAction );
283                 commandService.getCommand( ra.getActionDefinitionId() ).setHandler( null );
284             }
285         }
286
287         if ( commandService != null )
288         {
289             IAction ua = ( IAction ) browserActionMap.get( upAction );
290             commandService.getCommand( ua.getActionDefinitionId() ).setHandler( null );
291         }
292
293     }
294
295
296     /**
297      * Sets the connection input to all actions
298      *
299      * @param connection the connection
300      */

301     public void setInput( IConnection connection )
302     {
303         for ( Iterator JavaDoc it = browserActionMap.values().iterator(); it.hasNext(); )
304         {
305             BrowserViewActionProxy action = ( BrowserViewActionProxy ) it.next();
306             action.inputChanged( connection );
307         }
308     }
309
310 }
311
Popular Tags