1 package net.sourceforge.phpdt.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
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.HTMLPartitionScanner;
14 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
17 import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner;
18 import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner;
19 import net.sourceforge.phpeclipse.xml.ui.XMLPlugin;
20 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLPartitionScanner;
21 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
23 import org.eclipse.core.runtime.Preferences;
24 import org.eclipse.jface.preference.IPreferenceStore;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.IDocumentExtension3;
27 import org.eclipse.jface.text.IDocumentPartitioner;
28 import org.eclipse.jface.text.rules.DefaultPartitioner;
29 import org.eclipse.jface.text.rules.RuleBasedScanner;
30 import org.eclipse.jface.util.IPropertyChangeListener;
31 import org.eclipse.jface.util.PropertyChangeEvent;
34 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
35 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
36 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
37 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
38 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
39 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
42 * Tools required to configure a Java text viewer. The color manager and all scanner exist only one time, i.e. the same instances
43 * are returned to all clients. Thus, clients share those tools.
45 * This class may be instantiated; it is not intended to be subclassed.
48 public class JavaTextTools implements IPHPPartitions {
49 // private static final String[] TOKENS = {
50 // JSPScriptScanner.JSP_DEFAULT,
51 // JSPScriptScanner.JSP_BRACKET };
52 private final static String[] LEGAL_CONTENT_TYPES = new String[] {
54 PHP_MULTILINE_COMMENT,
55 PHP_SINGLELINE_COMMENT,
60 // private static XMLPartitionScanner HTML_PARTITION_SCANNER = null;
62 // private static FastJavaPartitionScanner PHP_PARTITION_SCANNER = null;
64 private static HTMLPartitionScanner SMARTY_PARTITION_SCANNER = null;
66 // private static XMLPartitionScanner XML_PARTITION_SCANNER = null;
68 // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC,
69 // PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
70 // private final static String[] TYPES = new String[] {
71 // IPHPPartitions.PHP_PARTITIONING,
72 // IPHPPartitions.PHP_PHPDOC_COMMENT,
73 // // IPHPPartitions.HTML,
74 // // IPHPPartitions.HTML_MULTILINE_COMMENT,
75 // IPHPPartitions.JAVASCRIPT,
76 // IPHPPartitions.CSS,
77 // IPHPPartitions.SMARTY,
78 // IPHPPartitions.SMARTY_MULTILINE_COMMENT };
81 * This tools' preference listener.
83 private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
84 public void propertyChange(PropertyChangeEvent event) {
85 adaptToPreferenceChange(event);
88 public void propertyChange(Preferences.PropertyChangeEvent event) {
89 adaptToPreferenceChange(new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event
94 // /** The color manager */
95 private JavaColorManager colorManager;
97 /** The PHP source code scanner */
98 private PHPCodeScanner fCodeScanner;
100 /** The PHP multiline comment scanner */
101 private SingleTokenPHPScanner fMultilineCommentScanner;
103 /** The Java singleline comment scanner */
104 private SingleTokenPHPScanner fSinglelineCommentScanner;
106 /** The PHP double quoted string scanner */
107 // private SingleTokenPHPScanner fStringDQScanner;
109 /** The PHP single quoted string scanner */
110 // private SingleTokenPHPScanner fStringSQScanner;
112 /** The PHPDoc scanner */
113 private PHPDocCodeScanner fPHPDocScanner;
115 /** The HTML scanner */
116 // private HTMLCodeScanner fHTMLScanner;
117 /** The Smarty scanner */
118 private SmartyCodeScanner fSmartyScanner;
120 /** The SmartyDoc scanner */
121 private SmartyDocCodeScanner fSmartyDocScanner;
123 /** The Java partitions scanner. */
124 private FastJavaPartitionScanner fPartitionScanner;
126 /** The preference store */
127 private IPreferenceStore fPreferenceStore;
129 /** The XML Language text tools */
130 private XMLTextTools xmlTextTools;
133 * The core preference store.
137 private Preferences fCorePreferenceStore;
139 /** The preference change listener */
140 private PreferenceListener fPreferenceListener = new PreferenceListener();
142 /** The JSP partitions scanner */
143 private PHPPartitionScanner jspPartitionScanner = null;
145 /** The JSP script subpartitions scanner */
146 // private JSPScriptScanner jspScriptScanner;
147 /** The PHP plain text scanner */
148 // private RuleBasedScanner jspTextScanner;
149 /** The PHP brackets scanner */
150 // private RuleBasedScanner jspBracketScanner;
152 * Creates a new Java text tools collection.
155 * the preference store to initialize the text tools. The text tool instance installs a listener on the passed preference
156 * store to adapt itself to changes in the preference store. In general <code>PreferenceConstants.
157 * getPreferenceStore()</code>
158 * should be used to initialize the text tools.
160 * optional preference store to initialize the text tools. The text tool instance installs a listener on the passed
161 * preference store to adapt itself to changes in the preference store.
162 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
165 public JavaTextTools(IPreferenceStore store, Preferences coreStore) {
166 this(store, coreStore, true);
170 * Creates a new Java text tools collection.
173 * the preference store to initialize the text tools. The text tool instance installs a listener on the passed preference
174 * store to adapt itself to changes in the preference store. In general <code>PreferenceConstants.
175 * getPreferenceStore()</code>
176 * shoould be used to initialize the text tools.
178 * optional preference store to initialize the text tools. The text tool instance installs a listener on the passed
179 * preference store to adapt itself to changes in the preference store.
180 * @param autoDisposeOnDisplayDispose
181 * if <code>true</code> the color manager automatically disposes all managed colors when the current display gets
182 * disposed and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()}are ignored.
183 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
186 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
187 // super(store, TOKENS, );
188 // REVISIT: preference store
189 xmlTextTools = new XMLTextTools(XMLPlugin.getDefault().getPreferenceStore());
191 colorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
192 fPreferenceStore = store;
193 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
195 fCorePreferenceStore = coreStore;
196 if (fCorePreferenceStore != null)
197 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
199 fCodeScanner = new PHPCodeScanner((JavaColorManager) colorManager, store);
200 fMultilineCommentScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store,
201 IPreferenceConstants.PHP_MULTILINE_COMMENT);
202 fSinglelineCommentScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store,
203 IPreferenceConstants.PHP_SINGLELINE_COMMENT);
204 // fStringDQScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store, IPreferenceConstants.PHP_STRING);
205 // fStringSQScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store, IPreferenceConstants.PHP_STRING);
207 fPHPDocScanner = new PHPDocCodeScanner((JavaColorManager) colorManager, store);
208 // fHTMLScanner = new HTMLCodeScanner((JavaColorManager)fColorManager, store);
209 fSmartyScanner = new SmartyCodeScanner((JavaColorManager) colorManager, store);
210 fSmartyDocScanner = new SmartyDocCodeScanner((JavaColorManager) colorManager, store);
212 fPartitionScanner = new FastJavaPartitionScanner();
214 // jspScriptScanner = new JSPScriptScanner();
215 // fPartitionScanner = new FastJavaPartitionScanner();
216 // fPartitionScanner = new PHPPartitionScanner();
218 // jspBracketScanner = new RuleBasedScanner();
219 // jspBracketScanner.setDefaultReturnToken(new Token(JSPScriptScanner.JSP_BRACKET));
220 // jspTextScanner = new RuleBasedScanner();
221 // jspTextScanner.setDefaultReturnToken(new Token(JSPScriptScanner.JSP_DEFAULT));
227 public XMLTextTools getXMLTextTools() {
232 * Disposes all the individual tools of this tools collection.
234 public void dispose() {
237 fMultilineCommentScanner = null;
238 fSinglelineCommentScanner = null;
239 // fStringDQScanner = null;
240 // fStringSQScanner = null;
241 fPHPDocScanner = null;
242 // fPartitionScanner = null;
244 if (colorManager != null) {
245 colorManager.dispose();
249 if (fPreferenceStore != null) {
250 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
251 fPreferenceStore = null;
253 if (fCorePreferenceStore != null) {
254 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
255 fCorePreferenceStore = null;
258 fPreferenceListener = null;
263 * Returns the color manager which is used to manage any Java-specific colors needed for such things like syntax highlighting.
265 * @return the color manager to be used for Java text viewers
267 public JavaColorManager getColorManager() {
268 return (JavaColorManager) colorManager;
272 * Returns a scanner which is configured to scan Java source code.
274 * @return a Java source code scanner
276 public RuleBasedScanner getCodeScanner() {
281 * Returns a scanner which is configured to scan Java multiline comments.
283 * @return a Java multiline comment scanner
287 public RuleBasedScanner getMultilineCommentScanner() {
288 return fMultilineCommentScanner;
292 * Returns a scanner which is configured to scan HTML code.
294 * @return a HTML scanner
298 // public RuleBasedScanner getHTMLScanner() {
299 // return fHTMLScanner;
302 * Returns a scanner which is configured to scan Smarty code.
304 * @return a Smarty scanner
308 public RuleBasedScanner getSmartyScanner() {
309 return fSmartyScanner;
313 * Returns a scanner which is configured to scan Smarty code.
315 * @return a Smarty scanner
319 public RuleBasedScanner getSmartyDocScanner() {
320 return fSmartyDocScanner;
324 * Returns a scanner which is configured to scan Java singleline comments.
326 * @return a Java singleline comment scanner
330 public RuleBasedScanner getSinglelineCommentScanner() {
331 return fSinglelineCommentScanner;
335 * Returns a scanner which is configured to scan Java strings.
337 * @return a Java string scanner
341 // public RuleBasedScanner getStringScanner() {
342 // return fStringDQScanner;
346 * Returns a scanner which is configured to scan JavaDoc compliant comments. Notes that the start sequence "/**" and the
347 * corresponding end sequence are part of the JavaDoc comment.
349 * @return a JavaDoc scanner
351 public RuleBasedScanner getJavaDocScanner() {
352 return fPHPDocScanner;
356 * Returns a scanner which is configured to scan Java-specific partitions, which are multi-line comments, JavaDoc comments, and
357 * regular Java source code.
359 * @return a Java partition scanner
361 // public IPartitionTokenScanner getPartitionScanner() {
362 // return fPartitionScanner;
365 * Factory method for creating a PHP-specific document partitioner using this object's partitions scanner. This method is a
366 * convenience method.
368 * @return a newly created Java document partitioner
370 public IDocumentPartitioner createDocumentPartitioner() {
371 return createDocumentPartitioner(".php");
375 * Factory method for creating a PHP-specific document partitioner using this object's partitions scanner. This method is a
376 * convenience method.
378 * @return a newly created Java document partitioner
380 public IDocumentPartitioner createDocumentPartitioner(String extension) {
384 // FastJavaPartitionScanner.JAVA_DOC,
385 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
386 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
387 // FastJavaPartitionScanner.JAVA_STRING };
389 // return new DefaultPartitioner(getPartitionScanner(), types);
390 IDocumentPartitioner partitioner = null;
391 // System.out.println(extension);
392 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
394 partitioner = createHTMLPartitioner();
395 partitioner = createJSPPartitioner();
396 } else if (extension.equalsIgnoreCase(".xml")) {
398 partitioner = createXMLPartitioner();
399 // } else if (extension.equalsIgnoreCase(".js")) {
401 // partitioner = createJavaScriptPartitioner();
402 // } else if (extension.equalsIgnoreCase(".css")) {
403 // // cascading style sheets
404 // partitioner = createCSSPartitioner();
405 } else if (extension.equalsIgnoreCase(".tpl")) {
407 partitioner = createSmartyPartitioner();
408 // } else if (extension.equalsIgnoreCase(".inc")) {
409 // // php include files ?
410 // partitioner = createIncludePartitioner();
413 if (partitioner == null) {
414 partitioner = createJSPPartitioner();
421 * Sets up the Java document partitioner for the given document for the given partitioning.
424 * the document to be set up
425 * @param partitioning
426 * the document partitioning
432 // public void setupJavaDocumentPartitioner(IDocument document, String partitioning, Object element) {
433 // IDocumentPartitioner partitioner = createDocumentPartitioner(".php");
435 // // if (document instanceof IDocumentExtension3) {
436 // // IDocumentExtension3 extension3= (IDocumentExtension3) document;
437 // // extension3.setDocumentPartitioner(partitioning, partitioner);
439 // document.setDocumentPartitioner(partitioner);
441 // partitioner.connect(document);
443 public void setupHTMLDocumentPartitioner(IDocument document, String partitioning, Object element) {
444 IDocumentPartitioner partitioner = createDocumentPartitioner(".html");
446 // if (document instanceof IDocumentExtension3) {
447 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
448 // extension3.setDocumentPartitioner(partitioning, partitioner);
450 document.setDocumentPartitioner(partitioner);
452 partitioner.connect(document);
455 public void setupSmartyDocumentPartitioner(IDocument document, String partitioning, Object element) {
456 IDocumentPartitioner partitioner = createDocumentPartitioner(".tpl");
458 // if (document instanceof IDocumentExtension3) {
459 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
460 // extension3.setDocumentPartitioner(partitioning, partitioner);
462 document.setDocumentPartitioner(partitioner);
464 partitioner.connect(document);
468 * Returns the names of the document position categories used by the document partitioners created by this object to manage their
469 * partition information. If the partitioners don't use document position categories, the returned result is <code>null</code>.
471 * @return the partition managing position categories or <code>null</code> if there is none
473 public String[] getPartitionManagingPositionCategories() {
474 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
478 * Determines whether the preference change encoded by the given event changes the behavior of one its contained components.
481 * the event to be investigated
482 * @return <code>true</code> if event causes a behavioral change
484 * @deprecated As of 3.0, replaced by
485 * {@link net.sourceforge.phpdt.ui.text.JavaSourceViewerConfiguration#affectsTextPresentation(PropertyChangeEvent)}
487 // public boolean affectsBehavior(PropertyChangeEvent event) {
488 // return fCodeScanner.affectsBehavior(event)
489 // || fMultilineCommentScanner.affectsBehavior(event)
490 // || fSinglelineCommentScanner.affectsBehavior(event)
491 // || fStringDQScanner.affectsBehavior(event)
492 // || fPHPDocScanner.affectsBehavior(event);
495 * Adapts the behavior of the contained components to the change encoded in the given event.
498 * the event to which to adapt
501 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
502 if (fCodeScanner.affectsBehavior(event))
503 fCodeScanner.adaptToPreferenceChange(event);
504 if (fMultilineCommentScanner.affectsBehavior(event))
505 fMultilineCommentScanner.adaptToPreferenceChange(event);
506 if (fSinglelineCommentScanner.affectsBehavior(event))
507 fSinglelineCommentScanner.adaptToPreferenceChange(event);
508 // if (fStringDQScanner.affectsBehavior(event))
509 // fStringDQScanner.adaptToPreferenceChange(event);
510 if (fPHPDocScanner.affectsBehavior(event))
511 fPHPDocScanner.adaptToPreferenceChange(event);
512 // if (fHTMLScanner.affectsBehavior(event))
513 // fHTMLScanner.adaptToPreferenceChange(event);
514 if (fSmartyScanner.affectsBehavior(event))
515 fSmartyScanner.adaptToPreferenceChange(event);
516 if (fSmartyDocScanner.affectsBehavior(event))
517 fSmartyDocScanner.adaptToPreferenceChange(event);
518 // if (XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event)) {
519 // XMLPlugin.getDefault().getXMLTextTools().adaptToPreferenceChange(event);
524 * Return a partitioner for .html files.
526 public IDocumentPartitioner createHTMLPartitioner() {
527 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
528 return xmlTextTools.createXMLPartitioner();
531 // private static IDocumentPartitioner createIncludePartitioner() {
532 // // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
533 // return new DefaultPartitioner(getPHPPartitionScanner(), FastJavaPartitionScanner.PHP_PARTITION_TYPES);
537 // private static IDocumentPartitioner createJavaScriptPartitioner() {
538 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
542 * Return a partitioner for .php files.
544 public IDocumentPartitioner createPHPPartitioner() {
545 // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
546 return new DefaultPartitioner(getPHPPartitionScanner(), LEGAL_CONTENT_TYPES);
549 private IDocumentPartitioner createJSPPartitioner() {
550 return new PHPDocumentPartitioner(getJSPPartitionScanner());
551 // return new JSPDocumentPartitioner(getJSPPartitionScanner(), jspScriptScanner);
557 // public IPartitionTokenScanner getJSPScriptScanner() {
558 // return jspScriptScanner;
560 private IDocumentPartitioner createSmartyPartitioner() {
561 return new DefaultPartitioner(getSmartyPartitionScanner(), XMLTextTools.TYPES);
564 private IDocumentPartitioner createXMLPartitioner() {
565 // return new DefaultPartitioner(getXMLPartitionScanner(), XMLTextTools.TYPES);
566 return xmlTextTools.createXMLPartitioner();
569 // private IDocumentPartitioner createCSSPartitioner() {
570 // return new DefaultPartitioner(getHTMLPartitionScanner(), XMLTextTools.TYPES);
574 * Return a scanner for creating html partitions.
576 // private static XMLPartitionScanner getHTMLPartitionScanner() {
577 // // if (HTML_PARTITION_SCANNER == null)
578 // // HTML_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.HTML_FILE);
579 // // return HTML_PARTITION_SCANNER;^
580 // if (HTML_PARTITION_SCANNER == null)
581 // HTML_PARTITION_SCANNER = new XMLPartitionScanner(false);
582 // return HTML_PARTITION_SCANNER;
585 * Return a scanner for creating php partitions.
587 private FastJavaPartitionScanner getPHPPartitionScanner() {
588 // if (PHP_PARTITION_SCANNER == null)
589 // PHP_PARTITION_SCANNER = new FastJavaPartitionScanner(); //new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
590 // return PHP_PARTITION_SCANNER;
591 return fPartitionScanner;
595 * Returns a scanner which is configured to scan plain text in JSP.
597 * @return a JSP text scanner
599 // public RuleBasedScanner getJSPTextScanner() {
600 // return jspTextScanner;
603 * Returns a scanner which is configured to scan plain text in JSP.
605 * @return a JSP text scanner
607 // public RuleBasedScanner getJSPBracketScanner() {
608 // return jspBracketScanner;
611 * Return a scanner for creating smarty partitions.
613 private static HTMLPartitionScanner getSmartyPartitionScanner() {
614 if (SMARTY_PARTITION_SCANNER == null)
615 SMARTY_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.SMARTY_FILE);
616 return SMARTY_PARTITION_SCANNER;
620 * Return a scanner for creating xml partitions.
622 // private static XMLPartitionScanner getXMLPartitionScanner() {
623 // // if (XML_PARTITION_SCANNER == null)
624 // // XML_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.XML_FILE);
625 // // return XML_PARTITION_SCANNER;
626 // if (XML_PARTITION_SCANNER == null)
627 // XML_PARTITION_SCANNER = new XMLPartitionScanner(false);
628 // return XML_PARTITION_SCANNER;
631 private PHPPartitionScanner getJSPPartitionScanner() {
632 if (jspPartitionScanner == null)
633 jspPartitionScanner = new PHPPartitionScanner();
634 return jspPartitionScanner;
638 * Sets up the Java document partitioner for the given document for the default partitioning.
641 * the document to be set up
644 public void setupJavaDocumentPartitioner(IDocument document) {
645 setupJavaDocumentPartitioner(document, IDocumentExtension3.DEFAULT_PARTITIONING);
649 * Sets up the Java document partitioner for the given document for the given partitioning.
652 * the document to be set up
653 * @param partitioning
654 * the document partitioning
657 public void setupJavaDocumentPartitioner(IDocument document, String partitioning) {
658 IDocumentPartitioner partitioner = createDocumentPartitioner();
659 if (document instanceof IDocumentExtension3) {
660 IDocumentExtension3 extension3 = (IDocumentExtension3) document;
661 extension3.setDocumentPartitioner(partitioning, partitioner);
663 document.setDocumentPartitioner(partitioner);
665 partitioner.connect(document);
669 * Returns this text tool's preference store.
671 * @return the preference store
674 protected IPreferenceStore getPreferenceStore() {
675 return fPreferenceStore;
679 * Returns this text tool's core preference store.
681 * @return the core preference store
684 protected Preferences getCorePreferenceStore() {
685 return fCorePreferenceStore;