1 11 12 package org.eclipse.jdt.apt.core.internal.generatedfile; 13 14 import org.eclipse.core.resources.IFolder; 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IWorkspace; 17 import org.eclipse.core.resources.IWorkspaceRoot; 18 import org.eclipse.core.resources.IWorkspaceRunnable; 19 import org.eclipse.core.resources.ResourcesPlugin; 20 import org.eclipse.core.runtime.CoreException; 21 import org.eclipse.core.runtime.IPath; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.core.runtime.IStatus; 24 import org.eclipse.core.runtime.OperationCanceledException; 25 import org.eclipse.core.runtime.Path; 26 import org.eclipse.jdt.apt.core.internal.AptPlugin; 27 import org.eclipse.jdt.apt.core.internal.AptProject; 28 import org.eclipse.jdt.apt.core.internal.util.FileSystemUtil; 29 import org.eclipse.jdt.apt.core.util.AptConfig; 30 import org.eclipse.jdt.core.IClasspathEntry; 31 import org.eclipse.jdt.core.IJavaProject; 32 import org.eclipse.jdt.core.JavaModelException; 33 34 58 public class GeneratedSourceFolderManager { 59 60 private final AptProject _aptProject; 61 62 80 private IFolder _generatedSourceFolder = null; 81 82 86 public GeneratedSourceFolderManager(AptProject aptProject) 87 { 88 _aptProject = aptProject; 89 final IJavaProject javaProject = aptProject.getJavaProject(); 90 91 if (AptConfig.isEnabled(javaProject)) { 95 final IFolder folder = getFolder(); 96 if (folder.exists()) { 97 if (isOnClasspath(folder)) { 98 _generatedSourceFolder = folder; 99 } 100 } 101 } 102 } 103 104 109 private boolean addToClasspath(IFolder srcFolder) { 110 boolean onClasspath = false; 111 try { 112 ClasspathUtil.updateProjectClasspath( _aptProject.getJavaProject(), srcFolder, null ); 113 if(AptPlugin.DEBUG) 114 AptPlugin.trace("Ensured classpath has an entry for " + srcFolder); onClasspath = true; 116 } 117 catch (CoreException e) { 118 e.printStackTrace(); 119 AptPlugin.log(e, "Failed to add classpath entry for generated source folder " + srcFolder.getName()); } 121 return onClasspath; 122 } 123 124 137 private void configure() { 138 139 assert(_generatedSourceFolder == null): "Should have already removed old folder by now"; IFolder srcFolder = getFolderPreference(); 141 if (srcFolder == null) { 142 IStatus status = AptPlugin.createStatus(null, "Could not create generated source folder (" + AptConfig.getGenSrcDir(_aptProject.getJavaProject()) + ")"); AptPlugin.log(status); 145 return; 146 } 147 148 if (createOnDisk(srcFolder)) { 150 if (addToClasspath(srcFolder)) { 152 synchronized ( this ) { 154 _generatedSourceFolder = srcFolder; 155 } 156 } 157 } 158 } 159 160 161 166 public void ensureFolderExists(){ 167 if (!AptConfig.isEnabled(_aptProject.getJavaProject())) { 169 return; 170 } 171 172 IFolder srcFolder = getFolder(); 177 if (srcFolder == null) { 178 IStatus status = AptPlugin.createStatus(null, "Could not create generated source folder (" + AptConfig.getGenSrcDir(_aptProject.getJavaProject()) + ")"); AptPlugin.log(status); 181 return; 182 } 183 184 if (createOnDisk(srcFolder)) { 185 if (isOnClasspath(srcFolder)) { 186 synchronized (this) { 187 _generatedSourceFolder = srcFolder; 189 } 190 } 191 } 192 } 193 194 202 private boolean createOnDisk(IFolder srcFolder) { 203 boolean exists = false; 204 try { 205 srcFolder.refreshLocal( IResource.DEPTH_INFINITE, null ); 207 if (!srcFolder.exists()) { 208 FileSystemUtil.makeDerivedParentFolders(srcFolder); 209 if(AptPlugin.DEBUG) 210 AptPlugin.trace("Created folder " + srcFolder + " on disk"); } 212 exists = true; 213 } 214 catch (CoreException e) { 215 e.printStackTrace(); 216 AptPlugin.log(e, "Failed to ensure existence of generated source folder " + srcFolder.getName()); } 218 return exists; 219 } 220 221 231 public void enabledPreferenceChanged() 232 { 233 final boolean enable = AptConfig.isEnabled(_aptProject.getJavaProject()); 234 if (enable == (_generatedSourceFolder != null)) { 236 if( AptPlugin.DEBUG ) { 237 AptPlugin.trace("enabledChanged() doing nothing; state is already " + enable); } 239 return; 241 } 242 243 if ( AptPlugin.DEBUG ) { 244 AptPlugin.trace("enabledChanged() changing state to " + enable + " for " + _aptProject.getJavaProject().getElementName()); } 247 if( enable ) { 248 configure(); 249 } 250 else { 251 removeFolder(); 252 } 253 } 254 255 263 public void folderNamePreferenceChanged() 264 { 265 final boolean aptEnabled = AptConfig.isEnabled(_aptProject.getJavaProject()); 267 if (!aptEnabled) { 268 return; 269 } 270 271 if (_generatedSourceFolder != null && _generatedSourceFolder.equals(getFolderPreference())) { 273 if( AptPlugin.DEBUG ) { 274 AptPlugin.trace("folderNameChanged() doing nothing; name is already " + _generatedSourceFolder.getProjectRelativePath()); 276 } 277 return; 278 } 279 280 removeFolder(); 281 configure(); 282 } 283 284 292 public void folderDeleted() 293 { 294 _aptProject.projectClean( false ); 295 296 IFolder srcFolder; 297 synchronized(this){ 298 srcFolder = _generatedSourceFolder; 299 _generatedSourceFolder = null; 300 } 301 if(AptPlugin.DEBUG) 302 AptPlugin.trace("set _generatedSourceFolder to null; was " + srcFolder ); } 304 305 318 public IPath getBinaryOutputLocation() 319 throws JavaModelException 320 { 321 IPath outputRootPath = null; 322 IFolder generatedSourceFolder = getFolder(); 323 if ( generatedSourceFolder != null && generatedSourceFolder.exists() ) 324 { 325 IClasspathEntry cpe = ClasspathUtil.findProjectSourcePath( _aptProject.getJavaProject(), generatedSourceFolder ); 326 if ( cpe != null ) 327 outputRootPath = cpe.getOutputLocation(); 328 } 329 330 if ( outputRootPath == null ) 332 outputRootPath = _aptProject.getJavaProject().getOutputLocation(); 333 334 int segments = outputRootPath.matchingFirstSegments( _aptProject.getJavaProject().getPath() ); 336 outputRootPath = outputRootPath.removeFirstSegments( segments ); 337 338 return outputRootPath; 339 } 340 341 348 public IFolder getFolder(){ 349 350 synchronized (this) { 351 if( _generatedSourceFolder != null ) 352 return _generatedSourceFolder; 353 } 354 355 return getFolderPreference(); 356 } 357 358 366 private IFolder getFolderPreference() { 367 final String folderName = AptConfig.getGenSrcDir(_aptProject.getJavaProject()); 368 IFolder folder = null; 369 try { 370 folder = _aptProject.getJavaProject().getProject().getFolder( folderName ); 371 } 372 catch (IllegalArgumentException e) { 373 } 375 return folder; 376 } 377 378 387 public boolean isGeneratedSourceFolder( IFolder folder ) 388 { 389 return folder != null && folder.equals( getFolder() ); 390 } 391 392 private boolean isOnClasspath(IFolder srcFolder) { 393 boolean found = false; 394 try { 395 if (ClasspathUtil.doesClasspathContainEntry( 396 _aptProject.getJavaProject(), null, srcFolder.getFullPath(), null)) { 397 found = true; 398 } 399 } catch (JavaModelException e) { 400 e.printStackTrace(); 401 } 402 return found; 403 } 404 405 409 private void removeFolder() { 410 final IFolder srcFolder; 411 synchronized ( this ) 412 { 413 srcFolder = _generatedSourceFolder; 414 _generatedSourceFolder = null; 415 } 416 if (srcFolder == null) { 417 return; 418 } 419 420 _aptProject.projectClean(false); 422 423 try { 426 if (srcFolder.isDerived()) { 427 ClasspathUtil.removeFromProjectClasspath( _aptProject.getJavaProject(), srcFolder, null ); 428 } 429 } catch (JavaModelException e) { 430 AptPlugin.log( e, "Failed to remove classpath entry for old generated src folder " + srcFolder.getName() ); } 432 433 final IWorkspaceRunnable runnable = new IWorkspaceRunnable(){ 434 public void run(IProgressMonitor monitor) 435 { 436 try { 437 IResource parent = srcFolder.getParent(); 438 boolean deleted = FileSystemUtil.deleteDerivedResources(srcFolder); 439 440 if (deleted) { 442 while (parent.isDerived() && parent.getType() == IResource.FOLDER) { 443 IFolder parentFolder = (IFolder)parent; 444 if (parentFolder.members().length == 0) { 445 parent = parentFolder.getParent(); 446 FileSystemUtil.deleteDerivedResources(parentFolder); 447 } 448 else { 449 break; 450 } 451 } 452 } 453 454 } catch(CoreException e) { 455 AptPlugin.log(e, "failed to delete old generated source folder " + srcFolder.getName() ); } catch(OperationCanceledException cancel) { 457 AptPlugin.log(cancel, "deletion of generated source folder got cancelled"); } 459 } 460 }; 461 IWorkspace ws = ResourcesPlugin.getWorkspace(); 462 try{ 463 ws.run(runnable, ws.getRoot(), IWorkspace.AVOID_UPDATE, null); 464 }catch(CoreException e){ 465 AptPlugin.log(e, "Runnable for deleting old generated source folder " + srcFolder.getName() + " failed."); } 467 } 468 469 475 public static boolean validate(final IJavaProject jproj, final String folderName) { 476 boolean succeeded = false; 477 try { 478 if (jproj != null) { 479 IFolder folder = null; 481 folder = jproj.getProject().getFolder( folderName ); 482 succeeded = (folder != null); 483 } 484 else { 485 IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); 490 IPath state = AptPlugin.getPlugin().getStateLocation(); 491 IPath proposed = new Path(folderName); 492 IPath combined = state.append(proposed); 493 if (combined.segmentCount() <= state.segmentCount()) { 494 return false; 496 } 497 IFolder folder = root.getFolder(combined); 498 succeeded = (folder != null); 499 } 500 } 501 catch (IllegalArgumentException e) { 502 return false; 503 } 504 return succeeded; 505 } 506 507 } 508 | Popular Tags |