KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.CharConversionException JavaDoc;
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 public class ExtractInterfaceAction extends SelectionDispatchAction {
50
51     private JavaEditor fEditor;
52     
53     /**
54      * Note: This constructor is for internal use only. Clients should not call this constructor.
55      * @param editor the java editor
56      */

57     public ExtractInterfaceAction(JavaEditor editor) {
58         this(editor.getEditorSite());
59         fEditor= editor;
60         setEnabled(SelectionConverter.canOperateOn(fEditor));
61     }
62
63     /**
64      * Creates a new <code>ExtractInterfaceAction</code>. The action requires
65      * that the selection provided by the site's selection provider is of type <code>
66      * org.eclipse.jface.viewers.IStructuredSelection</code>.
67      *
68      * @param site the site providing context information for this action
69      */

70     public ExtractInterfaceAction(IWorkbenchSite site) {
71         super(site);
72         setText(RefactoringMessages.ExtractInterfaceAction_Extract_Interface);
73         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.EXTRACT_INTERFACE_ACTION);
74     }
75     
76     //---- structured selection -------------------------------------------
77

78     /*
79      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
80      */

81     public void selectionChanged(IStructuredSelection selection) {
82         try {
83             setEnabled(RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection));
84         } catch (JavaModelException e) {
85             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
86
if (!(e.getException() instanceof CharConversionException JavaDoc) && JavaModelUtil.isExceptionToBeLogged(e))
87                 JavaPlugin.log(e);
88             setEnabled(false);//no UI - happens on selection changes
89
}
90     }
91
92     /*
93      * @see SelectionDispatchAction#run(IStructuredSelection)
94      */

95     public void run(IStructuredSelection selection) {
96         try {
97             if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection)) {
98                 IType singleSelectedType= RefactoringAvailabilityTester.getSingleSelectedType(selection);
99                 if (! ActionUtil.isEditable(getShell(), singleSelectedType))
100                     return;
101                 RefactoringExecutionStarter.startExtractInterfaceRefactoring(singleSelectedType, getShell());
102             }
103         } catch (JavaModelException e) {
104             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
105         }
106     }
107
108     /*
109      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
110      */

111     public void selectionChanged(ITextSelection selection) {
112         setEnabled(true);
113     }
114     
115     /**
116      * Note: This method is for internal use only. Clients should not call this method.
117      */

118     public void selectionChanged(JavaTextSelection selection) {
119         try {
120             setEnabled(RefactoringAvailabilityTester.isExtractInterfaceAvailable(selection));
121         } catch (JavaModelException e) {
122             setEnabled(false);
123         }
124     }
125     
126     /*
127      * @see SelectionDispatchAction#run(ITextSelection)
128      */

129     public void run(ITextSelection selection) {
130         try {
131             IType type= RefactoringActions.getEnclosingOrPrimaryType(fEditor);
132             if (RefactoringAvailabilityTester.isExtractInterfaceAvailable(type)) {
133                 if (! ActionUtil.isEditable(fEditor, getShell(), type))
134                     return;
135                 RefactoringExecutionStarter.startExtractInterfaceRefactoring(type, getShell());
136             } else {
137                 String JavaDoc unavailable= RefactoringMessages.ExtractInterfaceAction_To_activate;
138                 MessageDialog.openInformation(getShell(), RefactoringMessages.OpenRefactoringWizardAction_unavailable, unavailable);
139             }
140         } catch (JavaModelException e) {
141             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
142         }
143     }
144 }
145
Popular Tags