KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > filebuffers > manipulation > ContainerCreator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.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 /**
35  * Helper class to create a container and all missing
36  * parent containers.
37  *
38  * @since 3.1
39  */

40 public class ContainerCreator {
41
42     private IPath fContainerFullPath;
43     private IContainer fContainer;
44     private IWorkspace fWorkspace;
45
46     /**
47      * Constructs a container creator for the given path
48      * in the given workspace.
49      *
50      * @param workspace the workspace in which to create the container
51      * @param fullPath the full path of the container, must not denote a file
52      */

53     public ContainerCreator(IWorkspace workspace, IPath fullPath) {
54         fWorkspace= workspace;
55         fContainerFullPath = fullPath;
56     }
57
58
59     /**
60      * Creates this container.
61      *
62      * @param progressMonitor the progress monitor or <code>null</code> if none
63      * @return the container specified by this container creator's full path
64      * @throws CoreException if this container creator's full path denotes a file or creating
65      * either the project or folders for the given container fails
66      */

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                 // Does the container exist already?
75
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                     // fContainerFullPath specifies a file as directory
82
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, NLSUtility.format(FileBuffersMessages.ContainerCreator_destinationMustBeAContainer, fContainerFullPath), null));
83                 }
84
85                 // Create the containers for the given path
86
fContainer= root;
87                 for (int i = 0; i < fContainerFullPath.segmentCount(); i++) {
88                     String JavaDoc 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                             // fContainerFullPath specifies a file as directory
96
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         // Get scheduling rule
118
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 JavaDoc folderName) {
136         return container.getFolder(new Path(folderName));
137     }
138
139     private IProject createProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException {
140         monitor.beginTask("", 100);//$NON-NLS-1$
141
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 JavaDoc projectName) {
165         return root.getProject(projectName);
166     }
167 }
168
Popular Tags