1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.ui.actions;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.List;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.OperationCanceledException;
19 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.text.ITextSelection;
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.jdt.core.ICompilationUnit;
28 import org.eclipse.jdt.core.IField;
29 import org.eclipse.jdt.core.IMember;
30 import org.eclipse.jdt.core.IMethod;
31 import org.eclipse.jdt.core.IType;
33 import org.eclipse.jdt.internal.corext.codemanipulation.AddJavaDocStubOperation;
34 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
36 import org.eclipse.jdt.ui.JavaUI;
38 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds;
39 import org.eclipse.jdt.internal.ui.actions.ActionMessages;
40 import org.eclipse.jdt.internal.ui.actions.ActionUtil;
41 import org.eclipse.jdt.internal.ui.actions.SelectionConverter;
42 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
43 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
44 import org.eclipse.jdt.internal.ui.util.ElementValidator;
45 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
48 * Create Javadoc comment stubs for the selected members.
50 * Will open the parent compilation unit in a Java editor. The result is
51 * unsaved, so the user can decide if the changes are acceptable.
53 * The action is applicable to structured selections containing elements
54 * of type <code>IMember</code>.
57 * This class may be instantiated; it is not intended to be subclassed.
62 * @noextend This class is not intended to be subclassed by clients.
64 public class AddJavaDocStubAction extends SelectionDispatchAction {
66 public CompilationUnitEditor fEditor;
69 * Creates a new <code>AddJavaDocStubAction</code>. The action requires
70 * that the selection provided by the site's selection provider is of type <code>
71 * org.eclipse.jface.viewers.IStructuredSelection</code>.
73 * @param site the site providing context information for this action
75 public AddJavaDocStubAction(IWorkbenchSite site) {
77 setText(ActionMessages.AddJavaDocStubAction_label);
78 setDescription(ActionMessages.AddJavaDocStubAction_description);
79 setToolTipText(ActionMessages.AddJavaDocStubAction_tooltip);
80 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_JAVADOC_STUB_ACTION);
84 * Note: This constructor is for internal use only. Clients should not call this constructor.
85 * @param editor the compilation unit editor
87 * @noreference This constructor is not intended to be referenced by clients.
89 public AddJavaDocStubAction(CompilationUnitEditor editor) {
90 this(editor.getEditorSite());
92 setEnabled(checkEnabledEditor());
95 //---- Structured Viewer -----------------------------------------------------------
98 * Method declared on SelectionDispatchAction
101 public void selectionChanged(IStructuredSelection selection) {
102 IMember[] members= getSelectedMembers(selection);
103 setEnabled(members != null && members.length > 0);
107 * Method declared on SelectionDispatchAction
110 public void run(IStructuredSelection selection) {
111 IMember[] members= getSelectedMembers(selection);
112 if (members == null || members.length == 0) {
117 ICompilationUnit cu= members[0].getCompilationUnit();
118 if (!ActionUtil.isEditable(getShell(), cu)) {
122 // open the editor, forces the creation of a working copy
123 IEditorPart editor= JavaUI.openInEditor(cu);
125 if (ElementValidator.check(members, getShell(), getDialogTitle(), false))
127 JavaModelUtil.reconcile(cu);
128 EditorUtility.revealInEditor(editor, members[0]);
130 } catch (CoreException e) {
131 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
135 //---- Java Editor --------------------------------------------------------------
138 * Method declared on SelectionDispatchAction
141 public void selectionChanged(ITextSelection selection) {
144 private boolean checkEnabledEditor() {
145 return fEditor != null && SelectionConverter.canOperateOn(fEditor);
149 * Method declared on SelectionDispatchAction
152 public void run(ITextSelection selection) {
153 fEditor.generated_2825797301311231316(this);
158 //---- Helpers -------------------------------------------------------------------
161 * Note this method is for internal use only.
163 * @param members an array of members
165 public void run(IMember[] members) {
166 AddJavaDocStubOperation op= new AddJavaDocStubOperation(members);
167 if (members.length < 11) {
170 } catch (CoreException e) {
171 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
172 } catch (OperationCanceledException e) {
173 // operation canceled
179 op.generated_4900133015593962688();
180 } catch (InvocationTargetException e) {
181 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
182 } catch (InterruptedException e) {
183 // operation canceled
187 private IMember[] getSelectedMembers(IStructuredSelection selection) {
188 List<?> elements= selection.toList();
189 int nElements= elements.size();
191 IMember[] res= new IMember[nElements];
192 ICompilationUnit cu= null;
193 for (int i= 0; i < nElements; i++) {
194 Object curr= elements.get(i);
195 if (curr instanceof IMethod || curr instanceof IType || curr instanceof IField) {
196 IMember member= (IMember)curr; // limit to methods, types & fields
197 if (! member.exists()) {
201 cu= member.getCompilationUnit();
205 } else if (!cu.equals(member.getCompilationUnit())) {
208 if (member instanceof IType && member.getElementName().length() == 0) {
209 return null; // anonymous type
221 public String getDialogTitle() {
222 return ActionMessages.AddJavaDocStubsAction_error_dialogTitle;