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;
13 import java.io.CharConversionException;
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.text.ITextSelection;
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.eclipse.ui.PlatformUI;
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.refactoring.actions.RefactoringActions;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
41 * Extract a new interface from a class and tries to use the interface instead
42 * of the concrete class where possible.
44 * This class may be instantiated; it is not intended to be subclassed.
49 * @noextend This class is not intended to be subclassed by clients.
51 public class ExtractInterfaceAction extends SelectionDispatchAction {
53 private JavaEditor fEditor;
56 * Note: This constructor is for internal use only. Clients should not call this constructor.
57 * @param editor the java editor
59 * @noreference This constructor is not intended to be referenced by clients.
61 public ExtractInterfaceAction(JavaEditor editor) {
62 this(editor.getEditorSite());
64 setEnabled(SelectionConverter.canOperateOn(fEditor));
68 * Creates a new <code>ExtractInterfaceAction</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>.
72 * @param site the site providing context information for this action
74 public ExtractInterfaceAction(IWorkbenchSite site) {
76 setText(RefactoringMessages.ExtractInterfaceAction_Extract_Interface);
77 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_INTERFACE_ACTION);
80 //---- structured selection -------------------------------------------
83 * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
86 public void selectionChanged(IStructuredSelection selection) {
88 setEnabled(RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection));
89 } catch (JavaModelException e) {
90 // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
91 if (!(e.getException() instanceof CharConversionException) && JavaModelUtil.isExceptionToBeLogged(e))
93 setEnabled(false);//no UI - happens on selection changes
98 * @see SelectionDispatchAction#run(IStructuredSelection)
101 public void run(IStructuredSelection selection) {
103 if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection)) {
104 IType singleSelectedType= RefactoringAvailabilityTester.getSingleSelectedType(selection);
105 if (! ActionUtil.isEditable(getShell(), singleSelectedType))
107 RefactoringExecutionStarter.startExtractInterfaceRefactoring(singleSelectedType, getShell());
109 } catch (JavaModelException e) {
110 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
115 * @see SelectionDispatchAction#selectionChanged(ITextSelection)
118 public void selectionChanged(ITextSelection selection) {
123 * Note: This method is for internal use only. Clients should not call this method.
124 * @param selection the Java text selection (internal type)
126 * @noreference This method is not intended to be referenced by clients.
129 public void selectionChanged(JavaTextSelection selection) {
131 setEnabled(RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection));
132 } catch (JavaModelException e) {
138 * @see SelectionDispatchAction#run(ITextSelection)
141 public void run(ITextSelection selection) {
143 if (!ActionUtil.isEditable(fEditor))
145 IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor);
146 if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(type)) {
147 RefactoringExecutionStarter.startExtractInterfaceRefactoring(type, getShell());
149 String unavailable= RefactoringMessages.ExtractInterfaceAction_To_activate;
150 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, unavailable);
152 } catch (JavaModelException e) {
153 ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);