1 11 package org.eclipse.core.filebuffers.manipulation; 12 13 import org.eclipse.core.internal.filebuffers.FileBuffersPlugin; 14 import org.eclipse.core.internal.filebuffers.NLSUtility; 15 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.OperationCanceledException; 21 import org.eclipse.core.runtime.Path; 22 import org.eclipse.core.runtime.Status; 23 import org.eclipse.core.runtime.SubProgressMonitor; 24 25 import org.eclipse.core.resources.IContainer; 26 import org.eclipse.core.resources.IFolder; 27 import org.eclipse.core.resources.IProject; 28 import org.eclipse.core.resources.IResource; 29 import org.eclipse.core.resources.IWorkspace; 30 import org.eclipse.core.resources.IWorkspaceRoot; 31 import org.eclipse.core.resources.IWorkspaceRunnable; 32 33 34 40 public class ContainerCreator { 41 42 private IPath fContainerFullPath; 43 private IContainer fContainer; 44 private IWorkspace fWorkspace; 45 46 53 public ContainerCreator(IWorkspace workspace, IPath fullPath) { 54 fWorkspace= workspace; 55 fContainerFullPath = fullPath; 56 } 57 58 59 67 public IContainer createContainer(IProgressMonitor progressMonitor) throws CoreException { 68 IWorkspaceRunnable runnable= new IWorkspaceRunnable() { 69 public void run(IProgressMonitor monitor) throws CoreException { 70 monitor.beginTask(FileBuffersMessages.ContainerCreator_task_creatingContainer, fContainerFullPath.segmentCount()); 71 if (fContainer != null) 72 return; 73 74 IWorkspaceRoot root= fWorkspace.getRoot(); 76 IResource found= root.findMember(fContainerFullPath); 77 if (found instanceof IContainer) { 78 fContainer= (IContainer) found; 79 return; 80 } else if (found != null) { 81 throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.ContainerCreator_destinationMustBeAContainer, fContainerFullPath), null)); 83 } 84 85 fContainer= root; 87 for (int i = 0; i < fContainerFullPath.segmentCount(); i++) { 88 String currentSegment = fContainerFullPath.segment(i); 89 IResource resource = fContainer.findMember(currentSegment); 90 if (resource != null) { 91 if (resource instanceof IContainer) { 92 fContainer= (IContainer) resource; 93 monitor.worked(1); 94 } else { 95 throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.ContainerCreator_destinationMustBeAContainer, resource.getFullPath()), null)); 97 } 98 } 99 else { 100 if (i == 0) { 101 IProject projectHandle= createProjectHandle(root, currentSegment); 102 IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1); 103 fContainer= createProject(projectHandle, subMonitor); 104 subMonitor.done(); 105 } 106 else { 107 IFolder folderHandle= createFolderHandle(fContainer, currentSegment); 108 IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 1); 109 fContainer= createFolder(folderHandle, subMonitor); 110 subMonitor.done(); 111 } 112 } 113 } 114 } 115 }; 116 117 IWorkspaceRoot root= fWorkspace.getRoot(); 119 IPath existingParentPath= fContainerFullPath; 120 while (!root.exists(existingParentPath)) 121 existingParentPath= existingParentPath.removeLastSegments(1); 122 123 IResource schedulingRule= root.findMember(existingParentPath); 124 fWorkspace.run(runnable, schedulingRule, IWorkspace.AVOID_UPDATE, progressMonitor); 125 return fContainer; 126 } 127 128 private IFolder createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException { 129 folderHandle.create(false, true, monitor); 130 if (monitor.isCanceled()) 131 throw new OperationCanceledException(); 132 return folderHandle; 133 } 134 135 private IFolder createFolderHandle(IContainer container, String folderName) { 136 return container.getFolder(new Path(folderName)); 137 } 138 139 private IProject createProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException { 140 monitor.beginTask("", 100); try { 142 143 IProgressMonitor subMonitor= new SubProgressMonitor(monitor, 50); 144 projectHandle.create(subMonitor); 145 subMonitor.done(); 146 147 if (monitor.isCanceled()) 148 throw new OperationCanceledException(); 149 150 subMonitor= new SubProgressMonitor(monitor, 50); 151 projectHandle.open(subMonitor); 152 subMonitor.done(); 153 154 if (monitor.isCanceled()) 155 throw new OperationCanceledException(); 156 157 } finally { 158 monitor.done(); 159 } 160 161 return projectHandle; 162 } 163 164 private IProject createProjectHandle(IWorkspaceRoot root, String projectName) { 165 return root.getProject(projectName); 166 } 167 } 168 | Popular Tags |