1 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 41 public class Resources { 42 43 private Resources() { 44 } 45 46 53 public static IStatus checkInSync(IResource resource) { 54 return checkInSync(new IResource[] {resource}); 55 } 56 57 65 public static IStatus checkInSync(IResource[] resources) { 66 IStatus result= null; 67 for (int i= 0; i < resources.length; i++) { 68 IResource resource= resources[i]; 69 if (!resource.isSynchronized(IResource.DEPTH_INFINITE)) { 70 result= addOutOfSync(result, resource); 71 } 72 } 73 if (result != null) 74 return result; 75 return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); } 77 78 89 public static IStatus makeCommittable(IResource resource, Object context) { 90 return makeCommittable(new IResource[] { resource }, context); 91 } 92 93 107 public static IStatus makeCommittable(IResource[] resources, Object context) { 108 List readOnlyFiles= new ArrayList (); 109 for (int i= 0; i < resources.length; i++) { 110 IResource resource= resources[i]; 111 if (resource.getType() == IResource.FILE && isReadOnly(resource)) 112 readOnlyFiles.add(resource); 113 } 114 if (readOnlyFiles.size() == 0) 115 return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); 117 Map oldTimeStamps= createModificationStampMap(readOnlyFiles); 118 IStatus status= ResourcesPlugin.getWorkspace().validateEdit( 119 (IFile[]) readOnlyFiles.toArray(new IFile[readOnlyFiles.size()]), context); 120 if (!status.isOK()) 121 return status; 122 123 IStatus modified= null; 124 Map newTimeStamps= createModificationStampMap(readOnlyFiles); 125 for (Iterator iter= oldTimeStamps.keySet().iterator(); iter.hasNext();) { 126 IFile file= (IFile) iter.next(); 127 if (!oldTimeStamps.get(file).equals(newTimeStamps.get(file))) 128 modified= addModified(modified, file); 129 } 130 if (modified != null) 131 return modified; 132 return new Status(IStatus.OK, JavaPlugin.getPluginId(), IStatus.OK, "", null); } 134 135 private static Map createModificationStampMap(List files){ 136 Map map= new HashMap (); 137 for (Iterator iter= files.iterator(); iter.hasNext(); ) { 138 IFile file= (IFile)iter.next(); 139 map.put(file, new Long (file.getModificationStamp())); 140 } 141 return map; 142 } 143 144 private static IStatus addModified(IStatus status, IFile file) { 145 IStatus entry= JavaUIStatus.createError( 146 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT, 147 Messages.format(CorextMessages.Resources_fileModified, file.getFullPath().toString()), 148 null); 149 if (status == null) { 150 return entry; 151 } else if (status.isMultiStatus()) { 152 ((MultiStatus)status).add(entry); 153 return status; 154 } else { 155 MultiStatus result= new MultiStatus(JavaPlugin.getPluginId(), 156 IJavaStatusConstants.VALIDATE_EDIT_CHANGED_CONTENT, 157 CorextMessages.Resources_modifiedResources, null); 158 result.add(status); 159 result.add(entry); 160 return result; 161 } 162 } 163 164 private static IStatus addOutOfSync(IStatus status, IResource resource) { 165 IStatus entry= new Status( 166 IStatus.ERROR, 167 ResourcesPlugin.PI_RESOURCES, 168 IResourceStatus.OUT_OF_SYNC_LOCAL, 169 Messages.format(CorextMessages.Resources_outOfSync, resource.getFullPath().toString()), 170 null); 171 if (status == null) { 172 return entry; 173 } else if (status.isMultiStatus()) { 174 ((MultiStatus)status).add(entry); 175 return status; 176 } else { 177 MultiStatus result= new MultiStatus( 178 ResourcesPlugin.PI_RESOURCES, 179 IResourceStatus.OUT_OF_SYNC_LOCAL, 180 CorextMessages.Resources_outOfSyncResources, null); 181 result.add(status); 182 result.add(entry); 183 return result; 184 } 185 } 186 187 195 public static String [] getLocationOSStrings(IResource[] resources) { 196 List result= new ArrayList (resources.length); 197 for (int i= 0; i < resources.length; i++) { 198 IPath location= resources[i].getLocation(); 199 if (location != null) 200 result.add(location.toOSString()); 201 } 202 return (String []) result.toArray(new String [result.size()]); 203 } 204 205 214 public static String getLocationString(IResource resource) { 215 URI uri= resource.getLocationURI(); 216 if (uri == null) 217 return null; 218 return EFS.SCHEME_FILE.equalsIgnoreCase(uri.getScheme()) 219 ? new File (uri).getAbsolutePath() 220 : uri.toString(); 221 } 222 223 public static boolean isReadOnly(IResource resource) { 224 ResourceAttributes resourceAttributes = resource.getResourceAttributes(); 225 if (resourceAttributes == null) return false; 227 return resourceAttributes.isReadOnly(); 228 } 229 230 static void setReadOnly(IResource resource, boolean readOnly) { 231 ResourceAttributes resourceAttributes = resource.getResourceAttributes(); 232 if (resourceAttributes == null) return; 234 235 resourceAttributes.setReadOnly(readOnly); 236 try { 237 resource.setResourceAttributes(resourceAttributes); 238 } catch (CoreException e) { 239 JavaPlugin.log(e); 240 } 241 } 242 } 243 | Popular Tags |