| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2000, 2007 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 | *******************************************************************************/ |
| 11 | package org.eclipse.ui.views.navigator; |
| 12 | |
| 13 | import org.eclipse.core.resources.IResource; |
| 14 | import org.eclipse.core.resources.IWorkspaceRoot; |
| 15 | import org.eclipse.core.runtime.IPath; |
| 16 | import org.eclipse.jface.viewers.StructuredSelection; |
| 17 | import org.eclipse.jface.viewers.TreeViewer; |
| 18 | import org.eclipse.swt.SWT; |
| 19 | import org.eclipse.swt.events.KeyEvent; |
| 20 | import org.eclipse.swt.widgets.Shell; |
| 21 | import org.eclipse.ui.PlatformUI; |
| 22 | import org.eclipse.ui.actions.RenameResourceAction; |
| 23 | |
| 24 | /** |
| 25 | * The ResourceNavigatorRenameAction is the rename action used by the |
| 26 | * ResourceNavigator that also allows updating after rename. |
| 27 | * @since 2.0 |
| 28 | */ |
| 29 | public class ResourceNavigatorRenameAction extends RenameResourceAction { |
| 30 | private TreeViewer viewer; |
| 31 | |
| 32 | /** |
| 33 | * Create a ResourceNavigatorRenameAction and use the tree of the supplied viewer |
| 34 | * for editing. |
| 35 | * @param shell Shell |
| 36 | * @param treeViewer TreeViewer |
| 37 | */ |
| 38 | public ResourceNavigatorRenameAction(Shell shell, TreeViewer treeViewer) { |
| 39 | super(shell, treeViewer.getTree()); |
| 40 | PlatformUI.getWorkbench().getHelpSystem().setHelp(this, |
| 41 | INavigatorHelpContextIds.RESOURCE_NAVIGATOR_RENAME_ACTION); |
| 42 | this.viewer = treeViewer; |
| 43 | } |
| 44 | |
| 45 | /* (non-Javadoc) |
| 46 | * Run the action to completion using the supplied path. |
| 47 | */ |
| 48 | protected void runWithNewPath(IPath path, IResource resource) { |
| 49 | IWorkspaceRoot root = resource.getProject().getWorkspace().getRoot(); |
| 50 | super.runWithNewPath(path, resource); |
| 51 | if (this.viewer != null) { |
| 52 | IResource newResource = root.findMember(path); |
| 53 | if (newResource != null) { |
| 54 | this.viewer.setSelection(new StructuredSelection(newResource), |
| 55 | true); |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Handle the key release |
| 62 | */ |
| 63 | public void handleKeyReleased(KeyEvent event) { |
| 64 | if (event.keyCode == SWT.F2 && event.stateMask == 0 && isEnabled()) { |
| 65 | run(); |
| 66 | } |
| 67 | } |
| 68 | } |