KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > IntroduceParameterObjectAction


1 /*******************************************************************************
2  * Copyright (c) 2007 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  *******************************************************************************/

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 JavaDoc ACTION_ID= "org.eclipse.jdt.ui.actions.IntroduceParameterObject"; //TODO Place in JdtActionConstants //$NON-NLS-1$
39

40     public static final String JavaDoc ACTION_DEFINITION_ID= "org.eclipse.jdt.ui.refactoring.introduceparamobject"; //TODO Place in IJavaEditorActionDefinitionIds //$NON-NLS-1$
41

42     private JavaEditor fEditor;
43
44     /**
45      * Note: This constructor is for internal use only. Clients should not call this constructor.
46      * @param editor the compilation unit editor
47      */

48     public IntroduceParameterObjectAction(JavaEditor editor) {
49         this(editor.getEditorSite());
50         fEditor= editor;
51         setEnabled(true);
52     }
53
54     /**
55      * Creates a new <code>IntroduceIndirectionAction</code>.
56      *
57      * @param site the site providing context information for this action
58      */

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         //PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INTRODUCE_INDIRECTION_ACTION);
65
}
66
67     //---- structured selection --------------------------------------------------
68

69     /*
70      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
71      */

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     /*
82      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
83      */

84     public void selectionChanged(ITextSelection selection) {
85         setEnabled(true);
86     }
87
88     /* (non-Javadoc)
89      * @see org.eclipse.jdt.ui.actions.SelectionDispatchAction#selectionChanged(org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection)
90      */

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     /*
102      * @see SelectionDispatchAction#run(IStructuredSelection)
103      */

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     /*
113      * (non-Javadoc) Method declared on SelectionDispatchAction
114      */

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 JavaDoc 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         // - when caret/selection on method name (call or declaration) -> that method
159
// - otherwise: caret position's enclosing method declaration
160
// - when caret inside argument list of method declaration -> enclosing method declaration
161
// - when caret inside argument list of method call -> enclosing method declaration (and NOT method call)
162
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