KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > views > navigator > RefactorActionGroup


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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  * Sebastian Davids <sdavids@gmx.de> - Images for menu items (27481)
11  *******************************************************************************/

12 package org.eclipse.ui.views.navigator;
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.jface.viewers.TreeViewer;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.dnd.Clipboard;
20 import org.eclipse.swt.events.KeyEvent;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IActionBars;
23 import org.eclipse.ui.ISharedImages;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.actions.ActionFactory;
26 import org.eclipse.ui.actions.DeleteResourceAction;
27 import org.eclipse.ui.actions.TextActionHandler;
28
29 /**
30  * This is the action group for refactor actions,
31  * including global action handlers for copy, paste and delete.
32  *
33  * @since 2.0
34  */

35 public class RefactorActionGroup extends ResourceNavigatorActionGroup {
36
37     private Clipboard clipboard;
38
39     private CopyAction copyAction;
40
41     private DeleteResourceAction deleteAction;
42
43     private PasteAction pasteAction;
44
45     private ResourceNavigatorRenameAction renameAction;
46
47     private ResourceNavigatorMoveAction moveAction;
48
49     private TextActionHandler textActionHandler;
50
51     public RefactorActionGroup(IResourceNavigator navigator) {
52         super(navigator);
53     }
54
55     public void dispose() {
56         if (clipboard != null) {
57             clipboard.dispose();
58             clipboard = null;
59         }
60         super.dispose();
61     }
62
63     public void fillContextMenu(IMenuManager menu) {
64         IStructuredSelection selection = (IStructuredSelection) getContext()
65                 .getSelection();
66
67         boolean anyResourceSelected = !selection.isEmpty()
68                 && ResourceSelectionUtil.allResourcesAreOfType(selection,
69                         IResource.PROJECT | IResource.FOLDER | IResource.FILE);
70
71         copyAction.selectionChanged(selection);
72         menu.add(copyAction);
73         pasteAction.selectionChanged(selection);
74         menu.add(pasteAction);
75
76         if (anyResourceSelected) {
77             deleteAction.selectionChanged(selection);
78             menu.add(deleteAction);
79             moveAction.selectionChanged(selection);
80             menu.add(moveAction);
81             renameAction.selectionChanged(selection);
82             menu.add(renameAction);
83         }
84     }
85
86     public void fillActionBars(IActionBars actionBars) {
87         textActionHandler = new TextActionHandler(actionBars); // hooks handlers
88
textActionHandler.setCopyAction(copyAction);
89         textActionHandler.setPasteAction(pasteAction);
90         textActionHandler.setDeleteAction(deleteAction);
91         renameAction.setTextActionHandler(textActionHandler);
92
93         actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(),
94                 moveAction);
95         actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(),
96                 renameAction);
97     }
98
99     /**
100      * Handles a key pressed event by invoking the appropriate action.
101      */

102     public void handleKeyPressed(KeyEvent event) {
103         if (event.character == SWT.DEL && event.stateMask == 0) {
104             if (deleteAction.isEnabled()) {
105                 deleteAction.run();
106             }
107
108             // Swallow the event.
109
event.doit = false;
110
111         } else if (event.keyCode == SWT.F2 && event.stateMask == 0) {
112             if (renameAction.isEnabled()) {
113                 renameAction.run();
114             }
115
116             // Swallow the event.
117
event.doit = false;
118         }
119     }
120
121     protected void makeActions() {
122         TreeViewer treeViewer = navigator.getViewer();
123         Shell shell = navigator.getSite().getShell();
124         clipboard = new Clipboard(shell.getDisplay());
125
126         pasteAction = new PasteAction(shell, clipboard);
127         ISharedImages images = PlatformUI.getWorkbench().getSharedImages();
128         pasteAction.setDisabledImageDescriptor(images
129                 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));
130         pasteAction.setImageDescriptor(images
131                 .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
132
133         copyAction = new CopyAction(shell, clipboard, pasteAction);
134         copyAction.setDisabledImageDescriptor(images
135                 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
136         copyAction.setImageDescriptor(images
137                 .getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
138
139         moveAction = new ResourceNavigatorMoveAction(shell, treeViewer);
140         renameAction = new ResourceNavigatorRenameAction(shell, treeViewer);
141
142         deleteAction = new DeleteResourceAction(shell);
143         deleteAction.setDisabledImageDescriptor(images
144                 .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
145         deleteAction.setImageDescriptor(images
146                 .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
147     }
148
149     public void updateActionBars() {
150         IStructuredSelection selection = (IStructuredSelection) getContext()
151                 .getSelection();
152
153         copyAction.selectionChanged(selection);
154         pasteAction.selectionChanged(selection);
155         deleteAction.selectionChanged(selection);
156         moveAction.selectionChanged(selection);
157         renameAction.selectionChanged(selection);
158     }
159
160 }
161
Popular Tags