KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > Folder


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.core.internal.resources;
12
13 import java.net.URI JavaDoc;
14 import org.eclipse.core.filesystem.IFileInfo;
15 import org.eclipse.core.filesystem.IFileStore;
16 import org.eclipse.core.internal.utils.Messages;
17 import org.eclipse.core.internal.utils.Policy;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.core.runtime.jobs.ISchedulingRule;
21 import org.eclipse.osgi.util.NLS;
22
23 public class Folder extends Container implements IFolder {
24     protected Folder(IPath path, Workspace container) {
25         super(path, container);
26     }
27
28     protected void assertCreateRequirements(IFileStore store, IFileInfo localInfo, int updateFlags) throws CoreException {
29         checkDoesNotExist();
30         Container parent = (Container) getParent();
31         ResourceInfo info = parent.getResourceInfo(false, false);
32         parent.checkAccessible(getFlags(info));
33
34         final boolean force = (updateFlags & IResource.FORCE) != 0;
35         if (!force && localInfo.exists()) {
36             //return an appropriate error message for case variant collisions
37
if (!Workspace.caseSensitive) {
38                 String JavaDoc name = getLocalManager().getLocalName(store);
39                 if (name != null && !store.getName().equals(name)) {
40                     String JavaDoc msg = NLS.bind(Messages.resources_existsLocalDifferentCase, new Path(store.toString()).removeLastSegments(1).append(name).toOSString());
41                     throw new ResourceException(IResourceStatus.CASE_VARIANT_EXISTS, getFullPath(), msg, null);
42                 }
43             }
44             String JavaDoc msg = NLS.bind(Messages.resources_fileExists, store.toString());
45             throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, getFullPath(), msg, null);
46         }
47     }
48
49     /* (non-Javadoc)
50      * Changes this folder to be a file in the resource tree and returns the newly
51      * created file. All related properties are deleted. It is assumed that on
52      * disk the resource is already a file so no action is taken to delete the disk
53      * contents.
54      * <p>
55      * <b>This method is for the exclusive use of the local refresh mechanism</b>
56      *
57      * @see org.eclipse.core.internal.localstore.RefreshLocalVisitor#folderToFile(UnifiedTreeNode, Resource)
58      */

59     public IFile changeToFile() throws CoreException {
60         getPropertyManager().deleteProperties(this, IResource.DEPTH_INFINITE);
61         IFile result = workspace.getRoot().getFile(path);
62         if (isLinked()) {
63             URI JavaDoc location = getRawLocationURI();
64             delete(IResource.NONE, null);
65             result.createLink(location, IResource.ALLOW_MISSING_LOCAL, null);
66         } else {
67             workspace.deleteResource(this);
68             workspace.createResource(result, false);
69         }
70         return result;
71     }
72
73     /* (non-Javadoc)
74      * @see IFolder#create(int, boolean, IProgressMonitor)
75      */

