]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/core extension/org/eclipse/jdt/internal/corext/util/Resources.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / core extension / org / eclipse / jdt / internal / corext / util / Resources.java
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
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.util;
12
13 import java.io.File;
14 import java.net.URI;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.core.filesystem.EFS;
22
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;
28
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;
34
35 import org.eclipse.jdt.internal.corext.CorextMessages;
36
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;
41
42 public class Resources {
43
44         private Resources() {
45         }
46
47         /**
48          * Checks if the given resource is in sync with the underlying file system.
49          *
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
53          */
54         public static IStatus checkInSync(IResource resource) {
55                 return checkInSync(new IResource[] {resource});
56         }
57
58         /**
59          * Checks if the given resources are in sync with the underlying file
60          * system.
61          *
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
65          */
66         public static IStatus checkInSync(IResource[] resources) {
67                 IStatus result= null;
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);
72                         }
73                 }
74                 if (result != null)
75                         return result;
76                 return Status.OK_STATUS;
77         }
78
79         /**
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>.
83          *
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.
87          *
88          * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
89          */
90         public static IStatus makeCommittable(IResource resource, Object context) {
91                 return makeCommittable(new IResource[] { resource }, context);
92         }
93
94         /**
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>.
99          *
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
104          * committable
105          *
106          * @see org.eclipse.core.resources.IWorkspace#validateEdit(org.eclipse.core.resources.IFile[], java.lang.Object)
107          */
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);
114                 }
115                 if (readOnlyFiles.size() == 0)
116                         return Status.OK_STATUS;
117
118                 Map<IFile, Long> oldTimeStamps= createModificationStampMap(readOnlyFiles);
119                 IStatus status= ResourcesPlugin.getWorkspace().validateEdit(
120                         readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context);
121                 if (!status.isOK())
122                         return status;
123
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);
130                 }
131                 if (modified != null)
132                         return modified;
133                 return Status.OK_STATUS;
134         }
135
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()));
141                 }
142                 return map;
143         }
144
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)),
149                         null);
150                 if (status == null) {
151                         return entry;
152                 } else if (status.isMultiStatus()) {
153                         ((MultiStatus)status).add(entry);
154                         return status;
155                 } else {
156                         MultiStatus result= new MultiStatus(JavaPlugin.getPluginId(),
157                                 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT,
158                                 CorextMessages.Resources_modifiedResources, null);
159                         result.add(status);
160                         result.add(entry);
161                         return result;
162                 }
163         }
164
165         private static IStatus addOutOfSync(IStatus status, IResource resource) {
166                 IStatus entry= new Status(
167                         IStatus.ERROR,
168                         ResourcesPlugin.PI_RESOURCES,
169                         IResourceStatus.OUT_OF_SYNC_LOCAL,
170                         Messages.format(CorextMessages.Resources_outOfSync, BasicElementLabels.getPathLabel(resource.getFullPath(), false)),
171                         null);
172                 if (status == null) {
173                         return entry;
174                 } else if (status.isMultiStatus()) {
175                         ((MultiStatus)status).add(entry);
176                         return status;
177                 } else {
178                         MultiStatus result= new MultiStatus(
179                                 ResourcesPlugin.PI_RESOURCES,
180                                 IResourceStatus.OUT_OF_SYNC_LOCAL,
181                                 CorextMessages.Resources_outOfSyncResources, null);
182                         result.add(status);
183                         result.add(entry);
184                         return result;
185                 }
186         }
187
188         /**
189          * This method is used to generate a list of local locations to
190          * be used in DnD for file transfers.
191          *
192          * @param resources the array of resources to get the local
193          *  locations for
194          * @return the local locations
195          */
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());
202                 }
203                 return result.toArray(new String[result.size()]);
204         }
205
206         /**
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.
210          *
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>
214          */
215         public static String getLocationString(IResource resource) {
216                 URI uri= resource.getLocationURI();
217                 if (uri == null)
218                         return null;
219                 return EFS.SCHEME_FILE.equalsIgnoreCase(uri.getScheme())
220                         ? new File(uri).getAbsolutePath()
221                         : uri.toString();
222         }
223
224         public static boolean isReadOnly(IResource resource) {
225                 ResourceAttributes resourceAttributes = resource.getResourceAttributes();
226                 if (resourceAttributes == null)  // not supported on this platform for this resource
227                         return false;
228                 return resourceAttributes.isReadOnly();
229         }
230
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
234                         return;
235
236                 resourceAttributes.setReadOnly(readOnly);
237                 try {
238                         resource.setResourceAttributes(resourceAttributes);
239                 } catch (CoreException e) {
240                         JavaPlugin.log(e);
241                 }
242         }
243 }