1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.action.IMenuManager;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.ISelectionProvider;
17 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.ui.IActionBars;
20 import org.eclipse.ui.IViewPart;
21 import org.eclipse.ui.IWorkbenchCommandConstants;
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.eclipse.ui.actions.ActionFactory;
24 import org.eclipse.ui.actions.ActionGroup;
25 import org.eclipse.ui.navigator.ICommonMenuConstants;
26 import org.eclipse.ui.part.Page;
28 import org.eclipse.jdt.internal.ui.actions.CopyQualifiedNameAction;
29 import org.eclipse.jdt.internal.ui.refactoring.reorg.CopyToClipboardAction;
30 import org.eclipse.jdt.internal.ui.refactoring.reorg.CutAction;
31 import org.eclipse.jdt.internal.ui.refactoring.reorg.DeleteAction;
32 import org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction;
35 * Action group that adds copy, cut, paste, and delete actions to a view part's context
36 * menu and installs handlers for the corresponding global menu actions.
39 * This class may be instantiated; it is not intended to be subclassed.
44 * @noextend This class is not intended to be subclassed by clients.
46 public class CCPActionGroup extends ActionGroup {
48 private final SelectionDispatchAction[] fActions;
50 private final SelectionDispatchAction fDeleteAction;
51 private final SelectionDispatchAction fCopyAction;
52 private final SelectionDispatchAction fCopyQualifiedNameAction;
53 private final SelectionDispatchAction fPasteAction;
54 private final SelectionDispatchAction fCutAction;
55 private final ISelectionProvider fSelectionProvider;
59 * Creates a new <code>CCPActionGroup</code>. The group requires that the selection provided by
60 * the view part's selection provider is of type
61 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
63 * @param part the view part that owns this action group
64 * @param includeOnlyCopyActions <code>true</code> if the group only includes the copy actions,
65 * <code>false</code> to include all actions
68 public CCPActionGroup(IViewPart part, boolean includeOnlyCopyActions) {
69 this(part.getSite(), null, includeOnlyCopyActions);
73 * Creates a new <code>CCPActionGroup</code>. The group requires that
74 * the selection provided by the view part's selection provider is of type
75 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
77 * @param part the view part that owns this action group
79 public CCPActionGroup(IViewPart part) {
80 this(part.getSite(), null, false);
84 * Creates a new <code>CCPActionGroup</code>. The group requires that
85 * the selection provided by the page's selection provider is of type
86 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
88 * @param page the page that owns this action group
90 public CCPActionGroup(Page page) {
91 this(page.getSite(), null, false);
95 * Creates a new <code>CCPActionGroup</code>. The group requires
96 * that the selection provided by the given selection provider is of type
97 * {@link IStructuredSelection}.
99 * @param site the site that will own the action group.
100 * @param specialSelectionProvider the selection provider used instead of the
101 * sites selection provider.
105 public CCPActionGroup(IWorkbenchSite site, ISelectionProvider specialSelectionProvider) {
106 this(site, specialSelectionProvider, false);
110 * Creates a new <code>CCPActionGroup</code>. The group requires that the selection provided by
111 * the given selection provider is of type {@link IStructuredSelection}.
113 * @param site the site that will own the action group.
114 * @param specialSelectionProvider the selection provider used instead of the sites selection
116 * @param includeOnlyCopyActions <code>true</code> if the group only included the copy actions,
117 * <code>false</code> otherwise
120 private CCPActionGroup(IWorkbenchSite site, ISelectionProvider specialSelectionProvider, boolean includeOnlyCopyActions) {
121 fSelectionProvider= specialSelectionProvider == null ? site.getSelectionProvider() : specialSelectionProvider;
123 fCopyAction= new CopyToClipboardAction(site);
124 fCopyAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);
126 fCopyQualifiedNameAction= new CopyQualifiedNameAction(site);
127 fCopyQualifiedNameAction.setActionDefinitionId(CopyQualifiedNameAction.ACTION_DEFINITION_ID);
130 if (!includeOnlyCopyActions) {
131 fPasteAction= new PasteAction(site);
132 fPasteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE);
133 fDeleteAction= new DeleteAction(site);
134 fDeleteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_DELETE);
135 fCutAction= new CutAction(site);
136 fCutAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT);
137 fActions= new SelectionDispatchAction[] { fCutAction, fCopyAction, fCopyQualifiedNameAction, fPasteAction, fDeleteAction };
142 fActions= new SelectionDispatchAction[] { fCopyAction, fCopyQualifiedNameAction };
145 if (specialSelectionProvider != null) {
146 for (int i= 0; i < fActions.length; i++) {
147 fActions[i].setSpecialSelectionProvider(specialSelectionProvider);
151 registerActionsAsSelectionChangeListeners();
154 private void registerActionsAsSelectionChangeListeners() {
155 ISelectionProvider provider= fSelectionProvider;
156 ISelection selection= provider.getSelection();
157 for (int i= 0; i < fActions.length; i++) {
158 SelectionDispatchAction action= fActions[i];
159 action.update(selection);
160 provider.addSelectionChangedListener(action);
164 private void deregisterActionsAsSelectionChangeListeners() {
165 ISelectionProvider provider= fSelectionProvider;
166 for (int i= 0; i < fActions.length; i++) {
167 provider.removeSelectionChangedListener(fActions[i]);
173 * Returns the delete action managed by this action group.
175 * @return the delete action. Returns <code>null</code> if the group
176 * doesn't provide any delete action
178 public IAction getDeleteAction() {
179 return fDeleteAction;
183 * Method declared in ActionGroup
186 public void fillActionBars(IActionBars actionBars) {
187 super.fillActionBars(actionBars);
188 if (fDeleteAction != null)
189 actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), fDeleteAction);
190 actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), fCopyAction);
191 actionBars.setGlobalActionHandler(CopyQualifiedNameAction.ACTION_HANDLER_ID, fCopyQualifiedNameAction);
192 if (fCopyAction != null)
193 actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), fCutAction);
194 if (fPasteAction != null)
195 actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), fPasteAction);
199 * Method declared in ActionGroup
202 public void fillContextMenu(IMenuManager menu) {
203 super.fillContextMenu(menu);
204 for (int i= 0; i < fActions.length; i++) {
205 SelectionDispatchAction action= fActions[i];
206 if (action == fCutAction && !fCutAction.isEnabled())
208 menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, action);
213 * @see ActionGroup#dispose()
216 public void dispose() {
218 deregisterActionsAsSelectionChangeListeners();