KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > CCPActionGroup


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.ui.actions;
12
13 import org.eclipse.swt.dnd.Clipboard;
14
15 import org.eclipse.jface.action.IAction;
16 import org.eclipse.jface.action.IMenuManager;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ISelectionProvider;
19
20 import org.eclipse.ui.IActionBars;
21 import org.eclipse.ui.IViewPart;
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 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
28
29 import org.eclipse.jdt.internal.ui.actions.CopyQualifiedNameAction;
30 import org.eclipse.jdt.internal.ui.refactoring.reorg.CopyToClipboardAction;
31 import org.eclipse.jdt.internal.ui.refactoring.reorg.CutAction;
32 import org.eclipse.jdt.internal.ui.refactoring.reorg.DeleteAction;
33 import org.eclipse.jdt.internal.ui.refactoring.reorg.PasteAction;
34
35 /**
36  * Action group that adds the copy, cut, paste actions to a view part's context
37  * menu and installs handlers for the corresponding global menu actions.
38  *
39  * <p>
40  * This class may be instantiated; it is not intended to be subclassed.
41  * </p>
42  *
43  * @since 2.0
44  */

45 public class CCPActionGroup extends ActionGroup {
46
47     private IWorkbenchSite fSite;
48     private Clipboard fClipboard;
49
50     private SelectionDispatchAction[] fActions;
51
52     private SelectionDispatchAction fDeleteAction;
53     private SelectionDispatchAction fCopyAction;
54     private SelectionDispatchAction fCopyQualifiedNameAction;
55     private SelectionDispatchAction fPasteAction;
56     private SelectionDispatchAction fCutAction;
57     
58     /**
59      * Creates a new <code>CCPActionGroup</code>. The group requires that
60      * the selection provided by 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      */

65     public CCPActionGroup(IViewPart part) {
66         this(part.getSite());
67     }
68     
69     /**
70      * Creates a new <code>CCPActionGroup</code>. The group requires that
71      * the selection provided by the page's selection provider is of type
72      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
73      *
74      * @param page the page that owns this action group
75      */

76     public CCPActionGroup(Page page) {
77         this(page.getSite());
78     }
79
80     private CCPActionGroup(IWorkbenchSite site) {
81         fSite= site;
82         fClipboard= new Clipboard(site.getShell().getDisplay());
83         
84         fPasteAction= new PasteAction(fSite, fClipboard);
85         fPasteAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.PASTE);
86         
87         fCopyAction= new CopyToClipboardAction(fSite, fClipboard);
88         fCopyAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY);
89         
90         fCopyQualifiedNameAction= new CopyQualifiedNameAction(fSite);
91         fCopyQualifiedNameAction.setActionDefinitionId(CopyQualifiedNameAction.ACTION_DEFINITION_ID);
92         
93         fCutAction= new CutAction(fSite, fClipboard);
94         fCutAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.CUT);
95         
96         fDeleteAction= new DeleteAction(fSite);
97         fDeleteAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.DELETE);
98         
99         fActions= new SelectionDispatchAction[] { fCutAction, fCopyAction, fCopyQualifiedNameAction, fPasteAction, fDeleteAction };
100         registerActionsAsSelectionChangeListeners();
101     }
102
103     private void registerActionsAsSelectionChangeListeners() {
104         ISelectionProvider provider = fSite.getSelectionProvider();
105         ISelection selection= provider.getSelection();
106         for (int i= 0; i < fActions.length; i++) {
107             SelectionDispatchAction action= fActions[i];
108             action.update(selection);
109             provider.addSelectionChangedListener(action);
110         }
111     }
112     
113     private void deregisterActionsAsSelectionChangeListeners() {
114         ISelectionProvider provider = fSite.getSelectionProvider();
115         for (int i= 0; i < fActions.length; i++) {
116             provider.removeSelectionChangedListener(fActions[i]);
117         }
118     }
119     
120     
121     /**
122      * Returns the delete action managed by this action group.
123      *
124      * @return the delete action. Returns <code>null</code> if the group
125      * doesn't provide any delete action
126      */

127     public IAction getDeleteAction() {
128         return fDeleteAction;
129     }
130
131     /* (non-Javadoc)
132      * Method declared in ActionGroup
133      */

134     public void fillActionBars(IActionBars actionBars) {
135         super.fillActionBars(actionBars);
136         actionBars.setGlobalActionHandler(ActionFactory.DELETE.getId(), fDeleteAction);
137         actionBars.setGlobalActionHandler(ActionFactory.COPY.getId(), fCopyAction);
138         actionBars.setGlobalActionHandler(CopyQualifiedNameAction.ACTION_HANDLER_ID, fCopyQualifiedNameAction);
139         actionBars.setGlobalActionHandler(ActionFactory.CUT.getId(), fCutAction);
140         actionBars.setGlobalActionHandler(ActionFactory.PASTE.getId(), fPasteAction);
141     }
142     
143     /* (non-Javadoc)
144      * Method declared in ActionGroup
145      */

146     public void fillContextMenu(IMenuManager menu) {
147         super.fillContextMenu(menu);
148         for (int i= 0; i < fActions.length; i++) {
149             SelectionDispatchAction action= fActions[i];
150             if (action == fCutAction && !fCutAction.isEnabled())
151                 continue;
152             menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, action);
153         }
154     }
155     
156     /*
157      * @see ActionGroup#dispose()
158      */

159     public void dispose() {
160         super.dispose();
161         if (fClipboard != null){
162             fClipboard.dispose();
163             fClipboard= null;
164         }
165         deregisterActionsAsSelectionChangeListeners();
166     }
167
168 }
169
Popular Tags