1 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 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 63 public class AddJavaDocStubAction extends SelectionDispatchAction { 64 65 private CompilationUnitEditor fEditor; 66 67 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 86 public AddJavaDocStubAction(CompilationUnitEditor editor) { 87 this(editor.getEditorSite()); 88 fEditor= editor; 89 setEnabled(checkEnabledEditor()); 90 } 91 92 94 97 public void selectionChanged(IStructuredSelection selection) { 98 IMember[] members= getSelectedMembers(selection); 99 setEnabled(members != null && members.length > 0); 100 } 101 102 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 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 132 135 public void selectionChanged(ITextSelection selection) { 136 } 137 138 private boolean checkEnabledEditor() { 139 return fEditor != null && SelectionConverter.canOperateOn(fEditor); 140 } 141 142 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 169 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 e) { 183 ExceptionHandler.handle(e, getShell(), getDialogTitle(), ActionMessages.AddJavaDocStubsAction_error_actionFailed); 184 } catch (InterruptedException e) { 185 } 187 } 188 189 private IMember[] getSelectedMembers(IStructuredSelection selection) { 190 List 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 curr= elements.get(i); 197 if (curr instanceof IMethod || curr instanceof IType || curr instanceof IField) { 198 IMember member= (IMember)curr; 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; } 213 res[i]= member; 214 } else { 215 return null; 216 } 217 } 218 return res; 219 } 220 return null; 221 } 222 223 private String getDialogTitle() { 224 return ActionMessages.AddJavaDocStubsAction_error_dialogTitle; 225 } 226 } 227 | Popular Tags |