KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.CharConversionException JavaDoc;
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 import org.eclipse.ui.PlatformUI;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IType;
25 import org.eclipse.jdt.core.JavaModelException;
26
27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
29 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
30 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
31
32 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
33 import org.eclipse.jdt.internal.ui.JavaPlugin;
34 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
35 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
36 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
38 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
39 import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions;
40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
41
42 /**
43  * Tries to use a super type of a class where possible.
44  *
45  * <p>
46  * This class may be instantiated; it is not intended to be subclassed.
47  * </p>
48  *
49  * @since 2.1
50  */

51 // Note: The disclaimer about instantiating and subclassing got added in 3.1.
52
// Don't make this class final or remove a constructor!
53
public class UseSupertypeAction extends SelectionDispatchAction{
54     
55     private JavaEditor fEditor;
56     
57     /**
58      * Note: This constructor is for internal use only. Clients should not call this constructor.
59      * @param editor the java editor
60      */

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

74     public UseSupertypeAction(IWorkbenchSite site) {
75         super(site);
76         setText(RefactoringMessages.UseSupertypeAction_use_Supertype);
77         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.USE_SUPERTYPE_ACTION);
78     }
79     
80     //---- structured selection ---------------------------------------------------
81

82     /*
83      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
84      */

85     public void selectionChanged(IStructuredSelection selection) {
86         try {
87             setEnabled(RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection));
88         } catch (JavaModelException e) {
89             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
90
if (!(e.getException() instanceof CharConversionException JavaDoc) && JavaModelUtil.isExceptionToBeLogged(e))
91                 JavaPlugin.log(e);
92             setEnabled(false);// no UI - happens on selection changes
93
}
94     }
95
96     /*
97      * @see SelectionDispatchAction#run(IStructuredSelection)
98      */

99     public void run(IStructuredSelection selection) {
100         try {
101             if (RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection)) {
102                 IType singleSelectedType= getSingleSelectedType(selection);
103                 if (! ActionUtil.isEditable(getShell(), singleSelectedType))
104                     return;
105                 RefactoringExecutionStarter.startUseSupertypeRefactoring(singleSelectedType, getShell());
106             }
107         } catch (JavaModelException e) {
108             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
109         }
110     }
111
112     private static IType getSingleSelectedType(IStructuredSelection selection) throws JavaModelException{
113         if (selection.isEmpty() || selection.size() != 1)
114             return null;
115         
116         Object JavaDoc first= selection.getFirstElement();
117         if (first instanceof IType)
118             return (IType)first;
119         if (first instanceof ICompilationUnit)
120             return JavaElementUtil.getMainType((ICompilationUnit)first);
121         return null;
122     }
123     
124     //---- text selection ------------------------------------------------------
125

126     /*
127      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
128      */

129     public void selectionChanged(ITextSelection selection) {
130         setEnabled(true);
131     }
132     
133     /**
134      * Note: This method is for internal use only. Clients should not call this method.
135      */

136     public void selectionChanged(JavaTextSelection selection) {
137         try {
138             setEnabled(RefactoringAvailabilityTester.isUseSuperTypeAvailable(selection));
139         } catch (JavaModelException e) {
140             setEnabled(false);
141         }
142     }
143     
144     /*
145      * @see SelectionDispatchAction#run(ITextSelection)
146      */

147     public void run(ITextSelection selection) {
148         try {
149             IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor);
150             if (RefactoringAvailabilityTester.isUseSuperTypeAvailable(type)) {
151                 if (! ActionUtil.isEditable(fEditor, getShell(), type))
152                     return;
153                 RefactoringExecutionStarter.startUseSupertypeRefactoring(type, getShell());
154             } else {
155                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.UseSupertypeAction_to_activate);
156             }
157         } catch (JavaModelException e) {
158             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
159         }
160     }
161 }
162
Popular Tags