1 11 package org.eclipse.core.internal.filebuffers; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.Assert; 16 import org.eclipse.core.runtime.CoreException; 17 import org.eclipse.core.runtime.IPath; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.ISafeRunnable; 20 import org.eclipse.core.runtime.IStatus; 21 import org.eclipse.core.runtime.Platform; 22 import org.eclipse.core.runtime.SafeRunner; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.core.runtime.content.IContentDescription; 25 import org.eclipse.core.runtime.content.IContentType; 26 import org.eclipse.core.runtime.content.IContentTypeManager; 27 import org.eclipse.core.runtime.jobs.ISchedulingRule; 28 import org.eclipse.core.runtime.preferences.IScopeContext; 29 import org.eclipse.core.runtime.preferences.InstanceScope; 30 31 import org.eclipse.core.resources.IFile; 32 import org.eclipse.core.resources.IResource; 33 import org.eclipse.core.resources.IResourceRuleFactory; 34 import org.eclipse.core.resources.IWorkspace; 35 import org.eclipse.core.resources.IWorkspaceRunnable; 36 import org.eclipse.core.resources.ProjectScope; 37 import org.eclipse.core.resources.ResourcesPlugin; 38 39 import org.eclipse.core.filebuffers.FileBuffers; 40 import org.eclipse.core.filebuffers.IAnnotationModelFactory; 41 import org.eclipse.core.filebuffers.IDocumentFactory; 42 import org.eclipse.core.filebuffers.IDocumentSetupParticipant; 43 import org.eclipse.core.filebuffers.IFileBuffer; 44 import org.eclipse.core.filebuffers.IStateValidationSupport; 45 import org.eclipse.core.filebuffers.LocationKind; 46 47 import org.eclipse.jface.text.IDocument; 48 import org.eclipse.jface.text.IDocumentExtension4; 49 import org.eclipse.jface.text.source.IAnnotationModel; 50 51 52 55 public class ResourceTextFileBufferManager extends TextFileBufferManager { 56 57 58 59 public ResourceTextFileBufferManager() { 60 fRegistry= new ResourceExtensionRegistry(); 61 } 62 63 67 public boolean isTextFileLocation(IPath location, boolean strict) { 68 Assert.isNotNull(location); 69 location= FileBuffers.normalizeLocation(location); 70 71 IFile file= FileBuffers.getWorkspaceFileAtLocation(location, true); 72 if (file != null) { 73 if (file.exists()) { 74 try { 75 IContentDescription description= file.getContentDescription(); 76 if (description != null) { 77 IContentType type= description.getContentType(); 78 if (type != null) 79 return type.isKindOf(TEXT_CONTENT_TYPE); 80 } 81 } catch (CoreException x) { 82 } 84 } else { 85 IContentTypeManager manager= Platform.getContentTypeManager(); 86 IContentType[] contentTypes= manager.findContentTypesFor(file.getName()); 87 if (contentTypes != null && contentTypes.length > 0) { 88 for (int i= 0; i < contentTypes.length; i++) 89 if (contentTypes[i].isKindOf(TEXT_CONTENT_TYPE)) 90 return true; 91 return false; 92 } 93 } 94 return !strict; 95 } 96 97 return isTextFileLocation(FileBuffers.getFileStoreAtLocation(location), strict); 98 } 99 100 103 public String getDefaultEncoding() { 104 return ResourcesPlugin.getEncoding(); 105 } 106 107 110 protected IPath normalizeLocation(IPath location) { 111 return FileBuffers.normalizeLocation(location); 112 } 113 114 protected AbstractFileBuffer createTextFileBuffer(IPath location, LocationKind locationKind) { 115 if (locationKind == LocationKind.IFILE || locationKind == LocationKind.NORMALIZE && FileBuffers.getWorkspaceFileAtLocation(location, true) != null) 116 return new ResourceTextFileBuffer(this); 117 return new FileStoreTextFileBuffer(this); 118 } 119 120 IAnnotationModel createAnnotationModel(IFile file) { 121 Assert.isNotNull(file); 122 IAnnotationModelFactory factory= ((ResourceExtensionRegistry)fRegistry).getAnnotationModelFactory(file); 123 if (factory != null) 124 return factory.createAnnotationModel(file.getFullPath()); 125 return null; 126 } 127 128 IDocument createEmptyDocument(IFile file) { 129 final IDocument[] runnableResult= new IDocument[1]; 130 final IDocumentFactory factory= ((ResourceExtensionRegistry)fRegistry).getDocumentFactory(file); 131 if (factory != null) { 132 ISafeRunnable runnable= new ISafeRunnable() { 133 public void run() throws Exception { 134 runnableResult[0]= factory.createDocument(); 135 } 136 public void handleException(Throwable t) { 137 IStatus status= new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, FileBuffersMessages.TextFileBufferManager_error_documentFactoryFailed, t); 138 FileBuffersPlugin.getDefault().getLog().log(status); 139 if (t instanceof VirtualMachineError ) 140 throw (VirtualMachineError )t; 141 } 142 }; 143 SafeRunner.run(runnable); 144 } 145 146 final IDocument document; 147 if (runnableResult[0] != null) 148 document= runnableResult[0]; 149 else 150 document= new SynchronizableDocument(); 151 152 if (document instanceof IDocumentExtension4) { 154 String initalLineDelimiter= getLineDelimiterPreference(file); 155 if (initalLineDelimiter != null) 156 ((IDocumentExtension4)document).setInitialLineDelimiter(initalLineDelimiter); 157 } 158 159 final IDocumentSetupParticipant[] participants= ((ResourceExtensionRegistry)fRegistry).getDocumentSetupParticipants(file); 160 if (participants != null) { 161 for (int i= 0; i < participants.length; i++) { 162 final IDocumentSetupParticipant participant= participants[i]; 163 ISafeRunnable runnable= new ISafeRunnable() { 164 public void run() throws Exception { 165 participant.setup(document); 166 if (document.getDocumentPartitioner() != null) { 167 String message= NLSUtility.format(FileBuffersMessages.TextFileBufferManager_warning_documentSetupInstallsDefaultPartitioner, participant.getClass()); 168 IStatus status= new Status(IStatus.WARNING, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, message, null); 169 FileBuffersPlugin.getDefault().getLog().log(status); 170 } 171 } 172 public void handleException(Throwable t) { 173 IStatus status= new Status(IStatus.ERROR, FileBuffersPlugin.PLUGIN_ID, IStatus.OK, FileBuffersMessages.TextFileBufferManager_error_documentSetupFailed, t); 174 FileBuffersPlugin.getDefault().getLog().log(status); 175 if (t instanceof VirtualMachineError ) 176 throw (VirtualMachineError )t; 177 } 178 }; 179 SafeRunner.run(runnable); 180 } 181 } 182 183 return document; 184 } 185 186 private String getLineDelimiterPreference(IFile file) { 187 IScopeContext[] scopeContext; 188 if (file != null && file.getProject() != null) { 189 scopeContext= new IScopeContext[] { new ProjectScope(file.getProject()) }; 191 String lineDelimiter= Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext); 192 if (lineDelimiter != null) 193 return lineDelimiter; 194 } 195 scopeContext= new IScopeContext[] { new InstanceScope() }; 197 return Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext); 198 } 199 200 protected String getLineDelimiterPreference(IPath location, LocationKind locationKind) { 201 IFile file= null; 202 if (locationKind != LocationKind.LOCATION) 203 file= FileBuffers.getWorkspaceFileAtLocation(location); 204 return getLineDelimiterPreference(file); 205 } 206 207 211 public void validateState(final IFileBuffer[] fileBuffers, IProgressMonitor monitor, final Object computationContext) throws CoreException { 212 IWorkspaceRunnable runnable= new IWorkspaceRunnable() { 213 public void run(IProgressMonitor progressMonitor) throws CoreException { 214 IFileBuffer[] toValidate= findFileBuffersToValidate(fileBuffers); 215 validationStateAboutToBeChanged(toValidate); 216 try { 217 IStatus status= validateEdit(toValidate, computationContext); 218 validationStateChanged(toValidate, true, status); 219 } catch (RuntimeException x) { 220 validationStateChangedFailed(toValidate); 221 } 222 } 223 }; 224 ResourcesPlugin.getWorkspace().run(runnable, computeValidateStateRule(fileBuffers), IWorkspace.AVOID_UPDATE, monitor); 225 } 226 227 private IStatus validateEdit(IFileBuffer[] fileBuffers, Object computationContext) { 228 ArrayList list= new ArrayList (); 229 for (int i= 0; i < fileBuffers.length; i++) { 230 IFile file= getWorkspaceFile(fileBuffers[i]); 231 if (file != null) 232 list.add(file); 233 } 234 IFile[] files= new IFile[list.size()]; 235 list.toArray(files); 236 return ResourcesPlugin.getWorkspace().validateEdit(files, computationContext); 237 } 238 239 private IFileBuffer[] findFileBuffersToValidate(IFileBuffer[] fileBuffers) { 240 ArrayList list= new ArrayList (); 241 for (int i= 0; i < fileBuffers.length; i++) { 242 if (!fileBuffers[i].isStateValidated()) 243 list.add(fileBuffers[i]); 244 } 245 return (IFileBuffer[]) list.toArray(new IFileBuffer[list.size()]); 246 } 247 248 private void validationStateAboutToBeChanged(IFileBuffer[] fileBuffers) { 249 for (int i= 0; i < fileBuffers.length; i++) { 250 if (fileBuffers[i] instanceof IStateValidationSupport) { 251 IStateValidationSupport support= (IStateValidationSupport) fileBuffers[i]; 252 support.validationStateAboutToBeChanged(); 253 } 254 } 255 } 256 257 private void validationStateChanged(IFileBuffer[] fileBuffers, boolean validationState, IStatus status) { 258 for (int i= 0; i < fileBuffers.length; i++) { 259 if (fileBuffers[i] instanceof IStateValidationSupport) { 260 IStateValidationSupport support= (IStateValidationSupport) fileBuffers[i]; 261 support.validationStateChanged(validationState, status); 262 } 263 } 264 } 265 266 private void validationStateChangedFailed(IFileBuffer[] fileBuffers) { 267 for (int i= 0; i < fileBuffers.length; i++) { 268 if (fileBuffers[i] instanceof IStateValidationSupport) { 269 IStateValidationSupport support= (IStateValidationSupport) fileBuffers[i]; 270 support.validationStateChangeFailed(); 271 } 272 } 273 } 274 275 private IFile getWorkspaceFile(IFileBuffer fileBuffer) { 276 return FileBuffers.getWorkspaceFileAtLocation(fileBuffer.getLocation()); 277 } 278 279 private ISchedulingRule computeValidateStateRule(IFileBuffer[] fileBuffers) { 280 ArrayList list= new ArrayList (); 281 for (int i= 0; i < fileBuffers.length; i++) { 282 IResource resource= getWorkspaceFile(fileBuffers[i]); 283 if (resource != null) 284 list.add(resource); 285 } 286 IResource[] resources= new IResource[list.size()]; 287 list.toArray(resources); 288 IResourceRuleFactory factory= ResourcesPlugin.getWorkspace().getRuleFactory(); 289 return factory.validateEditRule(resources); 290 } 291 292 } 293 | Popular Tags |