KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import java.net.URI JavaDoc;
15 import java.util.*;
16 import org.eclipse.core.filesystem.URIUtil;
17 import org.eclipse.core.internal.utils.FileUtil;
18 import org.eclipse.core.resources.*;
19 import org.eclipse.core.runtime.*;
20
21 public class WorkspaceRoot extends Container implements IWorkspaceRoot {
22     /**
23      * As an optimization, we store a table of project handles
24      * that have been requested from this root. This maps project
25      * name strings to project handles.
26      */

27     private final Map projectTable = Collections.synchronizedMap(new HashMap(16));
28     
29     /**
30      * Cache of the canonicalized platform location.
31      */

32     private final IPath workspaceLocation;
33
34     protected WorkspaceRoot(IPath path, Workspace container) {
35         super(path, container);
36         Assert.isTrue(path.equals(Path.ROOT));
37         workspaceLocation = FileUtil.canonicalPath(Platform.getLocation());
38         Assert.isNotNull(workspaceLocation);
39     }
40
41     /**
42      * @see IWorkspaceRoot#delete(boolean, boolean, IProgressMonitor)
43      */

44     public void delete(boolean deleteContent, boolean force, IProgressMonitor monitor) throws CoreException {
45         int updateFlags = force ? IResource.FORCE : IResource.NONE;
46         updateFlags |= deleteContent ? IResource.ALWAYS_DELETE_PROJECT_CONTENT : IResource.NEVER_DELETE_PROJECT_CONTENT;
47         delete(updateFlags, monitor);
48     }
49
50     /**
51      * @see org.eclipse.core.internal.resources.Resource#delete(boolean, org.eclipse.core.runtime.IProgressMonitor)
52      */

53     public void delete(boolean force, IProgressMonitor monitor) throws CoreException {
54         int updateFlags = force ? IResource.FORCE : IResource.NONE;
55         delete(updateFlags, monitor);
56     }
57
58     /**
59      * @see org.eclipse.core.internal.resources.Resource#exists(int, boolean)
60      */

61     public boolean exists(int flags, boolean checkType) {
62         return true;
63     }
64
65     /**
66      * @see org.eclipse.core.resources.IWorkspaceRoot#findContainersForLocation(org.eclipse.core.runtime.IPath)
67      */

68     public IContainer[] findContainersForLocation(IPath location) {
69         return findContainersForLocationURI(URIUtil.toURI(location.makeAbsolute()));
70     }
71
72     /**
73      * @see org.eclipse.core.resources.IWorkspaceRoot#findContainersForLocationURI(java.net.URI)
74      */

75     public IContainer[] findContainersForLocationURI(URI JavaDoc location) {
76         if (!location.isAbsolute())
77             throw new IllegalArgumentException JavaDoc();
78         return (IContainer[]) getLocalManager().allResourcesFor(location, false);
79     }
80
81     /**
82      * @see org.eclipse.core.resources.IWorkspaceRoot#findFilesForLocation(org.eclipse.core.runtime.IPath)
83      */

84     public IFile[] findFilesForLocation(IPath location) {
85         return findFilesForLocationURI(URIUtil.toURI(location.makeAbsolute()));
86     }
87
88     /**
89      * @see org.eclipse.core.resources.IWorkspaceRoot#findFilesForLocationURI(java.net.URI)
90      */

91     public IFile[] findFilesForLocationURI(URI JavaDoc location) {
92         if (!location.isAbsolute())
93             throw new IllegalArgumentException JavaDoc();
94         return (IFile[]) getLocalManager().allResourcesFor(location, true);
95     }
96
97     /**
98      * @see IWorkspaceRoot#getContainerForLocation(IPath)
99      */

100     public IContainer getContainerForLocation(IPath location) {
101         return getLocalManager().containerForLocation(location);
102     }
103
104     /**
105      * @see IContainer#getDefaultCharset(boolean)
106      */

107     public String JavaDoc getDefaultCharset(boolean checkImplicit) {
108         if (checkImplicit)
109             return ResourcesPlugin.getEncoding();
110         String JavaDoc enc = ResourcesPlugin.getPlugin().getPluginPreferences().getString(ResourcesPlugin.PREF_ENCODING);
111         return enc == null || enc.length() == 0 ? null : enc;
112     }
113
114     /**
115      * @see IWorkspaceRoot#getFileForLocation(IPath)
116      */

117     public IFile getFileForLocation(IPath location) {
118         return getLocalManager().fileForLocation(location);
119     }
120
121     /**
122      * @see IResource#getLocalTimeStamp()
123      */

124     public long getLocalTimeStamp() {
125         return IResource.NULL_STAMP;
126     }
127
128     /**
129      * @see IResource#getLocation()
130      */

131     public IPath getLocation() {
132         return workspaceLocation;
133     }
134
135     /**
136      * @see IResource#getName()
137      */

138     public String JavaDoc getName() {
139         return ""; //$NON-NLS-1$
140
}
141
142     /**
143      * @see IResource#getParent()
144      */

145     public IContainer getParent() {
146         return null;
147     }
148
149     /**
150      * @see IResource#getProject()
151      */

152     public IProject getProject() {
153         return null;
154     }
155
156     /**
157      * @see IWorkspaceRoot#getProject(String)
158      */

159     public IProject getProject(String JavaDoc name) {
160         //first check our project cache
161
Project result = (Project) projectTable.get(name);
162         if (result == null) {
163             IPath projectPath = new Path(null, name).makeAbsolute();
164             String JavaDoc message = "Path for project must have only one segment."; //$NON-NLS-1$
165
Assert.isLegal(projectPath.segmentCount() == ICoreConstants.PROJECT_SEGMENT_LENGTH, message);
166             //try to get the project using a canonical name
167
String JavaDoc canonicalName = projectPath.lastSegment();
168             result = (Project) projectTable.get(canonicalName);
169             if (result != null)
170                 return result;
171             result = new Project(projectPath, workspace);
172             projectTable.put(canonicalName, result);
173         }
174         return result;
175     }
176
177     /**
178      * @see IResource#getProjectRelativePath()
179      */

180     public IPath getProjectRelativePath() {
181         return Path.EMPTY;
182     }
183
184     /**
185      * @see IWorkspaceRoot#getProjects()
186      */

187     public IProject[] getProjects() {
188         IResource[] roots = getChildren(IResource.NONE);
189         IProject[] result = new IProject[roots.length];
190         System.arraycopy(roots, 0, result, 0, roots.length);
191         return result;
192     }
193
194     /**
195      * @see IResource#getType()
196      */

197     public int getType() {
198         return IResource.ROOT;
199     }
200
201     public void internalSetLocal(boolean flag, int depth) throws CoreException {
202         // do nothing for the root, but call for its children
203
if (depth == IResource.DEPTH_ZERO)
204             return;
205         if (depth == IResource.DEPTH_ONE)
206             depth = IResource.DEPTH_ZERO;
207         // get the children via the workspace since we know that this
208
// resource exists (it is local).
209
IResource[] children = getChildren(IResource.NONE);
210         for (int i = 0; i < children.length; i++)
211             ((Resource) children[i]).internalSetLocal(flag, depth);
212     }
213
214     /* (non-Javadoc)
215      * @see org.eclipse.core.internal.resources.Resource#isLinked(int)
216      */

217     public boolean isLinked(int options) {
218         return false;//the root is never linked
219
}
220     
221     /**
222      * @see IResource#isLocal(int)
223      * @deprecated
224      */

225     public boolean isLocal(int depth) {
226         // the flags parameter is ignored for the workspace root so pass anything
227
return isLocal(-1, depth);
228     }
229
230     /**
231      * @see IResource#isLocal(int)
232      * @deprecated
233      */

234     public boolean isLocal(int flags, int depth) {
235         // don't check the flags....workspace root is always local
236
if (depth == DEPTH_ZERO)
237             return true;
238         if (depth == DEPTH_ONE)
239             depth = DEPTH_ZERO;
240         // get the children via the workspace since we know that this
241
// resource exists (it is local).
242
IResource[] children = getChildren(IResource.NONE);
243         for (int i = 0; i < children.length; i++)
244             if (!children[i].isLocal(depth))
245                 return false;
246         return true;
247     }
248
249     /**
250      * @see IResource#isPhantom()
251      */

252     public boolean isPhantom() {
253         return false;
254     }
255
256     /**
257      * @see IContainer#setDefaultCharset(String)
258      * @deprecated Replaced by {@link #setDefaultCharset(String, IProgressMonitor)} which
259      * is a workspace operation and reports changes in resource deltas.
260      */

261     public void setDefaultCharset(String JavaDoc charset) {
262         // directly change the Resource plugin's preference for encoding
263
Preferences resourcesPreferences = ResourcesPlugin.getPlugin().getPluginPreferences();
264         if (charset != null)
265             resourcesPreferences.setValue(ResourcesPlugin.PREF_ENCODING, charset);
266         else
267             resourcesPreferences.setToDefault(ResourcesPlugin.PREF_ENCODING);
268     }
269
270     /**
271      * @see IResource#setLocalTimeStamp(long)
272      */

273     public long setLocalTimeStamp(long value) {
274         if (value < 0)
275             throw new IllegalArgumentException JavaDoc("Illegal time stamp: " + value); //$NON-NLS-1$
276
//can't set local time for root
277
return value;
278     }
279
280     /**
281      * @deprecated
282      * @see IResource#setReadOnly(boolean)
283      */

284     public void setReadOnly(boolean readonly) {
285         //can't set the root read only
286
}
287
288     /**
289      * @see IResource#touch(IProgressMonitor)
290      */

291     public void touch(IProgressMonitor monitor) {
292         // do nothing for the workspace root
293
}
294 }
295
Popular Tags