1 package org.enhydra.kelp.eclipse.enhydraWizard; 2 3 import java.io.File ; 4 import java.lang.reflect.InvocationTargetException ; 5 import java.util.ArrayList ; 6 import java.util.HashSet ; 7 import java.util.Iterator ; 8 9 import org.eclipse.core.resources.IFile; 10 import org.eclipse.core.resources.IProject; 11 import org.eclipse.core.resources.IResource; 12 import org.eclipse.core.resources.IResourceVisitor; 13 import org.eclipse.core.resources.ResourcesPlugin; 14 import org.eclipse.core.runtime.CoreException; 15 import org.eclipse.core.runtime.IPath; 16 import org.eclipse.core.runtime.IProgressMonitor; 17 import org.eclipse.core.runtime.NullProgressMonitor; 18 import org.eclipse.core.runtime.Path; 19 import org.eclipse.core.runtime.Platform; 20 import org.eclipse.core.runtime.SubProgressMonitor; 21 import org.eclipse.jdt.core.IClasspathEntry; 22 import org.eclipse.jdt.core.ICompilationUnit; 23 import org.eclipse.jdt.core.IJavaProject; 24 import org.eclipse.jdt.core.IPackageDeclaration; 25 import org.eclipse.jdt.core.IPackageFragmentRoot; 26 import org.eclipse.jdt.core.JavaConventions; 27 import org.eclipse.jdt.core.JavaCore; 28 import org.eclipse.jdt.core.JavaModelException; 29 import org.eclipse.jdt.internal.core.ClasspathEntry; 30 import org.eclipse.jdt.internal.ui.JavaPlugin; 31 import org.eclipse.jdt.internal.ui.preferences.NewJavaProjectPreferencePage; 32 import org.eclipse.jdt.internal.ui.util.ExceptionHandler; 33 import org.eclipse.jdt.internal.ui.wizards.buildpaths.BuildPathsBlock; 34 import org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage; 35 import org.eclipse.jface.dialogs.MessageDialog; 36 import org.eclipse.jface.operation.IRunnableWithProgress; 37 import org.eclipse.jface.wizard.IWizardPage; 38 import org.enhydra.tool.ToolBoxInfo; 39 40 48 public class NewEnhydraWizardSecondPage 49 extends JavaCapabilityConfigurationPage { 50 51 private NewEnhydraWizardPage fMainPage; 52 private IPath fCurrProjectLocation; 53 private boolean fProjectCreated; 54 private boolean firstUpdate = true; 55 56 59 public NewEnhydraWizardSecondPage() { 60 super(); 61 62 fCurrProjectLocation= fMainPage.getLocationPath(); 63 fProjectCreated= false; 64 } 65 66 69 public NewEnhydraWizardSecondPage(NewEnhydraWizardPage mainPage) { 70 super(); 71 72 fMainPage= mainPage; 73 fCurrProjectLocation= fMainPage.getLocationPath(); 74 fProjectCreated= false; 75 } 76 77 private boolean canDetectExistingClassPath(IPath projLocation) { 78 return projLocation.toFile().exists() && !Platform.getLocation().equals(projLocation); 79 } 80 81 private void update() { 82 IPath projLocation = fMainPage.getLocationPath(); 83 if (!projLocation.equals(fCurrProjectLocation) && canDetectExistingClassPath(projLocation)) { 84 String title = NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.EarlyCreationDialog.title"); String description = NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.EarlyCreationDialog.description"); if (MessageDialog.openQuestion(getShell(), title, description)) { 87 createAndDetect(); 88 } 89 } 90 91 fCurrProjectLocation = projLocation; 92 93 IJavaProject prevProject = getJavaProject(); 94 IProject currProject = fMainPage.getProjectHandle(); 95 if ((prevProject == null) || !currProject.equals(prevProject.getProject())) { 96 init(JavaCore.create(currProject), null, null, false); 97 } 98 99 if (firstUpdate) { 100 IClasspathEntry[] sourceDirectories = createSourceDirectories(); 101 IClasspathEntry[] myClasspaths = createMyClasspaths(); 102 IClasspathEntry[] allEntries = joinEntries(sourceDirectories, myClasspaths); 103 104 try { 105 init(getJavaProject(), new Path(getOutputLocation() + "/classes"), allEntries, false); 106 } catch (NullPointerException e) { 107 NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.CreatingEnhydraClasspathOperation.error"); 108 } 109 110 firstUpdate = false; 111 } 112 113 } 114 115 private void createAndDetect() { 116 IRunnableWithProgress op= new IRunnableWithProgress() { 117 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 118 if (monitor == null) 119 monitor= new NullProgressMonitor(); 120 121 monitor.beginTask(NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.EarlyCreationOperation.desc"), 3); try { 123 createProject(new SubProgressMonitor(monitor, 1)); 124 initFromExistingStructures(new SubProgressMonitor(monitor, 2)); 125 } catch (CoreException e) { 126 throw new InvocationTargetException (e); 127 } 128 } 129 }; 130 131 try { 132 getContainer().run(false, true, op); 133 } catch (InvocationTargetException e) { 134 String title= NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.EarlyCreationOperation.error.title"); String message= NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.EarlyCreationOperation.error.desc"); ExceptionHandler.handle(e, getShell(), title, message); 137 } catch (InterruptedException e) { 138 } 140 } 141 142 145 public void setVisible(boolean visible) { 146 if (visible) { 147 update(); 148 } 149 super.setVisible(visible); 150 } 151 152 155 public IWizardPage getPreviousPage() { 156 if (fProjectCreated) { 157 return null; 158 } 159 return super.getPreviousPage(); 160 } 161 162 public IRunnableWithProgress getRunnable() { 163 return new IRunnableWithProgress() { 164 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 165 if (monitor == null) 166 monitor= new NullProgressMonitor(); 167 168 monitor.beginTask(NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.NormalCreationOperation.desc"), 4); try { 170 createProject(new SubProgressMonitor(monitor, 1)); 171 if (getJavaProject() == null) { 172 initFromExistingStructures(new SubProgressMonitor(monitor, 1)); 173 } else { 174 monitor.worked(1); 175 } 176 configureJavaProject(new SubProgressMonitor(monitor, 2)); 177 } catch (CoreException e) { 178 throw new InvocationTargetException (e); 179 } finally { 180 monitor.done(); 181 } 182 } 183 }; 184 } 185 186 private void createProject(IProgressMonitor monitor) throws CoreException { 187 IProject project= fMainPage.getProjectHandle(); 188 IPath projectLocation= fMainPage.getLocationPath(); 189 BuildPathsBlock.createProject(project, projectLocation, monitor); 190 fProjectCreated= true; 191 } 192 193 private void initFromExistingStructures(IProgressMonitor monitor) throws CoreException { 194 if (monitor == null) 195 monitor= new NullProgressMonitor(); 196 197 monitor.beginTask(NewEnhydraWizardMessages.getString("NewEnhydraWizardSecondPage.DetectingClasspathOperation.desc"), 2); try { 199 IProject project= fMainPage.getProjectHandle(); 200 201 if (project.getFile(".classpath").exists()) { init(JavaCore.create(project), null, null, false); 203 monitor.worked(2); 204 } else{ 205 final HashSet sourceFolders= new HashSet (); 206 IResourceVisitor visitor= new IResourceVisitor() { 207 public boolean visit(IResource resource) throws CoreException { 208 return doVisit(resource, sourceFolders); 209 } 210 }; 211 project.accept(visitor); 212 monitor.worked(1); 213 214 IClasspathEntry[] entries= null; 215 IPath outputLocation= null; 216 217 if (!sourceFolders.isEmpty()) { 218 int nSourceFolders= sourceFolders.size(); 219 IClasspathEntry[] jreEntries= NewJavaProjectPreferencePage.getDefaultJRELibrary(); 220 entries= new IClasspathEntry[nSourceFolders + jreEntries.length]; 221 Iterator iter = sourceFolders.iterator(); 222 for (int i = 0; i < nSourceFolders; i++) { 223 entries[i]= JavaCore.newSourceEntry((IPath) iter.next()); 224 } 225 System.arraycopy(jreEntries, 0, entries, nSourceFolders, jreEntries.length); 226 227 IPath projPath= project.getFullPath(); 228 if (nSourceFolders == 1 && entries[0].getPath().equals(projPath)) { 229 outputLocation= projPath; 230 } else { 231 outputLocation= projPath.append(NewJavaProjectPreferencePage.getOutputLocationName()); 232 } 233 if (!JavaConventions.validateClasspath(JavaCore.create(project), entries, outputLocation).isOK()) { 234 outputLocation= null; 235 entries= null; 236 } 237 } 238 init(JavaCore.create(project), outputLocation, entries, false); 239 monitor.worked(1); 240 } 241 } finally { 242 monitor.done(); 243 } 244 245 } 246 247 private boolean doVisit(IResource resource, HashSet sourceFolders) throws JavaModelException { 248 if (!sourceFolders.isEmpty()) { 249 IResource curr= resource; 250 while (curr.getType() != IResource.ROOT) { 251 if (sourceFolders.contains(curr.getFullPath())) { 252 return false; 253 } 254 curr= curr.getParent(); 255 } 256 } 257 if (resource.getType() == IResource.FILE) { 258 if ("java".equals(resource.getFileExtension())) { ICompilationUnit cu= JavaCore.createCompilationUnitFrom((IFile) resource); 260 if (cu != null) { 261 IPath packPath= resource.getParent().getFullPath(); 262 IPackageDeclaration[] decls= cu.getPackageDeclarations(); 263 if (decls.length == 0) { 264 sourceFolders.add(packPath); 265 } else { 266 IPath relpath= new Path(decls[0].getElementName().replace('.', '/')); 267 int remainingSegments= packPath.segmentCount() - relpath.segmentCount(); 268 if (remainingSegments >= 0) { 269 IPath prefix= packPath.uptoSegment(remainingSegments); 270 IPath common= packPath.removeFirstSegments(remainingSegments); 271 if (common.equals(relpath)) { 272 sourceFolders.add(prefix); 273 } 274 } 275 } 276 } 277 } 278 } 279 return true; 280 } 281 282 283 286 public void performCancel() { 287 if (fProjectCreated) { 288 try { 289 fMainPage.getProjectHandle().delete(false, false, null); 290 } catch (CoreException e) { 291 JavaPlugin.log(e); 292 } 293 } 294 } 295 296 297 316 public void init(IJavaProject jproject, IPath defaultOutputLocation, IClasspathEntry[] defaultEntries, boolean defaultsOverrideExistingClasspath) { 317 super.init(jproject, defaultOutputLocation, defaultEntries, defaultsOverrideExistingClasspath); 318 } 319 320 public IClasspathEntry[] createMyClasspaths() { 321 322 final int jarFileElements = 29; 323 IClasspathEntry[] classpaths = getRawClassPath(); 324 IClasspathEntry[] myClasspaths = new IClasspathEntry[classpaths.length + jarFileElements - 1]; 329 for(int i = 1; i < classpaths.length; i++) { 330 myClasspaths[i-1] = classpaths[i]; 331 } 332 333 334 String enh_home = ToolBoxInfo.getEnhydraRoot(); 335 String java_home = System.getProperty("java.home"); 336 337 int i = classpaths.length - 1; 338 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 339 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/log4j.jar"), null, null, false); 340 341 i++; 342 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 343 ClasspathEntry.CPE_LIBRARY, new Path(java_home+"/../lib/tools.jar"), null, null, false); 344 345 i++; 346 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 347 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/admin.jar"), null, null, false); 348 349 i++; 350 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 351 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/core.jar"), null, null, false); 352 353 i++; 354 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 355 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/util.jar"), null, null, false); 356 357 i++; 358 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 359 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/dbmanager.jar"), null, null, false); 360 361 i++; 362 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 363 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/dbmanager-api.jar"), null, null, false); 364 i++; 365 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 366 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/dsconnection.jar"), null, null, false); 367 i++; 368 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 369 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/stdcaches.jar"), null, null, false); 370 i++; 371 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 372 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/stdconnection.jar"), null, null, false); 373 i++; 374 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 375 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/stdtransaction.jar"), null, null, false); 376 i++; 377 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 378 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/enhydra.jar"), null, null, false); 379 380 i++; 381 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 382 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/gnu-regexp.jar"), null, null, false); 383 384 i++; 385 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 386 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/jtidy.jar"), null, null, false); 387 388 i++; 389 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 390 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/servlet.jar"), null, null, false); 391 392 i++; 393 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 394 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/snoop.jar"), null, null, false); 395 396 i++; 397 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 398 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/tomcat.jar"), null, null, false); 399 404 i++; 405 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 406 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/dom/xmlc-chtml.jar"), null, null, false); 407 408 i++; 409 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 410 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/dom/xmlc-voicexml.jar"), null, null, false); 411 412 i++; 413 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 414 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/dom/xmlc-wml.jar"), null, null, false); 415 417 i++; 418 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 422 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/xercesImpl.jar"), null, null, false); 423 424 i++; 425 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 426 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/xml-apis.jar"), null, null, false); 427 428 i++; 429 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 430 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/dom3-xml-apis.jar"), null, null, false); 431 433 438 i++; 439 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 440 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/xhtml.jar"), null, null, false); 441 443 i++; 444 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 445 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/xmlc.jar"), null, null, false); 446 447 i++; 448 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 449 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/tools/SunClasses/xmlcSupport.jar"), null, null, false); 450 451 455 i++; 456 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 457 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/lib/build/toolbox.jar"), null, null, false); 458 459 i++; 460 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 461 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/dods.jar"), null, null, false); 462 463 i++; 464 myClasspaths[i] = new ClasspathEntry(IPackageFragmentRoot.K_SOURCE, 465 ClasspathEntry.CPE_LIBRARY, new Path(enh_home + "/dods/lib/ejen.jar"), null, null, false); 466 467 468 return myClasspaths; 469 470 } 471 472 473 private IClasspathEntry[] createSourceDirectories() { 474 final HashSet sourceFolders = new HashSet (); 475 IClasspathEntry[] entries = null; 476 IPath outputLocation = null; 477 IProject project = fMainPage.getProjectHandle(); 478 IPath projPath = project.getFullPath(); 479 480 if (!sourceFolders.isEmpty()) { 481 int nSourceFolders = sourceFolders.size(); 482 IClasspathEntry[] jreEntries = NewJavaProjectPreferencePage.getDefaultJRELibrary(); 483 entries = new IClasspathEntry[nSourceFolders + jreEntries.length]; 484 Iterator iter = sourceFolders.iterator(); 485 for (int i = 0; i < nSourceFolders; i++) { 486 entries[i] = JavaCore.newSourceEntry((IPath) iter.next()); 487 } 488 System.arraycopy(jreEntries, 0, entries, nSourceFolders, jreEntries.length); 489 490 491 if (nSourceFolders == 1 && entries[0].getPath().equals(projPath)) { 492 outputLocation = projPath; 493 } else { 494 outputLocation = projPath.append(NewJavaProjectPreferencePage.getOutputLocationName()); 495 } 496 if (!JavaConventions.validateClasspath(JavaCore.create(project), entries, outputLocation).isOK()) { 497 outputLocation= null; 498 entries= null; 499 } 500 501 } else { 502 entries = new IClasspathEntry[0]; 503 } 504 505 String projDirName = projPath.segment(projPath.segmentCount() - 1); 506 IClasspathEntry[] myEntries = new IClasspathEntry[entries.length + 3]; 507 myEntries[entries.length] = JavaCore.newSourceEntry((IPath) new Path("/" + projDirName + "/src")); 508 myEntries[entries.length + 1] = JavaCore.newSourceEntry((IPath) new Path("/" + projDirName + "/xmlc_generated_src")); 509 myEntries[entries.length + 2] = JavaCore.newSourceEntry((IPath) new Path("/" + projDirName + "/dods_generated_src")); 510 511 return myEntries; 512 } 513 514 private IClasspathEntry[] joinEntries(IClasspathEntry[] a, IClasspathEntry[] b) { 515 516 IClasspathEntry[] result; 517 try { 518 result = new IClasspathEntry[a.length + b.length]; 519 for(int i = 0; i < a.length; i++) { 520 result[i] = a[i]; 521 } 522 for(int i = a.length; i < a.length + b.length; i++) { 523 result[i] = b[i - a.length]; 524 } 525 } catch (NullPointerException e) { 526 if(a == null) { 527 if(b == null) return null; 528 else return b; 529 } else { 530 if(b == null) return null; 531 else return a; 532 } 533 } 534 535 return result; 536 } 537 538 539 } 540 | Popular Tags |