KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > SurroundWithActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.actions;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IMenuListener;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.action.MenuManager;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19
20 import org.eclipse.jface.text.ITextSelection;
21
22 import org.eclipse.ui.IActionBars;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.actions.ActionGroup;
25 import org.eclipse.ui.keys.IBindingService;
26
27 import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
28 import org.eclipse.jdt.ui.actions.JdtActionConstants;
29 import org.eclipse.jdt.ui.actions.SurroundWithTryCatchAction;
30
31 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
32
33 public class SurroundWithActionGroup extends ActionGroup {
34     
35     private CompilationUnitEditor fEditor;
36     private SurroundWithTryCatchAction fSurroundWithTryCatchAction;
37     private final String JavaDoc fGroup;
38     
39     public SurroundWithActionGroup(CompilationUnitEditor editor, String JavaDoc group) {
40         fEditor= editor;
41         fGroup= group;
42         fSurroundWithTryCatchAction= createSurroundWithTryCatchAction(fEditor);
43     }
44     
45     public void fillActionBars(IActionBars actionBar) {
46         actionBar.setGlobalActionHandler(JdtActionConstants.SURROUND_WITH_TRY_CATCH, fSurroundWithTryCatchAction);
47     }
48     
49     /**
50      * The Menu to show when right click on the editor
51      * {@inheritDoc}
52      */

53     public void fillContextMenu(IMenuManager menu) {
54         ISelectionProvider selectionProvider= fEditor.getSelectionProvider();
55         if (selectionProvider == null)
56             return;
57         
58         ISelection selection= selectionProvider.getSelection();
59         if (!(selection instanceof ITextSelection))
60             return;
61         
62         ITextSelection textSelection= (ITextSelection)selection;
63         if (textSelection.getLength() == 0)
64             return;
65         
66         String JavaDoc menuText= ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTemplateSubMenuName;
67                 
68         String JavaDoc shortcutString= getShortcutString();
69         if (shortcutString != null) {
70             menuText= menuText + '\t' + shortcutString;
71         }
72         
73         MenuManager subMenu = new MenuManager(menuText, SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
74         menu.appendToGroup(fGroup, subMenu);
75         subMenu.add(new Action() {});
76         subMenu.addMenuListener(new IMenuListener() {
77             public void menuAboutToShow(IMenuManager manager) {
78                 manager.removeAll();
79                 SurroundWithTemplateMenuAction.fillMenu(manager, fEditor, fSurroundWithTryCatchAction);
80             }
81         });
82     }
83     
84     private String JavaDoc getShortcutString() {
85         IBindingService bindingService= (IBindingService)PlatformUI.getWorkbench().getAdapter(IBindingService.class);
86         if (bindingService == null)
87             return null;
88         return bindingService.getBestActiveBindingFormattedFor(SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
89     }
90     
91     private static SurroundWithTryCatchAction createSurroundWithTryCatchAction(CompilationUnitEditor editor) {
92         SurroundWithTryCatchAction result= new SurroundWithTryCatchAction(editor);
93         result.setText(ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTryCatchActionName);
94         result.setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
95         editor.setAction("SurroundWithTryCatch", result); //$NON-NLS-1$
96
return result;
97     }
98 }
99
Popular Tags