KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > ui > actions > ChangeTypeAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.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 /**
45  * Action to generalize the type of a local or field declaration or the
46  * return type of a method declaration.
47  *
48  * <p>
49  * This class may be instantiated; it is not intended to be subclassed.
50  * </p>
51  *
52  * @since 3.0
53  */

54 public class ChangeTypeAction extends SelectionDispatchAction {
55     
56     private JavaEditor fEditor;
57
58     /**
59      * Note: This constructor is for internal use only. Clients should not call
60      * this constructor.
61      * @param editor the java editor
62      */

63     public ChangeTypeAction(JavaEditor editor) {
64         this(editor.getEditorSite());
65         fEditor= editor;
66         setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
67     }
68
69     /**
70      * Creates a new <code>ChangeTypeAction</code>. The action requires that
71      * the selection provided by the site's selection provider is of type
72      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
73      *
74      * @param site the site providing context information for this action
75      */

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     //---- structured selection ---------------------------------------------
85

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 JavaDoc element= selection.getFirstElement();
113         if (!(element instanceof IMember))
114             return null;
115         
116         if (element instanceof IMethod) {
117             IMethod method= (IMethod)element;
118             String JavaDoc 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     //---- text selection ------------------------------------------------------------
129

130     /*
131      * (non-Javadoc) Method declared on SelectionDispatchAction
132      */

133     public void selectionChanged(ITextSelection selection) {
134         setEnabled(true);
135     }
136
137     /**
138      * Note: This method is for internal use only. Clients should not call this method.
139      * @param selection the java text selection
140      */

141     public void selectionChanged(JavaTextSelection selection) {
142         try {
143             setEnabled(RefactoringAvailabilityTester.isGeneralizeTypeAvailable(selection));
144         } catch (JavaModelException e) {
145             setEnabled(false);
146         }
147     }
148
149     /*
150      * (non-Javadoc) Method declared on SelectionDispatchAction
151      */

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