d555c130056be0fdc2403d4406bb73f7e0482f9a
[phpeclipse.git] / net.sourceforge.phpeclipse / 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 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
44          */
45         public static IStatus checkInSync(IResource resource) {
46                 return checkInSync(new IResource[] {resource});
47         }
48         
49         /**
50          * Checks if the given resources are in sync with the underlying file
51          * system.
52          * 
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
56          */
57         public static IStatus checkInSync(IResource[] resources) {
58                 IStatus result= null;
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);
63                         }                       
64                 }
65                 if (result != null)
66                         return result;
67                 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$              
68         }
69
70         /**
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>.
74          * 
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.
78          * 
79          * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
80          */
81         public static IStatus makeCommittable(IResource resource, Object context) {
82                 return makeCommittable(new IResource[] { resource }, context);
83         }
84         
85         /**
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>.
90          * 
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
95          * committable
96          * 
97          * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
98          */
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);
105                 }
106                 if (readOnlyFiles.size() == 0)
107                         return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
108                         
109                 Map oldTimeStamps= createModificationStampMap(readOnlyFiles);
110                 IStatus status= ResourcesPlugin.getWorkspace().validateEdit(
111                         (IFile[]) readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context);
112                 if (!status.isOK())
113                         return status;
114                         
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);
121                 }
122                 if (modified != null)   
123                         return modified;
124                 return new Status(IStatus.OK, PHPeclipsePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
125         }
126
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()));
132                 }
133                 return map;
134         }
135         
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$ 
140                         null);
141                 if (status == null) {
142                         return entry;
143                 } else if (status.isMultiStatus()) {
144                         ((MultiStatus)status).add(entry);
145                         return status;
146                 } else {
147                         MultiStatus result= new MultiStatus(PHPeclipsePlugin.getPluginId(),
148                                 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
149                                 CorextMessages.getString("Resources.modifiedResources"), null); //$NON-NLS-1$ 
150                         result.add(status);
151                         result.add(entry);
152                         return result;
153                 }
154         }       
155
156         private static IStatus addOutOfSync(IStatus status, IResource resource) {
157                 IStatus entry= new Status(
158                         IStatus.ERROR,
159                         ResourcesPlugin.PI_RESOURCES,
160                         IResourceStatus.OUT_OF_SYNC_LOCAL,
161                         CorextMessages.getFormattedString("Resources.outOfSync", resource.getFullPath().toString()), //$NON-NLS-1$ 
162                         null);
163                 if (status == null) {
164                         return entry;
165                 } else if (status.isMultiStatus()) {
166                         ((MultiStatus)status).add(entry);
167                         return status;
168                 } else {
169                         MultiStatus result= new MultiStatus(
170                                 ResourcesPlugin.PI_RESOURCES,
171                                 IResourceStatus.OUT_OF_SYNC_LOCAL,
172                                 CorextMessages.getString("Resources.outOfSyncResources"), null); //$NON-NLS-1$ 
173                         result.add(status);
174                         result.add(entry);
175                         return result;
176                 }
177         }
178
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());
185                 }
186                 return (String[]) result.toArray(new String[result.size()]);
187         }
188 }