KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > editor > text > AntEditorDocumentProvider


1 /*******************************************************************************
2  * Copyright (c) 2002, 2006 GEBIT Gesellschaft fuer EDV-Beratung
3  * und Informatik-Technologien mbH,
4  * Berlin, Duesseldorf, Frankfurt (Germany) and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * GEBIT Gesellschaft fuer EDV-Beratung und Informatik-Technologien mbH - initial API and implementation
12  * IBM Corporation - bug fixes
13  *******************************************************************************/

14
15 package org.eclipse.ant.internal.ui.editor.text;
16
17
18 import org.eclipse.ant.internal.ui.model.AntModel;
19 import org.eclipse.ant.internal.ui.model.IProblemRequestor;
20 import org.eclipse.ant.internal.ui.model.LocationProvider;
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.runtime.CoreException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IDocumentExtension3;
25 import org.eclipse.jface.text.ISynchronizable;
26 import org.eclipse.jface.text.source.IAnnotationModel;
27 import org.eclipse.ui.IEditorInput;
28 import org.eclipse.ui.editors.text.TextFileDocumentProvider;
29 import org.eclipse.ui.texteditor.IDocumentProvider;
30
31 public class AntEditorDocumentProvider extends TextFileDocumentProvider {
32
33     private final AntDocumentSetupParticipant fAntDocumentSetupParticipant;
34
35     /**
36      * Remembers a Ant document model for each element.
37      */

38     protected class AntFileInfo extends FileInfo {
39         
40         public AntModel fAntModel;
41         
42         public AntFileInfo() {
43         }
44     }
45     
46     public AntEditorDocumentProvider() {
47         IDocumentProvider provider= new TextFileDocumentProvider(new AntStorageDocumentProvider());
48         setParentDocumentProvider(provider);
49         fAntDocumentSetupParticipant = new AntDocumentSetupParticipant();
50     }
51
52     public AntModel getAntModel(Object JavaDoc element) {
53         FileInfo info= getFileInfo(element);
54         if (info instanceof AntFileInfo) {
55             AntFileInfo xmlInfo= (AntFileInfo) info;
56             return xmlInfo.fAntModel;
57         }
58         return null;
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createAnnotationModel(org.eclipse.core.resources.IFile)
63      */

64     protected IAnnotationModel createAnnotationModel(IFile file) {
65            return new AntAnnotationModel(file);
66     }
67
68     protected AntModel createAntModel(Object JavaDoc element, IDocument document, IAnnotationModel annotationModel) {
69         IProblemRequestor requestor= annotationModel instanceof IProblemRequestor ? (IProblemRequestor) annotationModel : null;
70         return new AntModel(document, requestor, new LocationProvider(element instanceof IEditorInput ? (IEditorInput) element : null));
71     }
72     
73     /*
74      * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createFileInfo(java.lang.Object)
75      */

76     protected FileInfo createFileInfo(Object JavaDoc element) throws CoreException {
77         FileInfo info= super.createFileInfo(element);
78         if (!(info instanceof AntFileInfo)) {
79             return null;
80         }
81     
82         //This is a required workaround for the disconnect between workbench file associations
83
//and content types based document setup and creation
84
//This ensures that a workbench file association for the AntEditor will have a document
85
//that is setup with the correct document setup participant since it was "missed" by the
86
//document setup extensions (bug 72598).
87
IDocument document= info.fTextFileBuffer.getDocument();
88         if (document instanceof IDocumentExtension3) {
89             IDocumentExtension3 extension= (IDocumentExtension3) document;
90             if (extension.getDocumentPartitioner(AntDocumentSetupParticipant.ANT_PARTITIONING) == null)
91                 fAntDocumentSetupParticipant.setup(document);
92         }
93         
94         //Check if the annotation model has been set by the annotation model factory extension for Ant UI
95
//and is an annotation model that was specified by the extension (all are IProblemRequestors).
96
//If we do not have an annotation model or not a correct annotation model, defer to the annotation model
97
//created from this document provider. The document provider is only queried for an annotation model for workspace files.
98
//Therefore if the annotation model is still null we are dealing with an external file that is associated with
99
//the Ant editor from a user preference setting.
100
//In all cases the determined annotation model is set for the file info to be used in the editor.
101
AntFileInfo xmlInfo= (AntFileInfo) info;
102         IAnnotationModel annotationModel= xmlInfo.fTextFileBuffer.getAnnotationModel();
103         if (annotationModel instanceof IProblemRequestor) {
104             xmlInfo.fModel= annotationModel;
105         } else {
106             annotationModel= xmlInfo.fModel;
107         }
108         
109         if (annotationModel == null) {
110             annotationModel= new AntExternalAnnotationModel();
111             xmlInfo.fModel= annotationModel;
112         }
113          
114         AntModel antModel= createAntModel(element, document, annotationModel);
115         antModel.install();
116         xmlInfo.fAntModel= antModel;
117         setUpSynchronization(xmlInfo);
118         
119         return xmlInfo;
120     }
121     
122     /* (non-Javadoc)
123      * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#disposeFileInfo(java.lang.Object, org.eclipse.ui.editors.text.TextFileDocumentProvider.FileInfo)
124      */

125     protected void disposeFileInfo(Object JavaDoc element, FileInfo info) {
126         if (info instanceof AntFileInfo) {
127             AntFileInfo xmlInfo= (AntFileInfo) info;
128             if (xmlInfo.fAntModel != null) {
129                 IDocument doc= xmlInfo.fTextFileBuffer.getDocument();
130                 Object JavaDoc lock= null;
131                 if (doc instanceof ISynchronizable) {
132                     lock= ((ISynchronizable) doc).getLockObject();
133                 } else {
134                     lock= xmlInfo.fAntModel;
135                 }
136                 if (lock == null) {
137                     xmlInfo.fAntModel.dispose();
138                     xmlInfo.fAntModel= null;
139                 } else {
140                     synchronized (lock) {
141                         xmlInfo.fAntModel.dispose();
142                         xmlInfo.fAntModel= null;
143                     }
144                 }
145             }
146         }
147         super.disposeFileInfo(element, info);
148     }
149     
150     /*
151      * @see org.eclipse.ui.editors.text.TextFileDocumentProvider#createEmptyFileInfo()
152      */

153     protected FileInfo createEmptyFileInfo() {
154         return new AntFileInfo();
155     }
156 }
Popular Tags