1 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 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); 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 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 event.doit = false; 110 111 } else if (event.keyCode == SWT.F2 && event.stateMask == 0) { 112 if (renameAction.isEnabled()) { 113 renameAction.run(); 114 } 115 116 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 |