1 12 package org.eclipse.jdt.internal.junit.util; 13 14 import java.util.ArrayList ; 15 import java.util.HashMap ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.MultiStatus; 22 import org.eclipse.core.runtime.Status; 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 29 import org.eclipse.jdt.internal.junit.Messages; 30 import org.eclipse.jdt.internal.junit.ui.JUnitMessages; 31 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 32 33 34 public class Resources { 35 36 private Resources() { 37 } 38 39 46 public static IStatus checkInSync(IResource resource) { 47 return checkInSync(new IResource[] {resource}); 48 } 49 50 58 public static IStatus checkInSync(IResource[] resources) { 59 IStatus result= null; 60 for (int i= 0; i < resources.length; i++) { 61 IResource resource= resources[i]; 62 if (!resource.isSynchronized(IResource.DEPTH_INFINITE)) { 63 result= addOutOfSync(result, resource); 64 } 65 } 66 if (result != null) 67 return result; 68 return new Status(IStatus.OK, JUnitPlugin.getPluginId(), IStatus.OK, "", null); } 70 71 84 public static IStatus makeCommittable(IResource resource, Object context) { 85 return makeCommittable(new IResource[] { resource }, context); 86 } 87 88 102 public static IStatus makeCommittable(IResource[] resources, Object context) { 103 List readOnlyFiles= new ArrayList (); 104 for (int i= 0; i < resources.length; i++) { 105 IResource resource= resources[i]; 106 if (resource.getType() == IResource.FILE && resource.getResourceAttributes().isReadOnly()) 107 readOnlyFiles.add(resource); 108 } 109 if (readOnlyFiles.size() == 0) 110 return new Status(IStatus.OK, JUnitPlugin.getPluginId(), IStatus.OK, "", null); 112 Map oldTimeStamps= createModificationStampMap(readOnlyFiles); 113 IStatus status= ResourcesPlugin.getWorkspace().validateEdit( 114 (IFile[]) readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context); 115 if (!status.isOK()) 116 return status; 117 118 IStatus modified= null; 119 Map newTimeStamps= createModificationStampMap(readOnlyFiles); 120 for (Iterator iter= oldTimeStamps.keySet().iterator(); iter.hasNext();) { 121 IFile file= (IFile) iter.next(); 122 if (!oldTimeStamps.get(file).equals(newTimeStamps.get(file))) 123 modified= addModified(modified, file); 124 } 125 if (modified != null) 126 return modified; 127 return new Status(IStatus.OK, JUnitPlugin.getPluginId(), IStatus.OK, "", null); } 129 130 private static Map createModificationStampMap(List files){ 131 Map map= new HashMap (); 132 for (Iterator iter= files.iterator(); iter.hasNext(); ) { 133 IFile file= (IFile)iter.next(); 134 map.put(file, new Long (file.getModificationStamp())); 135 } 136 return map; 137 } 138 139 private static IStatus addModified(IStatus status, IFile file) { 140 IStatus entry= JUnitStatus.createError(Messages.format(JUnitMessages.Resources_fileModified, file.getFullPath().toString())); 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(JUnitPlugin.getPluginId(), 148 IJUnitStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT, 149 JUnitMessages.Resources_modifiedResources, null); 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 Messages.format(JUnitMessages.Resources_outOfSync, resource.getFullPath().toString()), 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 JUnitMessages.Resources_outOfSyncResources, null); 173 result.add(status); 174 result.add(entry); 175 return result; 176 } 177 } 178 } | Popular Tags |