Compatibility fragment commit
[phpeclipse.git] / net.sourceforge.phpeclipse.32.compatibility / src / net / sourceforge / phpdt / internal / corext / util / Resources.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.corext.util;
12
13 import java.util.ArrayList;
14 import java.util.HashMap;
15 import java.util.Iterator;
16 import java.util.List;
17 import java.util.Map;
18
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;
23
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;
32
33 public class Resources {
34
35         private Resources() {
36         }
37
38         /**
39          * Checks if the given resource is in sync with the underlying file system.
40          * 
41          * @param resource
42          *            the resource to be checked
43          * @return IStatus status describing the check's result. If <code>status.
44          * isOK()</code>
45          *         returns <code>true</code> then the resource is in sync
46          */
47         public static IStatus checkInSync(IResource resource) {
48                 return checkInSync(new IResource[] { resource });
49         }
50
51         /**
52          * Checks if the given resources are in sync with the underlying file
53          * system.
54          * 
55          * @param resources
56          *            the resources to be checked
57          * @return IStatus status describing the check's result. If <code>status.
58          *  isOK() </code>
59          *         returns <code>true</code> then the resources are in sync
60          */
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);
67                         }
68                 }
69                 if (result != null)
70                         return result;
71                 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(),
72                                 IStatus.OK, "", null); //$NON-NLS-1$
73         }
74
75         /**
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>.
79          * 
80          * @param resource
81          *            the resource to be checked
82          * @param context
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.
87          * 
88          * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[],
89          *      java.lang.Object)
90          */
91         public static IStatus makeCommittable(IResource resource, Object context) {
92                 return makeCommittable(new IResource[] { resource }, context);
93         }
94
95         /**
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>.
100          * 
101          * @param resources
102          *            the resources to be checked
103          * @param context
104          *            the context passed to <code>validateEdit</code>
105          * @return IStatus status describing the method's result. If <code>status.
106          * isOK()</code>
107          *         returns <code>true</code> then the add resources are
108          *         committable
109          * 
110          * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[],
111          *      java.lang.Object)
112          */
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);
120                 }
121                 if (readOnlyFiles.size() == 0)
122                         return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(),
123                                         IStatus.OK, "", null); //$NON-NLS-1$
124
125                 Map oldTimeStamps = createModificationStampMap(readOnlyFiles);
126                 IStatus status = ResourcesPlugin.getWorkspace().validateEdit(
127                                 (IFile[]) readOnlyFiles
128                                                 .toArray(new IFile[readOnlyFiles.size()]), context);
129                 if (!status.isOK())
130                         return status;
131
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);
138                 }
139                 if (modified != null)
140                         return modified;
141                 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(),
142                                 IStatus.OK, "", null); //$NON-NLS-1$
143         }
144
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()));
150                 }
151                 return map;
152         }
153
154         private static IStatus addModified(IStatus status, IFile file) {
155                 IStatus entry = PHPUIStatus
156                                 .createError(
157                                                 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
158                                                 CorextMessages
159                                                                 .getFormattedString(
160                                                                                 "Resources.fileModified", file.getFullPath().toString()), //$NON-NLS-1$
161                                                 null);
162                 if (status == null) {
163                         return entry;
164                 } else if (status.isMultiStatus()) {
165                         ((MultiStatus) status).add(entry);
166                         return status;
167                 } else {
168                         MultiStatus result = new MultiStatus(
169                                         PHPeclipsePlugin.getPluginId(),
170                                         IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
171                                         CorextMessages.getString("Resources.modifiedResources"), null); //$NON-NLS-1$
172                         result.add(status);
173                         result.add(entry);
174                         return result;
175                 }
176         }
177
178         private static IStatus addOutOfSync(IStatus status, IResource resource) {
179                 IStatus entry = new Status(
180                                 IStatus.ERROR,
181                                 ResourcesPlugin.PI_RESOURCES,
182                                 IResourceStatus.OUT_OF_SYNC_LOCAL,
183                                 CorextMessages
184                                                 .getFormattedString(
185                                                                 "Resources.outOfSync", resource.getFullPath().toString()), //$NON-NLS-1$
186                                 null);
187                 if (status == null) {
188                         return entry;
189                 } else if (status.isMultiStatus()) {
190                         ((MultiStatus) status).add(entry);
191                         return status;
192                 } else {
193                         MultiStatus result = new MultiStatus(ResourcesPlugin.PI_RESOURCES,
194                                         IResourceStatus.OUT_OF_SYNC_LOCAL, CorextMessages
195                                                         .getString("Resources.outOfSyncResources"), null); //$NON-NLS-1$
196                         result.add(status);
197                         result.add(entry);
198                         return result;
199                 }
200         }
201
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());
208                 }
209                 return (String[]) result.toArray(new String[result.size()]);
210         }
211 }