KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > core > refactoring > Resources


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.ltk.internal.core.refactoring;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Map.Entry;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.MultiStatus;
23 import org.eclipse.core.runtime.Status;
24
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IResource;
27 import org.eclipse.core.resources.IResourceStatus;
28 import org.eclipse.core.resources.ResourceAttributes;
29 import org.eclipse.core.resources.ResourcesPlugin;
30
31 import org.eclipse.ltk.core.refactoring.IRefactoringCoreStatusCodes;
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, RefactoringCorePlugin.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 JavaDoc 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 JavaDoc context) {
100         List JavaDoc readOnlyFiles= new ArrayList JavaDoc();
101         for (int i= 0; i < resources.length; i++) {
102             IResource resource= resources[i];
103             if (resource.getType() == IResource.FILE && isReadOnly(resource))
104                 readOnlyFiles.add(resource);
105         }
106         if (readOnlyFiles.size() == 0)
107             return new Status(IStatus.OK, RefactoringCorePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
108

109         Map JavaDoc 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 JavaDoc newTimeStamps= createModificationStampMap(readOnlyFiles);
117         for (Iterator JavaDoc iter= oldTimeStamps.entrySet().iterator(); iter.hasNext();) {
118             Map.Entry JavaDoc entry= (Entry) iter.next();
119             if (!entry.getValue().equals(newTimeStamps.get(entry.getKey())))
120                 modified= addModified(modified, (IFile) entry.getKey());
121         }
122         if (modified != null)
123             return modified;
124         return new Status(IStatus.OK, RefactoringCorePlugin.getPluginId(), IStatus.OK, "", null); //$NON-NLS-1$
125
}
126
127     private static Map JavaDoc createModificationStampMap(List JavaDoc files){
128         Map JavaDoc map= new HashMap JavaDoc();
129         for (Iterator JavaDoc iter= files.iterator(); iter.hasNext(); ) {
130             IFile file= (IFile)iter.next();
131             map.put(file, new Long JavaDoc(file.getModificationStamp()));
132         }
133         return map;
134     }
135     
136     private static IStatus addModified(IStatus status, IFile file) {
137         IStatus entry= new Status(
138             IStatus.ERROR, RefactoringCorePlugin.getPluginId(),
139             IRefactoringCoreStatusCodes.VALIDATE_EDIT_CHANGED_CONTENT,
140             Messages.format(RefactoringCoreMessages.Resources_fileModified, file.getFullPath().toString()),
141             null);
142         if (status == null) {
143             return entry;
144         } else if (status.isMultiStatus()) {
145             ((MultiStatus)status).add(entry);
146             return status;
147         } else {
148             MultiStatus result= new MultiStatus(RefactoringCorePlugin.getPluginId(),
149                 IRefactoringCoreStatusCodes.VALIDATE_EDIT_CHANGED_CONTENT,
150                 RefactoringCoreMessages.Resources_modifiedResources, null);
151             result.add(status);
152             result.add(entry);
153             return result;
154         }
155     }
156
157     private static IStatus addOutOfSync(IStatus status, IResource resource) {
158         IStatus entry= new Status(
159             IStatus.ERROR,
160             ResourcesPlugin.PI_RESOURCES,
161             IResourceStatus.OUT_OF_SYNC_LOCAL,
162             Messages.format(RefactoringCoreMessages.Resources_outOfSync, resource.getFullPath().toString()),
163             null);
164         if (status == null) {
165             return entry;
166         } else if (status.isMultiStatus()) {
167             ((MultiStatus)status).add(entry);
168             return status;
169         } else {
170             MultiStatus result= new MultiStatus(
171                 ResourcesPlugin.PI_RESOURCES,
172                 IResourceStatus.OUT_OF_SYNC_LOCAL,
173                 RefactoringCoreMessages.Resources_outOfSyncResources, null);
174             result.add(status);
175             result.add(entry);
176             return result;
177         }
178     }
179
180     public static boolean isReadOnly(IResource resource) {
181         ResourceAttributes resourceAttributes = resource.getResourceAttributes();
182         if (resourceAttributes == null) // not supported on this platform for this resource
183
return false;
184         return resourceAttributes.isReadOnly();
185     }
186     
187     static void setReadOnly(IResource resource, boolean readOnly) {
188         ResourceAttributes resourceAttributes = resource.getResourceAttributes();
189         if (resourceAttributes == null) // not supported on this platform for this resource
190
return;
191         
192         resourceAttributes.setReadOnly(readOnly);
193         try {
194             resource.setResourceAttributes(resourceAttributes);
195         } catch (CoreException e) {
196             RefactoringCorePlugin.log(e);
197         }
198     }
199 }
200
Popular Tags