]> git.uio.no Git - ifi-stolz-refaktor.git/blame - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/ConvertAnonymousToNestedAction.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / ConvertAnonymousToNestedAction.java
CommitLineData
1b2798f6
EK
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
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11package org.eclipse.jdt.ui.actions;
12
13
14import org.eclipse.jface.viewers.IStructuredSelection;
15
16import org.eclipse.jface.text.ITextSelection;
17
18import org.eclipse.ui.IWorkbenchSite;
19import org.eclipse.ui.PlatformUI;
20
21import org.eclipse.jdt.core.ICompilationUnit;
22import org.eclipse.jdt.core.ISourceRange;
23import org.eclipse.jdt.core.IType;
24import org.eclipse.jdt.core.JavaModelException;
25
26import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
27import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
28import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
29
30import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
31import org.eclipse.jdt.internal.ui.JavaPlugin;
32import org.eclipse.jdt.internal.ui.actions.ActionUtil;
33import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
34import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
35import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
36import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
37import 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 * @noextend This class is not intended to be subclassed by clients.
49 */
50public class ConvertAnonymousToNestedAction extends SelectionDispatchAction {
51
52 private final JavaEditor fEditor;
53
54 /**
55 * Note: This constructor is for internal use only. Clients should not call this constructor.
56 * @param editor the java editor
57 *
58 * @noreference This constructor is not intended to be referenced by clients.
59 */
60 public ConvertAnonymousToNestedAction(JavaEditor editor) {
61 super(editor.getEditorSite());
62 setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous);
63 fEditor= editor;
64 setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
65 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
66 }
67
68 /**
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>.
72 *
73 * @param site the site providing context information for this action
74 */
75 public ConvertAnonymousToNestedAction(IWorkbenchSite site) {
76 super(site);
77 fEditor= null;
78 setText(RefactoringMessages.ConvertAnonymousToNestedAction_Convert_Anonymous);
79 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.CONVERT_ANONYMOUS_TO_NESTED_ACTION);
80 }
81
82 //---- Structured selection -----------------------------------------------------
83
84 /* (non-Javadoc)
85 * Method declared on SelectionDispatchAction
86 */
87 @Override
88 public void selectionChanged(IStructuredSelection selection) {
89 try {
90 setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection));
91 } catch (JavaModelException e) {
92 if (JavaModelUtil.isExceptionToBeLogged(e))
93 JavaPlugin.log(e);
94 setEnabled(false);
95 }
96 }
97
98 /* (non-Javadoc)
99 * Method declared on SelectionDispatchAction
100 */
101 @Override
102 public void run(IStructuredSelection selection) {
103 IType type= getElement(selection);
104 if (type == null)
105 return;
106 ISourceRange range;
107 try {
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);
112 }
113 }
114
115 private IType getElement(IStructuredSelection selection) {
116 if (selection.size() != 1)
117 return null;
118 Object element= selection.getFirstElement();
119 if (!(element instanceof IType))
120 return null;
121 IType type= (IType)element;
122 try {
123 if (type.isAnonymous())
124 return type;
125 } catch (JavaModelException e) {
126 // fall through
127 }
128 return null;
129 }
130
131 //---- Text selection -----------------------------------------------------------
132
133 /* (non-Javadoc)
134 * Method declared on SelectionDispatchAction
135 */
136 @Override
137 public void run(ITextSelection selection) {
138 run(SelectionConverter.getInputAsCompilationUnit(fEditor), selection.getOffset(), selection.getLength());
139 }
140
141 /* (non-Javadoc)
142 * Method declared on SelectionDispatchAction
143 */
144 @Override
145 public void selectionChanged(ITextSelection selection) {
146 setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
147 }
148
149 /**
150 * Note: This method is for internal use only. Clients should not call this method.
151 * @param selection the Java text selection (internal type)
152 *
153 * @noreference This method is not intended to be referenced by clients.
154 */
155 @Override
156 public void selectionChanged(JavaTextSelection selection) {
157 try {
158 setEnabled(RefactoringAvailabilityTester.isConvertAnonymousAvailable(selection));
159 } catch (JavaModelException e) {
160 setEnabled(false);
161 }
162 }
163
164 //---- helpers -------------------------------------------------------------------
165
166 private void run(ICompilationUnit unit, int offset, int length) {
167 if (!ActionUtil.isEditable(fEditor, getShell(), unit))
168 return;
169 RefactoringExecutionStarter.startConvertAnonymousRefactoring(unit, offset, length, getShell());
170 }
171}