]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/ui/actions/ExtractInterfaceAction.java
77fabd953c2b080180baf73e3ae352099a48416c
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / ui / actions / ExtractInterfaceAction.java
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  *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
12
13 import java.io.CharConversionException;
14
15 import org.eclipse.jface.dialogs.MessageDialog;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17
18 import org.eclipse.jface.text.ITextSelection;
19
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.eclipse.ui.PlatformUI;
22
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.refactoring.actions.RefactoringActions;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
39
40 /**
41  * Extract a new interface from a class and tries to use the interface instead
42  * of the concrete class where possible.
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  *
47  * @since 2.1
48  *
49  * @noextend This class is not intended to be subclassed by clients.
50  */
51 public class ExtractInterfaceAction extends SelectionDispatchAction {
52
53         private JavaEditor fEditor;
54
55         /**
56          * Note: This constructor is for internal use only. Clients should not call this constructor.
57          * @param editor the java editor
58          *
59          * @noreference This constructor is not intended to be referenced by clients.
60          */
61         public ExtractInterfaceAction(JavaEditor editor) {
62                 this(editor.getEditorSite());
63                 fEditor= editor;
64                 setEnabled(SelectionConverter.canOperateOn(fEditor));
65         }
66
67         /**
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>.
71          *
72          * @param site the site providing context information for this action
73          */
74         public ExtractInterfaceAction(IWorkbenchSite site) {
75                 super(site);
76                 setText(RefactoringMessages.ExtractInterfaceAction_Extract_Interface);
77                 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_INTERFACE_ACTION);
78         }
79
80         //---- structured selection -------------------------------------------
81
82         /*
83          * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
84          */
85         @Override
86         public void selectionChanged(IStructuredSelection selection) {
87                 try {
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))
92                                 JavaPlugin.log(e);
93                         setEnabled(false);//no UI - happens on selection changes
94                 }
95         }
96
97         /*
98          * @see SelectionDispatchAction#run(IStructuredSelection)
99          */
100         @Override
101         public void run(IStructuredSelection selection) {
102                 try {
103                         if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection)) {
104                                 IType singleSelectedType= RefactoringAvailabilityTester.getSingleSelectedType(selection);
105                                 if (! ActionUtil.isEditable(getShell(), singleSelectedType))
106                                         return;
107                                 RefactoringExecutionStarter.startExtractInterfaceRefactoring(singleSelectedType, getShell());
108                         }
109                 } catch (JavaModelException e) {
110                         ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
111                 }
112         }
113
114     /*
115      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
116      */
117         @Override
118         public void selectionChanged(ITextSelection selection) {
119                 setEnabled(true);
120         }
121
122         /**
123          * Note: This method is for internal use only. Clients should not call this method.
124          * @param selection the Java text selection (internal type)
125          *
126          * @noreference This method is not intended to be referenced by clients.
127          */
128         @Override
129         public void selectionChanged(JavaTextSelection selection) {
130                 try {
131                         setEnabled(RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection));
132                 } catch (JavaModelException e) {
133                         setEnabled(false);
134                 }
135         }
136
137         /*
138      * @see SelectionDispatchAction#run(ITextSelection)
139      */
140         @Override
141         public void run(ITextSelection selection) {
142                 try {
143                         if (!ActionUtil.isEditable(fEditor))
144                                 return;
145                         IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor);
146                         if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(type)) {
147                                 RefactoringExecutionStarter.startExtractInterfaceRefactoring(type, getShell());
148                         } else {
149                                 String unavailable= RefactoringMessages.ExtractInterfaceAction_To_activate;
150                                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, unavailable);
151                         }
152                 } catch (JavaModelException e) {
153                         ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
154                 }
155         }
156 }