1 11 package org.eclipse.ltk.internal.core.refactoring; 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 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 45 public static IStatus checkInSync(IResource resource) { 46 return checkInSync(new IResource[] {resource}); 47 } 48 49 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); } 69 70 81 public static IStatus makeCommittable(IResource resource, Object context) { 82 return makeCommittable(new IResource[] { resource }, context); 83 } 84 85 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 && isReadOnly(resource)) 104 readOnlyFiles.add(resource); 105 } 106 if (readOnlyFiles.size() == 0) 107 return new Status(IStatus.OK, RefactoringCorePlugin.getPluginId(), IStatus.OK, "", null); 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.entrySet().iterator(); iter.hasNext();) { 118 Map.Entry 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); } 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= 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) 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) 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 |