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 java.lang.reflect.InvocationTargetException;
013: import java.util.List;
014:
015: import org.eclipse.core.runtime.CoreException;
016:
017: import org.eclipse.jdt.core.ICompilationUnit;
018: import org.eclipse.jdt.core.IField;
019: import org.eclipse.jdt.core.IJavaElement;
020: import org.eclipse.jdt.core.IMember;
021: import org.eclipse.jdt.core.IMethod;
022: import org.eclipse.jdt.core.IType;
023:
024: import org.eclipse.jface.dialogs.MessageDialog;
025: import org.eclipse.jface.text.ITextSelection;
026: import org.eclipse.jface.viewers.IStructuredSelection;
027:
028: import org.eclipse.ui.IEditorPart;
029: import org.eclipse.ui.IWorkbenchSite;
030: import org.eclipse.ui.PlatformUI;
031:
032: import org.eclipse.jdt.internal.corext.codemanipulation.AddJavaDocStubOperation;
033: import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
034:
035: import org.eclipse.jdt.ui.JavaUI;
036:
037: import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
038: import org.eclipse.jdt.internal.ui.actions.ActionMessages;
039: import org.eclipse.jdt.internal.ui.actions.ActionUtil;
040: import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
041: import org.eclipse.jdt.internal.ui.actions.WorkbenchRunnableAdapter;
042: import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
043: import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
044: import org.eclipse.jdt.internal.ui.util.ElementValidator;
045: import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
046:
047: /**
048: * Create Javadoc comment stubs for the selected members.
049: * <p>
050: * Will open the parent compilation unit in a Java editor. The result is
051: * unsaved, so the user can decide if the changes are acceptable.
052: * <p>
053: * The action is applicable to structured selections containing elements
054: * of type <code>IMember</code>.
055: *
056: * <p>
057: * This class may be instantiated; it is not intended to be subclassed.
058: * </p>
059: *
060: * @since 2.0
061: */
062: public class AddJavaDocStubAction extends SelectionDispatchAction {
063:
064: private CompilationUnitEditor fEditor;
065:
066: /**
067: * Creates a new <code>AddJavaDocStubAction</code>. The action requires
068: * that the selection provided by the site's selection provider is of type <code>
069: * org.eclipse.jface.viewers.IStructuredSelection</code>.
070: *
071: * @param site the site providing context information for this action
072: */
073: public AddJavaDocStubAction(IWorkbenchSite site) {
074: super (site);
075: setText(ActionMessages.AddJavaDocStubAction_label);
076: setDescription(ActionMessages.AddJavaDocStubAction_description);
077: setToolTipText(ActionMessages.AddJavaDocStubAction_tooltip);
078: PlatformUI.getWorkbench().getHelpSystem().setHelp(this ,
079: IJavaHelpContextIds.ADD_JAVADOC_STUB_ACTION);
080: }
081:
082: /**
083: * Note: This constructor is for internal use only. Clients should not call this constructor.
084: * @param editor the compilation unit editor
085: */
086: public AddJavaDocStubAction(CompilationUnitEditor editor) {
087: this (editor.getEditorSite());
088: fEditor = editor;
089: setEnabled(checkEnabledEditor());
090: }
091:
092: //---- Structured Viewer -----------------------------------------------------------
093:
094: /* (non-Javadoc)
095: * Method declared on SelectionDispatchAction
096: */
097: public void selectionChanged(IStructuredSelection selection) {
098: IMember[] members = getSelectedMembers(selection);
099: setEnabled(members != null && members.length > 0);
100: }
101:
102: /* (non-Javadoc)
103: * Method declared on SelectionDispatchAction
104: */
105: public void run(IStructuredSelection selection) {
106: IMember[] members = getSelectedMembers(selection);
107: if (members == null || members.length == 0) {
108: return;
109: }
110:
111: try {
112: ICompilationUnit cu = members[0].getCompilationUnit();
113: if (!ActionUtil.isEditable(getShell(), cu)) {
114: return;
115: }
116:
117: // open the editor, forces the creation of a working copy
118: IEditorPart editor = JavaUI.openInEditor(cu);
119:
120: if (ElementValidator.check(members, getShell(),
121: getDialogTitle(), false))
122: run(cu, members);
123: JavaModelUtil.reconcile(cu);
124: EditorUtility.revealInEditor(editor, members[0]);
125:
126: } catch (CoreException e) {
127: ExceptionHandler
128: .handle(
129: e,
130: getShell(),
131: getDialogTitle(),
132: ActionMessages.AddJavaDocStubsAction_error_actionFailed);
133: }
134: }
135:
136: //---- Java Editor --------------------------------------------------------------
137:
138: /* (non-Javadoc)
139: * Method declared on SelectionDispatchAction
140: */
141: public void selectionChanged(ITextSelection selection) {
142: }
143:
144: private boolean checkEnabledEditor() {
145: return fEditor != null
146: && SelectionConverter.canOperateOn(fEditor);
147: }
148:
149: /* (non-Javadoc)
150: * Method declared on SelectionDispatchAction
151: */
152: public void run(ITextSelection selection) {
153: try {
154: IJavaElement element = SelectionConverter
155: .getElementAtOffset(fEditor);
156: if (!ActionUtil.isEditable(fEditor, getShell(), element))
157: return;
158: int type = element != null ? element.getElementType() : -1;
159: if (type != IJavaElement.METHOD
160: && type != IJavaElement.TYPE
161: && type != IJavaElement.FIELD) {
162: element = SelectionConverter.getTypeAtOffset(fEditor);
163: if (element == null) {
164: MessageDialog
165: .openInformation(
166: getShell(),
167: getDialogTitle(),
168: ActionMessages.AddJavaDocStubsAction_not_applicable);
169: return;
170: }
171: }
172: IMember[] members = new IMember[] { (IMember) element };
173: if (ElementValidator.checkValidateEdit(members, getShell(),
174: getDialogTitle()))
175: run(members[0].getCompilationUnit(), members);
176: } catch (CoreException e) {
177: ExceptionHandler
178: .handle(
179: e,
180: getShell(),
181: getDialogTitle(),
182: ActionMessages.AddJavaDocStubsAction_error_actionFailed);
183: }
184: }
185:
186: //---- Helpers -------------------------------------------------------------------
187:
188: /**
189: * Note this method is for internal use only.
190: *
191: * @param cu the compilation unit
192: * @param members an array of members
193: */
194: public void run(ICompilationUnit cu, IMember[] members) {
195: try {
196: AddJavaDocStubOperation op = new AddJavaDocStubOperation(
197: members);
198: PlatformUI.getWorkbench().getProgressService().runInUI(
199: PlatformUI.getWorkbench().getProgressService(),
200: new WorkbenchRunnableAdapter(op, op
201: .getScheduleRule()), op.getScheduleRule());
202: } catch (InvocationTargetException e) {
203: ExceptionHandler
204: .handle(
205: e,
206: getShell(),
207: getDialogTitle(),
208: ActionMessages.AddJavaDocStubsAction_error_actionFailed);
209: } catch (InterruptedException e) {
210: // operation canceled
211: }
212: }
213:
214: private IMember[] getSelectedMembers(IStructuredSelection selection) {
215: List elements = selection.toList();
216: int nElements = elements.size();
217: if (nElements > 0) {
218: IMember[] res = new IMember[nElements];
219: ICompilationUnit cu = null;
220: for (int i = 0; i < nElements; i++) {
221: Object curr = elements.get(i);
222: if (curr instanceof IMethod || curr instanceof IType
223: || curr instanceof IField) {
224: IMember member = (IMember) curr; // limit to methods, types & fields
225: if (!member.exists()) {
226: return null;
227: }
228: if (i == 0) {
229: cu = member.getCompilationUnit();
230: if (cu == null) {
231: return null;
232: }
233: } else if (!cu.equals(member.getCompilationUnit())) {
234: return null;
235: }
236: if (member instanceof IType
237: && member.getElementName().length() == 0) {
238: return null; // anonymous type
239: }
240: res[i] = member;
241: } else {
242: return null;
243: }
244: }
245: return res;
246: }
247: return null;
248: }
249:
250: private String getDialogTitle() {
251: return ActionMessages.AddJavaDocStubsAction_error_dialogTitle;
252: }
253: }
|