KickJava   Java API By Example, From Geeks To Geeks.

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


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.text.ITextSelection;
16
17 import org.eclipse.ui.PlatformUI;
18
19 import org.eclipse.jdt.core.ICompilationUnit;
20
21 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
22 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
23
24 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
25 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
26 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
27 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
28 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
29 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
30 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
31
32 /**
33  * Introduces a new method parameter from a selected expression.
34  *
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p>
38  *
39  * @since 3.0
40  */

41 public class IntroduceParameterAction extends SelectionDispatchAction {
42
43     private final JavaEditor fEditor;
44
45     /**
46      * Note: This constructor is for internal use only. Clients should not call this constructor.
47      * @param editor the java editor
48      */

49     public IntroduceParameterAction(JavaEditor editor) {
50         super(editor.getEditorSite());
51         setText(RefactoringMessages.IntroduceParameterAction_label);
52         fEditor= editor;
53         setEnabled(SelectionConverter.getInputAsCompilationUnit(fEditor) != null);
54         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INTRODUCE_PARAMETER_ACTION);
55     }
56
57     /* (non-Javadoc)
58      * Method declared on SelectionDispatchAction
59      */

60     public void selectionChanged(ITextSelection selection) {
61         setEnabled((fEditor != null && SelectionConverter.getInputAsCompilationUnit(fEditor) != null));
62     }
63     
64     /**
65      * Note: This method is for internal use only. Clients should not call this method.
66      */

67     public void selectionChanged(JavaTextSelection selection) {
68         setEnabled(RefactoringAvailabilityTester.isIntroduceParameterAvailable(selection));
69     }
70     
71     /* (non-Javadoc)
72      * Method declared on SelectionDispatchAction
73      */

74     public void run(ITextSelection selection) {
75         if (!ActionUtil.isEditable(fEditor))
76             return;
77         try{
78             ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(fEditor);
79             RefactoringExecutionStarter.startIntroduceParameter(unit, selection.getOffset(), selection.getLength(), getShell());
80         } catch (CoreException e){
81             ExceptionHandler.handle(e, RefactoringMessages.IntroduceParameterAction_dialog_title, RefactoringMessages.NewTextRefactoringAction_exception);
82         }
83     }
84 }
85
Popular Tags