1 5 package com.bull.eclipse.jonas.wizard; 6 7 import java.io.File ; 8 import java.util.StringTokenizer ; 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 44 45 public class CreateEJBWizard extends Wizard implements INewWizard 46 { 47 48 private CreateEJBWizardPage EjbPage; 50 private boolean jonasproj = false; 51 private String jonasprojName = ""; 52 private boolean jonasRootSet = false; 53 private String pkgName; 54 private IProject proj; 55 private JonasProject projNature; 56 private IStructuredSelection selection; 57 private String 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 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 jonasRoot = JonasLauncherPlugin.getDefault().getJonasDir(); 94 String jonasBase = JonasLauncherPlugin.getDefault().getBaseDir(); 95 jonasRootSet = ( new File (jonasRoot).isDirectory() ) 96 && (new File (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 120 private String getPathRelativeToProject( IJavaElement elem ) 121 { 122 IJavaProject proj = elem.getJavaProject(); 123 String ps = proj.getPath().toString(); 124 String 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 getPathRelativeToProject( IFolder f ) 132 { 133 IProject proj = f.getProject(); 134 String ps = proj.getFullPath().toString(); 135 String 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 147 public static IFolder getPkgFolder( IProject p, IFolder base, String pkg ) 148 { 149 if( pkg==null || pkg.equals("") ) return base; 150 IFolder result = base; 151 StringTokenizer st = new StringTokenizer (pkg,"."); 152 while( st.hasMoreTokens() ) { 153 String 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 msg ) 161 { 162 MessageDialog.openInformation( 163 getShell(), 164 "Info", 165 msg ); 166 } 167 168 170 public void init( IWorkbench wb, IStructuredSelection sel ) 171 { 172 workbench = wb; 173 selection = sel; 174 175 checkIfJonasRootSet(); 176 177 IJavaElement jelem = getInitialJavaElement( sel ); 180 if( jelem!=null ) { 181 IJavaProject jproj = jelem.getJavaProject(); 182 if( jproj!=null ) { 183 IPackageFragment frag; 184 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 Object 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 214 public boolean performFinish() 215 { 216 String 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 e.printStackTrace(); 228 } 229 } 230 231 jonasprojName = EjbPage.getProjName(); 232 proj = ResourcesPlugin.getWorkspace().getRoot().getProject(jonasprojName); 234 projNature = JonasProject.create(proj); 235 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 createFolderAndParents( pkgfolder ); 242 } 243 if (!EjbPage.getEJBType().equals("Deploy Unit")) { 245 NewbeanRunner runner = new NewbeanRunner(projNature); 246 runner.setNature(projNature); 247 runner.setPkgFolder(pkgfolder); runner.RunNewbean(EjbPage); 249 } else { 250 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 jelemCls = "null"; 266 String jelemName = "n/a"; 267 String jelemProj = "n/a"; 268 Object obj = sel.getFirstElement(); 269 String 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 303 protected IJavaElement getInitialJavaElement( IStructuredSelection selection ) 304 { 305 IJavaElement jelem= null; 306 if( selection!=null && !selection.isEmpty() ) { 307 Object 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); } 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 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 e ) {} 346 } 347 return jelem; 348 } 349 350 351 } 352 | Popular Tags |