001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import org.eclipse.jface.action.Action;
013: import org.eclipse.jface.action.IMenuListener;
014: import org.eclipse.jface.action.IMenuManager;
015: import org.eclipse.jface.action.MenuManager;
016: import org.eclipse.jface.viewers.ISelection;
017: import org.eclipse.jface.viewers.ISelectionProvider;
018:
019: import org.eclipse.jface.text.ITextSelection;
020:
021: import org.eclipse.ui.IActionBars;
022: import org.eclipse.ui.PlatformUI;
023: import org.eclipse.ui.actions.ActionGroup;
024: import org.eclipse.ui.keys.IBindingService;
025:
026: import org.eclipse.jdt.ui.actions.IJavaEditorActionDefinitionIds;
027: import org.eclipse.jdt.ui.actions.JdtActionConstants;
028: import org.eclipse.jdt.ui.actions.SurroundWithTryCatchAction;
029:
030: import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
031:
032: public class SurroundWithActionGroup extends ActionGroup {
033:
034: private CompilationUnitEditor fEditor;
035: private SurroundWithTryCatchAction fSurroundWithTryCatchAction;
036: private final String fGroup;
037:
038: public SurroundWithActionGroup(CompilationUnitEditor editor,
039: String group) {
040: fEditor = editor;
041: fGroup = group;
042: fSurroundWithTryCatchAction = createSurroundWithTryCatchAction(fEditor);
043: }
044:
045: public void fillActionBars(IActionBars actionBar) {
046: actionBar.setGlobalActionHandler(
047: JdtActionConstants.SURROUND_WITH_TRY_CATCH,
048: fSurroundWithTryCatchAction);
049: }
050:
051: /**
052: * The Menu to show when right click on the editor
053: * {@inheritDoc}
054: */
055: public void fillContextMenu(IMenuManager menu) {
056: ISelectionProvider selectionProvider = fEditor
057: .getSelectionProvider();
058: if (selectionProvider == null)
059: return;
060:
061: ISelection selection = selectionProvider.getSelection();
062: if (!(selection instanceof ITextSelection))
063: return;
064:
065: ITextSelection textSelection = (ITextSelection) selection;
066: if (textSelection.getLength() == 0)
067: return;
068:
069: String menuText = ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTemplateSubMenuName;
070:
071: String shortcutString = getShortcutString();
072: if (shortcutString != null) {
073: menuText = menuText + '\t' + shortcutString;
074: }
075:
076: MenuManager subMenu = new MenuManager(
077: menuText,
078: SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
079: menu.appendToGroup(fGroup, subMenu);
080: subMenu.add(new Action() {
081: });
082: subMenu.addMenuListener(new IMenuListener() {
083: public void menuAboutToShow(IMenuManager manager) {
084: manager.removeAll();
085: SurroundWithTemplateMenuAction.fillMenu(manager,
086: fEditor, fSurroundWithTryCatchAction);
087: }
088: });
089: }
090:
091: private String getShortcutString() {
092: IBindingService bindingService = (IBindingService) PlatformUI
093: .getWorkbench().getAdapter(IBindingService.class);
094: if (bindingService == null)
095: return null;
096: return bindingService
097: .getBestActiveBindingFormattedFor(SurroundWithTemplateMenuAction.SURROUND_WITH_QUICK_MENU_ACTION_ID);
098: }
099:
100: private static SurroundWithTryCatchAction createSurroundWithTryCatchAction(
101: CompilationUnitEditor editor) {
102: SurroundWithTryCatchAction result = new SurroundWithTryCatchAction(
103: editor);
104: result
105: .setText(ActionMessages.SurroundWithTemplateMenuAction_SurroundWithTryCatchActionName);
106: result
107: .setActionDefinitionId(IJavaEditorActionDefinitionIds.SURROUND_WITH_TRY_CATCH);
108: editor.setAction("SurroundWithTryCatch", result); //$NON-NLS-1$
109: return result;
110: }
111: }
|