KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
14 import org.eclipse.core.internal.localstore.IHistoryStore;
15 import org.eclipse.core.internal.utils.*;
16 import org.eclipse.core.internal.watson.*;
17 import org.eclipse.core.resources.*;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.core.runtime.jobs.ISchedulingRule;
20 import org.eclipse.osgi.util.NLS;
21
22 public abstract class Container extends Resource implements IContainer {
23     protected Container(IPath path, Workspace container) {
24         super(path, container);
25     }
26
27     /**
28      * Converts this resource and all its children into phantoms by modifying
29      * their resource infos in-place.
30      */

31     public void convertToPhantom() throws CoreException {
32         if (isPhantom())
33             return;
34         super.convertToPhantom();
35         IResource[] members = members(IContainer.INCLUDE_PHANTOMS | IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS);
36         for (int i = 0; i < members.length; i++)
37             ((Resource) members[i]).convertToPhantom();
38     }
39
40     /* (non-Javadoc)
41      * @see IContainer#exists(IPath)
42      */

43     public boolean exists(IPath childPath) {
44         return workspace.getResourceInfo(getFullPath().append(childPath), false, false) != null;
45     }
46
47     /* (non-Javadoc)
48      * @see IContainer#findMember(String)
49      */

50     public IResource findMember(String JavaDoc name) {
51         return findMember(name, false);
52     }
53
54     /* (non-Javadoc)
55      * @see IContainer#findMember(String, boolean)
56      */

57     public IResource findMember(String JavaDoc name, boolean phantom) {
58         IPath childPath = getFullPath().append(name);
59         ResourceInfo info = workspace.getResourceInfo(childPath, phantom, false);
60         return info == null ? null : workspace.newResource(childPath, info.getType());
61     }
62
63     /* (non-Javadoc)
64      * @see IContainer#findMember(IPath)
65      */

66     public IResource findMember(IPath childPath) {
67         return findMember(childPath, false);
68     }
69
70     /* (non-Javadoc)
71      * @see IContainer#findMember(IPath)
72      */

73     public IResource findMember(IPath childPath, boolean phantom) {
74         childPath = getFullPath().append(childPath);
75         ResourceInfo info = workspace.getResourceInfo(childPath, phantom, false);
76         return (info == null) ? null : workspace.newResource(childPath, info.getType());
77     }
78
79     protected void fixupAfterMoveSource() throws CoreException {
80         super.fixupAfterMoveSource();
81         if (!synchronizing(getResourceInfo(true, false)))
82             return;
83         IResource[] members = members(IContainer.INCLUDE_PHANTOMS | IContainer.INCLUDE_TEAM_PRIVATE_MEMBERS);
84         for (int i = 0; i < members.length; i++)
85             ((Resource) members[i]).fixupAfterMoveSource();
86     }
87
88     protected IResource[] getChildren(int memberFlags) {
89         IPath[] children = null;
90         try {
91             children = workspace.tree.getChildren(path);
92         } catch (IllegalArgumentException JavaDoc e) {
93             //concurrency problem: the container has been deleted by another
94
//thread during this call. Just return empty children set
95
}
96         if (children == null || children.length == 0)
97             return ICoreConstants.EMPTY_RESOURCE_ARRAY;
98         Resource[] result = new Resource[children.length];
99         int found = 0;
100         for (int i = 0; i < children.length; i++) {
101             ResourceInfo info = workspace.getResourceInfo(children[i], true, false);
102             if (info != null && isMember(info.getFlags(), memberFlags))
103                 result[found++] = workspace.newResource(children[i], info.getType());
104         }
105         if (found == result.length)
106             return result;
107         Resource[] trimmedResult = new Resource[found];
108         System.arraycopy(result, 0, trimmedResult, 0, found);
109         return trimmedResult;
110     }
111
112     /* (non-Javadoc)
113      * @see IFolder#getFile(String) and IProject#getFile(String)
114      */

115     public IFile getFile(String JavaDoc name) {
116         return (IFile) workspace.newResource(getFullPath().append(name), FILE);
117     }
118
119     /* (non-Javadoc)
120      * @see IContainer#getFile(IPath)
121      */

122     public IFile getFile(IPath childPath) {
123         return (IFile) workspace.newResource(getFullPath().append(childPath), FILE);
124     }
125
126     /* (non-Javadoc)
127      * @see IFolder#getFolder(String) and IProject#getFolder(String)
128      */

129     public IFolder getFolder(String JavaDoc name) {
130         return (IFolder) workspace.newResource(getFullPath().append(name), FOLDER);
131     }
132
133     /* (non-Javadoc)
134      * @see IContainer#getFolder(IPath)
135      */

136     public IFolder getFolder(IPath childPath) {
137         return (IFolder) workspace.newResource(getFullPath().append(childPath), FOLDER);
138     }
139
140     /**
141      * @deprecated
142      */

143     public boolean isLocal(int flags, int depth) {
144         if (!super.isLocal(flags, depth))
145             return false;
146         if (depth == DEPTH_ZERO)
147             return true;
148         if (depth == DEPTH_ONE)
149             depth = DEPTH_ZERO;
150         // get the children via the workspace since we know that this
151
// resource exists (it is local).
152
IResource[] children = getChildren(IResource.NONE);
153         for (int i = 0; i < children.length; i++)
154             if (!children[i].isLocal(depth))
155                 return false;
156         return true;
157     }
158
159     /* (non-Javadoc)
160      * @see IContainer#members()
161      */

162     public IResource[] members() throws CoreException {
163         // forward to central method
164
return members(IResource.NONE);
165     }
166
167     /* (non-Javadoc)
168      * @see IContainer#members(boolean)
169      */

170     public IResource[] members(boolean phantom) throws CoreException {
171         // forward to central method
172
return members(phantom ? INCLUDE_PHANTOMS : IResource.NONE);
173     }
174
175     /* (non-Javadoc)
176      * @see IContainer#members(int)
177      */

178     public IResource[] members(int memberFlags) throws CoreException {
179         final boolean phantom = (memberFlags & INCLUDE_PHANTOMS) != 0;
180         ResourceInfo info = getResourceInfo(phantom, false);
181         checkAccessible(getFlags(info));
182         //if children are currently unknown, ask for immediate refresh
183
if (info.isSet(ICoreConstants.M_CHILDREN_UNKNOWN))
184             workspace.refreshManager.refresh(this);
185         return getChildren(memberFlags);
186     }
187
188     /* (non-Javadoc)
189      * @see IContainer#getDefaultCharset()
190      */

191     public String JavaDoc getDefaultCharset() throws CoreException {
192         return getDefaultCharset(true);
193     }
194
195     /* (non-Javadoc)
196      * @see IContainer#findDeletedMembersWithHistory(int, IProgressMonitor)
197      */

198     public IFile[] findDeletedMembersWithHistory(int depth, IProgressMonitor monitor) {
199         IHistoryStore historyStore = getLocalManager().getHistoryStore();
200         IPath basePath = getFullPath();
201         IWorkspaceRoot root = getWorkspace().getRoot();
202         Set deletedFiles = new HashSet();
203
204         if (depth == IResource.DEPTH_ZERO) {
205             // this folder might have been a file in a past life
206
if (historyStore.getStates(basePath, monitor).length > 0) {
207                 IFile file = root.getFile(basePath);
208                 if (!file.exists()) {
209                     deletedFiles.add(file);
210                 }
211             }
212         } else {
213             Set allFilePaths = historyStore.allFiles(basePath, depth, monitor);
214             // convert IPaths to IFiles keeping only files that no longer exist
215
for (Iterator it = allFilePaths.iterator(); it.hasNext();) {
216                 IPath filePath = (IPath) it.next();
217                 IFile file = root.getFile(filePath);
218                 if (!file.exists()) {
219                     deletedFiles.add(file);
220                 }
221             }
222         }
223         return (IFile[]) deletedFiles.toArray(new IFile[deletedFiles.size()]);
224     }
225
226     /** (non-Javadoc)
227      * @see IContainer#setDefaultCharset(String)
228      * @deprecated Replaced by {@link #setDefaultCharset(String, IProgressMonitor)} which
229      * is a workspace operation and reports changes in resource deltas.
230      */

231     public void setDefaultCharset(String JavaDoc charset) throws CoreException {
232         ResourceInfo info = getResourceInfo(false, false);
233         checkAccessible(getFlags(info));
234         workspace.getCharsetManager().setCharsetFor(getFullPath(), charset);
235     }
236
237     /* (non-Javadoc)
238      * @see IContainer#setDefaultCharset(String, IProgressMonitor)
239      */

240     public void setDefaultCharset(String JavaDoc newCharset, IProgressMonitor monitor) throws CoreException {
241         monitor = Policy.monitorFor(monitor);
242         try {
243             String JavaDoc message = NLS.bind(Messages.resources_settingDefaultCharsetContainer, getFullPath());
244             monitor.beginTask(message, Policy.totalWork);
245             // need to get the project as a scheduling rule because we might be
246
// creating a new folder/file to hold the project settings
247
final ISchedulingRule rule = workspace.getRuleFactory().charsetRule(this);
248             try {
249                 workspace.prepareOperation(rule, monitor);
250                 checkAccessible(getFlags(getResourceInfo(false, false)));
251                 workspace.beginOperation(true);
252                 workspace.getCharsetManager().setCharsetFor(getFullPath(), newCharset);
253                 // now propagate the changes to all children inheriting their setting from this container
254
IElementContentVisitor visitor = new IElementContentVisitor() {
255                     boolean visitedRoot = false;
256
257                     public boolean visitElement(ElementTree tree, IPathRequestor requestor, Object JavaDoc elementContents) {
258                         if (elementContents == null)
259                             return false;
260                         IPath nodePath = requestor.requestPath();
261                         // we will always generate an event at least for the root of the sub tree
262
// (skip visiting the root because we already have set the charset above and
263
// that is the condition we are checking later)
264
if (!visitedRoot) {
265                             visitedRoot = true;
266                             ResourceInfo info = workspace.getResourceInfo(nodePath, false, true);
267                             if (info == null)
268                                 return false;
269                             info.incrementCharsetGenerationCount();
270                             return true;
271                         }
272                         // does it already have an encoding explicitly set?
273
if (workspace.getCharsetManager().getCharsetFor(nodePath, false) != null)
274                             return false;
275                         ResourceInfo info = workspace.getResourceInfo(nodePath, false, true);
276                         if (info == null)
277                             return false;
278                         info.incrementCharsetGenerationCount();
279                         return true;
280                     }
281                 };
282                 try {
283                     new ElementTreeIterator(workspace.getElementTree(), getFullPath()).iterate(visitor);
284                 } catch (WrappedRuntimeException e) {
285                     throw (CoreException) e.getTargetException();
286                 }
287                 monitor.worked(Policy.opWork);
288             } catch (OperationCanceledException e) {
289                 workspace.getWorkManager().operationCanceled();
290                 throw e;
291             } finally {
292                 workspace.endOperation(rule, true, Policy.subMonitorFor(monitor, Policy.endOpWork));
293             }
294         } finally {
295             monitor.done();
296         }
297     }
298 }
299
Popular Tags