KickJava   Java API By Example, From Geeks To Geeks.

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


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.entryeditor;
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.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 /**
56  * The EntryEditorWidgetActionGroup manages all actions of the entry editor widget.
57  *
58  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
59  * @version $Rev$, $Date$
60  */

61 public class EntryEditorWidgetActionGroup
62 {
63
64     /** The open sort dialog action. */
65     protected OpenSortDialogAction openSortDialogAction;
66
67     /** The show raw values action. */
68     protected ShowRawValuesAction showRawValuesAction;
69
70     /** The show quick filter action. */
71     protected ShowQuickFilterAction showQuickFilterAction;
72
73     /** The open default editor action. */
74     protected EntryEditorActionProxy openDefaultValueEditorActionProxy;
75
76     /** The open best editor action. */
77     protected EntryEditorActionProxy openBestValueEditorActionProxy;
78
79     /** The open editor actions. */
80     protected EntryEditorActionProxy[] openValueEditorActionProxies;
81
82     /** The open value editor preferences action. */
83     protected ValueEditorPreferencesAction openValueEditorPreferencesAction;
84
85     /** The Constant newValueAction. */
86     protected final static String JavaDoc newValueAction = "newValueAction";
87
88     /** The Constant copyAction. */
89     protected final static String JavaDoc copyAction = "copyAction";
90
91     /** The Constant pasteAction. */
92     protected final static String JavaDoc pasteAction = "pasteAction";
93
94     /** The Constant deleteAction. */
95     protected final static String JavaDoc deleteAction = "deleteAction";
96
97     /** The Constant selectAllAction. */
98     protected final static String JavaDoc selectAllAction = "selectAllAction";
99
100     /** The Constant propertyDialogAction. */
101     protected final static String JavaDoc propertyDialogAction = "propertyDialogAction";
102
103     /** The entry editor action map. */
104     protected Map JavaDoc<String JavaDoc, EntryEditorActionProxy> entryEditorActionMap;
105
106     /** The action bars. */
107     protected IActionBars actionBars;
108
109     /** The main widget. */
110     private EntryEditorWidget mainWidget;
111
112
113     /**
114      * Creates a new instance of EntryEditorWidgetActionGroup.
115      *
116      * @param mainWidget the main widget
117      * @param configuration the configuration
118      */

119     public EntryEditorWidgetActionGroup( EntryEditorWidget mainWidget, EntryEditorWidgetConfiguration configuration )
120     {
121         this.mainWidget = mainWidget;
122
123         entryEditorActionMap = new HashMap JavaDoc<String JavaDoc, 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     /**
156      * Disposes this action group.
157      */

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 JavaDoc it = entryEditorActionMap.keySet().iterator(); it.hasNext(); )
179             {
180                 String JavaDoc key = ( String JavaDoc ) 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     /**
196      * Enables global action handlers.
197      *
198      * @param actionBars the action bars
199      */

200     public void enableGlobalActionHandlers( IActionBars actionBars )
201     {
202         this.actionBars = actionBars;
203         activateGlobalActionHandlers();
204     }
205
206
207     /**
208      * Fill the tool bar.
209      *
210      * @param toolBarManager the tool bar manager
211      */

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     /**
226      * Fills the menu.
227      *
228      * @param menuManager the menu manager
229      */

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     /**
246      * Fills the context menu. Adds a menu listener to the given menu manager
247      * to fill the context menu whenever it pops up.
248      *
249      * @param menuManager the menu manager
250      */

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     /**
265      * Fills the context menu.
266      *
267      * @param menuManager the menu manager
268      */

269     protected void contextMenuAboutToShow( IMenuManager menuManager )
270     {
271         // new
272
menuManager.add( ( IAction ) entryEditorActionMap.get( newValueAction ) );
273         menuManager.add( new Separator() );
274
275         // copy, paste, delete
276
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         // edit
283
addEditMenu( menuManager );
284         menuManager.add( new Separator() );
285
286         // properties
287
menuManager.add( ( IAction ) entryEditorActionMap.get( propertyDialogAction ) );
288     }
289
290
291     /**
292      * Adds the value editors to the menu.
293      *
294      * @param menuManager the menu manager
295      */

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     /**
321      * Activates global action handlers.
322      */

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 ); // IWorkbenchActionDefinitionIds.FIND_REPLACE
342

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     /**
383      * Deactivates global action handlers.
384      */

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     /**
433      * Gets the open default editor action.
434      *
435      * @return the open default editor action
436      */

437     public OpenDefaultEditorAction getOpenDefaultEditorAction()
438     {
439         return ( OpenDefaultEditorAction ) openDefaultValueEditorActionProxy.getAction();
440     }
441
442 }
443
Popular Tags