KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > eclipse > editors > Configuration


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software
22  * itself, if and wherever such third-party acknowledgements
23  * normally appear.
24  *
25  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names
26  * of the project contributors may be used to endorse or promote
27  * products derived from this software without prior written
28  * permission. For written permission, please contact
29  * visigoths@visigoths.org.
30  *
31  * 5. Products derived from this software may not be called
32  * "FreeMarker" or "Visigoth" nor may "FreeMarker" or "Visigoth"
33  * appear in their names without prior written permission of the
34  * Visigoth Software Society.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Visigoth Software Society. For more
52  * information on the Visigoth Software Society, please see
53  * http://www.visigoths.org/
54  */

55
56 package freemarker.eclipse.editors;
57
58 import org.eclipse.jface.text.DocumentEvent;
59 import org.eclipse.jface.text.IDocument;
60 import org.eclipse.jface.text.IRegion;
61 import org.eclipse.jface.text.ITypedRegion;
62 import org.eclipse.jface.text.presentation.IPresentationReconciler;
63 import org.eclipse.jface.text.presentation.PresentationReconciler;
64 import org.eclipse.jface.text.reconciler.IReconciler;
65 import org.eclipse.jface.text.reconciler.MonoReconciler;
66 import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
67 import org.eclipse.jface.text.rules.IToken;
68 import org.eclipse.jface.text.rules.ITokenScanner;
69 import org.eclipse.jface.text.rules.RuleBasedScanner;
70 import org.eclipse.jface.text.source.IAnnotationHover;
71 import org.eclipse.jface.text.source.ISourceViewer;
72 import org.eclipse.jface.text.source.SourceViewerConfiguration;
73
74 import freemarker.eclipse.FreemarkerPlugin;
75 import freemarker.eclipse.preferences.IPreferenceConstants;
76
77 /**
78  * The FreeMarker editor configuration. This class holds instances of
79  * various objects related to a FreeMarker template editor. The most
80  * important of these objects is the presentation reconciler, that is
81  * the object that handles all the coloring (and color repairs) of
82  * the editor text.
83  *
84  * @version $Id: Configuration.java,v 1.10 2004/02/05 00:16:23 stephanmueller Exp $
85  * @author <a HREF="mailto:stephan@chaquotay.net">Stephan Mueller</a>
86  * @author <a HREF="mailto:per&#64;percederberg.net">Per Cederberg</a>
87  */

