1 7 package com.bull.eclipse.jonas.wizard; 8 9 import java.util.Arrays ; 10 11 import org.eclipse.core.runtime.Status; 12 import org.eclipse.jdt.core.IType; 13 import org.eclipse.jdt.core.jdom.DOMFactory; 14 import org.eclipse.jdt.core.jdom.IDOMCompilationUnit; 15 import org.eclipse.jdt.core.jdom.IDOMMethod; 16 import org.eclipse.jdt.core.jdom.IDOMType; 17 import org.eclipse.jdt.internal.ui.JavaPlugin; 18 import org.eclipse.jdt.internal.ui.JavaPluginImages; 19 import org.eclipse.jface.viewers.IStructuredSelection; 20 import org.eclipse.jface.wizard.Wizard; 21 import org.eclipse.ui.INewWizard; 22 import org.eclipse.ui.IWorkbench; 23 24 import com.bull.eclipse.jonas.JonasLauncherPlugin; 25 26 27 30 public class AddEjbMethodWizard extends Wizard 31 implements INewWizard 32 { 33 34 public AddEjbMethodWizard() 35 { 36 37 setDefaultPageImageDescriptor(JavaPluginImages.DESC_WIZBAN_NEWMETH); 38 setDialogSettings(JavaPlugin.getDefault().getDialogSettings()); 39 setWindowTitle("Create a new EJB Method"); 40 } 41 42 public boolean performFinish() 43 { 44 if(!methodPage.isPageValid()) 45 { 46 org.eclipse.core.runtime.IStatus status = new Status(4, "AddEjbMethodWizard", 4, "Invalid Type", new Exception ()); 47 methodPage.setStatus(status); 48 return false; 49 } 50 if(theClass != null) 51 try 52 { 53 DOMFactory jdom = new DOMFactory(); 54 IDOMMethod theMethod = jdom.createMethod(methodPage.getMethodName() + "{}"); 55 if(theMethod == null) 56 { 57 org.eclipse.core.runtime.IStatus status = new Status(4, JonasLauncherPlugin.getPluginId(), 4, "Invalid Type", new Exception ()); 58 methodPage.setStatus(status); 59 return false; 60 } 61 int ii = 0; 62 String returnType = theMethod.getReturnType(); 63 if(returnType == null) 64 { 65 org.eclipse.core.runtime.IStatus status = new Status(4, JonasLauncherPlugin.getPluginId(), 4, "Invalid return type", new Exception ()); 66 methodPage.setStatus(status); 67 return false; 68 } 69 if(theMethod.getParameterTypes() != null) 70 { 71 for(int i = 0; i < theMethod.getParameterTypes().length; i++) 72 { 73 String array_element = theMethod.getParameterTypes()[i]; 74 if(array_element == null) 75 { 76 org.eclipse.core.runtime.IStatus status = new Status(4, JonasLauncherPlugin.getPluginId(), 4, "Invalid parameter type", new Exception ()); 77 methodPage.setStatus(status); 78 return false; 79 } 80 } 81 82 } 83 theMethod.setBody(generateBody(returnType)); 84 theType.addChild(theMethod); 86 String newContents = theCompUnit.getContents(); 87 theClass.getCompilationUnit().getBuffer().setContents(newContents); 88 theClass.getCompilationUnit().save(null, true); 89 } 90 catch(Exception e) 91 { 92 JonasLauncherPlugin.log(e); 93 } 94 return true; 95 } 96 97 private String generateComment(String type, String viewType) 98 { 99 return ""; 103 } 104 105 private String generateBody(String returnType) 106 { 107 String body = null; 108 109 if(Arrays.binarySearch(integerTypes, returnType) >= 0) 110 body = "{ \n return 0; \n}\n"; 111 else 112 if(Arrays.binarySearch(doubleTypes, returnType) >= 0) 113 body = "{ \n return 0.0; \n}\n"; 114 else 115 if("boolean".equals(returnType)) 116 body = "{ \n return false;\n}\n"; 117 else 118 if("void".equals(returnType)) 119 body = "{ \n}\n"; 120 else 121 body = "{ \n return null; \n}\n"; 122 return body; 123 } 124 125 public void addPages() 126 { 127 super.addPages(); 128 129 org.eclipse.core.resources.IWorkspace workspace = JavaPlugin.getWorkspace(); 130 methodPage = new AddEjbMethodWizardPage("EJB Business Methods Page"); 131 addPage(methodPage); 132 } 133 134 public void init(IWorkbench workbench, IStructuredSelection selection) 135 { 136 this.workbench = workbench; 137 this.selection = selection; 138 try 139 { 140 if(selection != null && selection.getFirstElement() != null && (selection.getFirstElement() instanceof IType)) 141 { 142 theClass = (IType)selection.getFirstElement(); 143 DOMFactory jdom = new DOMFactory(); 144 theCompUnit = jdom.createCompilationUnit(theClass.getCompilationUnit().getSource(), theClass.getCompilationUnit().getElementName()); 145 theType = jdom.createType(theClass.getSource()); 146 theType = (IDOMType)theCompUnit.getChild(theType.getName()); 147 } 148 } 149 catch(Exception e) 150 { 151 JonasLauncherPlugin.log(e); 152 } 153 } 154 155 public void init(IWorkbench workbench, IType ejb) 156 { 157 this.workbench = workbench; 158 theClass = ejb; 159 try 160 { 161 DOMFactory jdom = new DOMFactory(); 162 theCompUnit = jdom.createCompilationUnit(theClass.getCompilationUnit().getSource(), theClass.getCompilationUnit().getElementName()); 163 theType = jdom.createType(theClass.getSource()); 164 theType = (IDOMType)theCompUnit.getChild(theType.getName()); 165 } 166 catch(Exception e) 167 { 168 JonasLauncherPlugin.log(e); 169 } 170 } 171 172 private AddEjbMethodWizardPage methodPage; 173 private IStructuredSelection selection; 174 private IWorkbench workbench; 175 private IType theClass; 176 private IDOMType theType; 177 private IDOMCompilationUnit theCompUnit; 178 private String integerTypes[] = { 179 "byte", "char", "int", "long", "short" 180 }; 181 private String doubleTypes[] = { 182 "double", "float" 183 }; 184 } 185 | Popular Tags |