1 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 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 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 updateActionBars(); 77 78 actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(), 79 moveAction); 80 actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(), 81 renameAction); 82 } 83 84 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 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 |