1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
14 import org.eclipse.jface.viewers.IStructuredSelection;
16 import org.eclipse.jface.text.ITextSelection;
18 import org.eclipse.ui.IWorkbenchSite;
19 import org.eclipse.ui.PlatformUI;
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;
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;
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;
40 * Action to convert an anonymous inner class to a nested class.
43 * This class may be instantiated; it is not intended to be subclassed.
48 * @noextend This class is not intended to be subclassed by clients.
50 public class ConvertAnonymousToNestedAction extends SelectionDispatchAction {
52 private final JavaEditor fEditor;
55 * Note: This constructor is for internal use only. Clients should not call this constructor.
56 * @param editor the java editor
58 * @noreference This constructor is not intended to be referenced by clients.
60 public ConvertAnonymousToNestedAction(JavaEditor editor) {
61 super(editor.getEditorSite());
62 setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous);
64 setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
65 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
69 * Creates a new <code>ConvertAnonymousToNestedAction</code>. The action requires
70 * that the selection provided by the site's selection provider is of type
71 * <code>org.eclipse.jface.viewers.IStructuredSelection</code>.
73 * @param site the site providing context information for this action
75 public ConvertAnonymousToNestedAction(IWorkbenchSite site) {
78 setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous);
79 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
82 //---- Structured selection -----------------------------------------------------
85 * Method declared on SelectionDispatchAction
88 public void selectionChanged(IStructuredSelection selection) {
90 setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection));
91 } catch (JavaModelException e) {
92 if (JavaModelUtil.isExceptionToBeLogged(e))
99 * Method declared on SelectionDispatchAction
102 public void run(IStructuredSelection selection) {
103 IType type= getElement(selection);
108 range= type.getNameRange();
109 run(type.getCompilationUnit(), range.getOffset(), range.getLength());
110 } catch (JavaModelException e) {
111 ExceptionHandler.handle(e, RefactoringMessages.ConvertAnonymousToNestedAction_dialog_title, RefactoringMessages.NewTextRefactoringAction_exception);
115 private IType getElement(IStructuredSelection selection) {
116 if (selection.size() != 1)
118 Object element= selection.getFirstElement();
119 if (!(element instanceof IType))
121 IType type= (IType)element;
123 if (type.isAnonymous())
125 } catch (JavaModelException e) {
131 //---- Text selection -----------------------------------------------------------
134 * Method declared on SelectionDispatchAction
137 public void run(ITextSelection selection) {
138 run(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength());
142 * Method declared on SelectionDispatchAction
145 public void selectionChanged(ITextSelection selection) {
146 setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
150 * Note: This method is for internal use only. Clients should not call this method.
151 * @param selection the Java text selection (internal type)
153 * @noreference This method is not intended to be referenced by clients.
156 public void selectionChanged(JavaTextSelection selection) {
158 setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection));
159 } catch (JavaModelException e) {
164 //---- helpers -------------------------------------------------------------------
166 private void run(ICompilationUnit unit, int offset, int length) {
167 if (!ActionUtil.isEditable(fEditor, getShell(), unit))
169 RefactoringExecutionStarter.startConvertAnonymousRefactoring(unit, offset, length, getShell());