KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.io.CharConversionException JavaDoc;
15
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18
19 import org.eclipse.jface.text.ITextSelection;
20
21 import org.eclipse.ui.IWorkbenchSite;
22 import org.eclipse.ui.PlatformUI;
23
24 import org.eclipse.jdt.core.ICompilationUnit;
25 import org.eclipse.jdt.core.IType;
26 import org.eclipse.jdt.core.JavaModelException;
27
28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
29 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
30 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
31 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
32
33 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
34 import org.eclipse.jdt.internal.ui.JavaPlugin;
35 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
36 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
37 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
38 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
39 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
40 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
41
42 /**
43  * Action to convert a nested class to a top level class.
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 public class ConvertNestedToTopAction extends SelectionDispatchAction {
52
53     private JavaEditor fEditor;
54
55     /**
56      * Note: This constructor is for internal use only. Clients should not call
57      * this constructor.
58      * @param editor the java editor
59      */

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

75     public ConvertNestedToTopAction(IWorkbenchSite site) {
76         super(site);
77         setText(RefactoringMessages.ConvertNestedToTopAction_Convert);
78         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.MOVE_INNER_TO_TOP_ACTION);
79     }
80
81     //---- Structured selection ------------------------------------------------
82

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

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

100     public void run(IStructuredSelection selection) {
101         try {
102             //we have to call this here - no selection changed event is sent
103
// after a refactoring but it may still invalidate enablement
104
if (RefactoringAvailabilityTester.isMoveInnerAvailable(selection)) {
105                 IType singleSelectedType= getSingleSelectedType(selection);
106                 if (! ActionUtil.isEditable(getShell(), singleSelectedType))
107                     return;
108                 RefactoringExecutionStarter.startMoveInnerRefactoring(singleSelectedType, getShell());
109             }
110         } catch (JavaModelException e) {
111             ExceptionHandler.handle(e,
112                 RefactoringMessages.OpenRefactoringWizardAction_refactoring,
113                 RefactoringMessages.OpenRefactoringWizardAction_exception);
114         }
115     }
116
117     private static IType getSingleSelectedType(IStructuredSelection selection) throws JavaModelException {
118         if (selection.isEmpty() || selection.size() != 1)
119             return null;
120
121         Object JavaDoc first= selection.getFirstElement();
122         if (first instanceof IType)
123             return (IType)first;
124         if (first instanceof ICompilationUnit)
125             return JavaElementUtil.getMainType((ICompilationUnit)first);
126         return null;
127     }
128
129     //---- Text Selection -------------------------------------------------------
130

131     /*
132      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
133      */

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

141     public void selectionChanged(JavaTextSelection selection) {
142         try {
143             setEnabled(RefactoringAvailabilityTester.isMoveInnerAvailable(selection));
144         } catch (JavaModelException e) {
145             setEnabled(false);
146         }
147     }
148     
149     /*
150      * @see SelectionDispatchAction#run(ITextSelection)
151      */

152     public void run(ITextSelection selection) {
153         try {
154             IType type= RefactoringAvailabilityTester.getDeclaringType(SelectionConverter.resolveEnclosingElement(fEditor, selection));
155             if (type != null && RefactoringAvailabilityTester.isMoveInnerAvailable(type)) {
156                 if (! ActionUtil.isEditable(fEditor, getShell(), type))
157                     return;
158                 RefactoringExecutionStarter.startMoveInnerRefactoring(type, getShell());
159             } else {
160                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, RefactoringMessages.ConvertNestedToTopAction_To_activate);
161             }
162         } catch (JavaModelException e) {
163             ExceptionHandler.handle(e,
164                 RefactoringMessages.OpenRefactoringWizardAction_refactoring,
165                 RefactoringMessages.OpenRefactoringWizardAction_exception);
166         }
167     }
168 }
169
Popular Tags