001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.dialogs;
011:
012: import org.eclipse.core.resources.IContainer;
013: import org.eclipse.core.resources.IFolder;
014: import org.eclipse.core.resources.IProject;
015: import org.eclipse.core.resources.IResource;
016: import org.eclipse.core.resources.IWorkspaceRoot;
017: import org.eclipse.core.resources.IWorkspaceRunnable;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.IPath;
020: import org.eclipse.core.runtime.IProgressMonitor;
021: import org.eclipse.core.runtime.IStatus;
022: import org.eclipse.core.runtime.OperationCanceledException;
023: import org.eclipse.core.runtime.Path;
024: import org.eclipse.core.runtime.Status;
025: import org.eclipse.core.runtime.SubProgressMonitor;
026: import org.eclipse.osgi.util.NLS;
027: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
029:
030: /**
031: * For creating folder resources that currently do not exist,
032: * along a given workspace path.
033: * <p>
034: * This class may be instantiated; it is not intended to be subclassed.
035: * </p>
036: * <p>
037: * Example usage:
038: * <pre>
039: * ContainerGenerator gen = new ContainerGenerator(new Path("/A/B"));
040: * IContainer res = null;
041: * try {
042: * res = gen.getContainer(monitor); // creates project A and folder B if required
043: * } catch (CoreException e) {
044: * // handle failure
045: * } catch (OperationCanceledException e) {
046: * // handle cancelation
047: * }
048: * </pre>
049: * </p>
050: */
051: public class ContainerGenerator {
052: private IPath containerFullPath;
053:
054: private IContainer container;
055:
056: /**
057: * Creates a generator for the container resource (folder or project) at the
058: * given workspace path. Assumes the path has already been validated.
059: * <p>
060: * Call <code>getContainer</code> to create any missing resources along the
061: * path.
062: * </p>
063: *
064: * @param containerPath the workspace path of the container
065: */
066: public ContainerGenerator(IPath containerPath) {
067: super ();
068: this .containerFullPath = containerPath;
069: }
070:
071: /**
072: * Creates a folder resource for the given folder handle.
073: *
074: * @param folderHandle the handle to create a folder resource
075: * @param monitor the progress monitor to show visual progress
076: * @return the folder handle (<code>folderHandle</code>)
077: * @exception CoreException if the operation fails
078: * @exception OperationCanceledException if the operation is canceled
079: */
080: private IFolder createFolder(IFolder folderHandle,
081: IProgressMonitor monitor) throws CoreException {
082: folderHandle.create(false, true, monitor);
083:
084: if (monitor.isCanceled()) {
085: throw new OperationCanceledException();
086: }
087:
088: return folderHandle;
089: }
090:
091: /**
092: * Creates a folder resource handle for the folder with the given name.
093: * This method does not create the folder resource; this is the responsibility
094: * of <code>createFolder</code>.
095: *
096: * @param container the resource container
097: * @param folderName the name of the folder
098: * @return the new folder resource handle
099: */
100: private IFolder createFolderHandle(IContainer container,
101: String folderName) {
102: return container.getFolder(new Path(folderName));
103: }
104:
105: /**
106: * Creates a project resource for the given project handle.
107: *
108: * @param projectHandle the handle to create a project resource
109: * @param monitor the progress monitor to show visual progress
110: * @return the project handle (<code>projectHandle</code>)
111: * @exception CoreException if the operation fails
112: * @exception OperationCanceledException if the operation is canceled
113: */
114: private IProject createProject(IProject projectHandle,
115: IProgressMonitor monitor) throws CoreException {
116: try {
117: monitor.beginTask("", 2000);//$NON-NLS-1$
118:
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: /**
136: * Creates a project resource handle for the project with the given name.
137: * This method does not create the project resource; this is the responsibility
138: * of <code>createProject</code>.
139: *
140: * @param root the workspace root resource
141: * @param projectName the name of the project
142: * @return the new project resource handle
143: */
144: private IProject createProjectHandle(IWorkspaceRoot root,
145: String projectName) {
146: return root.getProject(projectName);
147: }
148:
149: /**
150: * Ensures that this generator's container resource exists. Creates any missing
151: * resource containers along the path; does nothing if the container resource
152: * already exists.
153: * <p>
154: * Note: This method should be called within a workspace modify operation since
155: * it may create resources.
156: * </p>
157: *
158: * @param monitor a progress monitor
159: * @return the container resource
160: * @exception CoreException if the operation fails
161: * @exception OperationCanceledException if the operation is canceled
162: */
163: public IContainer generateContainer(IProgressMonitor monitor)
164: throws CoreException {
165: IDEWorkbenchPlugin.getPluginWorkspace().run(
166: new IWorkspaceRunnable() {
167: public void run(IProgressMonitor monitor)
168: throws CoreException {
169: monitor
170: .beginTask(
171: IDEWorkbenchMessages.ContainerGenerator_progressMessage,
172: 1000 * containerFullPath
173: .segmentCount());
174: if (container != null) {
175: return;
176: }
177:
178: // Does the container exist already?
179: IWorkspaceRoot root = getWorkspaceRoot();
180: container = (IContainer) root
181: .findMember(containerFullPath);
182: if (container != null) {
183: return;
184: }
185:
186: // Create the container for the given path
187: container = root;
188: for (int i = 0; i < containerFullPath
189: .segmentCount(); i++) {
190: String currentSegment = containerFullPath
191: .segment(i);
192: IResource resource = container
193: .findMember(currentSegment);
194: if (resource != null) {
195: if (resource.getType() == IResource.FILE) {
196: String msg = NLS
197: .bind(
198: IDEWorkbenchMessages.ContainerGenerator_pathOccupied,
199: resource
200: .getFullPath()
201: .makeRelative());
202: throw new CoreException(
203: new Status(
204: IStatus.ERROR,
205: IDEWorkbenchPlugin.IDE_WORKBENCH,
206: 1, msg, null));
207: }
208: container = (IContainer) resource;
209: monitor.worked(1000);
210: } else {
211: if (i == 0) {
212: IProject projectHandle = createProjectHandle(
213: root, currentSegment);
214: container = createProject(
215: projectHandle,
216: new SubProgressMonitor(
217: monitor, 1000));
218: } else {
219: IFolder folderHandle = createFolderHandle(
220: container, currentSegment);
221: container = createFolder(
222: folderHandle,
223: new SubProgressMonitor(
224: monitor, 1000));
225: }
226: }
227: }
228: }
229: }, null, IResource.NONE, monitor);
230: return container;
231: }
232:
233: /**
234: * Returns the workspace root resource handle.
235: *
236: * @return the workspace root resource handle
237: */
238: private IWorkspaceRoot getWorkspaceRoot() {
239: return IDEWorkbenchPlugin.getPluginWorkspace().getRoot();
240: }
241: }
|