KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > navigator > resources > actions > EditActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2006 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
12 package org.eclipse.ui.internal.navigator.resources.actions;
13
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.dnd.Clipboard;
19 import org.eclipse.swt.events.KeyEvent;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.IActionBars;
22 import org.eclipse.ui.ISharedImages;
23 import org.eclipse.ui.PlatformUI;
24 import org.eclipse.ui.actions.ActionGroup;
25 import org.eclipse.ui.actions.DeleteResourceAction;
26 import org.eclipse.ui.navigator.ICommonMenuConstants;
27 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
28
29 /**
30  * @since 3.2
31  *
32  */

33 public class EditActionGroup extends ActionGroup {
34
35     private Clipboard clipboard;
36
37     private CopyAction copyAction;
38
39     private DeleteResourceAction deleteAction;
40
41     private PasteAction pasteAction;
42
43     private TextActionHandler textActionHandler;
44
45     private Shell shell;
46
47     /**
48      *
49      * @param aShell
50      * @param aTree
51      */

52     public EditActionGroup(Shell aShell) {
53         shell = aShell;
54         makeActions();
55     }
56
57     public void dispose() {
58         if (clipboard != null) {
59             clipboard.dispose();
60             clipboard = null;
61         }
62         super.dispose();
63     }
64
65     public void fillContextMenu(IMenuManager menu) {
66         IStructuredSelection selection = (IStructuredSelection) getContext()
67                 .getSelection();
68
69         boolean anyResourceSelected = !selection.isEmpty()
70                 && ResourceSelectionUtil.allResourcesAreOfType(selection,
71                         IResource.PROJECT | IResource.FOLDER | IResource.FILE);
72
73         copyAction.selectionChanged(selection);
74         menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, copyAction);
75         pasteAction.selectionChanged(selection);
76         //menu.insertAfter(copyAction.getId(), pasteAction);
77
menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, pasteAction);
78
79         if (anyResourceSelected) {
80             deleteAction.selectionChanged(selection);
81             //menu.insertAfter(pasteAction.getId(), deleteAction);
82
menu.appendToGroup(ICommonMenuConstants.GROUP_EDIT, deleteAction);
83         }
84     }
85
86     public void fillActionBars(IActionBars actionBars) {
87
88         if (textActionHandler == null) {
89             textActionHandler = new TextActionHandler(actionBars); // hook handlers
90
}
91         textActionHandler.setCopyAction(copyAction);
92         textActionHandler.setPasteAction(pasteAction);
93         textActionHandler.setDeleteAction(deleteAction);
94         //renameAction.setTextActionHandler(textActionHandler);
95
updateActionBars();
96
97         textActionHandler.updateActionBars();
98     }
99
100     /**
101      * Handles a key pressed event by invoking the appropriate action.
102      *
103      * @param event
104      * The Key Event
105      */

106     public void handleKeyPressed(KeyEvent event) {
107         if (event.character == SWT.DEL && event.stateMask == 0) {
108             if (deleteAction.isEnabled()) {
109                 deleteAction.run();
110             }
111
112             // Swallow the event.
113
event.doit = false;
114
115         }
116     }
117
118     protected void makeActions() {
119         clipboard = new Clipboard(shell.getDisplay());
120
121         pasteAction = new PasteAction(shell, clipboard);
122         ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
123         pasteAction.setDisabledImageDescriptor(images
124                 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));
125         pasteAction.setImageDescriptor(images
126                 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
127         pasteAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.PASTE);
128
129         copyAction = new CopyAction(shell, clipboard, pasteAction);
130         copyAction.setDisabledImageDescriptor(images
131                 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
132         copyAction.setImageDescriptor(images
133                 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
134         copyAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.COPY);
135  
136         deleteAction = new DeleteResourceAction(shell);
137         deleteAction.setDisabledImageDescriptor(images
138                 .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
139         deleteAction.setImageDescriptor(images
140                 .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
141         deleteAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.DELETE);
142     }
143
144     public void updateActionBars() {
145         IStructuredSelection selection = (IStructuredSelection) getContext()
146                 .getSelection();
147
148         copyAction.selectionChanged(selection);
149         pasteAction.selectionChanged(selection);
150         deleteAction.selectionChanged(selection);
151     }
152 }
153
Popular Tags