88 public class Configuration extends SourceViewerConfiguration
89     implements IPreferenceConstants {
90
91     private DirectiveScanner directiveScanner;
92     private RuleBasedScanner commentScanner;
93     private RuleBasedScanner interpolationScanner;
94     private RuleBasedScanner defaultScanner;
95     private RuleBasedScanner xmlCommentScanner;
96     private XmlScanner xmlTagScanner;
97     private ITokenManager tokenManager;
98     private FreemarkerEditor fEditor;
99
100     /**
101      * Creates a new editor configuration. A new configuration
102      * instance is created for each and every editor, i.e. for each
103      * FreeMarker template opened for editing.
104      *
105      * @param anEditor the editor object
106      * @param colorManager the color manager
107      */

108     public Configuration(FreemarkerEditor anEditor, ITokenManager tokenManager) {
109         this.fEditor = anEditor;
110         this.tokenManager = tokenManager;
111     }
112
113     /*
114      * (non-Javadoc)
115      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredContentTypes(org.eclipse.jface.text.source.ISourceViewer)
116      */

117     public String JavaDoc[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
118         return PartitionScanner.PARTITIONS;
119     }
120
121     /*
122      * (non-Javadoc)
123      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer)
124      */

125     public IAnnotationHover getAnnotationHover(ISourceViewer aSourceViewer) {
126         return new AnnotationHover();
127     }
128
129     /*
130      * (non-Javadoc)
131      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
132      */

133     public IReconciler getReconciler(ISourceViewer aSourceViewer) {
134         return new MonoReconciler(fEditor.getReconcilingStrategy(), false);
135     }
136
137     /**
138      * Returns the normal text scanner. This scanner is used to color
139      * normal text in templates (i.e. everything that is neither XML
140      * nor FreeMarker syntax).
141      *
142      * @return the normal text scanner
143      */

144     protected ITokenScanner getDefaultScanner() {
145         IToken token;
146
147         if (defaultScanner == null) {
148             defaultScanner = new RuleBasedScanner();
149             token = tokenManager.getTextToken();
150             defaultScanner.setDefaultReturnToken(token);
151         }
152         return defaultScanner;
153     }
154
155     /**
156      * Returns the directive scanner. This scanner is used to color
157      * FreeMarker directives and macros.
158      *
159      * @return the directive scanner
160      */

161     protected DirectiveScanner getDirectiveScanner() {
162         IToken token;
163
164         if (directiveScanner == null) {
165             directiveScanner = new DirectiveScanner(tokenManager);
166             token = tokenManager.getDirectiveToken();
167             directiveScanner.setDefaultReturnToken(token);
168         }
169         return directiveScanner;
170     }
171
172     /**
173      * Returns the comment scanner. This scanner is used to color
174      * FreeMarker comments.
175      *
176      * @return the comment scanner
177      */

178     protected ITokenScanner getCommentScanner() {
179         IToken token;
180
181         if (commentScanner == null) {
182             commentScanner = new RuleBasedScanner();
183             token = tokenManager.getCommentToken();
184             commentScanner.setDefaultReturnToken(token);
185         }
186         return commentScanner;
187     }
188
189     /**
190      * Returns the interpolation scanner. This scanner is used to
191      * color FreeMarker interpolations.
192      *
193      * @return the interpolation scanner
194      */

195     protected ITokenScanner getInterpolationScanner() {
196         IToken token;
197
198         if (interpolationScanner == null) {
199             interpolationScanner = new RuleBasedScanner();
200             token = tokenManager.getInterpolationToken();
201             interpolationScanner.setDefaultReturnToken(token);
202         }
203         return interpolationScanner;
204     }
205
206     /**
207      * Returns the XML comment scanner. This scanner is used to color
208      * XML comments.
209      *
210      * @return the XML comment scanner
211      */

212     protected ITokenScanner getXmlCommentScanner() {
213         IToken token;
214
215         if (xmlCommentScanner == null) {
216             xmlCommentScanner = new XmlCommentScanner(tokenManager);
217             token = tokenManager.getXmlCommentToken();
218             xmlCommentScanner.setDefaultReturnToken(token);
219         }
220         return xmlCommentScanner;
221     }
222
223     /**
224      * Returns the XML tag scanner. This scanner is used to color
225      * XML tags.
226      *
227      * @return the XML tag scanner
228      */

229     protected XmlScanner getXmlTagScanner() {
230         IToken token;
231
232         if (xmlTagScanner == null) {
233             xmlTagScanner = new XmlScanner(tokenManager);
234             token = tokenManager.getXmlToken();
235             xmlTagScanner.setDefaultReturnToken(token);
236         }
237         return xmlTagScanner;
238     }
239
240     /*
241      * (non-Javadoc)
242      * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer)
243      */

244     public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
245         PresentationReconciler reconciler = new PresentationReconciler();
246         DefaultDamagerRepairer dr;
247         
248         boolean xmlHighlighting = true;
249         try {
250             xmlHighlighting = FreemarkerPlugin.getInstance().getPreferenceStore().getBoolean(XML_HIGHLIGHTING);
251         } catch (NullPointerException JavaDoc npe) {
252             
253         }
254
255         dr = new DefaultDamagerRepairer(getDefaultScanner());
256         reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
257         reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
258
259         dr = new DefaultDamagerRepairer(getDirectiveScanner());
260         reconciler.setDamager(dr, PartitionScanner.FTL_DIRECTIVE);
261         reconciler.setRepairer(dr, PartitionScanner.FTL_DIRECTIVE);
262
263         dr = new DefaultDamagerRepairer(getCommentScanner());
264         reconciler.setDamager(dr, PartitionScanner.FTL_COMMENT);
265         reconciler.setRepairer(dr, PartitionScanner.FTL_COMMENT);
266
267         dr = new DefaultDamagerRepairer(getInterpolationScanner());
268         reconciler.setDamager(dr, PartitionScanner.FTL_INTERPOLATION);
269         reconciler.setRepairer(dr, PartitionScanner.FTL_INTERPOLATION);
270
271         if(xmlHighlighting) {
272             dr = new DefaultDamagerRepairer(getXmlCommentScanner());
273             reconciler.setDamager(dr, PartitionScanner.XML_COMMENT);
274             reconciler.setRepairer(dr, PartitionScanner.XML_COMMENT);
275         
276             dr = new SimpleDamagerRepairer(getXmlTagScanner());
277             reconciler.setDamager(dr, PartitionScanner.XML_TAG);
278             reconciler.setRepairer(dr, PartitionScanner.XML_TAG);
279         }
280         
281         return reconciler;
282     }
283
284
285     /**
286      * A simple presentation damager and repairer. This only differs
287      * from the default damager and repairer by marking the whole
288      * partition as damaged upon every change. This is not efficient
289      * for large partitions, but allows the usage of stateful
290      * scanners within a partition.
291      *
292      * @version $Id: Configuration.java,v 1.10 2004/02/05 00:16:23 stephanmueller Exp $
293      * @author <a HREF="mailto:per&#64;percederberg.net">Per Cederberg</a>
294      */

295     private class SimpleDamagerRepairer extends DefaultDamagerRepairer {
296
297         /**
298          * Creates a new simple damager and repairer.
299          *
300          * @param scanner the lexical scanner to use
301          */

302         public SimpleDamagerRepairer(ITokenScanner scanner) {
303             super(scanner);
304         }
305         
306         /**
307          * Returns the region damaged in the document presentation.
308          * The damage region returned by this method is always the
309          * whole partition.
310          *
311          * @param partition the document partition
312          * @param e the document change event
313          * @param documentPartitioningChanged a flag set if the
314          * partitioning was changed
315          */

316         public IRegion getDamageRegion(ITypedRegion partition,
317                                        DocumentEvent e,
318                                        boolean documentPartitioningChanged) {
319
320             return partition;
321         }
322     }
323 }
Popular Tags