blob: e4c772dff991904d0a9b63c2e0c592ee6babfed1 [file] [log] [blame]
nitindb0f7b262004-11-23 19:12:23 +00001/*******************************************************************************
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 *******************************************************************************/
11package org.eclipse.jst.jsp.ui.internal.java.refactoring;
12
13import java.util.ResourceBundle;
14
15import org.eclipse.core.runtime.CoreException;
16import org.eclipse.jdt.core.IJavaElement;
17import org.eclipse.jdt.core.IMethod;
18import org.eclipse.jdt.core.IPackageFragment;
19import org.eclipse.jdt.core.IType;
20import org.eclipse.jdt.ui.refactoring.RenameSupport;
david_williams455e2172004-12-08 23:26:56 +000021import org.eclipse.jst.jsp.ui.internal.JSPUIPlugin;
nitindb0f7b262004-11-23 19:12:23 +000022import org.eclipse.jst.jsp.ui.internal.Logger;
nitindb0f7b262004-11-23 19:12:23 +000023import org.eclipse.ui.PlatformUI;
24import org.eclipse.ui.texteditor.ITextEditor;
25import org.eclipse.ui.texteditor.TextEditorAction;
26import org.eclipse.wst.sse.ui.util.PlatformStatusLineUtil;
27
28/**
29 * A TextEditorAction that launches JDT rename element wizard
30 *
31 * @author pavery
32 */
33public 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;
nitindcafa6752005-02-07 23:06:07 +000056 if (getTextEditor() != null) {
57 IJavaElement[] elements = JSPJavaSelectionProvider.getSelection(getTextEditor());
58 if (elements.length == 1)
nitindb0f7b262004-11-23 19:12:23 +000059 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_williams455e2172004-12-08 23:26:56 +000090 PlatformStatusLineUtil.displayErrorMessage(JSPUIPlugin.getResourceString("%JSPRenameElementAction.0")); //$NON-NLS-1$
nitindb0f7b262004-11-23 19:12:23 +000091 PlatformStatusLineUtil.addOneTimeClearListener();
92 }
93 }
94}