1 11 package org.eclipse.ui.dialogs; 12 13 import org.eclipse.core.resources.IContainer; 14 import org.eclipse.core.resources.IFolder; 15 import org.eclipse.core.resources.IProject; 16 import org.eclipse.core.resources.IResource; 17 import org.eclipse.core.resources.IWorkspaceRoot; 18 import org.eclipse.core.resources.IWorkspaceRunnable; 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.core.runtime.IPath; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.IStatus; 23 import org.eclipse.core.runtime.OperationCanceledException; 24 import org.eclipse.core.runtime.Path; 25 import org.eclipse.core.runtime.Status; 26 import org.eclipse.core.runtime.SubProgressMonitor; 27 import org.eclipse.osgi.util.NLS; 28 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 29 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 30 31 52 public class ContainerGenerator { 53 private IPath containerFullPath; 54 55 private IContainer container; 56 57 67 public ContainerGenerator(IPath containerPath) { 68 super(); 69 this.containerFullPath = containerPath; 70 } 71 72 81 private IFolder createFolder(IFolder folderHandle, IProgressMonitor monitor) 82 throws CoreException { 83 folderHandle.create(false, true, monitor); 84 85 if (monitor.isCanceled()) { 86 throw new OperationCanceledException(); 87 } 88 89 return folderHandle; 90 } 91 92 101 private IFolder createFolderHandle(IContainer container, String folderName) { 102 return container.getFolder(new Path(folderName)); 103 } 104 105 114 private IProject createProject(IProject projectHandle, 115 IProgressMonitor monitor) throws CoreException { 116 try { 117 monitor.beginTask("", 2000); 119 projectHandle.create(new SubProgressMonitor(monitor, 1000)); 120 if (monitor.isCanceled()) { 121 throw new OperationCanceledException(); 122 } 123 124 projectHandle.open(new SubProgressMonitor(monitor, 1000)); 125 if (monitor.isCanceled()) { 126 throw new OperationCanceledException(); 127 } 128 } finally { 129 monitor.done(); 130 } 131 132 return projectHandle; 133 } 134 135 144 private IProject createProjectHandle(IWorkspaceRoot root, String projectName) { 145 return root.getProject(projectName); 146 } 147 148 162 public IContainer generateContainer(IProgressMonitor monitor) 163 throws CoreException { 164 IDEWorkbenchPlugin.getPluginWorkspace().run(new IWorkspaceRunnable() { 165 public void run(IProgressMonitor monitor) throws CoreException { 166 monitor 167 .beginTask( 168 IDEWorkbenchMessages.ContainerGenerator_progressMessage, 1000 * containerFullPath.segmentCount()); 169 if (container != null) { 170 return; 171 } 172 173 IWorkspaceRoot root = getWorkspaceRoot(); 175 container = (IContainer) root.findMember(containerFullPath); 176 if (container != null) { 177 return; 178 } 179 180 container = root; 182 for (int i = 0; i < containerFullPath.segmentCount(); i++) { 183 String currentSegment = containerFullPath.segment(i); 184 IResource resource = container.findMember(currentSegment); 185 if (resource != null) { 186 if (resource.getType() == IResource.FILE) { 187 String msg = NLS.bind(IDEWorkbenchMessages.ContainerGenerator_pathOccupied, resource.getFullPath().makeRelative()); 188 throw new CoreException(new Status(IStatus.ERROR, IDEWorkbenchPlugin.IDE_WORKBENCH, 1, msg, null)); 189 } 190 container = (IContainer) resource; 191 monitor.worked(1000); 192 } else { 193 if (i == 0) { 194 IProject projectHandle = createProjectHandle(root, 195 currentSegment); 196 container = createProject(projectHandle, 197 new SubProgressMonitor(monitor, 1000)); 198 } else { 199 IFolder folderHandle = createFolderHandle( 200 container, currentSegment); 201 container = createFolder(folderHandle, 202 new SubProgressMonitor(monitor, 1000)); 203 } 204 } 205 } 206 } 207 }, null, IResource.NONE, monitor); 208 return container; 209 } 210 211 216 private IWorkspaceRoot getWorkspaceRoot() { 217 return IDEWorkbenchPlugin.getPluginWorkspace().getRoot(); 218 } 219 } 220 | Popular Tags |