KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import org.eclipse.jface.dialogs.MessageDialog;
15 import org.eclipse.jface.viewers.IStructuredSelection;
16
17 import org.eclipse.jface.text.ITextSelection;
18
19 import org.eclipse.ui.IWorkbenchSite;
20 import org.eclipse.ui.PlatformUI;
21
22 import org.eclipse.jdt.core.IField;
23 import org.eclipse.jdt.core.IJavaElement;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
27 import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
28 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
29
30 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
31 import org.eclipse.jdt.internal.ui.JavaPlugin;
32 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
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 to run the self encapsulate field refactoring.
42  * <p>
43  * Action is applicable to selections containing elements of type
44  * <code>IField</code>.
45  *
46  * <p>
47  * This class may be instantiated; it is not intended to be subclassed.
48  * </p>
49  *
50  * @since 2.0
51  */

52 public class SelfEncapsulateFieldAction extends SelectionDispatchAction {
53     
54     private JavaEditor fEditor;
55     
56     /**
57      * Creates a new <code>SelfEncapsulateFieldAction</code>. The action requires
58      * that the selection provided by the site's selection provider is of type <code>
59      * org.eclipse.jface.viewers.IStructuredSelection</code>.
60      *
61      * @param site the site providing context information for this action
62      */

63     public SelfEncapsulateFieldAction(IWorkbenchSite site) {
64         super(site);
65         setText(ActionMessages.SelfEncapsulateFieldAction_label);
66         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.SELF_ENCAPSULATE_ACTION);
67     }
68     
69     /**
70      * Note: This constructor is for internal use only. Clients should not call this constructor.
71      *
72      * @param editor the java editor
73      */

74     public SelfEncapsulateFieldAction(JavaEditor editor) {
75         this(editor.getEditorSite());
76         fEditor= editor;
77         setEnabled(SelectionConverter.canOperateOn(fEditor));
78     }
79     
80     //---- text selection -------------------------------------------------------
81

82     /* (non-Javadoc)
83      * Method declared on SelectionDispatchAction.
84      */

85     public void selectionChanged(ITextSelection selection) {
86         setEnabled(true);
87     }
88     
89     /**
90      * Note: This method is for internal use only. Clients should not call this method.
91      */

92     public void selectionChanged(JavaTextSelection selection) {
93         try {
94             setEnabled(RefactoringAvailabilityTester.isSelfEncapsulateAvailable(selection));
95         } catch (JavaModelException e) {
96             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
97
if (JavaModelUtil.isExceptionToBeLogged(e))
98                 JavaPlugin.log(e);
99             setEnabled(false);//no UI
100
}
101     }
102     
103     /* (non-Javadoc)
104      * Method declared on SelectionDispatchAction.
105      */

106     public void run(ITextSelection selection) {
107         try {
108             IJavaElement[] elements= SelectionConverter.codeResolve(fEditor);
109             if (elements.length != 1 || !(elements[0] instanceof IField)) {
110                 MessageDialog.openInformation(getShell(), ActionMessages.SelfEncapsulateFieldAction_dialog_title, ActionMessages.SelfEncapsulateFieldAction_dialog_unavailable);
111                 return;
112             }
113             IField field= (IField)elements[0];
114
115             if (!RefactoringAvailabilityTester.isSelfEncapsulateAvailable(field)) {
116                 MessageDialog.openInformation(getShell(), ActionMessages.SelfEncapsulateFieldAction_dialog_title, ActionMessages.SelfEncapsulateFieldAction_dialog_unavailable);
117                 return;
118             }
119             run(field);
120         } catch (JavaModelException exception) {
121             JavaPlugin.log(exception);
122             return;
123         }
124     }
125     
126     //---- structured selection -------------------------------------------------
127

128     /* (non-Javadoc)
129      * Method declared on SelectionDispatchAction.
130      */

131     public void selectionChanged(IStructuredSelection selection) {
132         try {
133             setEnabled(RefactoringAvailabilityTester.isSelfEncapsulateAvailable(selection));
134         } catch (JavaModelException e) {
135             // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
136
if (JavaModelUtil.isExceptionToBeLogged(e))
137                 JavaPlugin.log(e);
138             setEnabled(false);//no UI
139
}
140     }
141     
142     /*
143      * (non-Javadoc)
144      * Method declared on SelectionDispatchAction.
145      */

146     public void run(IStructuredSelection selection) {
147         try {
148             if (RefactoringAvailabilityTester.isSelfEncapsulateAvailable(selection))
149                 run((IField) selection.getFirstElement());
150         } catch (JavaModelException e) {
151             ExceptionHandler.handle(e, RefactoringMessages.OpenRefactoringWizardAction_refactoring, RefactoringMessages.OpenRefactoringWizardAction_exception);
152         }
153     }
154     
155     //---- private helpers --------------------------------------------------------
156

157     /*
158      * Should be private. But got shipped in this state in 2.0 so changing this is a
159      * breaking API change.
160      */

161     public void run(IField field) {
162         if (! ActionUtil.isEditable(fEditor, getShell(), field))
163             return;
164         RefactoringExecutionStarter.startSelfEncapsulateRefactoring(field, getShell());
165     }
166 }
167
Popular Tags