KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Sep 17, 2003
3  *
4  */

5 package com.bull.eclipse.jonas.wizard;
6
7 import java.io.File JavaDoc;
8 import java.util.StringTokenizer JavaDoc;
9
10 import org.eclipse.core.resources.IContainer;
11 import org.eclipse.core.resources.IFolder;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IAdaptable;
17 import org.eclipse.jdt.core.IJavaElement;
18 import org.eclipse.jdt.core.IJavaProject;
19 import org.eclipse.jdt.core.IPackageFragment;
20 import org.eclipse.jdt.core.IPackageFragmentRoot;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.internal.ui.JavaPlugin;
23 import org.eclipse.jdt.internal.ui.viewsupport.IViewPartInputProvider;
24 import org.eclipse.jface.dialogs.MessageDialog;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.wizard.Wizard;
27 import org.eclipse.ui.IEditorPart;
28 import org.eclipse.ui.INewWizard;
29 import org.eclipse.ui.IWorkbench;
30 import org.eclipse.ui.IWorkbenchPart;
31
32 import com.bull.eclipse.jonas.JonasLauncherPlugin;
33 import com.bull.eclipse.jonas.JonasProject;
34 import com.bull.eclipse.newbean.NewbeanRunner;
35
36 /**
37  * @author steve krupanszky
38  *
39  * A wizard to create an EJB and related files for a Jonas project.
40  * This is accomplished through a subordinate call to 'newbean' class,
41  * which is provided by the JOnAS product.
42  *
43  */

44
45 public class CreateEJBWizard extends Wizard implements INewWizard
46 {
47
48     // fields listed in alphabetical order
49
private CreateEJBWizardPage EjbPage;
50     private boolean jonasproj = false;
51     private String JavaDoc jonasprojName = "";
52     private boolean jonasRootSet = false;
53     private String JavaDoc pkgName;
54     private IProject proj;
55     private JonasProject projNature;
56     private IStructuredSelection selection;
57     private String JavaDoc srcDir;
58     private IWorkbench workbench;
59
60
61     public CreateEJBWizard()
62     {
63         super();
64         setDialogSettings( JavaPlugin.getDefault().getDialogSettings() );
65         setWindowTitle(JonasLauncherPlugin.getResourceString("wizard.createEJB.windowTitle"));
66     }
67
68     /** @see Wizard
69     */

70     public void addPages()
71     {
72         if( jonasRootSet ) {
73             EjbPage = new CreateEJBWizardPage("EJBWizardPage1");
74             EjbPage.setTitle(JonasLauncherPlugin.getResourceString("wizard.createEJB.mainPage.title"));
75             EjbPage.setDescription(JonasLauncherPlugin.getResourceString("wizard.createEJB.mainPage.description"));
76             if( jonasproj ) {
77                 EjbPage.setProjName(jonasprojName);
78                 EjbPage.setSrcDir(srcDir);
79                 EjbPage.setPkg(pkgName);
80             }
81             addPage(EjbPage);
82         }
83         else {
84             EjbErrorWizardPage errPage = new EjbErrorWizardPage("ErrorWizardPage1");
85             errPage.setTitle(JonasLauncherPlugin.getResourceString("wizard.createEJB.errPage.title"));
86             errPage.setErrorMessage(JonasLauncherPlugin.getResourceString("wizard.createEJB.errPage.text1"));
87             addPage(errPage);
88         }
89     }
90
91     private void checkIfJonasRootSet()
92     {
93         String JavaDoc jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir();
94         String JavaDoc jonasBase = JonasLauncherPlugin.getDefault().getBaseDir();
95         jonasRootSet = ( new File JavaDoc(jonasRoot).isDirectory() )
96                     && (new File JavaDoc(jonasBase).isDirectory() );
97     }
98
99     private void createFolderAndParents( IFolder f )
100     {
101         if( !f.getLocation().toFile().exists() ) {
102             try{
103                 IContainer p = f.getParent();
104                 if( p!=null && p instanceof IFolder ) {
105                     createFolderAndParents( (IFolder)p );
106                 }
107                 f.create( false,true,null );
108             }
109             catch( CoreException e ) {
110                 JonasLauncherPlugin.log("JOPE: exception creating folder: " + e );
111             }
112         }
113     }
114
115     /*
116      * returns the path relative to the project location (unlike
117      * getPath() which is relative to the workspace). used to get
118      * the complete name of a multi-component packageFragmentRoot.
119      */

120     private String JavaDoc getPathRelativeToProject( IJavaElement elem )
121     {
122         IJavaProject proj = elem.getJavaProject();
123         String JavaDoc ps = proj.getPath().toString();
124         String JavaDoc es = elem.getPath().toString();
125         if( es.startsWith(ps) ) {
126             es = es.substring(ps.length());
127             if( es.startsWith("/") ) { es = es.substring(1); }
128         }
129         return es;
130     }
131     private String JavaDoc getPathRelativeToProject( IFolder f )
132     {
133         IProject proj = f.getProject();
134         String JavaDoc ps = proj.getFullPath().toString();
135         String JavaDoc fs = f.getFullPath().toString();
136         if( fs.startsWith(ps) ) {
137             fs = fs.substring(ps.length());
138             if( fs.startsWith("/") ) { fs = fs.substring(1); }
139         }
140         return fs;
141     }
142
143     /*
144      * returns an IFolder object for the package 'pkg' emanating from
145      * the source folder 'base'.
146      */

147     public static IFolder getPkgFolder( IProject p, IFolder base, String JavaDoc pkg )
148     {
149         if( pkg==null || pkg.equals("") ) return base;
150         IFolder result = base;
151         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pkg,".");
152         while( st.hasMoreTokens() ) {
153             String JavaDoc tok = st.nextToken();
154             if( result==null ) { result = p.getFolder(tok); }
155             else { result = result.getFolder(tok); }
156         }
157         return result;
158     }
159
160     void info( String JavaDoc msg )
161     {
162         MessageDialog.openInformation(
163             /*workbench.getActiveWorkbenchWindow().*/getShell(),
164             "Info",
165             msg );
166     }
167
168     /** @see INewWizard
169     */

