KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.CoreException;
17
18 import org.eclipse.jdt.core.ICompilationUnit;
19 import org.eclipse.jdt.core.IField;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.IMember;
22 import org.eclipse.jdt.core.IMethod;
23 import org.eclipse.jdt.core.IType;
24
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.jface.text.ITextSelection;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.IWorkbenchSite;
31 import org.eclipse.ui.PlatformUI;
32
33 import org.eclipse.jdt.internal.corext.codemanipulation.AddJavaDocStubOperation;
34 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
35
36 import org.eclipse.jdt.ui.JavaUI;
37
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.actions.WorkbenchRunnableAdapter;
43 import org.eclipse.jdt.internal.ui.javaeditor.CompilationUnitEditor;
44 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility;
45 import org.eclipse.jdt.internal.ui.util.ElementValidator;
46 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
47
48 /**
49  * Create Javadoc comment stubs for the selected members.
50  * <p>
51  * Will open the parent compilation unit in a Java editor. The result is
52  * unsaved, so the user can decide if the changes are acceptable.
53  * <p>
54  * The action is applicable to structured selections containing elements
55  * of type <code>IMember</code>.
56  *
57  * <p>
58  * This class may be instantiated; it is not intended to be subclassed.
59  * </p>
60  *
61  * @since 2.0
62  */

63 public class AddJavaDocStubAction extends SelectionDispatchAction {
64
65     private CompilationUnitEditor fEditor;
66
67     /**
68      * Creates a new <code>AddJavaDocStubAction</code>. The action requires
69      * that the selection provided by the site's selection provider is of type <code>
70      * org.eclipse.jface.viewers.IStructuredSelection</code>.
71      *
72      * @param site the site providing context information for this action
73      */

74     public AddJavaDocStubAction(IWorkbenchSite site) {
75         super(site);
76         setText(ActionMessages.AddJavaDocStubAction_label);
77         setDescription(ActionMessages.AddJavaDocStubAction_description);
78         setToolTipText(ActionMessages.AddJavaDocStubAction_tooltip);
79         PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_JAVADOC_STUB_ACTION);
80     }
81
82     /**
83      * Note: This constructor is for internal use only. Clients should not call this constructor.
84      * @param editor the compilation unit editor
85      */

86     public AddJavaDocStubAction(CompilationUnitEditor editor) {
87         this(editor.getEditorSite());
88         fEditor= editor;
89         setEnabled(checkEnabledEditor());
90     }
91
92     //---- Structured Viewer -----------------------------------------------------------
93

94     /* (non-Javadoc)
95      * Method declared on SelectionDispatchAction
96      */

97     public void selectionChanged(IStructuredSelection selection) {
98         IMember[] members= getSelectedMembers(selection);
99         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(), getDialogTitle(), false))
121                 run(cu, members);
122             JavaModelUtil.reconcile(cu);
123             EditorUtility.revealInEditor(editor, members[0]);
124             
125         } catch (CoreException e) {
126             ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
127         }
128     }
129     
130     //---- Java Editor --------------------------------------------------------------
131

132     /* (non-Javadoc)
133      * Method declared on SelectionDispatchAction
134      */

135     public void selectionChanged(ITextSelection selection) {
136     }
137
138     private boolean checkEnabledEditor() {
139         return fEditor != null && SelectionConverter.canOperateOn(fEditor);
140     }
141     
142     /* (non-Javadoc)
143      * Method declared on SelectionDispatchAction
144      */

145     public void run(ITextSelection selection) {
146         try {
147             IJavaElement element= SelectionConverter.getElementAtOffset(fEditor);
148             if (!ActionUtil.isEditable(fEditor, getShell(), element))
149                 return;
150             int type= element != null ? element.getElementType() : -1;
151             if (type != IJavaElement.METHOD && type != IJavaElement.TYPE && type != IJavaElement.FIELD) {
152                 element= SelectionConverter.getTypeAtOffset(fEditor);
153                 if (element == null) {
154                     MessageDialog.openInformation(getShell(), getDialogTitle(),
155                         ActionMessages.AddJavaDocStubsAction_not_applicable);
156                     return;
157                 }
158             }
159             IMember[] members= new IMember[] { (IMember)element };
160             if (ElementValidator.checkValidateEdit(members, getShell(), getDialogTitle()))
161                 run(members[0].getCompilationUnit(), members);
162         } catch (CoreException e) {
163             ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
164         }
165     }
166
167     //---- Helpers -------------------------------------------------------------------
168

169     /**
170      * Note this method is for internal use only.
171      *
172      * @param cu the compilation unit
173      * @param members an array of members
174      */

175     public void run(ICompilationUnit cu, IMember[] members) {
176         try {
177             AddJavaDocStubOperation op= new AddJavaDocStubOperation(members);
178             PlatformUI.getWorkbench().getProgressService().runInUI(
179                 PlatformUI.getWorkbench().getProgressService(),
180                 new WorkbenchRunnableAdapter(op, op.getScheduleRule()),
181                 op.getScheduleRule());
182         } catch (InvocationTargetException JavaDoc e) {
183             ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
184         } catch (InterruptedException JavaDoc e) {
185             // operation canceled
186
}
187     }
188     
189     private IMember[] getSelectedMembers(IStructuredSelection selection) {
190         List JavaDoc elements= selection.toList();
191         int nElements= elements.size();
192         if (nElements > 0) {
193             IMember[] res= new IMember[nElements];
194             ICompilationUnit cu= null;
195             for (int i= 0; i < nElements; i++) {
196                 Object JavaDoc curr= elements.get(i);
197                 if (curr instanceof IMethod || curr instanceof IType || curr instanceof IField) {
198                     IMember member= (IMember)curr; // limit to methods, types & fields
199
if (! member.exists()) {
200                         return null;
201                     }
202                     if (i == 0) {
203                         cu= member.getCompilationUnit();
204                         if (cu == null) {
205                             return null;
206                         }
207                     } else if (!cu.equals(member.getCompilationUnit())) {
208                         return null;
209                     }
210                     if (member instanceof IType && member.getElementName().length() == 0) {
211                         return null; // anonymous type
212
}
213                     res[i]= member;
214                 } else {
215                     return null;
216                 }
217             }
218             return res;
219         }
220         return null;
221     }
222     
223     private String JavaDoc getDialogTitle() {
224         return ActionMessages.AddJavaDocStubsAction_error_dialogTitle;
225     }
226 }
227
Popular Tags