/net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/refactoring/participants/RenameFolderParticipant.java

https://github.com/thiagoarrais/eclipsefp · Java · 95 lines · 70 code · 11 blank · 14 comment · 5 complexity · 9b4f7e0031fa160f980303d7eafa4b66 MD5 · raw file

  1. /**
  2. * (c) 2011, Alejandro Serrano
  3. * Released under the terms of the EPL.
  4. */
  5. package net.sf.eclipsefp.haskell.ui.internal.refactoring.participants;
  6. import java.util.ArrayList;
  7. import java.util.Set;
  8. import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
  9. import net.sf.eclipsefp.haskell.util.FileUtil;
  10. import org.eclipse.core.resources.IFile;
  11. import org.eclipse.core.resources.IFolder;
  12. import org.eclipse.core.resources.IResource;
  13. import org.eclipse.core.resources.IResourceVisitor;
  14. import org.eclipse.core.runtime.IPath;
  15. import org.eclipse.core.runtime.IProgressMonitor;
  16. import org.eclipse.core.runtime.OperationCanceledException;
  17. import org.eclipse.core.runtime.Status;
  18. import org.eclipse.ltk.core.refactoring.Change;
  19. import org.eclipse.ltk.core.refactoring.RefactoringStatus;
  20. import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
  21. import org.eclipse.ltk.core.refactoring.participants.RenameParticipant;
  22. /**
  23. * Manages the renaming of a Haskell source folder.
  24. * @author Alejandro Serrano
  25. *
  26. */
  27. public class RenameFolderParticipant extends RenameParticipant {
  28. IFolder folder;
  29. ArrayList<IFile> haskellFiles;
  30. public RenameFolderParticipant() {
  31. // Do nothing
  32. }
  33. @Override
  34. protected boolean initialize( final Object element ) {
  35. if( element instanceof IFolder ) {
  36. this.folder = (IFolder)element;
  37. // Precompute files to be changed
  38. this.haskellFiles = new ArrayList<IFile>();
  39. try {
  40. folder.accept( new IResourceVisitor() {
  41. public boolean visit( final IResource resource ) {
  42. if (resource instanceof IFile && FileUtil.hasHaskellExtension( resource )) {
  43. haskellFiles.add( (IFile )resource ); // Found a Haskell file
  44. }
  45. return true;
  46. }
  47. } );
  48. } catch (Exception e) {
  49. this.haskellFiles.clear();
  50. }
  51. return this.haskellFiles.size() > 0;
  52. }
  53. return false;
  54. }
  55. @Override
  56. public String getName() {
  57. return UITexts.renameFolderParticipant_title;
  58. }
  59. @Override
  60. public RefactoringStatus checkConditions( final IProgressMonitor pm,
  61. final CheckConditionsContext context ) throws OperationCanceledException {
  62. return RefactoringStatus.create( Status.OK_STATUS );
  63. }
  64. @Override
  65. public Change createPreChange( final IProgressMonitor pm ) throws OperationCanceledException {
  66. // Get arguments
  67. String newName = getArguments().getNewName();
  68. IPath oldPath = folder.getProjectRelativePath();
  69. IPath newPath = oldPath.removeLastSegments( 1 ).append( newName );
  70. // Check if it is one of the source folders
  71. Set<IPath> sourcePaths = Util.getPaths( folder.getProject() );
  72. if (sourcePaths.contains( oldPath )) {
  73. return ChangeCreator.createRenameMoveFolderCabalChange( folder.getProject(), oldPath, newPath );
  74. } else {
  75. // Create Haskell file change
  76. return ChangeCreator.createRenameMoveFolderChange( haskellFiles, folder.getProjectRelativePath(),
  77. newPath, getArguments().getUpdateReferences(), UITexts.renameFolderParticipant_title, UITexts.renameParticipant_title );
  78. }
  79. }
  80. @Override
  81. public Change createChange( final IProgressMonitor pm ) throws OperationCanceledException {
  82. return null;
  83. }
  84. }