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.text.XMLTextTools;
22 import org.eclipse.core.runtime.Preferences;
23 import org.eclipse.jface.preference.IPreferenceStore;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.IDocumentExtension3;
26 import org.eclipse.jface.text.IDocumentPartitioner;
27 import org.eclipse.jface.text.rules.DefaultPartitioner;
28 import org.eclipse.jface.text.rules.RuleBasedScanner;
29 import org.eclipse.jface.util.IPropertyChangeListener;
30 import org.eclipse.jface.util.PropertyChangeEvent;
33 // import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
34 // import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
35 // import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
36 // import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
37 // import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
38 // import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
41 * Tools required to configure a Java text viewer. The color manager and all
42 * scanner exist only one time, i.e. the same instances are returned to all
43 * 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[] {
53 PHP_PHPDOC_COMMENT, PHP_MULTILINE_COMMENT, PHP_SINGLELINE_COMMENT,
54 PHP_STRING_DQ, PHP_STRING_SQ, PHP_STRING_HEREDOC };
56 // private static XMLPartitionScanner HTML_PARTITION_SCANNER = null;
58 // private static FastJavaPartitionScanner PHP_PARTITION_SCANNER = null;
60 private static HTMLPartitionScanner SMARTY_PARTITION_SCANNER = null;
62 // private static XMLPartitionScanner XML_PARTITION_SCANNER = null;
64 // private final static String[] TYPES= new String[] {
65 // PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC,
66 // PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
67 // private final static String[] TYPES = new String[] {
68 // IPHPPartitions.PHP_PARTITIONING,
69 // IPHPPartitions.PHP_PHPDOC_COMMENT,
70 // // IPHPPartitions.HTML,
71 // // IPHPPartitions.HTML_MULTILINE_COMMENT,
72 // IPHPPartitions.JAVASCRIPT,
73 // IPHPPartitions.CSS,
74 // IPHPPartitions.SMARTY,
75 // IPHPPartitions.SMARTY_MULTILINE_COMMENT };
78 * This tools' preference listener.
80 private class PreferenceListener implements IPropertyChangeListener,
81 Preferences.IPropertyChangeListener {
82 public void propertyChange(PropertyChangeEvent event) {
83 adaptToPreferenceChange(event);
86 public void propertyChange(Preferences.PropertyChangeEvent event) {
87 adaptToPreferenceChange(new PropertyChangeEvent(event.getSource(),
88 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 PHP double quoted string scanner */
106 // private SingleTokenPHPScanner fStringDQScanner;
107 /** The PHP single quoted string scanner */
108 // private SingleTokenPHPScanner fStringSQScanner;
109 /** The PHPDoc scanner */
110 private PHPDocCodeScanner fPHPDocScanner;
112 /** The HTML scanner */
113 // private HTMLCodeScanner fHTMLScanner;
114 /** The Smarty scanner */
115 private SmartyCodeScanner fSmartyScanner;
117 /** The SmartyDoc scanner */
118 private SmartyDocCodeScanner fSmartyDocScanner;
120 /** The Java partitions scanner. */
121 private FastJavaPartitionScanner fPartitionScanner;
123 /** The preference store */
124 private IPreferenceStore fPreferenceStore;
126 /** The XML Language text tools */
127 private XMLTextTools xmlTextTools;
130 * The core preference store.
134 private Preferences fCorePreferenceStore;
136 /** The preference change listener */
137 private PreferenceListener fPreferenceListener = new PreferenceListener();
139 /** The JSP partitions scanner */
140 private PHPPartitionScanner jspPartitionScanner = null;
142 /** The JSP script subpartitions scanner */
143 // private JSPScriptScanner jspScriptScanner;
144 /** The PHP plain text scanner */
145 // private RuleBasedScanner jspTextScanner;
146 /** The PHP brackets scanner */
147 // private RuleBasedScanner jspBracketScanner;
149 * Creates a new Java text tools collection.
152 * the preference store to initialize the text tools. The text
153 * tool instance installs a listener on the passed preference
154 * store to adapt itself to changes in the preference store. In
155 * general <code>PreferenceConstants.
156 * getPreferenceStore()</code>
157 * should be used to initialize the text tools.
159 * optional preference store to initialize the text tools. The
160 * text tool instance installs a listener on the passed
161 * preference store to adapt itself to changes in the preference
163 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
166 public JavaTextTools(IPreferenceStore store, Preferences coreStore) {
167 this(store, coreStore, true);
171 * Creates a new Java text tools collection.
174 * the preference store to initialize the text tools. The text
175 * tool instance installs a listener on the passed preference
176 * store to adapt itself to changes in the preference store. In
177 * general <code>PreferenceConstants.
178 * getPreferenceStore()</code>
179 * shoould be used to initialize the text tools.
181 * optional preference store to initialize the text tools. The
182 * text tool instance installs a listener on the passed
183 * preference store to adapt itself to changes in the preference
185 * @param autoDisposeOnDisplayDispose
186 * if <code>true</code> the color manager automatically
187 * disposes all managed colors when the current display gets
188 * disposed and all calls to
189 * {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()}are
191 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
194 public JavaTextTools(IPreferenceStore store, Preferences coreStore,
195 boolean autoDisposeOnDisplayDispose) {
196 // super(store, TOKENS, );
197 // REVISIT: preference store
198 xmlTextTools = new XMLTextTools(XMLPlugin.getDefault()
199 .getPreferenceStore());
201 colorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
202 fPreferenceStore = store;
203 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
205 fCorePreferenceStore = coreStore;
206 if (fCorePreferenceStore != null)
207 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
209 fCodeScanner = new PHPCodeScanner((JavaColorManager) colorManager,
211 fMultilineCommentScanner = new SingleTokenPHPScanner(
212 (JavaColorManager) colorManager, store,
213 IPreferenceConstants.PHP_MULTILINE_COMMENT);
214 fSinglelineCommentScanner = new SingleTokenPHPScanner(
215 (JavaColorManager) colorManager, store,
216 IPreferenceConstants.PHP_SINGLELINE_COMMENT);
217 // fStringDQScanner = new SingleTokenPHPScanner((JavaColorManager)
218 // colorManager, store, IPreferenceConstants.PHP_STRING);
219 // fStringSQScanner = new SingleTokenPHPScanner((JavaColorManager)
220 // colorManager, store, IPreferenceConstants.PHP_STRING);
222 fPHPDocScanner = new PHPDocCodeScanner((JavaColorManager) colorManager,
224 // fHTMLScanner = new HTMLCodeScanner((JavaColorManager)fColorManager,
226 fSmartyScanner = new SmartyCodeScanner((JavaColorManager) colorManager,
228 fSmartyDocScanner = new SmartyDocCodeScanner(
229 (JavaColorManager) colorManager, store);
231 fPartitionScanner = new FastJavaPartitionScanner();
233 // jspScriptScanner = new JSPScriptScanner();
234 // fPartitionScanner = new FastJavaPartitionScanner();
235 // fPartitionScanner = new PHPPartitionScanner();
237 // jspBracketScanner = new RuleBasedScanner();
238 // jspBracketScanner.setDefaultReturnToken(new
239 // Token(JSPScriptScanner.JSP_BRACKET));
240 // jspTextScanner = new RuleBasedScanner();
241 // jspTextScanner.setDefaultReturnToken(new
242 // Token(JSPScriptScanner.JSP_DEFAULT));
248 public XMLTextTools getXMLTextTools() {
253 * Disposes all the individual tools of this tools collection.
255 public void dispose() {
258 fMultilineCommentScanner = null;
259 fSinglelineCommentScanner = null;
260 // fStringDQScanner = null;
261 // fStringSQScanner = null;
262 fPHPDocScanner = null;
263 // fPartitionScanner = null;
265 if (colorManager != null) {
266 colorManager.dispose();
270 if (fPreferenceStore != null) {
271 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
272 fPreferenceStore = null;
274 if (fCorePreferenceStore != null) {
276 .removePropertyChangeListener(fPreferenceListener);
277 fCorePreferenceStore = null;
280 fPreferenceListener = null;
285 * Returns the color manager which is used to manage any Java-specific
286 * colors needed for such things like syntax highlighting.
288 * @return the color manager to be used for Java text viewers
290 public JavaColorManager getColorManager() {
291 return (JavaColorManager) colorManager;
295 * Returns a scanner which is configured to scan Java source code.
297 * @return a Java source code scanner
299 public RuleBasedScanner getCodeScanner() {
304 * Returns a scanner which is configured to scan Java multiline comments.
306 * @return a Java multiline comment scanner
310 public RuleBasedScanner getMultilineCommentScanner() {
311 return fMultilineCommentScanner;
315 * Returns a scanner which is configured to scan HTML code.
317 * @return a HTML scanner
321 // public RuleBasedScanner getHTMLScanner() {
322 // return fHTMLScanner;
325 * Returns a scanner which is configured to scan Smarty code.
327 * @return a Smarty scanner
331 public RuleBasedScanner getSmartyScanner() {
332 return fSmartyScanner;
336 * Returns a scanner which is configured to scan Smarty code.
338 * @return a Smarty scanner
342 public RuleBasedScanner getSmartyDocScanner() {
343 return fSmartyDocScanner;
347 * Returns a scanner which is configured to scan Java singleline comments.
349 * @return a Java singleline comment scanner
353 public RuleBasedScanner getSinglelineCommentScanner() {
354 return fSinglelineCommentScanner;
358 * Returns a scanner which is configured to scan Java strings.
360 * @return a Java string scanner
364 // public RuleBasedScanner getStringScanner() {
365 // return fStringDQScanner;
368 * Returns a scanner which is configured to scan JavaDoc compliant comments.
369 * Notes that the start sequence "/**" and the corresponding end sequence
370 * are part of the JavaDoc comment.
372 * @return a JavaDoc scanner
374 public RuleBasedScanner getJavaDocScanner() {
375 return fPHPDocScanner;
379 * Returns a scanner which is configured to scan Java-specific partitions,
380 * which are multi-line comments, JavaDoc comments, and regular Java source
383 * @return a Java partition scanner
385 // public IPartitionTokenScanner getPartitionScanner() {
386 // return fPartitionScanner;
389 * Factory method for creating a PHP-specific document partitioner using
390 * this object's partitions scanner. This method is a convenience method.
392 * @return a newly created Java document partitioner
394 public IDocumentPartitioner createDocumentPartitioner() {
395 return createDocumentPartitioner(".php");
399 * Factory method for creating a PHP-specific document partitioner using
400 * this object's partitions scanner. This method is a convenience method.
402 * @return a newly created Java document partitioner
404 public IDocumentPartitioner createDocumentPartitioner(String extension) {
408 // FastJavaPartitionScanner.JAVA_DOC,
409 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
410 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
411 // FastJavaPartitionScanner.JAVA_STRING };
413 // return new DefaultPartitioner(getPartitionScanner(), types);
414 IDocumentPartitioner partitioner = null;
415 // System.out.println(extension);
416 if (extension.equalsIgnoreCase(".html")
417 || extension.equalsIgnoreCase(".htm")) {
419 partitioner = createHTMLPartitioner();
420 partitioner = createJSPPartitioner();
421 } else if (extension.equalsIgnoreCase(".xml")) {
423 partitioner = createXMLPartitioner();
424 // } else if (extension.equalsIgnoreCase(".js")) {
426 // partitioner = createJavaScriptPartitioner();
427 // } else if (extension.equalsIgnoreCase(".css")) {
428 // // cascading style sheets
429 // partitioner = createCSSPartitioner();
430 } else if (extension.equalsIgnoreCase(".tpl")) {
432 partitioner = createSmartyPartitioner();
433 // } else if (extension.equalsIgnoreCase(".inc")) {
434 // // php include files ?
435 // partitioner = createIncludePartitioner();
438 if (partitioner == null) {
439 partitioner = createJSPPartitioner();
446 * Sets up the Java document partitioner for the given document for the
447 * given partitioning.
450 * the document to be set up
451 * @param partitioning
452 * the document partitioning
458 // public void setupJavaDocumentPartitioner(IDocument document, String
459 // partitioning, Object element) {
460 // IDocumentPartitioner partitioner = createDocumentPartitioner(".php");
462 // // if (document instanceof IDocumentExtension3) {
463 // // IDocumentExtension3 extension3= (IDocumentExtension3) document;
464 // // extension3.setDocumentPartitioner(partitioning, partitioner);
466 // document.setDocumentPartitioner(partitioner);
468 // partitioner.connect(document);
470 public void setupHTMLDocumentPartitioner(IDocument document,
471 String partitioning, Object element) {
472 IDocumentPartitioner partitioner = createDocumentPartitioner(".html");
474 // if (document instanceof IDocumentExtension3) {
475 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
476 // extension3.setDocumentPartitioner(partitioning, partitioner);
478 document.setDocumentPartitioner(partitioner);
480 partitioner.connect(document);
483 public void setupSmartyDocumentPartitioner(IDocument document,
484 String partitioning, Object element) {
485 IDocumentPartitioner partitioner = createDocumentPartitioner(".tpl");
487 // if (document instanceof IDocumentExtension3) {
488 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
489 // extension3.setDocumentPartitioner(partitioning, partitioner);
491 document.setDocumentPartitioner(partitioner);
493 partitioner.connect(document);
497 * Returns the names of the document position categories used by the
498 * document partitioners created by this object to manage their partition
499 * information. If the partitioners don't use document position categories,
500 * the returned result is <code>null</code>.
502 * @return the partition managing position categories or <code>null</code>
505 public String[] getPartitionManagingPositionCategories() {
506 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
510 * Determines whether the preference change encoded by the given event
511 * changes the behavior of one its contained components.
514 * the event to be investigated
515 * @return <code>true</code> if event causes a behavioral change
517 * @deprecated As of 3.0, replaced by
518 * {@link net.sourceforge.phpdt.ui.text.JavaSourceViewerConfiguration#affectsTextPresentation(PropertyChangeEvent)}
520 // public boolean affectsBehavior(PropertyChangeEvent event) {
521 // return fCodeScanner.affectsBehavior(event)
522 // || fMultilineCommentScanner.affectsBehavior(event)
523 // || fSinglelineCommentScanner.affectsBehavior(event)
524 // || fStringDQScanner.affectsBehavior(event)
525 // || fPHPDocScanner.affectsBehavior(event);
528 * Adapts the behavior of the contained components to the change encoded in
532 * the event to which to adapt
535 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
536 if (fCodeScanner.affectsBehavior(event))
537 fCodeScanner.adaptToPreferenceChange(event);
538 if (fMultilineCommentScanner.affectsBehavior(event))
539 fMultilineCommentScanner.adaptToPreferenceChange(event);
540 if (fSinglelineCommentScanner.affectsBehavior(event))
541 fSinglelineCommentScanner.adaptToPreferenceChange(event);
542 // if (fStringDQScanner.affectsBehavior(event))
543 // fStringDQScanner.adaptToPreferenceChange(event);
544 if (fPHPDocScanner.affectsBehavior(event))
545 fPHPDocScanner.adaptToPreferenceChange(event);
546 // if (fHTMLScanner.affectsBehavior(event))
547 // fHTMLScanner.adaptToPreferenceChange(event);
548 if (fSmartyScanner.affectsBehavior(event))
549 fSmartyScanner.adaptToPreferenceChange(event);
550 if (fSmartyDocScanner.affectsBehavior(event))
551 fSmartyDocScanner.adaptToPreferenceChange(event);
552 // if (XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event))
554 // XMLPlugin.getDefault().getXMLTextTools().adaptToPreferenceChange(event);
559 * Return a partitioner for .html files.
561 public IDocumentPartitioner createHTMLPartitioner() {
562 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
563 return xmlTextTools.createXMLPartitioner();
566 // private static IDocumentPartitioner createIncludePartitioner() {
567 // // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
568 // return new DefaultPartitioner(getPHPPartitionScanner(),
569 // FastJavaPartitionScanner.PHP_PARTITION_TYPES);
573 // private static IDocumentPartitioner createJavaScriptPartitioner() {
574 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
578 * Return a partitioner for .php files.
580 public IDocumentPartitioner createPHPPartitioner() {
581 // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
582 return new DefaultPartitioner(getPHPPartitionScanner(),
583 LEGAL_CONTENT_TYPES);
586 private IDocumentPartitioner createJSPPartitioner() {
587 return new PHPDocumentPartitioner(getJSPPartitionScanner());
588 // return new JSPDocumentPartitioner(getJSPPartitionScanner(),
589 // jspScriptScanner);
595 // public IPartitionTokenScanner getJSPScriptScanner() {
596 // return jspScriptScanner;
598 private IDocumentPartitioner createSmartyPartitioner() {
599 return new DefaultPartitioner(getSmartyPartitionScanner(),
603 private IDocumentPartitioner createXMLPartitioner() {
604 // return new DefaultPartitioner(getXMLPartitionScanner(),
605 // XMLTextTools.TYPES);
606 return xmlTextTools.createXMLPartitioner();
609 // private IDocumentPartitioner createCSSPartitioner() {
610 // return new DefaultPartitioner(getHTMLPartitionScanner(),
611 // XMLTextTools.TYPES);
615 * Return a scanner for creating html partitions.
617 // private static XMLPartitionScanner getHTMLPartitionScanner() {
618 // // if (HTML_PARTITION_SCANNER == null)
619 // // HTML_PARTITION_SCANNER = new
620 // HTMLPartitionScanner(IPHPPartitions.HTML_FILE);
621 // // return HTML_PARTITION_SCANNER;^
622 // if (HTML_PARTITION_SCANNER == null)
623 // HTML_PARTITION_SCANNER = new XMLPartitionScanner(false);
624 // return HTML_PARTITION_SCANNER;
627 * Return a scanner for creating php partitions.
629 private FastJavaPartitionScanner getPHPPartitionScanner() {
630 // if (PHP_PARTITION_SCANNER == null)
631 // PHP_PARTITION_SCANNER = new FastJavaPartitionScanner(); //new
632 // PHPPartitionScanner(IPHPPartitions.PHP_FILE);
633 // return PHP_PARTITION_SCANNER;
634 return fPartitionScanner;
638 * Returns a scanner which is configured to scan plain text in JSP.
640 * @return a JSP text scanner
642 // public RuleBasedScanner getJSPTextScanner() {
643 // return jspTextScanner;
646 * Returns a scanner which is configured to scan plain text in JSP.
648 * @return a JSP text scanner
650 // public RuleBasedScanner getJSPBracketScanner() {
651 // return jspBracketScanner;
654 * Return a scanner for creating smarty partitions.
656 private static HTMLPartitionScanner getSmartyPartitionScanner() {
657 if (SMARTY_PARTITION_SCANNER == null)
658 SMARTY_PARTITION_SCANNER = new HTMLPartitionScanner(
659 IPHPPartitions.SMARTY_FILE);
660 return SMARTY_PARTITION_SCANNER;
664 * Return a scanner for creating xml partitions.
666 // private static XMLPartitionScanner getXMLPartitionScanner() {
667 // // if (XML_PARTITION_SCANNER == null)
668 // // XML_PARTITION_SCANNER = new
669 // HTMLPartitionScanner(IPHPPartitions.XML_FILE);
670 // // return XML_PARTITION_SCANNER;
671 // if (XML_PARTITION_SCANNER == null)
672 // XML_PARTITION_SCANNER = new XMLPartitionScanner(false);
673 // return XML_PARTITION_SCANNER;
675 private PHPPartitionScanner getJSPPartitionScanner() {
676 if (jspPartitionScanner == null)
677 jspPartitionScanner = new PHPPartitionScanner();
678 return jspPartitionScanner;
682 * Sets up the Java document partitioner for the given document for the
683 * default partitioning.
686 * the document to be set up
689 public void setupJavaDocumentPartitioner(IDocument document) {
690 setupJavaDocumentPartitioner(document,
691 IDocumentExtension3.DEFAULT_PARTITIONING);
695 * Sets up the Java document partitioner for the given document for the
696 * given partitioning.
699 * the document to be set up
700 * @param partitioning
701 * the document partitioning
704 public void setupJavaDocumentPartitioner(IDocument document,
705 String partitioning) {
706 IDocumentPartitioner partitioner = createDocumentPartitioner();
707 if (document instanceof IDocumentExtension3) {
708 IDocumentExtension3 extension3 = (IDocumentExtension3) document;
709 extension3.setDocumentPartitioner(partitioning, partitioner);
711 document.setDocumentPartitioner(partitioner);
713 partitioner.connect(document);
717 * Returns this text tool's preference store.
719 * @return the preference store
722 protected IPreferenceStore getPreferenceStore() {
723 return fPreferenceStore;
727 * Returns this text tool's core preference store.
729 * @return the core preference store
732 protected Preferences getCorePreferenceStore() {
733 return fCorePreferenceStore;