1 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 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 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.appendToGroup(ICommonMenuConstants.GROUP_EDIT, pasteAction); 78 79 if (anyResourceSelected) { 80 deleteAction.selectionChanged(selection); 81 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); } 91 textActionHandler.setCopyAction(copyAction); 92 textActionHandler.setPasteAction(pasteAction); 93 textActionHandler.setDeleteAction(deleteAction); 94 updateActionBars(); 96 97 textActionHandler.updateActionBars(); 98 } 99 100 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 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 |