]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/ui/org/eclipse/jdt/ui/actions/AddJavaDocStubAction.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-after / ui / org / eclipse / jdt / ui / actions / AddJavaDocStubAction.java
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
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;
14 import java.util.List;
15
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.OperationCanceledException;
18
19 import org.eclipse.jface.viewers.IStructuredSelection;
20
21 import org.eclipse.jface.text.ITextSelection;
22
23 import org.eclipse.ui.IEditorPart;
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.eclipse.ui.PlatformUI;
26
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;
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.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;
46
47 /**
48  * Create Javadoc comment stubs for the selected members.
49  * <p>
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.
52  * <p>
53  * The action is applicable to structured selections containing elements
54  * of type <code>IMember</code>.
55  *
56  * <p>
57  * This class may be instantiated; it is not intended to be subclassed.
58  * </p>
59  *
60  * @since 2.0
61  *
62  * @noextend This class is not intended to be subclassed by clients.
63  */
64 public class AddJavaDocStubAction extends SelectionDispatchAction {
65
66         public CompilationUnitEditor fEditor;
67
68         /**
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>.
72          *
73          * @param site the site providing context information for this action
74          */
75         public AddJavaDocStubAction(IWorkbenchSite site) {
76                 super(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);
81         }
82
83         /**
84          * Note: This constructor is for internal use only. Clients should not call this constructor.
85          * @param editor the compilation unit editor
86          *
87          * @noreference This constructor is not intended to be referenced by clients.
88          */
89         public AddJavaDocStubAction(CompilationUnitEditor editor) {
90                 this(editor.getEditorSite());
91                 fEditor= editor;
92                 setEnabled(checkEnabledEditor());
93         }
94
95         //---- Structured Viewer -----------------------------------------------------------
96
97         /* (non-Javadoc)
98          * Method declared on SelectionDispatchAction
99          */
100         @Override
101         public void selectionChanged(IStructuredSelection selection) {
102                 IMember[] members= getSelectedMembers(selection);
103                 setEnabled(members != null && members.length > 0);
104         }
105
106         /* (non-Javadoc)
107          * Method declared on SelectionDispatchAction
108          */
109         @Override
110         public void run(IStructuredSelection selection) {
111                 IMember[] members= getSelectedMembers(selection);
112                 if (members == null || members.length == 0) {
113                         return;
114                 }
115
116                 try {
117                         ICompilationUnit cu= members[0].getCompilationUnit();
118                         if (!ActionUtil.isEditable(getShell(), cu)) {
119                                 return;
120                         }
121
122                         // open the editor, forces the creation of a working copy
123                         IEditorPart editor= JavaUI.openInEditor(cu);
124
125                         if (ElementValidator.check(members, getShell(), getDialogTitle(), false))
126                                 run(members);
127                         JavaModelUtil.reconcile(cu);
128                         EditorUtility.revealInEditor(editor, members[0]);
129
130                 } catch (CoreException e) {
131                         ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
132                 }
133         }
134
135         //---- Java Editor --------------------------------------------------------------
136
137         /* (non-Javadoc)
138          * Method declared on SelectionDispatchAction
139          */
140         @Override
141         public void selectionChanged(ITextSelection selection) {
142         }
143
144         private boolean checkEnabledEditor() {
145                 return fEditor != null && SelectionConverter.canOperateOn(fEditor);
146         }
147
148         /* (non-Javadoc)
149          * Method declared on SelectionDispatchAction
150          */
151         @Override
152         public void run(ITextSelection selection) {
153                 fEditor.generated_2825797301311231316(this);
154         }
155
156         
157
158         //---- Helpers -------------------------------------------------------------------
159
160         /**
161          * Note this method is for internal use only.
162          *
163          * @param members an array of members
164          */
165         public void run(IMember[] members) {
166                 AddJavaDocStubOperation op= new AddJavaDocStubOperation(members);
167                 if (members.length < 11) {
168                         try {
169                                 op.run(null);
170                         } catch (CoreException e) {
171                                 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed);
172                         } catch (OperationCanceledException e) {
173                                 // operation canceled
174                         }
175                         return;
176                 }
177
178                 try {
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
184                 }
185         }
186
187         private IMember[] getSelectedMembers(IStructuredSelection selection) {
188                 List<?> elements= selection.toList();
189                 int nElements= elements.size();
190                 if (nElements > 0) {
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()) {
198                                                 return null;
199                                         }
200                                         if (i == 0) {
201                                                 cu= member.getCompilationUnit();
202                                                 if (cu == null) {
203                                                         return null;
204                                                 }
205                                         } else if (!cu.equals(member.getCompilationUnit())) {
206                                                 return null;
207                                         }
208                                         if (member instanceof IType && member.getElementName().length() == 0) {
209                                                 return null; // anonymous type
210                                         }
211                                         res[i]= member;
212                                 } else {
213                                         return null;
214                                 }
215                         }
216                         return res;
217                 }
218                 return null;
219         }
220
221         public String getDialogTitle() {
222                 return ActionMessages.AddJavaDocStubsAction_error_dialogTitle;
223         }
224 }