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.ICompilationUnit;
023: import org.eclipse.jdt.core.IType;
024: import org.eclipse.jdt.core.JavaModelException;
025:
026: import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
027: import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
028: import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil;
029: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
030:
031: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
032: import org.eclipse.jdt.internal.ui.JavaPlugin;
033: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
034: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
035: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
036: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
037: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
038: import org.eclipse.jdt.internal.ui.refactoring.actions.RefactoringActions;
039: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
040:
041: /**
042: * Tries to use a super type of a class where possible.
043: *
044: * <p>
045: * This class may be instantiated; it is not intended to be subclassed.
046: * </p>
047: *
048: * @since 2.1
049: */
050: // Note: The disclaimer about instantiating and subclassing got added in 3.1.
051: // Don't make this class final or remove a constructor!
052: public class UseSupertypeAction extends SelectionDispatchAction {
053:
054: private JavaEditor fEditor;
055:
056: /**
057: * Note: This constructor is for internal use only. Clients should not call this constructor.
058: * @param editor the java editor
059: */
060: public UseSupertypeAction(JavaEditor editor) {
061: this (editor.getEditorSite());
062: fEditor = editor;
063: setEnabled(SelectionConverter.canOperateOn(fEditor));
064: }
065:
066: /**
067: * Creates a new <code>UseSupertypeAction</code>. The action requires
068: * that the selection provided by the site's selection provider is of type <code>
069: * org.eclipse.jface.viewers.IStructuredSelection</code>.
070: *
071: * @param site the site providing context information for this action
072: */
073: public UseSupertypeAction(IWorkbenchSite site) {
074: super (site);
075: setText(RefactoringMessages.UseSupertypeAction_use_Supertype);
076: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
077: IJavaHelpContextIds.USE_SUPERTYPE_ACTION);
078: }
079:
080: //---- structured selection ---------------------------------------------------
081:
082: /*
083: * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
084: */
085: public void selectionChanged(IStructuredSelection selection) {
086: try {
087: setEnabled(RefactoringAvailabilityTester
088: .isUseSuperTypeAvailable(selection));
089: } catch (JavaModelException e) {
090: // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
091: if (!(e.getException() instanceof CharConversionException)
092: && JavaModelUtil.isExceptionToBeLogged(e))
093: JavaPlugin.log(e);
094: setEnabled(false);// no UI - happens on selection changes
095: }
096: }
097:
098: /*
099: * @see SelectionDispatchAction#run(IStructuredSelection)
100: */
101: public void run(IStructuredSelection selection) {
102: try {
103: if (RefactoringAvailabilityTester
104: .isUseSuperTypeAvailable(selection)) {
105: IType singleSelectedType = getSingleSelectedType(selection);
106: if (!ActionUtil.isEditable(getShell(),
107: singleSelectedType))
108: return;
109: RefactoringExecutionStarter
110: .startUseSupertypeRefactoring(
111: singleSelectedType, getShell());
112: }
113: } catch (JavaModelException e) {
114: ExceptionHandler
115: .handle(
116: e,
117: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
118: RefactoringMessages.OpenRefactoringWizardAction_exception);
119: }
120: }
121:
122: private static IType getSingleSelectedType(
123: IStructuredSelection selection) throws JavaModelException {
124: if (selection.isEmpty() || selection.size() != 1)
125: return null;
126:
127: Object first = selection.getFirstElement();
128: if (first instanceof IType)
129: return (IType) first;
130: if (first instanceof ICompilationUnit)
131: return JavaElementUtil
132: .getMainType((ICompilationUnit) first);
133: return null;
134: }
135:
136: //---- text selection ------------------------------------------------------
137:
138: /*
139: * @see SelectionDispatchAction#selectionChanged(ITextSelection)
140: */
141: public void selectionChanged(ITextSelection selection) {
142: setEnabled(true);
143: }
144:
145: /**
146: * Note: This method is for internal use only. Clients should not call this method.
147: */
148: public void selectionChanged(JavaTextSelection selection) {
149: try {
150: setEnabled(RefactoringAvailabilityTester
151: .isUseSuperTypeAvailable(selection));
152: } catch (JavaModelException e) {
153: setEnabled(false);
154: }
155: }
156:
157: /*
158: * @see SelectionDispatchAction#run(ITextSelection)
159: */
160: public void run(ITextSelection selection) {
161: try {
162: IType type = RefactoringActions
163: .getEnclosingOrPrimaryType(fEditor);
164: if (RefactoringAvailabilityTester
165: .isUseSuperTypeAvailable(type)) {
166: if (!ActionUtil.isEditable(fEditor, getShell(), type))
167: return;
168: RefactoringExecutionStarter
169: .startUseSupertypeRefactoring(type, getShell());
170: } else {
171: MessageDialog
172: .openInformation(
173: getShell(),
174: RefactoringMessages.OpenRefactoringWizardAction_unavailable,
175: RefactoringMessages.UseSupertypeAction_to_activate);
176: }
177: } catch (JavaModelException e) {
178: ExceptionHandler
179: .handle(
180: e,
181: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
182: RefactoringMessages.OpenRefactoringWizardAction_exception);
183: }
184: }
185: }
|