nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2004 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.jst.jsp.ui.internal.java.refactoring; |
| 12 | |
| 13 | import java.util.ResourceBundle; |
| 14 | |
| 15 | import org.eclipse.core.runtime.CoreException; |
| 16 | import org.eclipse.jdt.core.IJavaElement; |
| 17 | import org.eclipse.jdt.core.IMethod; |
| 18 | import org.eclipse.jdt.core.IPackageFragment; |
| 19 | import org.eclipse.jdt.core.IType; |
| 20 | import org.eclipse.jdt.ui.refactoring.RenameSupport; |
david_williams | 455e217 | 2004-12-08 23:26:56 +0000 | [diff] [blame] | 21 | import org.eclipse.jst.jsp.ui.internal.JSPUIPlugin; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 22 | import org.eclipse.jst.jsp.ui.internal.Logger; |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 23 | import org.eclipse.ui.PlatformUI; |
| 24 | import org.eclipse.ui.texteditor.ITextEditor; |
| 25 | import org.eclipse.ui.texteditor.TextEditorAction; |
| 26 | import org.eclipse.wst.sse.ui.util.PlatformStatusLineUtil; |
| 27 | |
| 28 | /** |
| 29 | * A TextEditorAction that launches JDT rename element wizard |
| 30 | * |
| 31 | * @author pavery |
| 32 | */ |
| 33 | public class JSPRenameElementAction extends TextEditorAction { |
| 34 | |
| 35 | public JSPRenameElementAction(ResourceBundle bundle, String prefix, ITextEditor editor) { |
| 36 | |
| 37 | super(bundle, prefix, editor); |
| 38 | } |
| 39 | |
| 40 | public boolean isEnabled() { |
| 41 | // always enabled, just print appropriate status to window |
| 42 | // if for some reason the action can't run (like multiple java elements selected) |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * @see org.eclipse.ui.texteditor.TextEditorAction#update() |
| 48 | */ |
| 49 | public void update() { |
| 50 | super.update(); |
| 51 | PlatformStatusLineUtil.clearStatusLine(); |
| 52 | } |
| 53 | |
| 54 | private IJavaElement getSelectedElement() { |
| 55 | IJavaElement element = null; |
nitind | cafa675 | 2005-02-07 23:06:07 +0000 | [diff] [blame] | 56 | if (getTextEditor() != null) { |
| 57 | IJavaElement[] elements = JSPJavaSelectionProvider.getSelection(getTextEditor()); |
| 58 | if (elements.length == 1) |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 59 | element = elements[0]; |
| 60 | } |
| 61 | return element; |
| 62 | } |
| 63 | |
| 64 | public void run() { |
| 65 | IJavaElement element = getSelectedElement(); |
| 66 | if(element != null) { |
| 67 | RenameSupport renameSupport = null; |
| 68 | try { |
| 69 | switch(element.getElementType()) { |
| 70 | case IJavaElement.TYPE: |
| 71 | renameSupport= RenameSupport.create((IType)element, element.getElementName(), RenameSupport.UPDATE_REFERENCES); |
| 72 | break; |
| 73 | case IJavaElement.METHOD: |
| 74 | renameSupport= RenameSupport.create((IMethod)element, element.getElementName(), RenameSupport.UPDATE_REFERENCES); |
| 75 | break; |
| 76 | case IJavaElement.PACKAGE_FRAGMENT: |
| 77 | renameSupport= RenameSupport.create((IPackageFragment)element, element.getElementName(), RenameSupport.UPDATE_REFERENCES); |
| 78 | break; |
| 79 | } |
| 80 | if(renameSupport != null) { |
| 81 | renameSupport.openDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); |
| 82 | PlatformStatusLineUtil.clearStatusLine(); |
| 83 | } |
| 84 | } |
| 85 | catch (CoreException e) { |
| 86 | Logger.logException(e); |
| 87 | } |
| 88 | } |
| 89 | else { |
david_williams | 455e217 | 2004-12-08 23:26:56 +0000 | [diff] [blame] | 90 | PlatformStatusLineUtil.displayErrorMessage(JSPUIPlugin.getResourceString("%JSPRenameElementAction.0")); //$NON-NLS-1$ |
nitind | b0f7b26 | 2004-11-23 19:12:23 +0000 | [diff] [blame] | 91 | PlatformStatusLineUtil.addOneTimeClearListener(); |
| 92 | } |
| 93 | } |
| 94 | } |