1 11 package org.eclipse.jdt.ui.actions; 12 13 import org.eclipse.jface.dialogs.MessageDialog; 14 import org.eclipse.jface.viewers.IStructuredSelection; 15 16 import org.eclipse.jface.text.ITextSelection; 17 18 import org.eclipse.ui.IWorkbenchSite; 19 import org.eclipse.ui.PlatformUI; 20 21 import org.eclipse.jdt.core.IJavaElement; 22 import org.eclipse.jdt.core.IMethod; 23 import org.eclipse.jdt.core.JavaModelException; 24 25 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 27 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 28 29 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 30 import org.eclipse.jdt.internal.ui.JavaPlugin; 31 import org.eclipse.jdt.internal.ui.actions.ActionUtil; 32 import org.eclipse.jdt.internal.ui.actions.SelectionConverter; 33 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor; 34 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection; 35 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 36 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 37 38 51 public class ModifyParametersAction extends SelectionDispatchAction { 52 53 private JavaEditor fEditor; 54 55 59 public ModifyParametersAction(JavaEditor editor) { 60 this(editor.getEditorSite()); 61 fEditor= editor; 62 setEnabled(SelectionConverter.canOperateOn(fEditor)); 63 } 64 65 72 public ModifyParametersAction(IWorkbenchSite site) { 73 super(site); 74 setText(RefactoringMessages.RefactoringGroup_modify_Parameters_label); 75 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MODIFY_PARAMETERS_ACTION); 76 } 77 78 81 public void selectionChanged(IStructuredSelection selection) { 82 try { 83 setEnabled(RefactoringAvailabilityTester.isChangeSignatureAvailable(selection)); 84 } catch (JavaModelException e) { 85 if (JavaModelUtil.isExceptionToBeLogged(e)) 87 JavaPlugin.log(e); 88 setEnabled(false); } 90 } 91 92 95 public void selectionChanged(ITextSelection selection) { 96 setEnabled(true); 97 } 98 99 102 public void selectionChanged(JavaTextSelection selection) { 103 try { 104 setEnabled(RefactoringAvailabilityTester.isChangeSignatureAvailable(selection)); 105 } catch (JavaModelException e) { 106 setEnabled(false); 107 } 108 } 109 110 113 public void run(IStructuredSelection selection) { 114 try { 115 if (RefactoringAvailabilityTester.isChangeSignatureAvailable(selection)) { 117 IMethod method= getSingleSelectedMethod(selection); 118 if (! ActionUtil.isEditable(getShell(), method)) 119 return; 120 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell()); 121 } 122 } catch (JavaModelException e) { 123 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 124 } 125 } 126 127 130 public void run(ITextSelection selection) { 131 try { 132 if (! ActionUtil.isEditable(fEditor)) 133 return; 134 IMethod method= getSingleSelectedMethod(selection); 135 if (RefactoringAvailabilityTester.isChangeSignatureAvailable(method)){ 136 RefactoringExecutionStarter.startChangeSignatureRefactoring(method, this, getShell()); 137 } else { 138 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ModifyParametersAction_unavailable); 139 } 140 } catch (JavaModelException e) { 141 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception); 142 } 143 } 144 145 private static IMethod getSingleSelectedMethod(IStructuredSelection selection){ 146 if (selection.isEmpty() || selection.size() != 1) 147 return null; 148 if (selection.getFirstElement() instanceof IMethod) 149 return (IMethod)selection.getFirstElement(); 150 return null; 151 } 152 153 private IMethod getSingleSelectedMethod(ITextSelection selection) throws JavaModelException{ 154 IJavaElement[] elements= SelectionConverter.codeResolve(fEditor); 159 if (elements.length > 1) 160 return null; 161 if (elements.length == 1 && elements[0] instanceof IMethod) 162 return (IMethod)elements[0]; 163 IJavaElement elementAt= SelectionConverter.getInputAsCompilationUnit(fEditor).getElementAt(selection.getOffset()); 164 if (elementAt instanceof IMethod) 165 return (IMethod)elementAt; 166 return null; 167 } 168 } 169 | Popular Tags |