76     public void create(int updateFlags, boolean local, IProgressMonitor monitor) throws CoreException {
77         final boolean force = (updateFlags & IResource.FORCE) != 0;
78         monitor = Policy.monitorFor(monitor);
79         try {
80             String JavaDoc message = NLS.bind(Messages.resources_creating, getFullPath());
81             monitor.beginTask(message, Policy.totalWork);
82             checkValidPath(path, FOLDER, true);
83             final ISchedulingRule rule = workspace.getRuleFactory().createRule(this);
84             try {
85                 workspace.prepareOperation(rule, monitor);
86                 IFileStore store = getStore();
87                 IFileInfo localInfo = store.fetchInfo();
88                 assertCreateRequirements(store, localInfo, updateFlags);
89                 workspace.beginOperation(true);
90                 if (force && !Workspace.caseSensitive && localInfo.exists()) {
91                     String JavaDoc name = getLocalManager().getLocalName(store);
92                     if (name == null || localInfo.getName().equals(name)) {
93                         delete(true, null);
94                     } else {
95                         // The file system is not case sensitive and a case variant exists at this location
96
String JavaDoc msg = NLS.bind(Messages.resources_existsLocalDifferentCase, new Path(store.toString()).removeLastSegments(1).append(name).toOSString());
97                         throw new ResourceException(IResourceStatus.CASE_VARIANT_EXISTS, getFullPath(), msg, null);
98                     }
99                 }
100                 internalCreate(updateFlags, local, Policy.subMonitorFor(monitor, Policy.opWork));
101                 workspace.getAliasManager().updateAliases(this, getStore(), IResource.DEPTH_ZERO, monitor);
102             } catch (OperationCanceledException e) {
103                 workspace.getWorkManager().operationCanceled();
104                 throw e;
105             } finally {
106                 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork));
107             }
108         } finally {
109             monitor.done();
110         }
111     }
112
113     /* (non-Javadoc)
114      * @see IFolder#create(boolean, boolean, IProgressMonitor)
115      */

116     public void create(boolean force, boolean local, IProgressMonitor monitor) throws CoreException {
117         // funnel all operations to central method
118
create((force ? IResource.FORCE : IResource.NONE), local, monitor);
119     }
120
121     /**
122      * Ensures that this folder exists in the workspace. This is similar in
123      * concept to mkdirs but it does not work on projects.
124      * If this folder is created, it will be marked as being local.
125      */

126     public void ensureExists(IProgressMonitor monitor) throws CoreException {
127         ResourceInfo info = getResourceInfo(false, false);
128         int flags = getFlags(info);
129         if (exists(flags, true))
130             return;
131         if (exists(flags, false)) {
132             String JavaDoc message = NLS.bind(Messages.resources_folderOverFile, getFullPath());
133             throw new ResourceException(IResourceStatus.RESOURCE_WRONG_TYPE, getFullPath(), message, null);
134         }
135         Container parent = (Container) getParent();
136         if (parent.getType() == PROJECT) {
137             info = parent.getResourceInfo(false, false);
138             parent.checkExists(getFlags(info), true);
139         } else
140             ((Folder) parent).ensureExists(monitor);
141         internalCreate(IResource.FORCE, true, monitor);
142     }
143
144     /* (non-Javadoc)
145      * @see IContainer#getDefaultCharset(boolean)
146      */

147     public String JavaDoc getDefaultCharset(boolean checkImplicit) {
148         // non-existing resources default to parent's charset
149
if (!exists())
150             return checkImplicit ? workspace.getCharsetManager().getCharsetFor(getFullPath().removeLastSegments(1), true) : null;
151         return workspace.getCharsetManager().getCharsetFor(getFullPath(), checkImplicit);
152     }
153
154     /* (non-Javadoc)
155      * @see IResource#getType()
156      */

157     public int getType() {
158         return FOLDER;
159     }
160
161     public void internalCreate(int updateFlags, boolean local, IProgressMonitor monitor) throws CoreException {
162         monitor = Policy.monitorFor(monitor);
163         try {
164             String JavaDoc message = NLS.bind(Messages.resources_creating, getFullPath());
165             monitor.beginTask(message, Policy.totalWork);
166             workspace.createResource(this, updateFlags);
167             if (local) {
168                 try {
169                     final boolean force = (updateFlags & IResource.FORCE) != 0;
170                     getLocalManager().write(this, force, Policy.subMonitorFor(monitor, Policy.totalWork));
171                 } catch (CoreException e) {
172                     // a problem happened creating the folder on disk, so delete from the workspace
173
workspace.deleteResource(this);
174                     throw e; // rethrow
175
}
176             }
177             internalSetLocal(local, DEPTH_ZERO);
178             if (!local)
179                 getResourceInfo(true, true).clearModificationStamp();
180         } finally {
181             monitor.done();
182         }
183     }
184 }
185
Popular Tags