Added "Task Tags" functionality (TODO,...)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / text / JavaTextTools.java
1 package net.sourceforge.phpdt.ui.text;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
8 import net.sourceforge.phpdt.internal.ui.text.FastJavaPartitionScanner;
9 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
10 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
11 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCodeScanner;
12 import net.sourceforge.phpeclipse.IPreferenceConstants;
13 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCodeScanner;
14 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
16 import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner;
17 import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner;
18
19 import org.eclipse.core.runtime.Preferences;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentExtension3;
23 import org.eclipse.jface.text.IDocumentPartitioner;
24 import org.eclipse.jface.text.rules.DefaultPartitioner;
25 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
26 import org.eclipse.jface.text.rules.RuleBasedScanner;
27 import org.eclipse.jface.util.IPropertyChangeListener;
28 import org.eclipse.jface.util.PropertyChangeEvent;
29 //
30 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
31 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
32 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
33 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
34 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
35 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
36
37 /**
38  * Tools required to configure a Java text viewer. 
39  * The color manager and all scanner exist only one time, i.e.
40  * the same instances are returned to all clients. Thus, clients
41  * share those tools.
42  * <p>
43  * This class may be instantiated; it is not intended to be subclassed.
44  * </p>
45  */
46 public class JavaTextTools {
47
48   private static PHPPartitionScanner HTML_PARTITION_SCANNER = null;
49
50   private static PHPPartitionScanner PHP_PARTITION_SCANNER = null;
51   private static PHPPartitionScanner SMARTY_PARTITION_SCANNER = null;
52
53   // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC, PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
54   private final static String[] TYPES =
55     new String[] {
56       IPHPPartitions.PHP_PARTITIONING,
57       IPHPPartitions.PHP_PHPDOC_COMMENT,
58       IPHPPartitions.HTML,
59       IPHPPartitions.HTML_MULTILINE_COMMENT,
60       IPHPPartitions.JAVASCRIPT,
61       IPHPPartitions.CSS,
62       IPHPPartitions.SMARTY,
63       IPHPPartitions.SMARTY_MULTILINE_COMMENT };
64   private static PHPPartitionScanner XML_PARTITION_SCANNER = null;
65
66   /**
67    * This tools' preference listener. 
68    */
69   private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
70     public void propertyChange(PropertyChangeEvent event) {
71       adaptToPreferenceChange(event);
72     }
73     public void propertyChange(Preferences.PropertyChangeEvent event) {
74       adaptToPreferenceChange(
75         new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue()));
76     }
77   };
78
79   /** The color manager */
80   private JavaColorManager fColorManager;
81   /** The PHP source code scanner */
82   private PHPCodeScanner fCodeScanner;
83   /** The PHP multiline comment scanner */
84   private SingleTokenPHPScanner fMultilineCommentScanner;
85   /** The Java singleline comment scanner */
86   private SingleTokenPHPScanner fSinglelineCommentScanner;
87   /** The Java string scanner */
88   private SingleTokenPHPScanner fStringScanner;
89   /** The PHPDoc scanner */
90   private PHPDocCodeScanner fPHPDocScanner;
91   /** The HTML scanner */
92   private HTMLCodeScanner fHTMLScanner;
93   /** The Smarty scanner */
94   private SmartyCodeScanner fSmartyScanner;
95   /** The SmartyDoc scanner */
96   private SmartyDocCodeScanner fSmartyDocScanner;
97   /** The Java partitions scanner */
98   private PHPPartitionScanner fPartitionScanner;
99
100   /** The preference store */
101   private IPreferenceStore fPreferenceStore;
102   /**
103    * The core preference store.
104    * @since 2.1
105    */
106   private Preferences fCorePreferenceStore;
107   /** The preference change listener */
108   private PreferenceListener fPreferenceListener = new PreferenceListener();
109
110   /**
111    * Creates a new Java text tools collection.
112    * 
113    * @param store the preference store to initialize the text tools. The text tool
114    * instance installs a listener on the passed preference store to adapt itself to 
115    * changes in the preference store. In general <code>PreferenceConstants.
116    * getPreferenceStore()</code> shoould be used to initialize the text tools.
117    * 
118    * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
119    * @since 2.0
120    */
121   public JavaTextTools(IPreferenceStore store) {
122     fPreferenceStore = store;
123     fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
124
125     fColorManager = new JavaColorManager();
126     fCodeScanner = new PHPCodeScanner(fColorManager, store);
127     fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
128     fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
129     fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
130     fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
131     fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
132     fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
133     fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
134 //    fPartitionScanner = new FastJavaPartitionScanner();
135     fPartitionScanner = new PHPPartitionScanner();
136   }
137
138   /**
139    * Creates a new Java text tools collection.
140    * 
141    * @param store the preference store to initialize the text tools. The text tool
142    *                    instance installs a listener on the passed preference store to adapt itself to 
143    *                    changes in the preference store. In general <code>PreferenceConstants.
144    *                    getPreferenceStore()</code> shoould be used to initialize the text tools.
145    * @param coreStore optional preference store to initialize the text tools. The text tool
146    *                    instance installs a listener on the passed preference store to adapt itself to 
147    *                    changes in the preference store.
148    * @param autoDisposeOnDisplayDispose         if <code>true</code>  the color manager
149    *                    automatically disposes all managed colors when the current display gets disposed
150    *                    and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
151    * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
152    * @since 2.1
153    */
154   public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
155     fPreferenceStore = store;
156     fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
157
158     fCorePreferenceStore = coreStore;
159     if (fCorePreferenceStore != null)
160       fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
161
162     fColorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
163     fCodeScanner = new PHPCodeScanner(fColorManager, store);
164     fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
165     fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
166     fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
167     fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
168     fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
169     fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
170     fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
171   //  fPartitionScanner = new FastJavaPartitionScanner();
172     fPartitionScanner = new PHPPartitionScanner();
173   }
174
175   /**
176    * Disposes all the individual tools of this tools collection.
177    */
178   public void dispose() {
179
180     fCodeScanner = null;
181     fMultilineCommentScanner = null;
182     fSinglelineCommentScanner = null;
183     fStringScanner = null;
184     fPHPDocScanner = null;
185     fPartitionScanner = null;
186
187     if (fColorManager != null) {
188       fColorManager.dispose();
189       fColorManager = null;
190     }
191
192     if (fPreferenceStore != null) {
193       fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
194       fPreferenceStore = null;
195
196       if (fCorePreferenceStore != null) {
197         fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
198         fCorePreferenceStore = null;
199       }
200
201       fPreferenceListener = null;
202     }
203   }
204
205   /**
206    * Returns the color manager which is used to manage
207    * any Java-specific colors needed for such things like syntax highlighting.
208    *
209    * @return the color manager to be used for Java text viewers
210    */
211   public IColorManager getColorManager() {
212     return fColorManager;
213   }
214
215   /**
216    * Returns a scanner which is configured to scan Java source code.
217    *
218    * @return a Java source code scanner
219    */
220   public RuleBasedScanner getCodeScanner() {
221     return fCodeScanner;
222   }
223
224   /**
225    * Returns a scanner which is configured to scan Java multiline comments.
226    *
227    * @return a Java multiline comment scanner
228    * 
229    * @since 2.0
230    */
231   public RuleBasedScanner getMultilineCommentScanner() {
232     return fMultilineCommentScanner;
233   }
234
235   /**
236    * Returns a scanner which is configured to scan HTML code.
237    *
238    * @return a HTML scanner
239    * 
240    * @since 2.0
241    */
242   public RuleBasedScanner getHTMLScanner() {
243     return fHTMLScanner;
244   }
245
246   /**
247    * Returns a scanner which is configured to scan Smarty code.
248    *
249    * @return a Smarty scanner
250    * 
251    * @since 2.0
252    */
253   public RuleBasedScanner getSmartyScanner() {
254     return fSmartyScanner;
255   }
256
257   /**
258          * Returns a scanner which is configured to scan Smarty code.
259          *
260          * @return a Smarty scanner
261          * 
262          * @since 2.0
263          */
264   public RuleBasedScanner getSmartyDocScanner() {
265     return fSmartyDocScanner;
266   }
267   /**
268    * Returns a scanner which is configured to scan Java singleline comments.
269    *
270    * @return a Java singleline comment scanner
271    * 
272    * @since 2.0
273    */
274   public RuleBasedScanner getSinglelineCommentScanner() {
275     return fSinglelineCommentScanner;
276   }
277
278   /**
279    * Returns a scanner which is configured to scan Java strings.
280    *
281    * @return a Java string scanner
282    * 
283    * @since 2.0
284    */
285   public RuleBasedScanner getStringScanner() {
286     return fStringScanner;
287   }
288
289   /**
290    * Returns a scanner which is configured to scan JavaDoc compliant comments.
291    * Notes that the start sequence "/**" and the corresponding end sequence
292    * are part of the JavaDoc comment.
293    *
294    * @return a JavaDoc scanner
295    */
296   public RuleBasedScanner getJavaDocScanner() {
297     return fPHPDocScanner;
298   }
299
300   /**
301    * Returns a scanner which is configured to scan 
302    * Java-specific partitions, which are multi-line comments,
303    * JavaDoc comments, and regular Java source code.
304    *
305    * @return a Java partition scanner
306    */
307   public IPartitionTokenScanner getPartitionScanner() {
308     return fPartitionScanner;
309   }
310
311   /**
312    * Factory method for creating a PHP-specific document partitioner
313    * using this object's partitions scanner. This method is a 
314    * convenience method.
315    *
316    * @return a newly created Java document partitioner
317    */
318   public IDocumentPartitioner createDocumentPartitioner() {
319     return createDocumentPartitioner(".php");
320   }
321
322   /**
323    * Factory method for creating a PHP-specific document partitioner
324    * using this object's partitions scanner. This method is a 
325    * convenience method.
326    *
327    * @return a newly created Java document partitioner
328    */
329   public IDocumentPartitioner createDocumentPartitioner(String extension) {
330
331     //    String[] types =
332     //      new String[] {
333     //        FastJavaPartitionScanner.JAVA_DOC,
334     //        FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
335     //        FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
336     //        FastJavaPartitionScanner.JAVA_STRING };
337     //
338     //    return new DefaultPartitioner(getPartitionScanner(), types);
339     IDocumentPartitioner partitioner = null;
340     //  System.out.println(extension);
341     if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
342       // html
343       partitioner = createHTMLPartitioner();
344     } else if (extension.equalsIgnoreCase(".xml")) {
345       // xml
346       partitioner = createXMLPartitioner();
347     } else if (extension.equalsIgnoreCase(".js")) {
348       // javascript
349       partitioner = createJavaScriptPartitioner();
350     } else if (extension.equalsIgnoreCase(".css")) {
351       // cascading style sheets
352       partitioner = createCSSPartitioner();
353     } else if (extension.equalsIgnoreCase(".tpl")) {
354       // smarty ?
355       partitioner = createSmartyPartitioner();
356     } else if (extension.equalsIgnoreCase(".inc")) {
357       // php include files ?
358       partitioner = createIncludePartitioner();
359     }
360
361     if (partitioner == null) {
362       partitioner = createPHPPartitioner();
363     }
364
365     return partitioner;
366   }
367
368         /**
369          * Sets up the Java document partitioner for the given document for the given partitioning.
370          * 
371          * @param document the document to be set up
372          * @param partitioning the document partitioning
373          * @since 3.0
374          */
375         public void setupJavaDocumentPartitioner(IDocument document, String partitioning) {
376                 IDocumentPartitioner partitioner= createDocumentPartitioner();
377                 if (document instanceof IDocumentExtension3) {
378                         IDocumentExtension3 extension3= (IDocumentExtension3) document;
379                         extension3.setDocumentPartitioner(partitioning, partitioner);
380                 } else {
381                         document.setDocumentPartitioner(partitioner);
382                 }
383                 partitioner.connect(document);
384         }
385         
386   /**
387    * Returns the names of the document position categories used by the document
388    * partitioners created by this object to manage their partition information.
389    * If the partitioners don't use document position categories, the returned
390    * result is <code>null</code>.
391    *
392    * @return the partition managing position categories or <code>null</code> 
393    *                    if there is none
394    */
395   public String[] getPartitionManagingPositionCategories() {
396     return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
397   }
398
399   /**
400    * Determines whether the preference change encoded by the given event
401    * changes the behavior of one its contained components.
402    * 
403    * @param event the event to be investigated
404    * @return <code>true</code> if event causes a behavioral change
405    * 
406    * @since 2.0
407    */
408   public boolean affectsBehavior(PropertyChangeEvent event) {
409     return fCodeScanner.affectsBehavior(event)
410       || fMultilineCommentScanner.affectsBehavior(event)
411       || fSinglelineCommentScanner.affectsBehavior(event)
412       || fStringScanner.affectsBehavior(event)
413       || fPHPDocScanner.affectsBehavior(event);
414   }
415
416   /**
417    * Adapts the behavior of the contained components to the change
418    * encoded in the given event.
419    * 
420    * @param event the event to which to adapt
421    * @since 2.0
422    */
423   protected void adaptToPreferenceChange(PropertyChangeEvent event) {
424     if (fCodeScanner.affectsBehavior(event))
425       fCodeScanner.adaptToPreferenceChange(event);
426     if (fMultilineCommentScanner.affectsBehavior(event))
427       fMultilineCommentScanner.adaptToPreferenceChange(event);
428     if (fSinglelineCommentScanner.affectsBehavior(event))
429       fSinglelineCommentScanner.adaptToPreferenceChange(event);
430     if (fStringScanner.affectsBehavior(event))
431       fStringScanner.adaptToPreferenceChange(event);
432     if (fPHPDocScanner.affectsBehavior(event))
433       fPHPDocScanner.adaptToPreferenceChange(event);
434   }
435
436   /**
437          * Return a partitioner for .html files.
438          */
439   private static IDocumentPartitioner createHTMLPartitioner() {
440     return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
441   }
442
443   private static IDocumentPartitioner createIncludePartitioner() {
444     return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
445   }
446
447   private static IDocumentPartitioner createJavaScriptPartitioner() {
448     return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
449   }
450
451   /**
452         * Return a partitioner for .php files.
453         */
454   private static IDocumentPartitioner createPHPPartitioner() {
455     return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
456   }
457
458   private static IDocumentPartitioner createSmartyPartitioner() {
459     return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
460   }
461
462   private static IDocumentPartitioner createXMLPartitioner() {
463     return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
464   }
465
466   private static IDocumentPartitioner createCSSPartitioner() {
467     return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
468   }
469   /**
470         * Return a scanner for creating html partitions.
471         */
472   private static PHPPartitionScanner getHTMLPartitionScanner() {
473     if (HTML_PARTITION_SCANNER == null)
474       HTML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.HTML_FILE);
475     return HTML_PARTITION_SCANNER;
476   }
477   /**
478         * Return a scanner for creating php partitions.
479         */
480   private static PHPPartitionScanner getPHPPartitionScanner() {
481     if (PHP_PARTITION_SCANNER == null)
482       PHP_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
483     return PHP_PARTITION_SCANNER;
484   }
485
486   /**
487         * Return a scanner for creating smarty partitions.
488         */
489   private static PHPPartitionScanner getSmartyPartitionScanner() {
490     if (SMARTY_PARTITION_SCANNER == null)
491       SMARTY_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.SMARTY_FILE);
492     return SMARTY_PARTITION_SCANNER;
493   }
494
495   /**
496         * Return a scanner for creating xml partitions.
497         */
498   private static PHPPartitionScanner getXMLPartitionScanner() {
499     if (XML_PARTITION_SCANNER == null)
500       XML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.XML_FILE);
501     return XML_PARTITION_SCANNER;
502   }
503
504 }