001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.actions;
011:
012: import org.eclipse.core.runtime.CoreException;
013:
014: import org.eclipse.jface.dialogs.MessageDialog;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016:
017: import org.eclipse.jface.text.ITextSelection;
018:
019: import org.eclipse.ui.IWorkbenchSite;
020:
021: import org.eclipse.jdt.core.IJavaElement;
022: import org.eclipse.jdt.core.IMethod;
023: import org.eclipse.jdt.core.JavaModelException;
024:
025: import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
026: import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
027: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
028:
029: import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
030:
031: import org.eclipse.jdt.internal.ui.JavaPlugin;
032: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
033: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
034: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
035:
036: public class IntroduceParameterObjectAction extends
037: SelectionDispatchAction {
038:
039: private JavaEditor fEditor;
040:
041: /**
042: * Note: This constructor is for internal use only. Clients should not call this constructor.
043: * @param editor the compilation unit editor
044: */
045: public IntroduceParameterObjectAction(JavaEditor editor) {
046: this (editor.getEditorSite());
047: fEditor = editor;
048: setEnabled(true);
049: }
050:
051: /**
052: * Creates a new <code>IntroduceIndirectionAction</code>.
053: *
054: * @param site the site providing context information for this action
055: */
056: public IntroduceParameterObjectAction(IWorkbenchSite site) {
057: super (site);
058: setText(ActionMessages.IntroduceParameterObjectAction_action_text);
059: setToolTipText(ActionMessages.IntroduceParameterObjectAction_action_tooltip);
060: setDescription(ActionMessages.IntroduceParameterObjectAction_action_description);
061: }
062:
063: //---- structured selection --------------------------------------------------
064:
065: /*
066: * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
067: */
068: public void selectionChanged(IStructuredSelection selection) {
069: try {
070: setEnabled(RefactoringAvailabilityTester
071: .isIntroduceParameterObjectAvailable(selection));
072: } catch (JavaModelException e) {
073: if (JavaModelUtil.isExceptionToBeLogged(e))
074: JavaPlugin.log(e);
075: }
076: }
077:
078: /*
079: * @see SelectionDispatchAction#selectionChanged(ITextSelection)
080: */
081: public void selectionChanged(ITextSelection selection) {
082: setEnabled(true);
083: }
084:
085: /* (non-Javadoc)
086: * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection)
087: */
088: public void selectionChanged(JavaTextSelection selection) {
089: try {
090: setEnabled(RefactoringAvailabilityTester
091: .isIntroduceParameterObjectAvailable(selection));
092: } catch (JavaModelException e) {
093: if (JavaModelUtil.isExceptionToBeLogged(e))
094: JavaPlugin.log(e);
095: setEnabled(false);
096: }
097: }
098:
099: /*
100: * @see SelectionDispatchAction#run(IStructuredSelection)
101: */
102: public void run(IStructuredSelection selection) {
103: try {
104: run(getSingleSelectedMethod(selection));
105: } catch (CoreException e) {
106: ExceptionHandler
107: .handle(
108: e,
109: getShell(),
110: ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title,
111: ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
112: }
113: }
114:
115: /*
116: * (non-Javadoc) Method declared on SelectionDispatchAction
117: */
118: public void run(ITextSelection selection) {
119: try {
120: run(getSingleSelectedMethod(selection));
121: } catch (CoreException e) {
122: ExceptionHandler
123: .handle(
124: e,
125: getShell(),
126: ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title,
127: ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
128: }
129: }
130:
131: public void run(JavaTextSelection selection) {
132: try {
133: IJavaElement[] elements = selection
134: .resolveElementAtOffset();
135: if (elements.length != 1)
136: return;
137:
138: if (!(elements[0] instanceof IMethod))
139: return;
140:
141: run((IMethod) elements[0]);
142: } catch (CoreException e) {
143: ExceptionHandler
144: .handle(
145: e,
146: getShell(),
147: ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title,
148: ActionMessages.IntroduceParameterObjectAction_unexpected_exception);
149: }
150: }
151:
152: private void run(IMethod method) throws CoreException {
153: if (method == null) {
154: MessageDialog
155: .openError(
156: getShell(),
157: ActionMessages.IntroduceParameterObjectAction_exceptiondialog_title,
158: ActionMessages.IntroduceParameterObjectAction_can_not_run_refactoring_message);
159: } else if (fEditor == null || ActionUtil.isEditable(fEditor)) {
160: RefactoringExecutionStarter.startIntroduceParameterObject(
161: method, getShell());
162: }
163: }
164:
165: private static IMethod getSingleSelectedMethod(
166: IStructuredSelection selection) {
167: if (selection.size() != 1)
168: return null;
169:
170: Object element = selection.getFirstElement();
171: if (!(element instanceof IMethod))
172: return null;
173:
174: return (IMethod) element;
175: }
176:
177: private IMethod getSingleSelectedMethod(ITextSelection selection)
178: throws JavaModelException {
179: // - when caret/selection on method name (call or declaration) -> that method
180: // - otherwise: caret position's enclosing method declaration
181: // - when caret inside argument list of method declaration -> enclosing method declaration
182: // - when caret inside argument list of method call -> enclosing method declaration (and NOT method call)
183: IJavaElement[] elements = SelectionConverter
184: .codeResolve(fEditor);
185: if (elements.length > 1)
186: return null;
187:
188: if (elements.length == 1 && elements[0] instanceof IMethod) {
189: return (IMethod) elements[0];
190: } else {
191: IJavaElement elementAt = SelectionConverter
192: .getInputAsCompilationUnit(fEditor).getElementAt(
193: selection.getOffset());
194: if (!(elementAt instanceof IMethod))
195: return null;
196:
197: return (IMethod) elementAt;
198: }
199: }
200: }
|