Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8045b48be937533663a2f0f5eb8af3a1d352ea37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.filebuffers.manipulation;

import org.eclipse.core.internal.filebuffers.FileBuffersPlugin;
import org.eclipse.core.internal.filebuffers.NLSUtility;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;


/**
 * Helper class to create a container and all missing
 * parent containers.
 *
 * @since 3.1
 */
public class ContainerCreator {

	private IPath fContainerFullPath;
	private IContainer fContainer;
	private IWorkspace fWorkspace;

	/**
	 * Constructs a container creator for the given path
	 * in the given workspace.
	 *
	 * @param workspace the workspace in which to create the container
	 * @param fullPath the full path of the container, must not denote a file
	 */
	public ContainerCreator(IWorkspace workspace, IPath fullPath) {
		fWorkspace= workspace;
		fContainerFullPath = fullPath;
	}


	/**
	 * Creates this container.
	 *
	 * @param progressMonitor the progress monitor or <code>null</code> if none
	 * @return the container specified by this container creator's full path
	 * @throws CoreException if this container creator's full path denotes a file or creating
	 * 							either the project or folders for the given container fails
	 */
	public IContainer createContainer(IProgressMonitor progressMonitor) throws CoreException {
		IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
			@Override
			public void run(IProgressMonitor monitor) throws CoreException {
				SubMonitor subMonitor = SubMonitor.convert(monitor, FileBuffersMessages.ContainerCreator_task_creatingContainer, fContainerFullPath.segmentCount());
				if (fContainer != null)
					return;

				// Does the container exist already?
				IWorkspaceRoot root= fWorkspace.getRoot();
				IResource found= root.findMember(fContainerFullPath);
				if (found instanceof IContainer) {
					fContainer= (IContainer) found;
					return;
				} else if (found != null) {
					// fContainerFullPath specifies a file as directory
					throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.ContainerCreator_destinationMustBeAContainer, fContainerFullPath), null));
				}

				// Create the containers for the given path
				fContainer= root;
				for (int i = 0; i < fContainerFullPath.segmentCount(); i++) {
					String currentSegment = fContainerFullPath.segment(i);
					IResource resource = fContainer.findMember(currentSegment);
					if (resource != null) {
						if (resource instanceof IContainer) {
							fContainer= (IContainer) resource;
							subMonitor.step(1);
						} else {
							// fContainerFullPath specifies a file as directory
							throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.ContainerCreator_destinationMustBeAContainer, resource.getFullPath()), null));
						}
					}
					else {
						if (i == 0) {
							IProject projectHandle= createProjectHandle(root, currentSegment);
							fContainer= createProject(projectHandle, subMonitor.split(1));
						}
						else {
							IFolder folderHandle= createFolderHandle(fContainer, currentSegment);
							fContainer= createFolder(folderHandle, subMonitor.split(1));
						}
					}
				}
			}
		};

		// Get scheduling rule
		IWorkspaceRoot root= fWorkspace.getRoot();
		IPath existingParentPath= fContainerFullPath;
		while (!root.exists(existingParentPath))
			existingParentPath= existingParentPath.removeLastSegments(1);

		IResource schedulingRule= root.findMember(existingParentPath);
		fWorkspace.run(runnable, schedulingRule, IWorkspace.AVOID_UPDATE, progressMonitor);
		return fContainer;
	}

	private IFolder createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
		folderHandle.create(false, true, monitor);
		if (monitor.isCanceled())
			throw new OperationCanceledException();
		return folderHandle;
	}

	private IFolder createFolderHandle(IContainer container, String folderName) {
		return container.getFolder(new Path(folderName));
	}

	private IProject createProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException {
		SubMonitor subMonitor= SubMonitor.convert(monitor, 2);
		projectHandle.create(subMonitor.split(1));
		projectHandle.open(subMonitor.split(1));
		return projectHandle;
	}

	private IProject createProjectHandle(IWorkspaceRoot root, String projectName) {
		return root.getProject(projectName);
	}
}

Back to the top