KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14
15 import org.eclipse.jface.viewers.IStructuredSelection;
16
17 import org.eclipse.jface.text.ITextSelection;
18 import org.eclipse.jface.text.TextSelection;
19
20 import org.eclipse.ui.IWorkbenchSite;
21 import org.eclipse.ui.PlatformUI;
22
23 import org.eclipse.jdt.core.IMethod;
24 import org.eclipse.jdt.core.ISourceRange;
25 import org.eclipse.jdt.core.JavaModelException;
26
27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
28 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
29 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
30
31 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
32 import org.eclipse.jdt.internal.ui.JavaPlugin;
33 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
34 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
35 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
36 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
37 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
38 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
39
40 /**
41  * Action that encapsulates the a constructor call with a factory
42  * method.
43  * <p>
44  * This class may be instantiated; it is not intended to be subclassed.
45  * </p>
46  *
47  * @since 3.0
48  */

49 public class IntroduceFactoryAction 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 IntroduceFactoryAction(JavaEditor editor) {
58         this(editor.getEditorSite());
59         fEditor= editor;
60         setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
61     }
62
63     /**
64      * Creates a new <code>IntroduceFactoryAction</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 IntroduceFactoryAction(IWorkbenchSite site) {
71         super(site);
72         setText(RefactoringMessages.IntroduceFactoryAction_label);
73         setToolTipText(RefactoringMessages.IntroduceFactoryAction_tooltipText);
74         setDescription(RefactoringMessages.IntroduceFactoryAction_description);
75         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INTRODUCE_FACTORY_ACTION);
76     }
77     
78     //---- structured selection --------------------------------------------------
79

80     /*
81      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
82      */

83     public void selectionChanged(IStructuredSelection selection) {
84         try {
85             setEnabled(RefactoringAvailabilityTester.isIntroduceFactoryAvailable(selection));
86         } catch (JavaModelException e) {
87             if (JavaModelUtil.isExceptionToBeLogged(e))
88                 JavaPlugin.log(e);
89             setEnabled(false);//no UI here - happens on selection changes
90
}
91     }
92
93     /*
94      * @see SelectionDispatchAction#run(IStructuredSelection)
95      */

96     public void run(IStructuredSelection selection) {
97         try {
98             // we have to call this here - no selection changed event is sent after a refactoring but it may still invalidate enablement
99
if (RefactoringAvailabilityTester.isIntroduceFactoryAvailable(selection)) {
100                 IMethod method= (IMethod) selection.getFirstElement();
101                 if (!ActionUtil.isEditable(getShell(), method))
102                     return;
103                 ISourceRange range= method.getNameRange();
104                 RefactoringExecutionStarter.startIntroduceFactoryRefactoring(method.getCompilationUnit(), new TextSelection(range.getOffset(), range.getLength()), getShell());
105             }
106         } catch (CoreException e) {
107             ExceptionHandler.handle(e, RefactoringMessages.IntroduceFactoryAction_dialog_title, RefactoringMessages.IntroduceFactoryAction_exception);
108         }
109     }
110
111     /* (non-Javadoc)
112      * Method declared on SelectionDispatchAction
113      */

114     public void selectionChanged(ITextSelection selection) {
115         setEnabled(fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
116     }
117
118     /**
119      * Note: This method is for internal use only. Clients should not call this method.
120      */

121     public void selectionChanged(JavaTextSelection selection) {
122         try {
123             setEnabled(RefactoringAvailabilityTester.isIntroduceFactoryAvailable(selection));
124         } catch (JavaModelException e) {
125             setEnabled(false);
126         }
127     }
128
129     public void run(ITextSelection selection) {
130         if (!ActionUtil.isEditable(fEditor))
131             return;
132         try {
133             RefactoringExecutionStarter.startIntroduceFactoryRefactoring(SelectionConverter.getInputAsCompilationUnit(fEditor), selection, getShell());
134         } catch (CoreException e) {
135             ExceptionHandler.handle(e, RefactoringMessages.IntroduceFactoryAction_dialog_title, RefactoringMessages.IntroduceFactoryAction_exception);
136         }
137     }
138 }
139
Popular Tags