KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > util > Resources


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.jdt.internal.corext.util;
12
13 import java.io.File JavaDoc;
14 import java.net.URI JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.eclipse.core.filesystem.EFS;
22
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IStatus;
26 import org.eclipse.core.runtime.MultiStatus;
27 import org.eclipse.core.runtime.Status;
28
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.resources.IResourceStatus;
32 import org.eclipse.core.resources.ResourceAttributes;
33 import org.eclipse.core.resources.ResourcesPlugin;
34
35 import org.eclipse.jdt.internal.corext.CorextMessages;
36
37 import org.eclipse.jdt.internal.ui.IJavaStatusConstants;
38 import org.eclipse.jdt.internal.ui.JavaPlugin;
39 import org.eclipse.jdt.internal.ui.JavaUIStatus;
40
41 public class Resources {
42
43     private Resources() {
44     }
45
46     /**
47      * Checks if the given resource is in sync with the underlying file system.
48      *
49      * @param resource the resource to be checked
50      * @return IStatus status describing the check's result. If <code>status.
51      * isOK()</code> returns <code>true</code> then the resource is in sync
52      */

53     public static IStatus checkInSync(IResource resource) {
54         return checkInSync(new IResource[] {resource});
55     }
56     
57     /**
58      * Checks if the given resources are in sync with the underlying file
59      * system.
60      *
61      * @param resources the resources to be checked
62      * @return IStatus status describing the check's result. If <code>status.
63      * isOK() </code> returns <code>true</code> then the resources are in sync
64      */

65     public static IStatus checkInSync(IResource[] resources) {
66         IStatus result= null;
67         for (int i= 0; i < resources.length; i++) {
68             IResource resource= resources[i];
69             if (!resource.isSynchronized(IResource.DEPTH_INFINITE)) {
70                 result= addOutOfSync(result, resource);
71             }
72         }
73         if (result != null)
74             return result;
75         return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
76
}
77
78     /**
79      * Makes the given resource committable. Committable means that it is
80      * writeable and that its content hasn't changed by calling
81      * <code>validateEdit</code> for the given resource on <tt>IWorkspace</tt>.
82      *
83      * @param resource the resource to be checked
84      * @param context the context passed to <code>validateEdit</code>
85      * @return status describing the method's result. If <code>status.isOK()</code> returns <code>true</code> then the resources are committable.
86      *
87      * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
88      */

89     public static IStatus makeCommittable(IResource resource, Object JavaDoc context) {
90         return makeCommittable(new IResource[] { resource }, context);
91     }
92     
93     /**
94      * Makes the given resources committable. Committable means that all
95      * resources are writeable and that the content of the resources hasn't
96      * changed by calling <code>validateEdit</code> for a given file on
97      * <tt>IWorkspace</tt>.
98      *
99      * @param resources the resources to be checked
100      * @param context the context passed to <code>validateEdit</code>
101      * @return IStatus status describing the method's result. If <code>status.
102      * isOK()</code> returns <code>true</code> then the add resources are
103      * committable
104      *
105      * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
106      */

107     public static IStatus makeCommittable(IResource[] resources, Object JavaDoc context) {
108         List JavaDoc readOnlyFiles= new ArrayList JavaDoc();
109         for (int i= 0; i < resources.length; i++) {
110             IResource resource= resources[i];
111             if (resource.getType() == IResource.FILE && isReadOnly(resource))
112                 readOnlyFiles.add(resource);
113         }
114         if (readOnlyFiles.size() == 0)
115             return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
116

117         Map JavaDoc oldTimeStamps= createModificationStampMap(readOnlyFiles);
118         IStatus status= ResourcesPlugin.getWorkspace().validateEdit(
119             (IFile[]) readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context);
120         if (!status.isOK())
121             return status;
122             
123         IStatus modified= null;
124         Map JavaDoc newTimeStamps= createModificationStampMap(readOnlyFiles);
125         for (Iterator JavaDoc iter= oldTimeStamps.keySet().iterator(); iter.hasNext();) {
126             IFile file= (IFile) iter.next();
127             if (!oldTimeStamps.get(file).equals(newTimeStamps.get(file)))
128                 modified= addModified(modified, file);
129         }
130         if (modified != null)
131             return modified;
132         return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
133
}
134
135     private static Map JavaDoc createModificationStampMap(List JavaDoc files){
136         Map JavaDoc map= new HashMap JavaDoc();
137         for (Iterator JavaDoc iter= files.iterator(); iter.hasNext(); ) {
138             IFile file= (IFile)iter.next();
139             map.put(file, new Long JavaDoc(file.getModificationStamp()));
140         }
141         return map;
142     }
143     
144     private static IStatus addModified(IStatus status, IFile file) {
145         IStatus entry= JavaUIStatus.createError(
146             IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
147             Messages.format(CorextMessages.Resources_fileModified, file.getFullPath().toString()),
148             null);
149         if (status == null) {
150             return entry;
151         } else if (status.isMultiStatus()) {
152             ((MultiStatus)status).add(entry);
153             return status;
154         } else {
155             MultiStatus result= new MultiStatus(JavaPlugin.getPluginId(),
156                 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
157                 CorextMessages.Resources_modifiedResources, null);
158             result.add(status);
159             result.add(entry);
160             return result;
161         }
162     }
163
164     private static IStatus addOutOfSync(IStatus status, IResource resource) {
165         IStatus entry= new Status(
166             IStatus.ERROR,
167             ResourcesPlugin.PI_RESOURCES,
168             IResourceStatus.OUT_OF_SYNC_LOCAL,
169             Messages.format(CorextMessages.Resources_outOfSync, resource.getFullPath().toString()),
170             null);
171         if (status == null) {
172             return entry;
173         } else if (status.isMultiStatus()) {
174             ((MultiStatus)status).add(entry);
175             return status;
176         } else {
177             MultiStatus result= new MultiStatus(
178                 ResourcesPlugin.PI_RESOURCES,
179                 IResourceStatus.OUT_OF_SYNC_LOCAL,
180                 CorextMessages.Resources_outOfSyncResources, null);
181             result.add(status);
182             result.add(entry);
183             return result;
184         }
185     }
186
187     /**
188      * This method is used to generate a list of local locations to
189      * be used in DnD for file transfers.
190      *
191      * @param resources the array of resources to get the local
192      * locations for
193      * @return the local locations
194      */

195     public static String JavaDoc[] getLocationOSStrings(IResource[] resources) {
196         List JavaDoc result= new ArrayList JavaDoc(resources.length);
197         for (int i= 0; i < resources.length; i++) {
198             IPath location= resources[i].getLocation();
199             if (location != null)
200                 result.add(location.toOSString());
201         }
202         return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
203     }
204     
205     /**
206      * Returns the location of the given resource. For local
207      * resources this is the OS path in the local file system. For
208      * remote resource this is the URI.
209      *
210      * @param resource the resource
211      * @return the location string or <code>null</code> if the
212      * location URI of the resource is <code>null</code>
213      */

214     public static String JavaDoc getLocationString(IResource resource) {
215         URI JavaDoc uri= resource.getLocationURI();
216         if (uri == null)
217             return null;
218         return EFS.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())
219             ? new File JavaDoc(uri).getAbsolutePath()
220             : uri.toString();
221     }
222     
223     public static boolean isReadOnly(IResource resource) {
224         ResourceAttributes resourceAttributes = resource.getResourceAttributes();
225         if (resourceAttributes == null) // not supported on this platform for this resource
226
return false;
227         return resourceAttributes.isReadOnly();
228     }
229     
230     static void setReadOnly(IResource resource, boolean readOnly) {
231         ResourceAttributes resourceAttributes = resource.getResourceAttributes();
232         if (resourceAttributes == null) // not supported on this platform for this resource
233
return;
234         
235         resourceAttributes.setReadOnly(readOnly);
236         try {
237             resource.setResourceAttributes(resourceAttributes);
238         } catch (CoreException e) {
239             JavaPlugin.log(e);
240         }
241     }
242 }
243
Popular Tags