KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > wizard > AddEjbMethodWizard


1 /*
2  * Created on 14 févr. 2004
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package com.bull.eclipse.jonas.wizard;
8
9 import java.util.Arrays JavaDoc;
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 // Referenced classes of package com.objectlearn.jdt.j2ee.ui.wizards:
28
// CreateEJBBusinessMethodsPage, XDocletTemplates
29

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 JavaDoc());
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 JavaDoc());
58                     methodPage.setStatus(status);
59                     return false;
60                 }
61                 int ii = 0;
62                 String JavaDoc 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 JavaDoc());
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 JavaDoc 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 JavaDoc());
77                             methodPage.setStatus(status);
78                             return false;
79                         }
80                     }
81
82                 }
83                 theMethod.setBody(generateBody(returnType));
84 // theMethod.setComment(generateComment(methodPage.getMethodType(), methodPage.getInterfaceType()));
85
theType.addChild(theMethod);
86                 String JavaDoc newContents = theCompUnit.getContents();
87                 theClass.getCompilationUnit().getBuffer().setContents(newContents);
88                 theClass.getCompilationUnit().save(null, true);
89             }
90             catch(Exception JavaDoc e)
91             {
92                 JonasLauncherPlugin.log(e);
93             }
94         return true;
95     }
96
97     private String JavaDoc generateComment(String JavaDoc type, String JavaDoc viewType)
98     {
99     // String commentContent = XDocletTemplates.getStringReplacingParameter("xdocletTag@ejb_method", "type", type);
100
// commentContent = XDocletTemplates.setParameterValue(commentContent, "viewType", viewType);
101
// return commentContent;
102
return "";
103     }
104
105     private String JavaDoc generateBody(String JavaDoc returnType)
106     {
107         String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc integerTypes[] = {
179         "byte", "char", "int", "long", "short"
180     };
181     private String JavaDoc doubleTypes[] = {
182         "double", "float"
183     };
184 }
185
Popular Tags