170     public void init( IWorkbench wb, IStructuredSelection sel )
171     {
172         workbench = wb;
173         selection = sel;
174
175         checkIfJonasRootSet();
176
177         //showDebugInfo( sel );
178
// determine if selection represents a jonas project
179
IJavaElement jelem = getInitialJavaElement( sel );
180         if( jelem!=null ) {
181             IJavaProject jproj = jelem.getJavaProject();
182             if( jproj!=null ) {
183                 IPackageFragment frag;
184                 //THIS DOESN'T WORK: frag = (IPackageFragment)jelem.getAdapter(IPackageFragment.class);
185
frag = (IPackageFragment)jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
186                 if( frag!=null ) {
187                     pkgName = frag.getElementName();
188                 }
189                 IPackageFragmentRoot fragroot;
190                 fragroot = (IPackageFragmentRoot)jelem.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
191                 if( fragroot!=null ) {
192                     srcDir = getPathRelativeToProject(fragroot);
193                 } else {
194                     // it's not a fragroot, see if it's just a folder
195
Object JavaDoc selectedElement = selection.getFirstElement();
196                     if( selectedElement instanceof IFolder ) {
197                         srcDir = getPathRelativeToProject((IFolder)selectedElement);
198                     }
199                 }
200                 
201                 jonasprojName = jproj.getElementName();
202                 proj = jproj.getProject();
203                 try {
204                     jonasproj = proj.hasNature(JonasLauncherPlugin.NATURE_ID);
205                     projNature = (JonasProject)proj.getNature(JonasLauncherPlugin.NATURE_ID);
206                 }
207                 catch( CoreException e ) {}
208             }
209         }
210     }
211
212     /** @see Wizard
213     */

