1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.util;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
21 import org.eclipse.core.filesystem.EFS;
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;
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;
35 import org.eclipse.jdt.internal.corext.CorextMessages;
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 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
42 public class Resources {
48 * Checks if the given resource is in sync with the underlying file system.
50 * @param resource the resource to be checked
51 * @return IStatus status describing the check's result. If <code>status.
52 * isOK()</code> returns <code>true</code> then the resource is in sync
54 public static IStatus checkInSync(IResource resource) {
55 return checkInSync(new IResource[] {resource});
59 * Checks if the given resources are in sync with the underlying file
62 * @param resources the resources to be checked
63 * @return IStatus status describing the check's result. If <code>status.
64 * isOK() </code> returns <code>true</code> then the resources are in sync
66 public static IStatus checkInSync(IResource[] resources) {
68 for (int i= 0; i < resources.length; i++) {
69 IResource resource= resources[i];
70 if (!resource.isSynchronized(IResource.DEPTH_INFINITE)) {
71 result= addOutOfSync(result, resource);
76 return Status.OK_STATUS;
80 * Makes the given resource committable. Committable means that it is
81 * writeable and that its content hasn't changed by calling
82 * <code>validateEdit</code> for the given resource on <tt>IWorkspace</tt>.
84 * @param resource the resource to be checked
85 * @param context the context passed to <code>validateEdit</code>
86 * @return status describing the method's result. If <code>status.isOK()</code> returns <code>true</code> then the resources are committable.
88 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
90 public static IStatus makeCommittable(IResource resource, Object context) {
91 return makeCommittable(new IResource[] { resource }, context);
95 * Makes the given resources committable. Committable means that all
96 * resources are writeable and that the content of the resources hasn't
97 * changed by calling <code>validateEdit</code> for a given file on
98 * <tt>IWorkspace</tt>.
100 * @param resources the resources to be checked
101 * @param context the context passed to <code>validateEdit</code>
102 * @return IStatus status describing the method's result. If <code>status.
103 * isOK()</code> returns <code>true</code> then the add resources are
106 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
108 public static IStatus makeCommittable(IResource[] resources, Object context) {
109 List<IResource> readOnlyFiles= new ArrayList<IResource>();
110 for (int i= 0; i < resources.length; i++) {
111 IResource resource= resources[i];
112 if (resource.getType() == IResource.FILE && isReadOnly(resource))
113 readOnlyFiles.add(resource);
115 if (readOnlyFiles.size() == 0)
116 return Status.OK_STATUS;
118 Map<IFile, Long> oldTimeStamps= createModificationStampMap(readOnlyFiles);
119 IStatus status= ResourcesPlugin.getWorkspace().validateEdit(
120 readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context);
124 IStatus modified= null;
125 Map<IFile, Long> newTimeStamps= createModificationStampMap(readOnlyFiles);
126 for (Iterator<IFile> iter= oldTimeStamps.keySet().iterator(); iter.hasNext();) {
127 IFile file= iter.next();
128 if (!oldTimeStamps.get(file).equals(newTimeStamps.get(file)))
129 modified= addModified(modified, file);
131 if (modified != null)
133 return Status.OK_STATUS;
136 private static Map<IFile, Long> createModificationStampMap(List<IResource> files){
137 Map<IFile, Long> map= new HashMap<IFile, Long>();
138 for (Iterator<IResource> iter= files.iterator(); iter.hasNext(); ) {
139 IFile file= (IFile)iter.next();
140 map.put(file, new Long(file.getModificationStamp()));
145 private static IStatus addModified(IStatus status, IFile file) {
146 IStatus entry= JavaUIStatus.createError(
147 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
148 Messages.format(CorextMessages.Resources_fileModified, BasicElementLabels.getPathLabel(file.getFullPath(), false)),
150 if (status == null) {
152 } else if (status.isMultiStatus()) {
153 ((MultiStatus)status).add(entry);
156 MultiStatus result= new MultiStatus(JavaPlugin.getPluginId(),
157 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
158 CorextMessages.Resources_modifiedResources, null);
165 private static IStatus addOutOfSync(IStatus status, IResource resource) {
166 IStatus entry= new Status(
168 ResourcesPlugin.PI_RESOURCES,
169 IResourceStatus.OUT_OF_SYNC_LOCAL,
170 Messages.format(CorextMessages.Resources_outOfSync, BasicElementLabels.getPathLabel(resource.getFullPath(), false)),
172 if (status == null) {
174 } else if (status.isMultiStatus()) {
175 ((MultiStatus)status).add(entry);
178 MultiStatus result= new MultiStatus(
179 ResourcesPlugin.PI_RESOURCES,
180 IResourceStatus.OUT_OF_SYNC_LOCAL,
181 CorextMessages.Resources_outOfSyncResources, null);
189 * This method is used to generate a list of local locations to
190 * be used in DnD for file transfers.
192 * @param resources the array of resources to get the local
194 * @return the local locations
196 public static String[] getLocationOSStrings(IResource[] resources) {
197 List<String> result= new ArrayList<String>(resources.length);
198 for (int i= 0; i < resources.length; i++) {
199 IPath location= resources[i].getLocation();
200 if (location != null)
201 result.add(location.toOSString());
203 return result.toArray(new String[result.size()]);
207 * Returns the location of the given resource. For local
208 * resources this is the OS path in the local file system. For
209 * remote resource this is the URI.
211 * @param resource the resource
212 * @return the location string or <code>null</code> if the
213 * location URI of the resource is <code>null</code>
215 public static String getLocationString(IResource resource) {
216 URI uri= resource.getLocationURI();
219 return EFS.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())
220 ? new File(uri).getAbsolutePath()
224 public static boolean isReadOnly(IResource resource) {
225 ResourceAttributes resourceAttributes = resource.getResourceAttributes();
226 if (resourceAttributes == null) // not supported on this platform for this resource
228 return resourceAttributes.isReadOnly();
231 static void setReadOnly(IResource resource, boolean readOnly) {
232 ResourceAttributes resourceAttributes = resource.getResourceAttributes();
233 if (resourceAttributes == null) // not supported on this platform for this resource
236 resourceAttributes.setReadOnly(readOnly);
238 resource.setResourceAttributes(resourceAttributes);
239 } catch (CoreException e) {