1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.core.runtime.CoreException; 14 15 import org.eclipse.jface.viewers.IStructuredSelection; 16 17 import org.eclipse.jface.text.ITextSelection; 18 19 import org.eclipse.ui.IWorkbenchSite; 20 import org.eclipse.ui.PlatformUI; 21 22 import org.eclipse.jdt.core.IField; 23 import org.eclipse.jdt.core.IMember; 24 import org.eclipse.jdt.core.IMethod; 25 import org.eclipse.jdt.core.ISourceRange; 26 import org.eclipse.jdt.core.JavaModelException; 27 import org.eclipse.jdt.core.Signature; 28 import org.eclipse.jdt.core.dom.PrimitiveType; 29 30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 32 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 33 import org.eclipse.jdt.internal.corext.util.JdtFlags; 34 35 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 36 import org.eclipse.jdt.internal.ui.JavaPlugin; 37 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 38 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 39 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 40 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 41 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 42 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 43 44 54 public class ChangeTypeAction extends SelectionDispatchAction { 55 56 private JavaEditor fEditor; 57 58 63 public ChangeTypeAction(JavaEditor editor) { 64 this(editor.getEditorSite()); 65 fEditor= editor; 66 setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null); 67 } 68 69 76 public ChangeTypeAction(IWorkbenchSite site) { 77 super(site); 78 setText(RefactoringMessages.ChangeTypeAction_label); 79 setToolTipText(RefactoringMessages.ChangeTypeAction_tooltipText); 80 setDescription(RefactoringMessages.ChangeTypeAction_description); 81 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CHANGE_TYPE_ACTION); 82 } 83 84 86 public void selectionChanged(IStructuredSelection selection) { 87 try { 88 setEnabled(RefactoringAvailabilityTester.isGeneralizeTypeAvailable(selection)); 89 } catch (JavaModelException e) { 90 if (JavaModelUtil.isExceptionToBeLogged(e)) 91 JavaPlugin.log(e); 92 setEnabled(false); 93 } 94 } 95 96 public void run(IStructuredSelection selection) { 97 try { 98 IMember member= getMember(selection); 99 if (member == null || !ActionUtil.isEditable(getShell(), member)) 100 return; 101 ISourceRange range= member.getNameRange(); 102 RefactoringExecutionStarter.startChangeTypeRefactoring(member.getCompilationUnit(), getShell(), range.getOffset(), range.getLength()); 103 } catch (CoreException e) { 104 ExceptionHandler.handle(e, RefactoringMessages.ChangeTypeAction_dialog_title, RefactoringMessages.ChangeTypeAction_exception); 105 } 106 } 107 108 private static IMember getMember(IStructuredSelection selection) throws JavaModelException { 109 if (selection.size() != 1) 110 return null; 111 112 Object element= selection.getFirstElement(); 113 if (!(element instanceof IMember)) 114 return null; 115 116 if (element instanceof IMethod) { 117 IMethod method= (IMethod)element; 118 String returnType= method.getReturnType(); 119 if (PrimitiveType.toCode(Signature.toString(returnType)) != null) 120 return null; 121 return method; 122 } else if (element instanceof IField && !JdtFlags.isEnum((IMember) element)) { 123 return (IField)element; 124 } 125 return null; 126 } 127 128 130 133 public void selectionChanged(ITextSelection selection) { 134 setEnabled(true); 135 } 136 137 141 public void selectionChanged(JavaTextSelection selection) { 142 try { 143 setEnabled(RefactoringAvailabilityTester.isGeneralizeTypeAvailable(selection)); 144 } catch (JavaModelException e) { 145 setEnabled(false); 146 } 147 } 148 149 152 public void run(ITextSelection selection) { 153 if (!ActionUtil.isEditable(fEditor)) 154 return; 155 try { 156 RefactoringExecutionStarter.startChangeTypeRefactoring(SelectionConverter.getInputAsCompilationUnit(fEditor), getShell(), selection.getOffset(), selection.getLength()); 157 } catch (CoreException e) { 158 ExceptionHandler.handle(e, RefactoringMessages.ChangeTypeAction_dialog_title, RefactoringMessages.ChangeTypeAction_exception); 159 } 160 } 161 } 162 | Popular Tags |