214     public boolean performFinish()
215     {
216         // get the project/srcdir/pkg names from the wizard page
217
String JavaDoc pkg = EjbPage.getPkgName();
218         srcDir = projNature.getSrcEJBDir();
219         if (srcDir.trim().equals("")) {
220             srcDir = "components";
221             projNature.setSrcEJBDir("components");
222             projNature.saveProperties();
223             try {
224                 projNature.createSrcEJBFolder();
225             } catch (CoreException e) {
226                 // TODO Auto-generated catch block
227
e.printStackTrace();
228             }
229         }
230
231         jonasprojName = EjbPage.getProjName();
232         // get the corresponding JonasProject object
233
proj = ResourcesPlugin.getWorkspace().getRoot().getProject(jonasprojName);
234         projNature = JonasProject.create(proj);
235         // make sure that all directories in the path exist
236
IFolder pkgfolder = null;
237         if( srcDir.length()>0 ) { pkgfolder = proj.getFolder(srcDir); }
238         pkgfolder = getPkgFolder( proj, pkgfolder, pkg );
239         if( !pkgfolder.getLocation().toFile().exists() ) {
240             // (this can't be the best way to do this)
241
createFolderAndParents( pkgfolder );
242         }
243 // now call the Jonas 'newbean' class
244
if (!EjbPage.getEJBType().equals("Deploy Unit")) {
245             NewbeanRunner runner = new NewbeanRunner(projNature);
246             runner.setNature(projNature);
247             runner.setPkgFolder(pkgfolder); // tell it where to put java files
248
runner.RunNewbean(EjbPage);
249         } else {
250             // now to refresh project so it picks up the new files
251
try{
252                 projNature.setPackageProjectFolder(EjbPage.getPkgName());
253                 projNature.setJarEjbName(EjbPage.getJarName());
254                 projNature.getProject().refreshLocal( IResource.DEPTH_INFINITE, null );
255             }
256             catch( CoreException e ) {}
257
258         }
259         return true;
260     }
261
262     private void showDebugInfo( IStructuredSelection sel )
263     {
264         boolean hasJonasNature = false;
265         String JavaDoc jelemCls = "null";
266         String JavaDoc jelemName = "n/a";
267         String JavaDoc jelemProj = "n/a";
268         Object JavaDoc obj = sel.getFirstElement();
269         String JavaDoc selDescrip = "empty";
270
271         if( obj!=null ) {
272             selDescrip = obj.getClass().toString();
273             IJavaElement jelem = getInitialJavaElement( sel );
274             if( jelem!=null ) {
275                 jelemCls = jelem.getClass().getName();
276                 jelemName = jelem.getElementName();
277                 IJavaProject jproj = jelem.getJavaProject();
278                 if( jproj!=null ) {
279                     jelemProj = jproj.getElementName();
280                     try{ hasJonasNature = jproj.getProject().hasNature(JonasLauncherPlugin.NATURE_ID); }
281                     catch( CoreException e ) {}
282                 }
283             }
284         }
285         info(
286             "selection is: " + selDescrip + "\n" +
287             "corr java element is: " + jelemName +
288             " of type " + jelemCls + "\n" +
289             "in project: " + jelemProj +
290             (hasJonasNature?" (a Jonas project)":"")
291         );
292     }
293
294
295     /**
296      * Utility method to inspect a selection to find a Java element.
297      * from org.eclipse.jdt.ui.wizards
298      *
299      * @param selection the selection to be inspected
300      * @return a Java element to be used as the initial selection, or <code>null</code>
301      * if no Java element exists in the given selection
302      */

303     protected IJavaElement getInitialJavaElement( IStructuredSelection selection )
304     {
305         IJavaElement jelem= null;
306         if( selection!=null && !selection.isEmpty() ) {
307             Object JavaDoc selectedElement = selection.getFirstElement();
308             if( selectedElement instanceof IAdaptable ) {
309                 IAdaptable adaptable = (IAdaptable)selectedElement;
310                 
311                 jelem = (IJavaElement)adaptable.getAdapter(IJavaElement.class);
312                 if( jelem==null ) {
313                     IResource resource = (IResource)adaptable.getAdapter(IResource.class);
314                     if( resource!=null && resource.getType()!=IResource.ROOT ) {
315                         while( jelem==null && resource.getType()!=IResource.PROJECT ) {
316                             resource = resource.getParent();
317                             jelem = (IJavaElement)resource.getAdapter(IJavaElement.class);
318                         }
319                         if( jelem==null ) {
320                             jelem = JavaCore.create(resource); // java project
321
}
322                     }
323                 }
324             }
325         }
326         if( jelem==null ) {
327             IWorkbenchPart part = JavaPlugin.getActivePage().getActivePart();
328             if( part instanceof IEditorPart ) {
329                 part = JavaPlugin.getActivePage().getActiveEditor();
330             }
331             
332             if( part instanceof IViewPartInputProvider ) {
333                 Object JavaDoc elem = ((IViewPartInputProvider)part).getViewPartInput();
334                 if( elem instanceof IJavaElement ) {
335                     jelem = (IJavaElement)elem;
336                 }
337             }
338         }
339         if( jelem==null || jelem.getElementType() == IJavaElement.JAVA_MODEL ) {
340             try {
341                 IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
342                 if( projects.length==1 ) {
343                     jelem = projects[0];
344                 }
345             } catch( Exception JavaDoc e ) {}
346         }
347         return jelem;
348     }
349
350
351 }
352
Popular Tags