1 11 package org.eclipse.jdt.internal.ui.actions; 12 13 import org.eclipse.core.runtime.CoreException; 14 15 import org.eclipse.jface.dialogs.MessageDialog; 16 import org.eclipse.jface.viewers.IStructuredSelection; 17 18 import org.eclipse.jface.text.ITextSelection; 19 20 import org.eclipse.ui.IWorkbenchSite; 21 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.IMethod; 24 import org.eclipse.jdt.core.JavaModelException; 25 26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; 27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter; 28 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 29 30 import org.eclipse.jdt.ui.actions.SelectionDispatchAction; 31 32 import org.eclipse.jdt.internal.ui.JavaPlugin; 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.util.ExceptionHandler; 36 37 public class IntroduceParameterObjectAction extends SelectionDispatchAction { 38 public static final String ACTION_ID= "org.eclipse.jdt.ui.actions.IntroduceParameterObject"; 40 public static final String ACTION_DEFINITION_ID= "org.eclipse.jdt.ui.refactoring.introduceparamobject"; 42 private JavaEditor fEditor; 43 44 48 public IntroduceParameterObjectAction(JavaEditor editor) { 49 this(editor.getEditorSite()); 50 fEditor= editor; 51 setEnabled(true); 52 } 53 54 59 public IntroduceParameterObjectAction(IWorkbenchSite site) { 60 super(site); 61 setText(ActionMessages.IntroduceParameterObjectAction_action_text); 62 setToolTipText(ActionMessages.IntroduceParameterObjectAction_action_tooltip); 63 setDescription(ActionMessages.IntroduceParameterObjectAction_action_description); 64 } 66 67 69 72 public void selectionChanged(IStructuredSelection selection) { 73 try { 74 setEnabled(RefactoringAvailabilityTester.isIntroduceParameterObjectAvailable(selection)); 75 } catch (JavaModelException e) { 76 if (JavaModelUtil.isExceptionToBeLogged(e)) 77 JavaPlugin.log(e); 78 } 79 } 80 81 84 public void selectionChanged(ITextSelection selection) { 85 setEnabled(true); 86 } 87 88 91 public void selectionChanged(JavaTextSelection selection) { 92 try { 93 setEnabled(RefactoringAvailabilityTester.isIntroduceParameterObjectAvailable(selection)); 94 } catch (JavaModelException e) { 95 if (JavaModelUtil.isExceptionToBeLogged(e)) 96 JavaPlugin.log(e); 97 setEnabled(false); 98 } 99 } 100 101 104 public void run(IStructuredSelection selection) { 105 try { 106 run(getSingleSelectedMethod(selection)); 107 } catch (CoreException e) { 108 ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_unexpected_exception); 109 } 110 } 111 112 115 public void run(ITextSelection selection) { 116 try { 117 run(getSingleSelectedMethod(selection)); 118 } catch (CoreException e) { 119 ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_unexpected_exception); 120 } 121 } 122 123 public void run(JavaTextSelection selection) { 124 try { 125 IJavaElement[] elements= selection.resolveElementAtOffset(); 126 if (elements.length != 1) 127 return; 128 129 if (!(elements[0] instanceof IMethod)) 130 return; 131 132 run((IMethod) elements[0]); 133 } catch (CoreException e) { 134 ExceptionHandler.handle(e, getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_unexpected_exception); 135 } 136 } 137 138 private void run(IMethod method) throws CoreException { 139 if (method == null) { 140 MessageDialog.openError(getShell(), ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title, ActionMessages.IntroduceParameterObjectAction_can_not_run_refactoring_message); 141 } else if (ActionUtil.isEditable(fEditor)) { 142 RefactoringExecutionStarter.startIntroduceParameterObject(method, getShell()); 143 } 144 } 145 146 private static IMethod getSingleSelectedMethod(IStructuredSelection selection) { 147 if (selection.size() != 1) 148 return null; 149 150 Object element= selection.getFirstElement(); 151 if (!(element instanceof IMethod)) 152 return null; 153 154 return (IMethod)element; 155 } 156 157 private IMethod getSingleSelectedMethod(ITextSelection selection) throws JavaModelException { 158 IJavaElement[] elements= SelectionConverter.codeResolve(fEditor); 163 if (elements.length > 1) 164 return null; 165 166 if (elements.length == 1 && elements[0] instanceof IMethod) { 167 return (IMethod) elements[0]; 168 } else { 169 IJavaElement elementAt= SelectionConverter.getInputAsCompilationUnit(fEditor).getElementAt(selection.getOffset()); 170 if (!(elementAt instanceof IMethod)) 171 return null; 172 173 return (IMethod) elementAt; 174 } 175 } 176 } 177 | Popular Tags |