KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > editors > text > ForwardingDocumentProvider


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.editors.text;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.content.IContentType;
17
18 import org.eclipse.core.filebuffers.IDocumentSetupParticipant;
19
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.IDocumentExtension3;
22 import org.eclipse.jface.text.source.IAnnotationModel;
23
24 import org.eclipse.ui.texteditor.IDocumentProvider;
25 import org.eclipse.ui.texteditor.IDocumentProviderExtension;
26 import org.eclipse.ui.texteditor.IDocumentProviderExtension2;
27 import org.eclipse.ui.texteditor.IDocumentProviderExtension3;
28 import org.eclipse.ui.texteditor.IDocumentProviderExtension4;
29 import org.eclipse.ui.texteditor.IDocumentProviderExtension5;
30 import org.eclipse.ui.texteditor.IElementStateListener;
31
32
33 /**
34  * A forwarding document provider is a document provider that forwards all requests
35  * to a known parent document provider. The only functional addition of the
36  * forwarding document provider is that it ensures that all documents provided
37  * to clients are appropriately set up.
38  * <p>
39  * This document provider should be used by editors that directly or indirectly
40  * work with a {@link org.eclipse.ui.editors.text.TextFileDocumentProvider} and do not
41  * accept that they may be provided with documents which do not contain the partitioning
42  * they work on. This can happen either because of a plug-in configuration error
43  * or when a user associates a file name or file extension with an existing text
44  * editor and the file buffer infrastructure does not recognize that file name
45  * or file extension to be of the same file type the editor works on. Thus, the
46  * document provided for the files with that name or extension may not be set up
47  * in the way the editor expects it. The <code>ForwardingDocumentProvider</code>
48  * compensates for that situation.
49  * </p>
50  * <p>
51  * Editors that directly work with a {@link org.eclipse.ui.editors.text.TextFileDocumentProvider} can
52  * now use a <code>ForwardingDocumentProvider</code> instead and configure a
53  * {@link org.eclipse.ui.editors.text.TextFileDocumentProvider} as its parent provider. Editors that
54  * indirectly work with a {@link org.eclipse.ui.editors.text.TextFileDocumentProvider}, e.g. never
55  * set a document provider explicitly, should explicitly set a
56  * <code>ForwardingDocumentProvider</code> as document provider. In this case
57  * the forwarding document provider may not be shared between editors.
58  * </p>
59  *
60  * @since 3.0
61  */

