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 org.eclipse.core.runtime.CoreException;
013:
014: import org.eclipse.jface.viewers.IStructuredSelection;
015:
016: import org.eclipse.jface.text.ITextSelection;
017: import org.eclipse.jface.text.TextSelection;
018:
019: import org.eclipse.ui.IWorkbenchSite;
020: import org.eclipse.ui.PlatformUI;
021:
022: import org.eclipse.jdt.core.IMethod;
023: import org.eclipse.jdt.core.ISourceRange;
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.util.JavaModelUtil;
029:
030: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
031: import org.eclipse.jdt.internal.ui.JavaPlugin;
032: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
033: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
034: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
035: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
036: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
037: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
038:
039: /**
040: * Action that encapsulates the a constructor call with a factory
041: * method.
042: * <p>
043: * This class may be instantiated; it is not intended to be subclassed.
044: * </p>
045: *
046: * @since 3.0
047: */
048: public class IntroduceFactoryAction 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 IntroduceFactoryAction(JavaEditor editor) {
057: this (editor.getEditorSite());
058: fEditor = editor;
059: setEnabled(SelectionConverter
060: .getInputAsCompilationUnit(fEditor) != null);
061: }
062:
063: /**
064: * Creates a new <code>IntroduceFactoryAction</code>. The action requires
065: * that the selection provided by the site's selection provider is of type <code>
066: * org.eclipse.jface.viewers.IStructuredSelection</code>.
067: *
068: * @param site the site providing context information for this action
069: */
070: public IntroduceFactoryAction(IWorkbenchSite site) {
071: super (site);
072: setText(RefactoringMessages.IntroduceFactoryAction_label);
073: setToolTipText(RefactoringMessages.IntroduceFactoryAction_tooltipText);
074: setDescription(RefactoringMessages.IntroduceFactoryAction_description);
075: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
076: IJavaHelpContextIds.INTRODUCE_FACTORY_ACTION);
077: }
078:
079: //---- structured selection --------------------------------------------------
080:
081: /*
082: * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
083: */
084: public void selectionChanged(IStructuredSelection selection) {
085: try {
086: setEnabled(RefactoringAvailabilityTester
087: .isIntroduceFactoryAvailable(selection));
088: } catch (JavaModelException e) {
089: if (JavaModelUtil.isExceptionToBeLogged(e))
090: JavaPlugin.log(e);
091: setEnabled(false);//no UI here - happens on selection changes
092: }
093: }
094:
095: /*
096: * @see SelectionDispatchAction#run(IStructuredSelection)
097: */
098: public void run(IStructuredSelection selection) {
099: try {
100: // we have to call this here - no selection changed event is sent after a refactoring but it may still invalidate enablement
101: if (RefactoringAvailabilityTester
102: .isIntroduceFactoryAvailable(selection)) {
103: IMethod method = (IMethod) selection.getFirstElement();
104: if (!ActionUtil.isEditable(getShell(), method))
105: return;
106: ISourceRange range = method.getNameRange();
107: RefactoringExecutionStarter
108: .startIntroduceFactoryRefactoring(method
109: .getCompilationUnit(),
110: new TextSelection(range.getOffset(),
111: range.getLength()), getShell());
112: }
113: } catch (CoreException e) {
114: ExceptionHandler
115: .handle(
116: e,
117: RefactoringMessages.IntroduceFactoryAction_dialog_title,
118: RefactoringMessages.IntroduceFactoryAction_exception);
119: }
120: }
121:
122: /* (non-Javadoc)
123: * Method declared on SelectionDispatchAction
124: */
125: public void selectionChanged(ITextSelection selection) {
126: setEnabled(fEditor != null
127: && SelectionConverter
128: .getInputAsCompilationUnit(fEditor) != null);
129: }
130:
131: /**
132: * Note: This method is for internal use only. Clients should not call this method.
133: */
134: public void selectionChanged(JavaTextSelection selection) {
135: try {
136: setEnabled(RefactoringAvailabilityTester
137: .isIntroduceFactoryAvailable(selection));
138: } catch (JavaModelException e) {
139: setEnabled(false);
140: }
141: }
142:
143: public void run(ITextSelection selection) {
144: if (!ActionUtil.isEditable(fEditor))
145: return;
146: try {
147: RefactoringExecutionStarter
148: .startIntroduceFactoryRefactoring(
149: SelectionConverter
150: .getInputAsCompilationUnit(fEditor),
151: selection, getShell());
152: } catch (CoreException e) {
153: ExceptionHandler
154: .handle(
155: e,
156: RefactoringMessages.IntroduceFactoryAction_dialog_title,
157: RefactoringMessages.IntroduceFactoryAction_exception);
158: }
159: }
160: }
|