001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Sebastian Davids <sdavids@gmx.de> - Images for menu items (27481)
011: *******************************************************************************/package org.eclipse.ui.views.navigator;
012:
013: import org.eclipse.core.resources.IResource;
014: import org.eclipse.jface.action.IMenuManager;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016: import org.eclipse.jface.viewers.TreeViewer;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.dnd.Clipboard;
019: import org.eclipse.swt.events.KeyEvent;
020: import org.eclipse.swt.widgets.Shell;
021: import org.eclipse.ui.IActionBars;
022: import org.eclipse.ui.ISharedImages;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.actions.ActionFactory;
025: import org.eclipse.ui.actions.DeleteResourceAction;
026: import org.eclipse.ui.actions.TextActionHandler;
027:
028: /**
029: * This is the action group for refactor actions,
030: * including global action handlers for copy, paste and delete.
031: *
032: * @since 2.0
033: */
034: public class RefactorActionGroup extends ResourceNavigatorActionGroup {
035:
036: private Clipboard clipboard;
037:
038: private CopyAction copyAction;
039:
040: private DeleteResourceAction deleteAction;
041:
042: private PasteAction pasteAction;
043:
044: private ResourceNavigatorRenameAction renameAction;
045:
046: private ResourceNavigatorMoveAction moveAction;
047:
048: private TextActionHandler textActionHandler;
049:
050: public RefactorActionGroup(IResourceNavigator navigator) {
051: super (navigator);
052: }
053:
054: public void dispose() {
055: if (clipboard != null) {
056: clipboard.dispose();
057: clipboard = null;
058: }
059: super .dispose();
060: }
061:
062: public void fillContextMenu(IMenuManager menu) {
063: IStructuredSelection selection = (IStructuredSelection) getContext()
064: .getSelection();
065:
066: boolean anyResourceSelected = !selection.isEmpty()
067: && ResourceSelectionUtil.allResourcesAreOfType(
068: selection, IResource.PROJECT | IResource.FOLDER
069: | IResource.FILE);
070:
071: copyAction.selectionChanged(selection);
072: menu.add(copyAction);
073: pasteAction.selectionChanged(selection);
074: menu.add(pasteAction);
075:
076: if (anyResourceSelected) {
077: deleteAction.selectionChanged(selection);
078: menu.add(deleteAction);
079: moveAction.selectionChanged(selection);
080: menu.add(moveAction);
081: renameAction.selectionChanged(selection);
082: menu.add(renameAction);
083: }
084: }
085:
086: public void fillActionBars(IActionBars actionBars) {
087: textActionHandler = new TextActionHandler(actionBars); // hooks handlers
088: textActionHandler.setCopyAction(copyAction);
089: textActionHandler.setPasteAction(pasteAction);
090: textActionHandler.setDeleteAction(deleteAction);
091: renameAction.setTextActionHandler(textActionHandler);
092:
093: actionBars.setGlobalActionHandler(ActionFactory.MOVE.getId(),
094: moveAction);
095: actionBars.setGlobalActionHandler(ActionFactory.RENAME.getId(),
096: renameAction);
097: }
098:
099: /**
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()
128: .getSharedImages();
129: pasteAction
130: .setDisabledImageDescriptor(images
131: .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE_DISABLED));
132: pasteAction.setImageDescriptor(images
133: .getImageDescriptor(ISharedImages.IMG_TOOL_PASTE));
134:
135: copyAction = new CopyAction(shell, clipboard, pasteAction);
136: copyAction
137: .setDisabledImageDescriptor(images
138: .getImageDescriptor(ISharedImages.IMG_TOOL_COPY_DISABLED));
139: copyAction.setImageDescriptor(images
140: .getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
141:
142: moveAction = new ResourceNavigatorMoveAction(shell, treeViewer);
143: renameAction = new ResourceNavigatorRenameAction(shell,
144: treeViewer);
145:
146: deleteAction = new DeleteResourceAction(shell);
147: deleteAction
148: .setDisabledImageDescriptor(images
149: .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE_DISABLED));
150: deleteAction.setImageDescriptor(images
151: .getImageDescriptor(ISharedImages.IMG_TOOL_DELETE));
152: }
153:
154: public void updateActionBars() {
155: IStructuredSelection selection = (IStructuredSelection) getContext()
156: .getSelection();
157:
158: copyAction.selectionChanged(selection);
159: pasteAction.selectionChanged(selection);
160: deleteAction.selectionChanged(selection);
161: moveAction.selectionChanged(selection);
162: renameAction.selectionChanged(selection);
163: }
164:
165: }
|