62 public class ForwardingDocumentProvider implements IDocumentProvider, IDocumentProviderExtension, IDocumentProviderExtension2, IDocumentProviderExtension3, IDocumentProviderExtension4, IDocumentProviderExtension5, IStorageDocumentProvider {
63
64     private IDocumentProvider fParentProvider;
65     private String JavaDoc fPartitioning;
66     private IDocumentSetupParticipant fDocumentSetupParticipant;
67     private boolean fAllowSetParentProvider;
68
69
70     /**
71      * Creates a new forwarding document provider with a fixed parent document provider. Calling
72      * {@link #setParentProvider(IDocumentProvider)} does not have any effect on this object.
73      *
74      * @param partitioning the partitioning
75      * @param documentSetupParticipant the document setup participant
76      * @param parentProvider the parent document provider
77      */

78     public ForwardingDocumentProvider(String JavaDoc partitioning, IDocumentSetupParticipant documentSetupParticipant, IDocumentProvider parentProvider) {
79         fPartitioning= partitioning;
80         fDocumentSetupParticipant= documentSetupParticipant;
81         fParentProvider= parentProvider;
82         fAllowSetParentProvider= false;
83     }
84
85     /**
86      * Creates a new forwarding document provider with a dynamically changeable
87      * parent provider. Forwarding document providers created with that method
88      * are not allowed to be shared by multiple editors.
89      *
90      * @param partitioning the partitioning
91      * @param documentSetupParticipant the document setup participant
92      */

93     public ForwardingDocumentProvider(String JavaDoc partitioning, IDocumentSetupParticipant documentSetupParticipant) {
94         fPartitioning= partitioning;
95         fDocumentSetupParticipant= documentSetupParticipant;
96         fAllowSetParentProvider= true;
97     }
98
99     /**
100      * Sets the parent document provider. This method has only an effect if the
101      * forwarding document provider has accordingly be created.
102      *
103      * @param parentProvider the new parent document provider
104      */

105     public void setParentProvider(IDocumentProvider parentProvider) {
106         if (fAllowSetParentProvider)
107             fParentProvider= parentProvider;
108     }
109
110     /*
111      * @see org.eclipse.ui.texteditor.IDocumentProvider#connect(java.lang.Object)
112      */

113     public void connect(Object JavaDoc element) throws CoreException {
114         fParentProvider.connect(element);
115         IDocument document= fParentProvider.getDocument(element);
116         if (document instanceof IDocumentExtension3) {
117             IDocumentExtension3 extension= (IDocumentExtension3) document;
118             if (extension.getDocumentPartitioner(fPartitioning) == null)
119                 fDocumentSetupParticipant.setup(document);
120         }
121     }
122
123     /*
124      * @see org.eclipse.ui.texteditor.IDocumentProvider#disconnect(java.lang.Object)
125      */

126     public void disconnect(Object JavaDoc element) {
127         fParentProvider.disconnect(element);
128     }
129
130     /*
131      * @see org.eclipse.ui.texteditor.IDocumentProvider#getDocument(java.lang.Object)
132      */

133     public IDocument getDocument(Object JavaDoc element) {
134         return fParentProvider.getDocument(element);
135     }
136
137     /*
138      * @see org.eclipse.ui.texteditor.IDocumentProvider#resetDocument(java.lang.Object)
139      */

140     public void resetDocument(Object JavaDoc element) throws CoreException {
141         fParentProvider.resetDocument(element);
142     }
143
144     /*
145      * @see org.eclipse.ui.texteditor.IDocumentProvider#saveDocument(org.eclipse.core.runtime.IProgressMonitor, java.lang.Object, org.eclipse.jface.text.IDocument, boolean)
146      */

147     public void saveDocument(IProgressMonitor monitor, Object JavaDoc element, IDocument document, boolean overwrite) throws CoreException {
148         fParentProvider.saveDocument(monitor, element, document, overwrite);
149     }
150
151     /*
152      * @see org.eclipse.ui.texteditor.IDocumentProvider#getModificationStamp(java.lang.Object)
153      */

154     public long getModificationStamp(Object JavaDoc element) {
155         return fParentProvider.getModificationStamp(element);
156     }
157
158     /*
159      * @see org.eclipse.ui.texteditor.IDocumentProvider#getSynchronizationStamp(java.lang.Object)
160      */

161     public long getSynchronizationStamp(Object JavaDoc element) {
162         return fParentProvider.getSynchronizationStamp(element);
163     }
164
165     /*
166      * @see org.eclipse.ui.texteditor.IDocumentProvider#isDeleted(java.lang.Object)
167      */

168     public boolean isDeleted(Object JavaDoc element) {
169         return fParentProvider.isDeleted(element);
170     }
171
172     /*
173      * @see org.eclipse.ui.texteditor.IDocumentProvider#mustSaveDocument(java.lang.Object)
174      */

175     public boolean mustSaveDocument(Object JavaDoc element) {
176         return fParentProvider.mustSaveDocument(element);
177     }
178
179     /*
180      * @see org.eclipse.ui.texteditor.IDocumentProvider#canSaveDocument(java.lang.Object)
181      */

182     public boolean canSaveDocument(Object JavaDoc element) {
183         return fParentProvider.canSaveDocument(element);
184     }
185
186     /*
187      * @see org.eclipse.ui.texteditor.IDocumentProvider#getAnnotationModel(java.lang.Object)
188      */

189     public IAnnotationModel getAnnotationModel(Object JavaDoc element) {
190         return fParentProvider.getAnnotationModel(element);
191     }
192
193     /*
194      * @see org.eclipse.ui.texteditor.IDocumentProvider#aboutToChange(java.lang.Object)
195      */

196     public void aboutToChange(Object JavaDoc element) {
197         fParentProvider.aboutToChange(element);
198     }
199
200     /*
201      * @see org.eclipse.ui.texteditor.IDocumentProvider#changed(java.lang.Object)
202      */

203     public void changed(Object JavaDoc element) {
204         fParentProvider.changed(element);
205     }
206
207     /*
208      * @see org.eclipse.ui.texteditor.IDocumentProvider#addElementStateListener(org.eclipse.ui.texteditor.IElementStateListener)
209      */

210     public void addElementStateListener(IElementStateListener listener) {
211         fParentProvider.addElementStateListener(listener);
212     }
213
214     /*
215      * @see org.eclipse.ui.texteditor.IDocumentProvider#removeElementStateListener(org.eclipse.ui.texteditor.IElementStateListener)
216      */

217     public void removeElementStateListener(IElementStateListener listener) {
218         fParentProvider.removeElementStateListener(listener);
219     }
220
221     /*
222      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#isReadOnly(java.lang.Object)
223      */

224     public boolean isReadOnly(Object JavaDoc element) {
225         if (fParentProvider instanceof IDocumentProviderExtension) {
226             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
227             return extension.isReadOnly(element);
228         }
229         return false;
230     }
231
232     /*
233      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#isModifiable(java.lang.Object)
234      */

235     public boolean isModifiable(Object JavaDoc element) {
236         if (fParentProvider instanceof IDocumentProviderExtension) {
237             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
238             return extension.isModifiable(element);
239         }
240         return true;
241     }
242
243     /*
244      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#validateState(java.lang.Object, java.lang.Object)
245      */

246     public void validateState(Object JavaDoc element, Object JavaDoc computationContext) throws CoreException {
247         if (fParentProvider instanceof IDocumentProviderExtension) {
248             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
249             extension.validateState(element, computationContext);
250         }
251     }
252
253     /*
254      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#isStateValidated(java.lang.Object)
255      */

256     public boolean isStateValidated(Object JavaDoc element) {
257         if (fParentProvider instanceof IDocumentProviderExtension) {
258             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
259             return extension.isStateValidated(element);
260         }
261         return true;
262     }
263
264     /*
265      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#updateStateCache(java.lang.Object)
266      */

267     public void updateStateCache(Object JavaDoc element) throws CoreException {
268         if (fParentProvider instanceof IDocumentProviderExtension) {
269             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
270             extension.updateStateCache(element);
271         }
272     }
273
274     /*
275      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#setCanSaveDocument(java.lang.Object)
276      */

277     public void setCanSaveDocument(Object JavaDoc element) {
278         if (fParentProvider instanceof IDocumentProviderExtension) {
279             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
280             extension.setCanSaveDocument(element);
281         }
282     }
283
284     /*
285      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#getStatus(java.lang.Object)
286      */

287     public IStatus getStatus(Object JavaDoc element) {
288         if (fParentProvider instanceof IDocumentProviderExtension) {
289             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
290             return extension.getStatus(element);
291         }
292         return null;
293     }
294
295     /*
296      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension#synchronize(java.lang.Object)
297      */

298     public void synchronize(Object JavaDoc element) throws CoreException {
299         if (fParentProvider instanceof IDocumentProviderExtension) {
300             IDocumentProviderExtension extension= (IDocumentProviderExtension)fParentProvider;
301             extension.synchronize(element);
302         }
303     }
304
305     /*
306      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension2#setProgressMonitor(org.eclipse.core.runtime.IProgressMonitor)
307      */

308     public void setProgressMonitor(IProgressMonitor progressMonitor) {
309         if (fParentProvider instanceof IDocumentProviderExtension2) {
310             IDocumentProviderExtension2 extension= (IDocumentProviderExtension2)fParentProvider;
311             extension.setProgressMonitor(progressMonitor);
312         }
313     }
314
315     /*
316      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension2#getProgressMonitor()
317      */

318     public IProgressMonitor getProgressMonitor() {
319         if (fParentProvider instanceof IDocumentProviderExtension2) {
320             IDocumentProviderExtension2 extension= (IDocumentProviderExtension2)fParentProvider;
321             return extension.getProgressMonitor();
322         }
323         return null;
324     }
325
326     /*
327      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension3#isSynchronized(java.lang.Object)
328      */

329     public boolean isSynchronized(Object JavaDoc element) {
330         if (fParentProvider instanceof IDocumentProviderExtension3) {
331             IDocumentProviderExtension3 extension= (IDocumentProviderExtension3)fParentProvider;
332             return extension.isSynchronized(element);
333         }
334         return true;
335     }
336
337     /*
338      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension5#isNotSynchronizedException(Object, CoreException)
339      * @since 3.2
340      */

341     public boolean isNotSynchronizedException(Object JavaDoc element, CoreException ex) {
342         if (fParentProvider instanceof IDocumentProviderExtension5) {
343             IDocumentProviderExtension5 extension= (IDocumentProviderExtension5)fParentProvider;
344             return extension.isNotSynchronizedException(element, ex);
345         }
346         return false;
347     }
348
349     /*
350      * @see org.eclipse.ui.texteditor.IDocumentProviderExtension4#getContentType(java.lang.Object)
351      * @since 3.1
352      */

353     public IContentType getContentType(Object JavaDoc element) throws CoreException {
354         if (fParentProvider instanceof IDocumentProviderExtension4) {
355             IDocumentProviderExtension4 extension= (IDocumentProviderExtension4)fParentProvider;
356             return extension.getContentType(element);
357         }
358         return null;
359     }
360
361     /*
362      * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#getDefaultEncoding()
363      */

364     public String JavaDoc getDefaultEncoding() {
365         if (fParentProvider instanceof IStorageDocumentProvider) {
366             IStorageDocumentProvider provider= (IStorageDocumentProvider)fParentProvider;
367             return provider.getDefaultEncoding();
368         }
369         return null;
370     }
371
372     /*
373      * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#getEncoding(java.lang.Object)
374      */

375     public String JavaDoc getEncoding(Object JavaDoc element) {
376         if (fParentProvider instanceof IStorageDocumentProvider) {
377             IStorageDocumentProvider provider= (IStorageDocumentProvider)fParentProvider;
378             return provider.getEncoding(element);
379         }
380         return null;
381     }
382
383     /*
384      * @see org.eclipse.ui.editors.text.IStorageDocumentProvider#setEncoding(java.lang.Object, java.lang.String)
385      */

386     public void setEncoding(Object JavaDoc element, String JavaDoc encoding) {
387         if (fParentProvider instanceof IStorageDocumentProvider) {
388             IStorageDocumentProvider provider= (IStorageDocumentProvider)fParentProvider;
389             provider.setEncoding(element, encoding);
390         }
391     }
392 }
393
Popular Tags