KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filebuffers > ResourceTextFileBufferManager


1 /*******************************************************************************
2  * Copyright (c) 2007 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.core.internal.filebuffers;
12
13 import java.util.ArrayList JavaDoc;
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 /**
53  * @since 3.3
54  */

55 public class ResourceTextFileBufferManager extends TextFileBufferManager {
56     
57     
58     
59     public ResourceTextFileBufferManager() {
60         fRegistry= new ResourceExtensionRegistry();
61     }
62
63     /*
64      * @see org.eclipse.core.filebuffers.ITextFileBufferManager#isTextFileLocation(org.eclipse.core.runtime.IPath, boolean)
65      * @since 3.2
66      */

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                     // ignore: API specification tells return true if content type can't be determined
83
}
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     /*
101      * @see org.eclipse.core.buffer.text.IBufferedFileManager#getDefaultEncoding()
102      */

103     public String JavaDoc getDefaultEncoding() {
104         return ResourcesPlugin.getEncoding();
105     }
106     
107     /*
108      * @see org.eclipse.core.internal.filebuffers.TextFileBufferManager#normalizeLocation(org.eclipse.core.runtime.IPath)
109      */

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 JavaDoc {
134                     runnableResult[0]= factory.createDocument();
135                 }
136                 public void handleException(Throwable JavaDoc 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 JavaDoc)
140                         throw (VirtualMachineError JavaDoc)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         // Set the initial line delimiter
153
if (document instanceof IDocumentExtension4) {
154             String JavaDoc 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 JavaDoc {
165                         participant.setup(document);
166                         if (document.getDocumentPartitioner() != null) {
167                             String JavaDoc 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 JavaDoc 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 JavaDoc)
176                             throw (VirtualMachineError JavaDoc)t;
177                     }
178                 };
179                 SafeRunner.run(runnable);
180             }
181         }
182         
183         return document;
184     }
185     
186     private String JavaDoc getLineDelimiterPreference(IFile file) {
187         IScopeContext[] scopeContext;
188         if (file != null && file.getProject() != null) {
189             // project preference
190
scopeContext= new IScopeContext[] { new ProjectScope(file.getProject()) };
191             String JavaDoc lineDelimiter= Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext);
192             if (lineDelimiter != null)
193                 return lineDelimiter;
194         }
195         // workspace preference
196
scopeContext= new IScopeContext[] { new InstanceScope() };
197         return Platform.getPreferencesService().getString(Platform.PI_RUNTIME, Platform.PREF_LINE_SEPARATOR, null, scopeContext);
198     }
199
200     protected String JavaDoc 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     /*
208      * @see org.eclipse.core.filebuffers.IFileBufferManager#validateState(org.eclipse.core.filebuffers.IFileBuffer[], org.eclipse.core.runtime.IProgressMonitor, java.lang.Object)
209      * @since 3.1
210      */

211     public void validateState(final IFileBuffer[] fileBuffers, IProgressMonitor monitor, final Object JavaDoc 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 JavaDoc 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 JavaDoc computationContext) {
228         ArrayList JavaDoc list= new ArrayList JavaDoc();
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 JavaDoc list= new ArrayList JavaDoc();
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 JavaDoc list= new ArrayList JavaDoc();
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