KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.jface.viewers.IStructuredSelection;
15
16 import org.eclipse.jface.text.ITextSelection;
17
18 import org.eclipse.ui.IWorkbenchSite;
19 import org.eclipse.ui.PlatformUI;
20
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.ISourceRange;
23 import org.eclipse.jdt.core.IType;
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.internal.ui.IJavaHelpContextIds;
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
33 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
34 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
35 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
36 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
37 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
38
39 /**
40  * Action to convert an anonymous inner class to a nested class.
41  *
42  * <p>
43  * This class may be instantiated; it is not intended to be subclassed.
44  * </p>
45  *
46  * @since 2.1
47  */

48 public class ConvertAnonymousToNestedAction extends SelectionDispatchAction {
49
50     private final JavaEditor fEditor;
51     
52     /**
53      * Note: This constructor is for internal use only. Clients should not call this constructor.
54      * @param editor the java editor
55      */

56     public ConvertAnonymousToNestedAction(JavaEditor editor) {
57         super(editor.getEditorSite());
58         setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous);
59         fEditor= editor;
60         setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
61         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
62     }
63
64     /**
65      * Creates a new <code>ConvertAnonymousToNestedAction</code>. The action requires
66      * that the selection provided by the site's selection provider is of type
67      * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
68      *
69      * @param site the site providing context information for this action
70      */

71     public ConvertAnonymousToNestedAction(IWorkbenchSite site) {
72         super(site);
73         fEditor= null;
74         setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous);
75         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
76     }
77     
78     //---- Structured selection -----------------------------------------------------
79

80     /* (non-Javadoc)
81      * Method declared on SelectionDispatchAction
82      */

83     public void selectionChanged(IStructuredSelection selection) {
84         try {
85             setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection));
86         } catch (JavaModelException e) {
87             if (JavaModelUtil.isExceptionToBeLogged(e))
88                 JavaPlugin.log(e);
89             setEnabled(false);
90         }
91     }
92     
93     /* (non-Javadoc)
94      * Method declared on SelectionDispatchAction
95      */

96     public void run(IStructuredSelection selection) {
97         IType type= getElement(selection);
98         if (type == null)
99             return;
100         ISourceRange range;
101         try {
102             range= type.getNameRange();
103             run(type.getCompilationUnit(), range.getOffset(), range.getLength());
104         } catch (JavaModelException e) {
105             ExceptionHandler.handle(e, RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title, RefactoringMessages.NewTextRefactoringAction_exception);
106         }
107     }
108
109     private IType getElement(IStructuredSelection selection) {
110         if (selection.size() != 1)
111             return null;
112         Object JavaDoc element= selection.getFirstElement();
113         if (!(element instanceof IType))
114             return null;
115         IType type= (IType)element;
116         try {
117             if (type.isAnonymous())
118                 return type;
119         } catch (JavaModelException e) {
120             // fall through
121
}
122         return null;
123     }
124     
125     //---- Text selection -----------------------------------------------------------
126

127     /* (non-Javadoc)
128      * Method declared on SelectionDispatchAction
129      */

130     public void run(ITextSelection selection) {
131         try{
132             run(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength());
133         } catch (JavaModelException e){
134             ExceptionHandler.handle(e, RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title, RefactoringMessages.NewTextRefactoringAction_exception);
135         }
136     }
137
138     /* (non-Javadoc)
139      * Method declared on SelectionDispatchAction
140      */

141     public void selectionChanged(ITextSelection selection) {
142         setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
143     }
144
145     /**
146      * Note: This method is for internal use only. Clients should not call this method.
147      */

148     public void selectionChanged(JavaTextSelection selection) {
149         try {
150             setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection));
151         } catch (JavaModelException e) {
152             setEnabled(false);
153         }
154     }
155
156     //---- helpers -------------------------------------------------------------------
157

158     private void run(ICompilationUnit unit, int offset, int length) throws JavaModelException {
159         if (!ActionUtil.isEditable(fEditor, getShell(), unit))
160             return;
161         RefactoringExecutionStarter.startConvertAnonymousRefactoring(unit, offset, length, getShell());
162     }
163 }
164
Popular Tags