KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filebuffers > ContainerGenerator


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

11 package org.eclipse.core.internal.filebuffers;
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.IWorkspace;
18 import org.eclipse.core.resources.IWorkspaceRoot;
19 import org.eclipse.core.resources.IWorkspaceRunnable;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IProgressMonitor;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.OperationCanceledException;
25 import org.eclipse.core.runtime.Path;
26 import org.eclipse.core.runtime.Status;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28
29 public class ContainerGenerator {
30     
31     private IPath fContainerFullPath;
32     private IContainer fContainer;
33     private IWorkspace fWorkspace;
34
35     public ContainerGenerator(IWorkspace workspace, IPath containerPath) {
36         fWorkspace= workspace;
37         fContainerFullPath = containerPath;
38     }
39
40     private IFolder createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
41         folderHandle.create(false, true, monitor);
42         if (monitor.isCanceled())
43             throw new OperationCanceledException();
44         return folderHandle;
45     }
46     
47     private IFolder createFolderHandle(IContainer container, String JavaDoc folderName) {
48         return container.getFolder(new Path(folderName));
49     }
50     
51     private IProject createProject(IProject projectHandle, IProgressMonitor monitor) throws CoreException {
52         try {
53             monitor.beginTask("",2000);//$NON-NLS-1$
54

55             projectHandle.create(new SubProgressMonitor(monitor, 1000));
56             if (monitor.isCanceled())
57                 throw new OperationCanceledException();
58     
59             projectHandle.open(new SubProgressMonitor(monitor, 1000));
60             if (monitor.isCanceled())
61                 throw new OperationCanceledException();
62                 
63         } finally {
64             monitor.done();
65         }
66     
67         return projectHandle;
68     }
69
70     private IProject createProjectHandle(IWorkspaceRoot root, String JavaDoc projectName) {
71         return root.getProject(projectName);
72     }
73
74     public IContainer generateContainer(IProgressMonitor monitor) throws CoreException {
75         IWorkspaceRunnable runnable= new IWorkspaceRunnable() {
76             public void run(IProgressMonitor monitor) throws CoreException {
77                 monitor.beginTask(FileBuffersMessages.getString("ContainerGenerator.task.creatingContainer"), 1000 * fContainerFullPath.segmentCount()); //$NON-NLS-1$
78
if (fContainer != null)
79                     return;
80         
81                 // Does the container exist already?
82
IWorkspaceRoot root= fWorkspace.getRoot();
83                 IResource found= root.findMember(fContainerFullPath);
84                 if (found instanceof IContainer) {
85                     fContainer= (IContainer) found;
86                     return;
87                 } else if (found != null) {
88                     // fContainerFullPath specifies a file as directory
89
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, FileBuffersMessages.getFormattedString("ContainerGenerator.destinationMustBeAContainer", fContainerFullPath), null)); //$NON-NLS-1$
90
}
91         
92                 // Create the container for the given path
93
fContainer= root;
94                 for (int i = 0; i < fContainerFullPath.segmentCount(); i++) {
95                     String JavaDoc currentSegment = fContainerFullPath.segment(i);
96                     IResource resource = fContainer.findMember(currentSegment);
97                     if (resource != null) {
98                         if (resource instanceof IContainer) {
99                             fContainer= (IContainer) resource;
100                             monitor.worked(1000);
101                         } else {
102                             // fContainerFullPath specifies a file as directory
103
throw new CoreException(new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, FileBuffersMessages.getFormattedString("ContainerGenerator.destinationMustBeAContainer", resource.getFullPath()), null)); //$NON-NLS-1$
104
}
105                     }
106                     else {
107                         if (i == 0) {
108                             IProject projectHandle= createProjectHandle(root, currentSegment);
109                             fContainer= createProject(projectHandle, new SubProgressMonitor(monitor,1000));
110                         }
111                         else {
112                             IFolder folderHandle= createFolderHandle(fContainer, currentSegment);
113                             fContainer= createFolder(folderHandle, new SubProgressMonitor(monitor,1000));
114                         }
115                     }
116                 }
117             }
118         };
119
120         // Get scheduling rule
121
IWorkspaceRoot root= fWorkspace.getRoot();
122         IPath existingParentPath= fContainerFullPath;
123         while (!root.exists(existingParentPath))
124             existingParentPath= existingParentPath.removeLastSegments(1);
125         
126         IResource schedulingRule= root.findMember(existingParentPath);
127         fWorkspace.run(runnable, schedulingRule, IWorkspace.AVOID_UPDATE, monitor);
128         return fContainer;
129     }
130 }
131
Popular Tags