]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/actions/CCPActionGroup.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / actions / CCPActionGroup.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
12
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;
18
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;
27
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;
33
34 /**
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.
37  *
38  * <p>
39  * This class may be instantiated; it is not intended to be subclassed.
40  * </p>
41  *
42  * @since 2.0
43  *
44  * @noextend This class is not intended to be subclassed by clients.
45  */
46 public class CCPActionGroup extends ActionGroup {
47
48         private final SelectionDispatchAction[] fActions;
49
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;
56
57
58         /**
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>.
62          * 
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
66          * @since 3.7
67          */
68         public CCPActionGroup(IViewPart part, boolean includeOnlyCopyActions) {
69                 this(part.getSite(), null, includeOnlyCopyActions);
70         }
71
72         /**
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>.
76          *
77          * @param part the view part that owns this action group
78          */
79         public CCPActionGroup(IViewPart  part) {
80                 this(part.getSite(), null, false);
81         }
82
83         /**
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>.
87          *
88          * @param page the page that owns this action group
89          */
90         public CCPActionGroup(Page page) {
91                 this(page.getSite(), null, false);
92         }
93
94         /**
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}.
98          *
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.
102          *
103          * @since 3.4
104          */
105         public CCPActionGroup(IWorkbenchSite site, ISelectionProvider specialSelectionProvider) {
106                 this(site, specialSelectionProvider, false);
107         }
108
109         /**
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}.
112          * 
113          * @param site the site that will own the action group.
114          * @param specialSelectionProvider the selection provider used instead of the sites selection
115          *            provider.
116          * @param includeOnlyCopyActions <code>true</code> if the group only included the copy actions,
117          *            <code>false</code> otherwise
118          * @since 3.7
119          */
120         private CCPActionGroup(IWorkbenchSite site, ISelectionProvider specialSelectionProvider, boolean includeOnlyCopyActions) {
121                 fSelectionProvider= specialSelectionProvider == null ? site.getSelectionProvider() : specialSelectionProvider;
122
123                 fCopyAction= new CopyToClipboardAction(site);
124                 fCopyAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY);
125
126                 fCopyQualifiedNameAction= new CopyQualifiedNameAction(site);
127                 fCopyQualifiedNameAction.setActionDefinitionId(CopyQualifiedNameAction.ACTION_DEFINITION_ID);
128
129
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 };
138                 } else {
139                         fPasteAction= null;
140                         fDeleteAction= null;
141                         fCutAction= null;
142                         fActions= new SelectionDispatchAction[] { fCopyAction, fCopyQualifiedNameAction };
143                 }
144
145                 if (specialSelectionProvider != null) {
146                         for (int i= 0; i < fActions.length; i++) {
147                                 fActions[i].setSpecialSelectionProvider(specialSelectionProvider);
148                         }
149                 }
150
151                 registerActionsAsSelectionChangeListeners();
152         }
153
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);
161                 }
162         }
163
164         private void deregisterActionsAsSelectionChangeListeners() {
165                 ISelectionProvider provider= fSelectionProvider;
166                 for (int i= 0; i < fActions.length; i++) {
167                         provider.removeSelectionChangedListener(fActions[i]);
168                 }
169         }
170
171
172         /**
173          * Returns the delete action managed by this action group.
174          *
175          * @return the delete action. Returns <code>null</code> if the group
176          *      doesn't provide any delete action
177          */
178         public IAction getDeleteAction() {
179                 return fDeleteAction;
180         }
181
182         /* (non-Javadoc)
183          * Method declared in ActionGroup
184          */
185         @Override
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);
196         }
197
198         /* (non-Javadoc)
199          * Method declared in ActionGroup
200          */
201         @Override
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())
207                                 continue;
208                         menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, action);
209                 }
210         }
211
212         /*
213          * @see ActionGroup#dispose()
214          */
215         @Override
216         public void dispose() {
217                 super.dispose();
218                 deregisterActionsAsSelectionChangeListeners();
219         }
220
221 }