KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.internal.ui.actions;
12
13 import java.io.CharConversionException JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21
22 import org.eclipse.jface.text.ITextSelection;
23
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.eclipse.ui.PlatformUI;
26
27 import org.eclipse.jdt.core.IJavaElement;
28 import org.eclipse.jdt.core.IMember;
29 import org.eclipse.jdt.core.IType;
30 import org.eclipse.jdt.core.JavaModelException;
31
32 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
33 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
34 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
35
36 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
37
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.JavaPlugin;
40 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
42 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
43 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
44
45 /**
46  * Action to extract a supertype from a class.
47  * <p>
48  * Action is applicable to selections containing elements of type
49  * <code>IType</code> (top-level types only), <code>IField</code> and
50  * <code>IMethod</code>.
51  * </p>
52  * <p>
53  * This class may be instantiated; it is not intended to be subclassed.
54  * </p>
55  *
56  * @since 3.2
57  */

58 public class ExtractSuperClassAction extends SelectionDispatchAction {
59
60     /**
61      * Action definition ID of the refactor -> extract supertype action (value
62      * <code>"org.eclipse.jdt.ui.edit.text.java.extract.superclass"</code>).
63      *
64      * @since 3.2
65      */

66     public static final String JavaDoc EXTRACT_SUPERTYPE= "org.eclipse.jdt.ui.edit.text.java.extract.superclass"; //$NON-NLS-1$
67

68     /**
69      * Refactor menu: name of standard Extract Supertype global action (value
70      * <code>"org.eclipse.jdt.ui.actions.ExtractSuperclass"</code>).
71      *
72      * @since 3.2
73      */

74     public static final String JavaDoc EXTRACT_SUPERTYPES= "org.eclipse.jdt.ui.actions.ExtractSuperclass"; //$NON-NLS-1$
75

76     private static IMember[] getSelectedMembers(final IStructuredSelection selection) {
77         if (selection.isEmpty())
78             return null;
79         if (selection.size() == 1) {
80             try {
81                 final IType type= RefactoringAvailabilityTester.getSingleSelectedType(selection);
82                 if (type != null)
83                     return new IType[] {type};
84             } catch (JavaModelException exception) {
85                 JavaPlugin.log(exception);
86             }
87         }
88         for (final Iterator JavaDoc iterator= selection.iterator(); iterator.hasNext();) {
89             if (!(iterator.next() instanceof IMember))
90                 return null;
91         }
92         final Set JavaDoc set= new HashSet JavaDoc();
93         set.addAll(Arrays.asList(selection.toArray()));
94         return (IMember[]) set.toArray(new IMember[set.size()]);
95     }
96
97     /** The java editor */
98     private JavaEditor fEditor;
99
100     /**
101      * Note: This constructor is for internal use only. Clients should not call
102      * this constructor.
103      *
104      * @param editor
105      * the java editor
106      */

107     public ExtractSuperClassAction(final JavaEditor editor) {
108         this(editor.getEditorSite());
109         fEditor= editor;
110         setEnabled(SelectionConverter.canOperateOn(fEditor));
111     }
112
113     /**
114      * Creates a new extract super type action. The action requires that the
115      * selection provided by the site's selection provider is of type
116      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
117      *
118      * @param site
119      * the workbench site
120      */

121     public ExtractSuperClassAction(final IWorkbenchSite site) {
122         super(site);
123         setText(RefactoringMessages.ExtractSuperTypeAction_label);
124         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_SUPERTYPE_ACTION);
125     }
126
127     private IMember getSelectedMemberFromEditor() throws JavaModelException {
128         final IJavaElement element= SelectionConverter.resolveEnclosingElement(fEditor, (ITextSelection) fEditor.getSelectionProvider().getSelection());
129         if (element == null || !(element instanceof IMember))
130             return null;
131         return (IMember) element;
132     }
133
134     /**
135      * {@inheritDoc}
136      */

137     public void run(final IStructuredSelection selection) {
138         try {
139             final IMember[] members= getSelectedMembers(selection);
140             if (RefactoringAvailabilityTester.isExtractSupertypeAvailable(members) && ActionUtil.isEditable(getShell(), members[0]))
141                 RefactoringExecutionStarter.startExtractSupertypeRefactoring(members, getShell());
142         } catch (final JavaModelException exception) {
143             ExceptionHandler.handle(exception, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
144         }
145     }
146
147     /**
148      * {@inheritDoc}
149      */

150     public void run(final ITextSelection selection) {
151         try {
152             if (! ActionUtil.isEditable(fEditor))
153                 return;
154             final IMember member= getSelectedMemberFromEditor();
155             final IMember[] array= new IMember[] { member};
156             if (member != null && RefactoringAvailabilityTester.isExtractSupertypeAvailable(array)) {
157                 RefactoringExecutionStarter.startExtractSupertypeRefactoring(array, getShell());
158             } else {
159                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ExtractSuperTypeAction_unavailable);
160             }
161         } catch (JavaModelException exception) {
162             ExceptionHandler.handle(exception, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
163         }
164     }
165
166     /**
167      * {@inheritDoc}
168      */

169     public void selectionChanged(final IStructuredSelection selection) {
170         try {
171             setEnabled(RefactoringAvailabilityTester.isExtractSupertypeAvailable(selection));
172         } catch (JavaModelException exception) {
173             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
174
if (!(exception.getException() instanceof CharConversionException JavaDoc) && JavaModelUtil.isExceptionToBeLogged(exception))
175                 JavaPlugin.log(exception);
176             setEnabled(false);//no UI - happens on selection changes
177
}
178     }
179
180     /**
181      * {@inheritDoc}
182      */

183     public void selectionChanged(final ITextSelection selection) {
184         setEnabled(true);
185     }
186
187     /**
188      * {@inheritDoc}
189      */

190     public void selectionChanged(final JavaTextSelection selection) {
191         try {
192             setEnabled(RefactoringAvailabilityTester.isExtractSupertypeAvailable(selection));
193         } catch (JavaModelException event) {
194             setEnabled(false);
195         }
196     }
197 }
198
Popular Tags