KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > ContainerGenerator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
32  * For creating folder resources that currently do not exist,
33  * along a given workspace path.
34  * <p>
35  * This class may be instantiated; it is not intended to be subclassed.
36  * </p>
37  * <p>
38  * Example usage:
39  * <pre>
40  * ContainerGenerator gen = new ContainerGenerator(new Path("/A/B"));
41  * IContainer res = null;
42  * try {
43  * res = gen.getContainer(monitor); // creates project A and folder B if required
44  * } catch (CoreException e) {
45  * // handle failure
46  * } catch (OperationCanceledException e) {
47  * // handle cancelation
48  * }
49  * </pre>
50  * </p>
51  */

52 public class ContainerGenerator {
53     private IPath containerFullPath;
54
55     private IContainer container;
56
57     /**
58      * Creates a generator for the container resource (folder or project) at the
59      * given workspace path. Assumes the path has already been validated.
60      * <p>
61      * Call <code>getContainer</code> to create any missing resources along the
62      * path.
63      * </p>
64      *
65      * @param containerPath the workspace path of the container
66      */

67     public ContainerGenerator(IPath containerPath) {
68         super();
69         this.containerFullPath = containerPath;
70     }
71
72     /**
73      * Creates a folder resource for the given folder handle.
74      *
75      * @param folderHandle the handle to create a folder resource
76      * @param monitor the progress monitor to show visual progress
77      * @return the folder handle (<code>folderHandle</code>)
78      * @exception CoreException if the operation fails
79      * @exception OperationCanceledException if the operation is canceled
80      */

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     /**
93      * Creates a folder resource handle for the folder with the given name.
94      * This method does not create the folder resource; this is the responsibility
95      * of <code>createFolder</code>.
96      *
97      * @param container the resource container
98      * @param folderName the name of the folder
99      * @return the new folder resource handle
100      */

101     private IFolder createFolderHandle(IContainer container, String JavaDoc 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, String JavaDoc projectName) {
145         return root.getProject(projectName);
146     }
147
148     /**
149      * Ensures that this generator's container resource exists. Creates any missing
150      * resource containers along the path; does nothing if the container resource
151      * already exists.
152      * <p>
153      * Note: This method should be called within a workspace modify operation since
154      * it may create resources.
155      * </p>
156      *
157      * @param monitor a progress monitor
158      * @return the container resource
159      * @exception CoreException if the operation fails
160      * @exception OperationCanceledException if the operation is canceled
161      */

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                 // Does the container exist already?
174
IWorkspaceRoot root = getWorkspaceRoot();
175                 container = (IContainer) root.findMember(containerFullPath);
176                 if (container != null) {
177                     return;
178                 }
179
180                 // Create the container for the given path
181
container = root;
182                 for (int i = 0; i < containerFullPath.segmentCount(); i++) {
183                     String JavaDoc currentSegment = containerFullPath.segment(i);
184                     IResource resource = container.findMember(currentSegment);
185                     if (resource != null) {
186                         if (resource.getType() == IResource.FILE) {
187                             String JavaDoc 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     /**
212      * Returns the workspace root resource handle.
213      *
214      * @return the workspace root resource handle
215      */

216     private IWorkspaceRoot getWorkspaceRoot() {
217         return IDEWorkbenchPlugin.getPluginWorkspace().getRoot();
218     }
219 }
220
Popular Tags