1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.util;
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
19 import net.sourceforge.phpdt.internal.corext.CorextMessages;
20 import net.sourceforge.phpdt.internal.ui.IJavaStatusConstants;
21 import net.sourceforge.phpdt.internal.ui.PHPUIStatus;
22 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IResourceStatus;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.IPath;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.MultiStatus;
31 import org.eclipse.core.runtime.Status;
33 public class Resources {
39 * Checks if the given resource is in sync with the underlying file system.
41 * @param resource the resource to be checked
42 * @return IStatus status describing the check's result. If <code>status.
43 * isOK()</code> returns <code>true</code> then the resource is in sync
45 public static IStatus checkInSync(IResource resource) {
46 return checkInSync(new IResource[] {resource});
50 * Checks if the given resources are in sync with the underlying file
53 * @param resources the resources to be checked
54 * @return IStatus status describing the check's result. If <code>status.
55 * isOK() </code> returns <code>true</code> then the resources are in sync
57 public static IStatus checkInSync(IResource[] resources) {
59 for (int i= 0; i < resources.length; i++) {
60 IResource resource= resources[i];
61 if (!resource.isSynchronized(IResource.DEPTH_INFINITE)) {
62 result= addOutOfSync(result, resource);
67 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
71 * Makes the given resource committable. Committable means that it is
72 * writeable and that its content hasn't changed by calling
73 * <code>validateEdit</code> for the given resource on <tt>IWorkspace</tt>.
75 * @param resource the resource to be checked
76 * @param context the context passed to <code>validateEdit</code>
77 * @return status describing the method's result. If <code>status.isOK()</code> returns <code>true</code> then the resources are committable.
79 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
81 public static IStatus makeCommittable(IResource resource, Object context) {
82 return makeCommittable(new IResource[] { resource }, context);
86 * Makes the given resources committable. Committable means that all
87 * resources are writeable and that the content of the resources hasn't
88 * changed by calling <code>validateEdit</code> for a given file on
89 * <tt>IWorkspace</tt>.
91 * @param resources the resources to be checked
92 * @param context the context passed to <code>validateEdit</code>
93 * @return IStatus status describing the method's result. If <code>status.
94 * isOK()</code> returns <code>true</code> then the add resources are
97 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
99 public static IStatus makeCommittable(IResource[] resources, Object context) {
100 List readOnlyFiles= new ArrayList();
101 for (int i= 0; i < resources.length; i++) {
102 IResource resource= resources[i];
103 if (resource.getType() == IResource.FILE && resource.isReadOnly())
104 readOnlyFiles.add(resource);
106 if (readOnlyFiles.size() == 0)
107 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
109 Map oldTimeStamps= createModificationStampMap(readOnlyFiles);
110 IStatus status= ResourcesPlugin.getWorkspace().validateEdit(
111 (IFile[]) readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context);
115 IStatus modified= null;
116 Map newTimeStamps= createModificationStampMap(readOnlyFiles);
117 for (Iterator iter= oldTimeStamps.keySet().iterator(); iter.hasNext();) {
118 IFile file= (IFile) iter.next();
119 if (!oldTimeStamps.get(file).equals(newTimeStamps.get(file)))
120 modified= addModified(modified, file);
122 if (modified != null)
124 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
127 private static Map createModificationStampMap(List files){
128 Map map= new HashMap();
129 for (Iterator iter= files.iterator(); iter.hasNext(); ) {
130 IFile file= (IFile)iter.next();
131 map.put(file, new Long(file.getModificationStamp()));
136 private static IStatus addModified(IStatus status, IFile file) {
137 IStatus entry= PHPUIStatus.createError(
138 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
139 CorextMessages.getFormattedString("Resources.fileModified", file.getFullPath().toString()), //$NON-NLS-1$
141 if (status == null) {
143 } else if (status.isMultiStatus()) {
144 ((MultiStatus)status).add(entry);
147 MultiStatus result= new MultiStatus(PHPeclipsePlugin.getPluginId(),
148 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
149 CorextMessages.getString("Resources.modifiedResources"), null); //$NON-NLS-1$
156 private static IStatus addOutOfSync(IStatus status, IResource resource) {
157 IStatus entry= new Status(
159 ResourcesPlugin.PI_RESOURCES,
160 IResourceStatus.OUT_OF_SYNC_LOCAL,
161 CorextMessages.getFormattedString("Resources.outOfSync", resource.getFullPath().toString()), //$NON-NLS-1$
163 if (status == null) {
165 } else if (status.isMultiStatus()) {
166 ((MultiStatus)status).add(entry);
169 MultiStatus result= new MultiStatus(
170 ResourcesPlugin.PI_RESOURCES,
171 IResourceStatus.OUT_OF_SYNC_LOCAL,
172 CorextMessages.getString("Resources.outOfSyncResources"), null); //$NON-NLS-1$
179 public static String[] getLocationOSStrings(IResource[] resources) {
180 List result= new ArrayList(resources.length);
181 for (int i= 0; i < resources.length; i++) {
182 IPath location= resources[i].getLocation();
183 if (location != null)
184 result.add(location.toOSString());
186 return (String[]) result.toArray(new String[result.size()]);