KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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  * Sebastian Davids <sdavids@gmx.de> - Images for menu items (27481)
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.events.KeyEvent;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.swt.widgets.Tree;
21 import org.eclipse.ui.IActionBars;
22 import org.eclipse.ui.actions.ActionFactory;
23 import org.eclipse.ui.actions.ActionGroup;
24 import org.eclipse.ui.actions.MoveResourceAction;
25 import org.eclipse.ui.actions.RenameResourceAction;
26 import org.eclipse.ui.navigator.ICommonMenuConstants;
27 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds;
28
29 /**
30  * This is the action group for refactor actions, including global action
31  * handlers for copy, paste and delete.
32  *
33  * @since 2.0
34  */

35 public class RefactorActionGroup extends ActionGroup {
36  
37
38     private RenameResourceAction renameAction;
39
40     private MoveResourceAction moveAction;
41
42     private Shell shell;
43
44     private Tree tree;
45
46     /**
47      *
48      * @param aShell
49      * @param aTree
50      */

51     public RefactorActionGroup(Shell aShell, Tree aTree) {
52         shell = aShell;
53         tree = aTree;
54         makeActions();
55     }
56
57     public void fillContextMenu(IMenuManager menu) {
58         IStructuredSelection selection = (IStructuredSelection) getContext()
59                 .getSelection();
60
61         boolean anyResourceSelected = !selection.isEmpty()
62                 && ResourceSelectionUtil.allResourcesAreOfType(selection,
63                         IResource.PROJECT | IResource.FOLDER | IResource.FILE);
64  
65         if (anyResourceSelected) {
66             moveAction.selectionChanged(selection);
67             menu.appendToGroup(ICommonMenuConstants.GROUP_REORGANIZE, moveAction);
68             renameAction.selectionChanged(selection);
69             menu.insertAfter(moveAction.getId(), renameAction);
70         }
71     }
72
73     public void fillActionBars(IActionBars actionBars) {
74  
75         //renameAction.setTextActionHandler(textActionHandler);
76
updateActionBars();
77
78         actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(),
79                 moveAction);
80         actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(),
81                 renameAction);
82     }
83
84     /**
85      * Handles a key pressed event by invoking the appropriate action.
86      *
87      * @param event
88      * The Key Event
89      */

90     public void handleKeyPressed(KeyEvent event) {
91
92         if (event.keyCode == SWT.F2 && event.stateMask == 0) {
93             if (renameAction.isEnabled()) {
94                 renameAction.run();
95             }
96
97             // Swallow the event.
98
event.doit = false;
99         }
100     }
101
102     protected void makeActions() {
103
104         moveAction = new MoveResourceAction(shell);
105         moveAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.MOVE);
106         
107         renameAction = new RenameResourceAction(shell, tree);
108         renameAction.setActionDefinitionId(IWorkbenchActionDefinitionIds.RENAME);
109     }
110
111     public void updateActionBars() {
112         IStructuredSelection selection = (IStructuredSelection) getContext()
113                 .getSelection();
114  
115         moveAction.selectionChanged(selection);
116         renameAction.selectionChanged(selection);
117     }
118
119 }
120
Popular Tags