KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > refactoring > actions > InlineConstantAction


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.internal.ui.refactoring.actions;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19
20 import org.eclipse.jface.text.ITextSelection;
21
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.eclipse.ui.PlatformUI;
24
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IField;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.core.dom.CompilationUnit;
29
30 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
31 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
32 import org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser;
33 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
34
35 import org.eclipse.jdt.ui.actions.SelectionDispatchAction;
36
37 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
40 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
41 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
42 import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
43 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
44 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
45
46 /**
47  * Inlines a constant.
48  *
49  * <p>
50  * This class may be instantiated; it is not intended to be subclassed.
51  * </p>
52  *
53  */

54 public class InlineConstantAction extends SelectionDispatchAction {
55
56     private JavaEditor fEditor;
57     
58     /**
59      * Note: This constructor is for internal use only. Clients should not call this constructor.
60      *
61      * @param editor the java editor
62      */

63     public InlineConstantAction(JavaEditor editor) {
64         this(editor.getEditorSite());
65         fEditor= editor;
66         setEnabled(SelectionConverter.canOperateOn(fEditor));
67     }
68
69     public InlineConstantAction(IWorkbenchSite site) {
70         super(site);
71         setText(RefactoringMessages.InlineConstantAction_inline_Constant);
72         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.INLINE_ACTION);
73     }
74     
75     //---- structured selection ---------------------------------------------
76

77     /*
78      * @see SelectionDispatchAction#selectionChanged(IStructuredSelection)
79      */

80     public void selectionChanged(IStructuredSelection selection) {
81         try {
82             setEnabled(RefactoringAvailabilityTester.isInlineConstantAvailable(selection));
83         } catch (JavaModelException e) {
84             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
85
if (JavaModelUtil.isExceptionToBeLogged(e))
86                 JavaPlugin.log(e);
87             setEnabled(false);//no ui
88
}
89     }
90
91     /*
92      * @see SelectionDispatchAction#run(IStructuredSelection)
93      */

94     public void run(IStructuredSelection selection) {
95         try {
96             Assert.isTrue(RefactoringAvailabilityTester.isInlineConstantAvailable(selection));
97             
98             Object JavaDoc first= selection.getFirstElement();
99             Assert.isTrue(first instanceof IField);
100             
101             IField field= (IField) first;
102             run(field.getNameRange().getOffset(), field.getNameRange().getLength(), field.getCompilationUnit());
103         } catch (JavaModelException e) {
104             ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_unexpected_exception);
105         }
106     }
107
108     //---- text selection -----------------------------------------------
109

110     /*
111      * @see SelectionDispatchAction#selectionChanged(ITextSelection)
112      */

113     public void selectionChanged(ITextSelection selection) {
114         setEnabled(true);
115     }
116     
117     /**
118      * Note: This method is for internal use only. Clients should not call this method.
119      * @param selection
120      */

121     public void selectionChanged(JavaTextSelection selection) {
122         try {
123             setEnabled(RefactoringAvailabilityTester.isInlineConstantAvailable(selection));
124         } catch (JavaModelException e) {
125             setEnabled(false);
126         }
127     }
128     
129     /* (non-Javadoc)
130      * Method declared on SelectionDispatchAction
131      */

132     public void run(ITextSelection selection) {
133         run(selection.getOffset(), selection.getLength(), SelectionConverter.getInputAsCompilationUnit(fEditor));
134     }
135
136     private void run(int offset, int length, ICompilationUnit cu) {
137         Assert.isNotNull(cu);
138         Assert.isTrue(offset >= 0);
139         Assert.isTrue(length >= 0);
140         if (!ActionUtil.isEditable(fEditor, getShell(), cu))
141             return;
142         try {
143             CompilationUnit node= RefactoringASTParser.parseWithASTProvider(cu, true, null);
144             if (! RefactoringExecutionStarter.startInlineConstantRefactoring(cu, node, offset, length, getShell())) {
145                 MessageDialog.openInformation(getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_no_constant_reference_or_declaration);
146             }
147             
148         } catch (JavaModelException e) {
149             ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_unexpected_exception);
150         }
151     }
152
153     public boolean tryInlineConstant(ICompilationUnit unit, CompilationUnit node, ITextSelection selection, Shell shell) {
154         try {
155             if (RefactoringExecutionStarter.startInlineConstantRefactoring(unit, node, selection.getOffset(), selection.getLength(), shell)) {
156                 return true;
157             }
158         } catch (JavaModelException e) {
159             ExceptionHandler.handle(e, getShell(), RefactoringMessages.InlineConstantAction_dialog_title, RefactoringMessages.InlineConstantAction_unexpected_exception);
160         }
161         return false;
162     }
163 }
164
Popular Tags