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.jface.dialogs.MessageDialog;
013: import org.eclipse.jface.viewers.IStructuredSelection;
014:
015: import org.eclipse.jface.text.ITextSelection;
016:
017: import org.eclipse.ui.IWorkbenchSite;
018: import org.eclipse.ui.PlatformUI;
019:
020: import org.eclipse.jdt.core.IField;
021: import org.eclipse.jdt.core.IJavaElement;
022: import org.eclipse.jdt.core.JavaModelException;
023:
024: import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester;
025: import org.eclipse.jdt.internal.corext.refactoring.RefactoringExecutionStarter;
026: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
027:
028: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
029: import org.eclipse.jdt.internal.ui.JavaPlugin;
030: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
031: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
032: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
033: import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
034: import org.eclipse.jdt.internal.ui.javaeditor.JavaTextSelection;
035: import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages;
036: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
037:
038: /**
039: * Action to run the self encapsulate field refactoring.
040: * <p>
041: * Action is applicable to selections containing elements of type
042: * <code>IField</code>.
043: *
044: * <p>
045: * This class may be instantiated; it is not intended to be subclassed.
046: * </p>
047: *
048: * @since 2.0
049: */
050: public class SelfEncapsulateFieldAction extends SelectionDispatchAction {
051:
052: private JavaEditor fEditor;
053:
054: /**
055: * Creates a new <code>SelfEncapsulateFieldAction</code>. The action requires
056: * that the selection provided by the site's selection provider is of type <code>
057: * org.eclipse.jface.viewers.IStructuredSelection</code>.
058: *
059: * @param site the site providing context information for this action
060: */
061: public SelfEncapsulateFieldAction(IWorkbenchSite site) {
062: super (site);
063: setText(ActionMessages.SelfEncapsulateFieldAction_label);
064: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
065: IJavaHelpContextIds.SELF_ENCAPSULATE_ACTION);
066: }
067:
068: /**
069: * Note: This constructor is for internal use only. Clients should not call this constructor.
070: *
071: * @param editor the java editor
072: */
073: public SelfEncapsulateFieldAction(JavaEditor editor) {
074: this (editor.getEditorSite());
075: fEditor = editor;
076: setEnabled(SelectionConverter.canOperateOn(fEditor));
077: }
078:
079: //---- text selection -------------------------------------------------------
080:
081: /* (non-Javadoc)
082: * Method declared on SelectionDispatchAction.
083: */
084: public void selectionChanged(ITextSelection selection) {
085: setEnabled(true);
086: }
087:
088: /**
089: * Note: This method is for internal use only. Clients should not call this method.
090: */
091: public void selectionChanged(JavaTextSelection selection) {
092: try {
093: setEnabled(RefactoringAvailabilityTester
094: .isSelfEncapsulateAvailable(selection));
095: } catch (JavaModelException e) {
096: // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
097: if (JavaModelUtil.isExceptionToBeLogged(e))
098: JavaPlugin.log(e);
099: 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
109: .codeResolve(fEditor);
110: if (elements.length != 1
111: || !(elements[0] instanceof IField)) {
112: MessageDialog
113: .openInformation(
114: getShell(),
115: ActionMessages.SelfEncapsulateFieldAction_dialog_title,
116: ActionMessages.SelfEncapsulateFieldAction_dialog_unavailable);
117: return;
118: }
119: IField field = (IField) elements[0];
120:
121: if (!RefactoringAvailabilityTester
122: .isSelfEncapsulateAvailable(field)) {
123: MessageDialog
124: .openInformation(
125: getShell(),
126: ActionMessages.SelfEncapsulateFieldAction_dialog_title,
127: ActionMessages.SelfEncapsulateFieldAction_dialog_unavailable);
128: return;
129: }
130: run(field);
131: } catch (JavaModelException exception) {
132: JavaPlugin.log(exception);
133: return;
134: }
135: }
136:
137: //---- structured selection -------------------------------------------------
138:
139: /* (non-Javadoc)
140: * Method declared on SelectionDispatchAction.
141: */
142: public void selectionChanged(IStructuredSelection selection) {
143: try {
144: setEnabled(RefactoringAvailabilityTester
145: .isSelfEncapsulateAvailable(selection));
146: } catch (JavaModelException e) {
147: // http://bugs.eclipse.org/bugs/show_bug.cgi?id=19253
148: if (JavaModelUtil.isExceptionToBeLogged(e))
149: JavaPlugin.log(e);
150: setEnabled(false);//no UI
151: }
152: }
153:
154: /*
155: * (non-Javadoc)
156: * Method declared on SelectionDispatchAction.
157: */
158: public void run(IStructuredSelection selection) {
159: try {
160: if (RefactoringAvailabilityTester
161: .isSelfEncapsulateAvailable(selection))
162: run((IField) selection.getFirstElement());
163: } catch (JavaModelException e) {
164: ExceptionHandler
165: .handle(
166: e,
167: RefactoringMessages.OpenRefactoringWizardAction_refactoring,
168: RefactoringMessages.OpenRefactoringWizardAction_exception);
169: }
170: }
171:
172: //---- private helpers --------------------------------------------------------
173:
174: /*
175: * Should be private. But got shipped in this state in 2.0 so changing this is a
176: * breaking API change.
177: */
178: public void run(IField field) {
179: if (!ActionUtil.isEditable(fEditor, getShell(), field))
180: return;
181: RefactoringExecutionStarter.startSelfEncapsulateRefactoring(
182: field, getShell());
183: }
184: }
|