1 11 package org.eclipse.jdt.internal.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.swt.widgets.Shell; 19 20 import org.eclipse.jface.action.Action; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.dialogs.MessageDialog; 23 import org.eclipse.jface.viewers.ISelection; 24 import org.eclipse.jface.viewers.IStructuredSelection; 25 import org.eclipse.jface.window.Window; 26 27 import org.eclipse.ui.IEditorPart; 28 import org.eclipse.ui.PlatformUI; 29 30 import org.eclipse.jdt.core.Flags; 31 import org.eclipse.jdt.core.IMember; 32 import org.eclipse.jdt.core.IMethod; 33 import org.eclipse.jdt.core.IType; 34 import org.eclipse.jdt.core.JavaModelException; 35 36 import org.eclipse.jdt.internal.corext.codemanipulation.AddMethodStubOperation; 37 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings; 38 import org.eclipse.jdt.internal.corext.codemanipulation.IRequestQuery; 39 import org.eclipse.jdt.internal.corext.util.JavaModelUtil; 40 import org.eclipse.jdt.internal.corext.util.Messages; 41 42 import org.eclipse.jdt.ui.JavaElementLabels; 43 44 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 45 import org.eclipse.jdt.internal.ui.JavaPlugin; 46 import org.eclipse.jdt.internal.ui.JavaUIMessages; 47 import org.eclipse.jdt.internal.ui.javaeditor.EditorUtility; 48 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings; 49 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 50 51 57 public class AddMethodStubAction extends Action { 58 59 private ISelection fSelection; 60 private IType fParentType; 61 62 public AddMethodStubAction() { 63 super(JavaUIMessages.AddMethodStubAction_label); 64 setDescription(JavaUIMessages.AddMethodStubAction_description); 65 setToolTipText(JavaUIMessages.AddMethodStubAction_tooltip); 66 67 PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.ADD_METHODSTUB_ACTION); 68 } 69 70 71 public boolean init(IType parentType, ISelection selection) { 72 if (canActionBeAdded(parentType, selection)) { 73 fParentType= parentType; 74 fSelection= selection; 75 if (parentType != null) { 76 try { 77 if (parentType.isInterface()) { 78 setText(Messages.format(JavaUIMessages.AddMethodStubAction_detailed_implement, parentType.getElementName())); 79 } else { 80 setText(Messages.format(JavaUIMessages.AddMethodStubAction_detailed_override, parentType.getElementName())); 81 } 82 } catch (JavaModelException e) { 83 JavaPlugin.log(e); 84 } 85 } else { 86 setText(JavaUIMessages.AddMethodStubAction_label); 87 } 88 return true; 89 } 90 fParentType= null; 91 fSelection= null; 92 return false; 93 } 94 95 public void run() { 96 if (!canActionBeAdded(fParentType, fSelection)) { 97 return; 98 } 99 100 Shell shell= JavaPlugin.getActiveWorkbenchShell(); 101 try { 102 IEditorPart editor= EditorUtility.openInEditor(fParentType); 104 105 CodeGenerationSettings settings= JavaPreferencesSettings.getCodeGenerationSettings(fParentType.getJavaProject()); 106 107 List list= ((IStructuredSelection)fSelection).toList(); 108 IMethod[] methods= (IMethod[]) list.toArray(new IMethod[list.size()]); 109 AddMethodStubOperation op= new AddMethodStubOperation(fParentType, methods, settings, createOverrideQuery(), createReplaceQuery(), false); 110 111 PlatformUI.getWorkbench().getProgressService().runInUI( 112 PlatformUI.getWorkbench().getProgressService(), 113 new WorkbenchRunnableAdapter(op, op.getScheduleRule()), 114 op.getScheduleRule()); 115 116 IMethod[] res= op.getCreatedMethods(); 117 if (res != null && res.length > 0 && editor != null) { 118 EditorUtility.revealInEditor(editor, res[0]); 119 } 120 } catch (InvocationTargetException e) { 121 ExceptionHandler.handle(e, shell, JavaUIMessages.AddMethodStubAction_error_title, null); 122 } catch (CoreException e) { 123 ExceptionHandler.handle(e, shell, JavaUIMessages.AddMethodStubAction_error_title, null); 124 } catch (InterruptedException e) { 125 } 127 } 128 129 private IRequestQuery createOverrideQuery() { 130 return new IRequestQuery() { 131 public int doQuery(IMember method) { 132 String methodName= JavaElementLabels.getElementLabel(method, JavaElementLabels.M_PARAMETER_TYPES); 133 String declTypeName= JavaElementLabels.getElementLabel(method.getDeclaringType(), 0); 134 String formattedMessage; 135 try { 136 if (Flags.isFinal(method.getFlags())) { 137 formattedMessage= Messages.format(JavaUIMessages.AddMethodStubAction_OverridesFinalDialog_message, new String [] { methodName, declTypeName }); 138 } else { 139 formattedMessage= Messages.format(JavaUIMessages.AddMethodStubAction_OverridesPrivateDialog_message, new String [] { methodName, declTypeName }); 140 } 141 } catch (JavaModelException e) { 142 JavaPlugin.log(e.getStatus()); 143 return IRequestQuery.CANCEL; 144 } 145 return showQueryDialog(formattedMessage); 146 } 147 }; 148 } 149 150 private IRequestQuery createReplaceQuery() { 151 return new IRequestQuery() { 152 public int doQuery(IMember method) { 153 String methodName= JavaElementLabels.getElementLabel(method, JavaElementLabels.M_PARAMETER_TYPES); 154 String formattedMessage= Messages.format(JavaUIMessages.AddMethodStubAction_ReplaceExistingDialog_message, methodName); 155 return showQueryDialog(formattedMessage); 156 } 157 }; 158 } 159 160 161 private int showQueryDialog(final String message) { 162 int[] returnCodes= {IRequestQuery.YES, IRequestQuery.YES_ALL, IRequestQuery.NO, IRequestQuery.CANCEL}; 164 final Shell shell= JavaPlugin.getActiveWorkbenchShell(); 165 if (shell == null) { 166 JavaPlugin.logErrorMessage("AddMethodStubAction.showQueryDialog: No active shell found"); return IRequestQuery.CANCEL; 168 } 169 final int[] result= { Window.CANCEL }; 170 shell.getDisplay().syncExec(new Runnable () { 171 public void run() { 172 String title= JavaUIMessages.AddMethodStubAction_QueryDialog_title; 173 String [] options= {IDialogConstants.YES_LABEL, IDialogConstants.YES_TO_ALL_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL}; 174 MessageDialog dialog= new MessageDialog(shell, title, null, message, MessageDialog.QUESTION, options, 0); 175 result[0]= dialog.open(); 176 } 177 }); 178 int returnVal= result[0]; 179 return returnVal < 0 ? IRequestQuery.CANCEL : returnCodes[returnVal]; 180 } 181 182 185 public static boolean canActionBeAdded(IType parentType, ISelection selection) { 186 if (parentType == null || parentType.getCompilationUnit() == null || !JavaModelUtil.isEditable(parentType.getCompilationUnit()) || 187 !(selection instanceof IStructuredSelection) || selection.isEmpty()) { 188 return false; 189 } 190 191 Object [] elems= ((IStructuredSelection)selection).toArray(); 192 int nSelected= elems.length; 193 if (nSelected > 0) { 194 for (int i= 0; i < nSelected; i++) { 195 Object elem= elems[i]; 196 if (!(elem instanceof IMethod)) { 197 return false; 198 } 199 IMethod meth= (IMethod)elem; 200 if (meth.getDeclaringType().equals(parentType)) { 201 return false; 202 } 203 } 204 return true; 205 } 206 return false; 207 } 208 209 210 } 211 | Popular Tags |