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.
42 * the resource to be checked
43 * @return IStatus status describing the check's result. If <code>status.
45 * returns <code>true</code> then the resource is in sync
47 public static IStatus checkInSync(IResource resource) {
48 return checkInSync(new IResource[] { resource });
52 * Checks if the given resources are in sync with the underlying file
56 * the resources to be checked
57 * @return IStatus status describing the check's result. If <code>status.
59 * returns <code>true</code> then the resources are in sync
61 public static IStatus checkInSync(IResource[] resources) {
62 IStatus result = null;
63 for (int i = 0; i < resources.length; i++) {
64 IResource resource = resources[i];
65 if (!resource.isSynchronized(IResource.DEPTH_INFINITE)) {
66 result = addOutOfSync(result, resource);
71 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(),
72 IStatus.OK, "", null); //$NON-NLS-1$
76 * Makes the given resource committable. Committable means that it is
77 * writeable and that its content hasn't changed by calling
78 * <code>validateEdit</code> for the given resource on <tt>IWorkspace</tt>.
81 * the resource to be checked
83 * the context passed to <code>validateEdit</code>
84 * @return status describing the method's result. If
85 * <code>status.isOK()</code> returns <code>true</code> then the
86 * resources are committable.
88 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[],
91 public static IStatus makeCommittable(IResource resource, Object context) {
92 return makeCommittable(new IResource[] { resource }, context);
96 * Makes the given resources committable. Committable means that all
97 * resources are writeable and that the content of the resources hasn't
98 * changed by calling <code>validateEdit</code> for a given file on
99 * <tt>IWorkspace</tt>.
102 * the resources to be checked
104 * the context passed to <code>validateEdit</code>
105 * @return IStatus status describing the method's result. If <code>status.
107 * returns <code>true</code> then the add resources are
110 * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[],
113 public static IStatus makeCommittable(IResource[] resources, Object context) {
114 List readOnlyFiles = new ArrayList();
115 for (int i = 0; i < resources.length; i++) {
116 IResource resource = resources[i];
117 if (resource.getType() == IResource.FILE
118 && resource.getResourceAttributes().isReadOnly())
119 readOnlyFiles.add(resource);
121 if (readOnlyFiles.size() == 0)
122 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(),
123 IStatus.OK, "", null); //$NON-NLS-1$
125 Map oldTimeStamps = createModificationStampMap(readOnlyFiles);
126 IStatus status = ResourcesPlugin.getWorkspace().validateEdit(
127 (IFile[]) readOnlyFiles
128 .toArray(new IFile[readOnlyFiles.size()]), context);
132 IStatus modified = null;
133 Map newTimeStamps = createModificationStampMap(readOnlyFiles);
134 for (Iterator iter = oldTimeStamps.keySet().iterator(); iter.hasNext();) {
135 IFile file = (IFile) iter.next();
136 if (!oldTimeStamps.get(file).equals(newTimeStamps.get(file)))
137 modified = addModified(modified, file);
139 if (modified != null)
141 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(),
142 IStatus.OK, "", null); //$NON-NLS-1$
145 private static Map createModificationStampMap(List files) {
146 Map map = new HashMap();
147 for (Iterator iter = files.iterator(); iter.hasNext();) {
148 IFile file = (IFile) iter.next();
149 map.put(file, new Long(file.getModificationStamp()));
154 private static IStatus addModified(IStatus status, IFile file) {
155 IStatus entry = PHPUIStatus
157 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
160 "Resources.fileModified", file.getFullPath().toString()), //$NON-NLS-1$
162 if (status == null) {
164 } else if (status.isMultiStatus()) {
165 ((MultiStatus) status).add(entry);
168 MultiStatus result = new MultiStatus(
169 PHPeclipsePlugin.getPluginId(),
170 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
171 CorextMessages.getString("Resources.modifiedResources"), null); //$NON-NLS-1$
178 private static IStatus addOutOfSync(IStatus status, IResource resource) {
179 IStatus entry = new Status(
181 ResourcesPlugin.PI_RESOURCES,
182 IResourceStatus.OUT_OF_SYNC_LOCAL,
185 "Resources.outOfSync", resource.getFullPath().toString()), //$NON-NLS-1$
187 if (status == null) {
189 } else if (status.isMultiStatus()) {
190 ((MultiStatus) status).add(entry);
193 MultiStatus result = new MultiStatus(ResourcesPlugin.PI_RESOURCES,
194 IResourceStatus.OUT_OF_SYNC_LOCAL, CorextMessages
195 .getString("Resources.outOfSyncResources"), null); //$NON-NLS-1$
202 public static String[] getLocationOSStrings(IResource[] resources) {
203 List result = new ArrayList(resources.length);
204 for (int i = 0; i < resources.length; i++) {
205 IPath location = resources[i].getLocation();
206 if (location != null)
207 result.add(location.toOSString());
209 return (String[]) result.toArray(new String[result.size()]);