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,
59 private static XMLPartitionScanner HTML_PARTITION_SCANNER = null;
61 private static FastJavaPartitionScanner PHP_PARTITION_SCANNER = null;
63 private static HTMLPartitionScanner SMARTY_PARTITION_SCANNER = null;
65 private static XMLPartitionScanner XML_PARTITION_SCANNER = null;
67 // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC,
68 // PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
69 // private final static String[] TYPES = new String[] {
70 // IPHPPartitions.PHP_PARTITIONING,
71 // IPHPPartitions.PHP_PHPDOC_COMMENT,
72 // // IPHPPartitions.HTML,
73 // // IPHPPartitions.HTML_MULTILINE_COMMENT,
74 // IPHPPartitions.JAVASCRIPT,
75 // IPHPPartitions.CSS,
76 // IPHPPartitions.SMARTY,
77 // IPHPPartitions.SMARTY_MULTILINE_COMMENT };
80 * This tools' preference listener.
82 private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
83 public void propertyChange(PropertyChangeEvent event) {
84 adaptToPreferenceChange(event);
87 public void propertyChange(Preferences.PropertyChangeEvent event) {
88 adaptToPreferenceChange(new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event
93 // /** The color manager */
94 private JavaColorManager colorManager;
96 /** The PHP source code scanner */
97 private PHPCodeScanner fCodeScanner;
99 /** The PHP multiline comment scanner */
100 private SingleTokenPHPScanner fMultilineCommentScanner;
102 /** The Java singleline comment scanner */
103 private SingleTokenPHPScanner fSinglelineCommentScanner;
105 /** The Java string scanner */
106 private SingleTokenPHPScanner fStringScanner;
108 /** The PHPDoc scanner */
109 private PHPDocCodeScanner fPHPDocScanner;
111 /** The HTML scanner */
112 // private HTMLCodeScanner fHTMLScanner;
113 /** The Smarty scanner */
114 private SmartyCodeScanner fSmartyScanner;
116 /** The SmartyDoc scanner */
117 private SmartyDocCodeScanner fSmartyDocScanner;
119 /** The Java partitions scanner. */
120 private FastJavaPartitionScanner fPartitionScanner;
122 /** The preference store */
123 private IPreferenceStore fPreferenceStore;
125 /** The XML Language text tools */
126 private XMLTextTools xmlTextTools;
129 * The core preference store.
133 private Preferences fCorePreferenceStore;
135 /** The preference change listener */
136 private PreferenceListener fPreferenceListener = new PreferenceListener();
138 /** The JSP partitions scanner */
139 private PHPPartitionScanner jspPartitionScanner = null;
141 /** The JSP script subpartitions scanner */
142 // private JSPScriptScanner jspScriptScanner;
143 /** The PHP plain text scanner */
144 // private RuleBasedScanner jspTextScanner;
145 /** The PHP brackets scanner */
146 // private RuleBasedScanner jspBracketScanner;
148 * Creates a new Java text tools collection.
151 * the preference store to initialize the text tools. The text tool instance installs a listener on the passed preference
152 * store to adapt itself to changes in the preference store. In general <code>PreferenceConstants.
153 * getPreferenceStore()</code>
154 * should be used to initialize the text tools.
156 * optional preference store to initialize the text tools. The text tool instance installs a listener on the passed
157 * preference store to adapt itself to changes in the preference store.
158 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
161 public JavaTextTools(IPreferenceStore store, Preferences coreStore) {
162 this(store, coreStore, true);
166 * Creates a new Java text tools collection.
169 * the preference store to initialize the text tools. The text tool instance installs a listener on the passed preference
170 * store to adapt itself to changes in the preference store. In general <code>PreferenceConstants.
171 * getPreferenceStore()</code>
172 * shoould be used to initialize the text tools.
174 * optional preference store to initialize the text tools. The text tool instance installs a listener on the passed
175 * preference store to adapt itself to changes in the preference store.
176 * @param autoDisposeOnDisplayDispose
177 * if <code>true</code> the color manager automatically disposes all managed colors when the current display gets
178 * disposed and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()}are ignored.
179 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
182 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
183 // super(store, TOKENS, );
184 // REVISIT: preference store
185 xmlTextTools = new XMLTextTools(XMLPlugin.getDefault().getPreferenceStore());
187 colorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
188 fPreferenceStore = store;
189 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
191 fCorePreferenceStore = coreStore;
192 if (fCorePreferenceStore != null)
193 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
195 fCodeScanner = new PHPCodeScanner((JavaColorManager) colorManager, store);
196 fMultilineCommentScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store,
197 IPreferenceConstants.PHP_MULTILINE_COMMENT);
198 fSinglelineCommentScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store,
199 IPreferenceConstants.PHP_SINGLELINE_COMMENT);
200 fStringScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store, IPreferenceConstants.PHP_STRING);
201 fPHPDocScanner = new PHPDocCodeScanner((JavaColorManager) colorManager, store);
202 // fHTMLScanner = new HTMLCodeScanner((JavaColorManager)fColorManager, store);
203 fSmartyScanner = new SmartyCodeScanner((JavaColorManager) colorManager, store);
204 fSmartyDocScanner = new SmartyDocCodeScanner((JavaColorManager) colorManager, store);
206 fPartitionScanner = new FastJavaPartitionScanner();
208 // jspScriptScanner = new JSPScriptScanner();
209 // fPartitionScanner = new FastJavaPartitionScanner();
210 // fPartitionScanner = new PHPPartitionScanner();
212 // jspBracketScanner = new RuleBasedScanner();
213 // jspBracketScanner.setDefaultReturnToken(new Token(JSPScriptScanner.JSP_BRACKET));
214 // jspTextScanner = new RuleBasedScanner();
215 // jspTextScanner.setDefaultReturnToken(new Token(JSPScriptScanner.JSP_DEFAULT));
221 public XMLTextTools getXMLTextTools() {
226 * Disposes all the individual tools of this tools collection.
228 public void dispose() {
231 fMultilineCommentScanner = null;
232 fSinglelineCommentScanner = null;
233 fStringScanner = null;
234 fPHPDocScanner = null;
235 // fPartitionScanner = null;
237 if (colorManager != null) {
238 colorManager.dispose();
242 if (fPreferenceStore != null) {
243 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
244 fPreferenceStore = null;
246 if (fCorePreferenceStore != null) {
247 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
248 fCorePreferenceStore = null;
251 fPreferenceListener = null;
256 * Returns the color manager which is used to manage any Java-specific colors needed for such things like syntax highlighting.
258 * @return the color manager to be used for Java text viewers
260 public JavaColorManager getColorManager() {
261 return (JavaColorManager) colorManager;
265 * Returns a scanner which is configured to scan Java source code.
267 * @return a Java source code scanner
269 public RuleBasedScanner getCodeScanner() {
274 * Returns a scanner which is configured to scan Java multiline comments.
276 * @return a Java multiline comment scanner
280 public RuleBasedScanner getMultilineCommentScanner() {
281 return fMultilineCommentScanner;
285 * Returns a scanner which is configured to scan HTML code.
287 * @return a HTML scanner
291 // public RuleBasedScanner getHTMLScanner() {
292 // return fHTMLScanner;
295 * Returns a scanner which is configured to scan Smarty code.
297 * @return a Smarty scanner
301 public RuleBasedScanner getSmartyScanner() {
302 return fSmartyScanner;
306 * Returns a scanner which is configured to scan Smarty code.
308 * @return a Smarty scanner
312 public RuleBasedScanner getSmartyDocScanner() {
313 return fSmartyDocScanner;
317 * Returns a scanner which is configured to scan Java singleline comments.
319 * @return a Java singleline comment scanner
323 public RuleBasedScanner getSinglelineCommentScanner() {
324 return fSinglelineCommentScanner;
328 * Returns a scanner which is configured to scan Java strings.
330 * @return a Java string scanner
334 public RuleBasedScanner getStringScanner() {
335 return fStringScanner;
339 * Returns a scanner which is configured to scan JavaDoc compliant comments. Notes that the start sequence "/**" and the
340 * corresponding end sequence are part of the JavaDoc comment.
342 * @return a JavaDoc scanner
344 public RuleBasedScanner getJavaDocScanner() {
345 return fPHPDocScanner;
349 * Returns a scanner which is configured to scan Java-specific partitions, which are multi-line comments, JavaDoc comments, and
350 * regular Java source code.
352 * @return a Java partition scanner
354 // public IPartitionTokenScanner getPartitionScanner() {
355 // return fPartitionScanner;
358 * Factory method for creating a PHP-specific document partitioner using this object's partitions scanner. This method is a
359 * convenience method.
361 * @return a newly created Java document partitioner
363 public IDocumentPartitioner createDocumentPartitioner() {
364 return createDocumentPartitioner(".php");
368 * Factory method for creating a PHP-specific document partitioner using this object's partitions scanner. This method is a
369 * convenience method.
371 * @return a newly created Java document partitioner
373 public IDocumentPartitioner createDocumentPartitioner(String extension) {
377 // FastJavaPartitionScanner.JAVA_DOC,
378 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
379 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
380 // FastJavaPartitionScanner.JAVA_STRING };
382 // return new DefaultPartitioner(getPartitionScanner(), types);
383 IDocumentPartitioner partitioner = null;
384 // System.out.println(extension);
385 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
387 partitioner = createHTMLPartitioner();
388 partitioner = createJSPPartitioner();
389 } else if (extension.equalsIgnoreCase(".xml")) {
391 partitioner = createXMLPartitioner();
392 // } else if (extension.equalsIgnoreCase(".js")) {
394 // partitioner = createJavaScriptPartitioner();
395 // } else if (extension.equalsIgnoreCase(".css")) {
396 // // cascading style sheets
397 // partitioner = createCSSPartitioner();
398 } else if (extension.equalsIgnoreCase(".tpl")) {
400 partitioner = createSmartyPartitioner();
401 // } else if (extension.equalsIgnoreCase(".inc")) {
402 // // php include files ?
403 // partitioner = createIncludePartitioner();
406 if (partitioner == null) {
407 partitioner = createJSPPartitioner();
414 * Sets up the Java document partitioner for the given document for the given partitioning.
417 * the document to be set up
418 * @param partitioning
419 * the document partitioning
425 // public void setupJavaDocumentPartitioner(IDocument document, String partitioning, Object element) {
426 // IDocumentPartitioner partitioner = createDocumentPartitioner(".php");
428 // // if (document instanceof IDocumentExtension3) {
429 // // IDocumentExtension3 extension3= (IDocumentExtension3) document;
430 // // extension3.setDocumentPartitioner(partitioning, partitioner);
432 // document.setDocumentPartitioner(partitioner);
434 // partitioner.connect(document);
436 public void setupHTMLDocumentPartitioner(IDocument document, String partitioning, Object element) {
437 IDocumentPartitioner partitioner = createDocumentPartitioner(".html");
439 // if (document instanceof IDocumentExtension3) {
440 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
441 // extension3.setDocumentPartitioner(partitioning, partitioner);
443 document.setDocumentPartitioner(partitioner);
445 partitioner.connect(document);
448 public void setupSmartyDocumentPartitioner(IDocument document, String partitioning, Object element) {
449 IDocumentPartitioner partitioner = createDocumentPartitioner(".tpl");
451 // if (document instanceof IDocumentExtension3) {
452 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
453 // extension3.setDocumentPartitioner(partitioning, partitioner);
455 document.setDocumentPartitioner(partitioner);
457 partitioner.connect(document);
461 * Returns the names of the document position categories used by the document partitioners created by this object to manage their
462 * partition information. If the partitioners don't use document position categories, the returned result is <code>null</code>.
464 * @return the partition managing position categories or <code>null</code> if there is none
466 public String[] getPartitionManagingPositionCategories() {
467 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
471 * Determines whether the preference change encoded by the given event changes the behavior of one its contained components.
474 * the event to be investigated
475 * @return <code>true</code> if event causes a behavioral change
477 * @deprecated As of 3.0, replaced by
478 * {@link net.sourceforge.phpdt.ui.text.JavaSourceViewerConfiguration#affectsTextPresentation(PropertyChangeEvent)}
480 // public boolean affectsBehavior(PropertyChangeEvent event) {
481 // return fCodeScanner.affectsBehavior(event)
482 // || fMultilineCommentScanner.affectsBehavior(event)
483 // || fSinglelineCommentScanner.affectsBehavior(event)
484 // || fStringScanner.affectsBehavior(event)
485 // || fPHPDocScanner.affectsBehavior(event);
488 * Adapts the behavior of the contained components to the change encoded in the given event.
491 * the event to which to adapt
494 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
495 if (fCodeScanner.affectsBehavior(event))
496 fCodeScanner.adaptToPreferenceChange(event);
497 if (fMultilineCommentScanner.affectsBehavior(event))
498 fMultilineCommentScanner.adaptToPreferenceChange(event);
499 if (fSinglelineCommentScanner.affectsBehavior(event))
500 fSinglelineCommentScanner.adaptToPreferenceChange(event);
501 // if (fStringScanner.affectsBehavior(event))
502 // fStringScanner.adaptToPreferenceChange(event);
503 if (fPHPDocScanner.affectsBehavior(event))
504 fPHPDocScanner.adaptToPreferenceChange(event);
505 // if (fHTMLScanner.affectsBehavior(event))
506 // fHTMLScanner.adaptToPreferenceChange(event);
507 if (fSmartyScanner.affectsBehavior(event))
508 fSmartyScanner.adaptToPreferenceChange(event);
509 if (fSmartyDocScanner.affectsBehavior(event))
510 fSmartyDocScanner.adaptToPreferenceChange(event);
511 // if (XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event)) {
512 // XMLPlugin.getDefault().getXMLTextTools().adaptToPreferenceChange(event);
517 * Return a partitioner for .html files.
519 public IDocumentPartitioner createHTMLPartitioner() {
520 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
521 return xmlTextTools.createXMLPartitioner();
524 // private static IDocumentPartitioner createIncludePartitioner() {
525 // // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
526 // return new DefaultPartitioner(getPHPPartitionScanner(), FastJavaPartitionScanner.PHP_PARTITION_TYPES);
530 // private static IDocumentPartitioner createJavaScriptPartitioner() {
531 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
535 * Return a partitioner for .php files.
537 public IDocumentPartitioner createPHPPartitioner() {
538 // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
539 return new DefaultPartitioner(getPHPPartitionScanner(), LEGAL_CONTENT_TYPES);
542 private IDocumentPartitioner createJSPPartitioner() {
543 return new PHPDocumentPartitioner(getJSPPartitionScanner(), getPHPPartitionScanner());
544 // return new JSPDocumentPartitioner(getJSPPartitionScanner(), jspScriptScanner);
550 // public IPartitionTokenScanner getJSPScriptScanner() {
551 // return jspScriptScanner;
553 private IDocumentPartitioner createSmartyPartitioner() {
554 return new DefaultPartitioner(getSmartyPartitionScanner(), XMLTextTools.TYPES);
557 private IDocumentPartitioner createXMLPartitioner() {
558 // return new DefaultPartitioner(getXMLPartitionScanner(), XMLTextTools.TYPES);
559 return xmlTextTools.createXMLPartitioner();
562 // private IDocumentPartitioner createCSSPartitioner() {
563 // return new DefaultPartitioner(getHTMLPartitionScanner(), XMLTextTools.TYPES);
567 * Return a scanner for creating html partitions.
569 // private static XMLPartitionScanner getHTMLPartitionScanner() {
570 // // if (HTML_PARTITION_SCANNER == null)
571 // // HTML_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.HTML_FILE);
572 // // return HTML_PARTITION_SCANNER;^
573 // if (HTML_PARTITION_SCANNER == null)
574 // HTML_PARTITION_SCANNER = new XMLPartitionScanner(false);
575 // return HTML_PARTITION_SCANNER;
578 * Return a scanner for creating php partitions.
580 private FastJavaPartitionScanner getPHPPartitionScanner() {
581 // if (PHP_PARTITION_SCANNER == null)
582 // PHP_PARTITION_SCANNER = new FastJavaPartitionScanner(); //new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
583 // return PHP_PARTITION_SCANNER;
584 return fPartitionScanner;
588 * Returns a scanner which is configured to scan plain text in JSP.
590 * @return a JSP text scanner
592 // public RuleBasedScanner getJSPTextScanner() {
593 // return jspTextScanner;
596 * Returns a scanner which is configured to scan plain text in JSP.
598 * @return a JSP text scanner
600 // public RuleBasedScanner getJSPBracketScanner() {
601 // return jspBracketScanner;
604 * Return a scanner for creating smarty partitions.
606 private static HTMLPartitionScanner getSmartyPartitionScanner() {
607 if (SMARTY_PARTITION_SCANNER == null)
608 SMARTY_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.SMARTY_FILE);
609 return SMARTY_PARTITION_SCANNER;
613 * Return a scanner for creating xml partitions.
615 private static XMLPartitionScanner getXMLPartitionScanner() {
616 // if (XML_PARTITION_SCANNER == null)
617 // XML_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.XML_FILE);
618 // return XML_PARTITION_SCANNER;
619 if (XML_PARTITION_SCANNER == null)
620 XML_PARTITION_SCANNER = new XMLPartitionScanner(false);
621 return XML_PARTITION_SCANNER;
624 private PHPPartitionScanner getJSPPartitionScanner() {
625 if (jspPartitionScanner == null)
626 jspPartitionScanner = new PHPPartitionScanner();
627 return jspPartitionScanner;
631 * Sets up the Java document partitioner for the given document for the default partitioning.
634 * the document to be set up
637 public void setupJavaDocumentPartitioner(IDocument document) {
638 setupJavaDocumentPartitioner(document, IDocumentExtension3.DEFAULT_PARTITIONING);
642 * Sets up the Java document partitioner for the given document for the given partitioning.
645 * the document to be set up
646 * @param partitioning
647 * the document partitioning
650 public void setupJavaDocumentPartitioner(IDocument document, String partitioning) {
651 IDocumentPartitioner partitioner = createDocumentPartitioner();
652 if (document instanceof IDocumentExtension3) {
653 IDocumentExtension3 extension3 = (IDocumentExtension3) document;
654 extension3.setDocumentPartitioner(partitioning, partitioner);
656 document.setDocumentPartitioner(partitioner);
658 partitioner.connect(document);
662 * Returns this text tool's preference store.
664 * @return the preference store
667 protected IPreferenceStore getPreferenceStore() {
668 return fPreferenceStore;
672 * Returns this text tool's core preference store.
674 * @return the core preference store
677 protected Preferences getCorePreferenceStore() {
678 return fCorePreferenceStore;