001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.ui.actions;
011:
012: import java.io.CharConversionException;
013:
014: import org.eclipse.jface.dialogs.MessageDialog;
015: import org.eclipse.jface.viewers.IStructuredSelection;
016:
017: import org.eclipse.jface.text.ITextSelection;
018:
019: import org.eclipse.ui.IWorkbenchSite;
020: import org.eclipse.ui.PlatformUI;
021:
022: import org.eclipse.jdt.core.IType;
023: import org.eclipse.jdt.core.JavaModelException;
024:
025: import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
026: import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
027: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
028:
029: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
030: import org.eclipse.jdt.internal.ui.JavaPlugin;
031: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
032: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
033: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
034: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
035: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
036: import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions;
037: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
038:
039: /**
040: * Extract a new interface from a class and tries to use the interface instead
041: * of the concrete class where possible.
042: * <p>
043: * This class may be instantiated; it is not intended to be subclassed.
044: * </p>
045: *
046: * @since 2.1
047: */
048: public class ExtractInterfaceAction extends SelectionDispatchAction {
049:
050: private JavaEditor fEditor;
051:
052: /**
053: * Note: This constructor is for internal use only. Clients should not call this constructor.
054: * @param editor the java editor
055: */
056: public ExtractInterfaceAction(JavaEditor editor) {
057: this (editor.getEditorSite());
058: fEditor = editor;
059: setEnabled(SelectionConverter.canOperateOn(fEditor));
060: }
061:
062: /**
063: * Creates a new <code>ExtractInterfaceAction</code>. The action requires
064: * that the selection provided by the site's selection provider is of type <code>
065: * org.eclipse.jface.viewers.IStructuredSelection</code>.
066: *
067: * @param site the site providing context information for this action
068: */
069: public ExtractInterfaceAction(IWorkbenchSite site) {
070: super (site);
071: setText(RefactoringMessages.ExtractInterfaceAction_Extract_Interface);
072: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
073: IJavaHelpContextIds.EXTRACT_INTERFACE_ACTION);
074: }
075:
076: //---- structured selection -------------------------------------------
077:
078: /*
079: * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
080: */
081: public void selectionChanged(IStructuredSelection selection) {
082: try {
083: setEnabled(RefactoringAvailabilityTester
084: .isExtractInterfaceAvailable(selection));
085: } catch (JavaModelException e) {
086: // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
087: if (!(e.getException() instanceof CharConversionException)
088: && JavaModelUtil.isExceptionToBeLogged(e))
089: JavaPlugin.log(e);
090: setEnabled(false);//no UI - happens on selection changes
091: }
092: }
093:
094: /*
095: * @see SelectionDispatchAction#run(IStructuredSelection)
096: */
097: public void run(IStructuredSelection selection) {
098: try {
099: if (RefactoringAvailabilityTester
100: .isExtractInterfaceAvailable(selection)) {
101: IType singleSelectedType = RefactoringAvailabilityTester
102: .getSingleSelectedType(selection);
103: if (!ActionUtil.isEditable(getShell(),
104: singleSelectedType))
105: return;
106: RefactoringExecutionStarter
107: .startExtractInterfaceRefactoring(
108: singleSelectedType, getShell());
109: }
110: } catch (JavaModelException e) {
111: ExceptionHandler
112: .handle(
113: e,
114: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
115: RefactoringMessages.OpenRefactoringWizardAction_exception);
116: }
117: }
118:
119: /*
120: * @see SelectionDispatchAction#selectionChanged(ITextSelection)
121: */
122: public void selectionChanged(ITextSelection selection) {
123: setEnabled(true);
124: }
125:
126: /**
127: * Note: This method is for internal use only. Clients should not call this method.
128: */
129: public void selectionChanged(JavaTextSelection selection) {
130: try {
131: setEnabled(RefactoringAvailabilityTester
132: .isExtractInterfaceAvailable(selection));
133: } catch (JavaModelException e) {
134: setEnabled(false);
135: }
136: }
137:
138: /*
139: * @see SelectionDispatchAction#run(ITextSelection)
140: */
141: public void run(ITextSelection selection) {
142: try {
143: IType type = RefactoringActions
144: .getEnclosingOrPrimaryType(fEditor);
145: if (RefactoringAvailabilityTester
146: .isExtractInterfaceAvailable(type)) {
147: if (!ActionUtil.isEditable(fEditor, getShell(), type))
148: return;
149: RefactoringExecutionStarter
150: .startExtractInterfaceRefactoring(type,
151: getShell());
152: } else {
153: String unavailable = RefactoringMessages.ExtractInterfaceAction_To_activate;
154: MessageDialog
155: .openInformation(
156: getShell(),
157: RefactoringMessages.OpenRefactoringWizardAction_unavailable,
158: unavailable);
159: }
160: } catch (JavaModelException e) {
161: ExceptionHandler
162: .handle(
163: e,
164: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
165: RefactoringMessages.OpenRefactoringWizardAction_exception);
166: }
167: }
168: }
|