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.FastPartitioner;
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
43 * scanner exist only one time, i.e. the same instances are returned to all
44 * clients. Thus, clients share those tools.
46 * This class may be instantiated; it is not intended to be subclassed.
49 public class JavaTextTools implements IPHPPartitions {
50 // private static final String[] TOKENS = {
51 // JSPScriptScanner.JSP_DEFAULT,
52 // JSPScriptScanner.JSP_BRACKET };
53 private final static String[] LEGAL_CONTENT_TYPES = new String[] {
55 PHP_MULTILINE_COMMENT,
56 PHP_SINGLELINE_COMMENT,
61 // private static XMLPartitionScanner HTML_PARTITION_SCANNER = null;
63 // private static FastJavaPartitionScanner PHP_PARTITION_SCANNER = null;
65 private static HTMLPartitionScanner SMARTY_PARTITION_SCANNER = null;
67 // private static XMLPartitionScanner XML_PARTITION_SCANNER = null;
69 // private final static String[] TYPES= new String[] {
70 // PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC,
71 // PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
72 // private final static String[] TYPES = new String[] {
73 // IPHPPartitions.PHP_PARTITIONING,
74 // IPHPPartitions.PHP_PHPDOC_COMMENT,
75 // // IPHPPartitions.HTML,
76 // // IPHPPartitions.HTML_MULTILINE_COMMENT,
77 // IPHPPartitions.JAVASCRIPT,
78 // IPHPPartitions.CSS,
79 // IPHPPartitions.SMARTY,
80 // IPHPPartitions.SMARTY_MULTILINE_COMMENT };
83 * This tools' preference listener.
85 private class PreferenceListener implements IPropertyChangeListener,
86 Preferences.IPropertyChangeListener {
87 public void propertyChange(PropertyChangeEvent event) {
88 adaptToPreferenceChange(event);
91 public void propertyChange(Preferences.PropertyChangeEvent event) {
92 adaptToPreferenceChange(new PropertyChangeEvent(event.getSource(),
93 event.getProperty(), event.getOldValue(), event
98 // /** The color manager */
99 private JavaColorManager colorManager;
101 /** The PHP source code scanner */
102 private PHPCodeScanner fCodeScanner;
104 /** The PHP multiline comment scanner */
105 private SingleTokenPHPScanner fMultilineCommentScanner;
107 /** The Java singleline comment scanner */
108 private SingleTokenPHPScanner fSinglelineCommentScanner;
110 /** The PHP double quoted string scanner */
111 // private SingleTokenPHPScanner fStringDQScanner;
112 /** The PHP single quoted string scanner */
113 // private SingleTokenPHPScanner fStringSQScanner;
114 /** The PHPDoc scanner */
115 private PHPDocCodeScanner fPHPDocScanner;
117 /** The HTML scanner */
118 // private HTMLCodeScanner fHTMLScanner;
119 /** The Smarty scanner */
120 private SmartyCodeScanner fSmartyScanner;
122 /** The SmartyDoc scanner */
123 private SmartyDocCodeScanner fSmartyDocScanner;
125 /** The Java partitions scanner. */
126 private FastJavaPartitionScanner fPartitionScanner;
128 /** The preference store */
129 private IPreferenceStore fPreferenceStore;
131 /** The XML Language text tools */
132 private XMLTextTools xmlTextTools;
135 * The core preference store.
139 private Preferences fCorePreferenceStore;
141 /** The preference change listener */
142 private PreferenceListener fPreferenceListener = new PreferenceListener();
144 /** The JSP partitions scanner */
145 private PHPPartitionScanner jspPartitionScanner = null;
147 /** The JSP script subpartitions scanner */
148 // private JSPScriptScanner jspScriptScanner;
149 /** The PHP plain text scanner */
150 // private RuleBasedScanner jspTextScanner;
151 /** The PHP brackets scanner */
152 // private RuleBasedScanner jspBracketScanner;
154 * Creates a new Java text tools collection.
157 * the preference store to initialize the text tools. The text
158 * tool instance installs a listener on the passed preference
159 * store to adapt itself to changes in the preference store. In
160 * general <code>PreferenceConstants.
161 * getPreferenceStore()</code>
162 * should be used to initialize the text tools.
164 * optional preference store to initialize the text tools. The
165 * text tool instance installs a listener on the passed
166 * preference store to adapt itself to changes in the preference
168 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
171 public JavaTextTools(IPreferenceStore store, Preferences coreStore) {
172 this(store, coreStore, true);
176 * Creates a new Java text tools collection.
179 * the preference store to initialize the text tools. The text
180 * tool instance installs a listener on the passed preference
181 * store to adapt itself to changes in the preference store. In
182 * general <code>PreferenceConstants.
183 * getPreferenceStore()</code>
184 * shoould be used to initialize the text tools.
186 * optional preference store to initialize the text tools. The
187 * text tool instance installs a listener on the passed
188 * preference store to adapt itself to changes in the preference
190 * @param autoDisposeOnDisplayDispose
191 * if <code>true</code> the color manager automatically
192 * disposes all managed colors when the current display gets
193 * disposed and all calls to
194 * {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()}are
196 * @see net.sourceforge.phpdt.ui.PreferenceConstants#getPreferenceStore()
199 public JavaTextTools(IPreferenceStore store, Preferences coreStore,
200 boolean autoDisposeOnDisplayDispose) {
201 // super(store, TOKENS, );
202 // REVISIT: preference store
203 xmlTextTools = new XMLTextTools(XMLPlugin.getDefault()
204 .getPreferenceStore());
206 colorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
207 fPreferenceStore = store;
208 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
210 fCorePreferenceStore = coreStore;
211 if (fCorePreferenceStore != null)
212 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
214 fCodeScanner = new PHPCodeScanner((JavaColorManager) colorManager,
216 fMultilineCommentScanner = new SingleTokenPHPScanner(
217 (JavaColorManager) colorManager, store,
218 IPreferenceConstants.PHP_MULTILINE_COMMENT);
219 fSinglelineCommentScanner = new SingleTokenPHPScanner(
220 (JavaColorManager) colorManager, store,
221 IPreferenceConstants.PHP_SINGLELINE_COMMENT);
222 // fStringDQScanner = new SingleTokenPHPScanner((JavaColorManager)
223 // colorManager, store, IPreferenceConstants.PHP_STRING);
224 // fStringSQScanner = new SingleTokenPHPScanner((JavaColorManager)
225 // colorManager, store, IPreferenceConstants.PHP_STRING);
227 fPHPDocScanner = new PHPDocCodeScanner((JavaColorManager) colorManager,
229 // fHTMLScanner = new HTMLCodeScanner((JavaColorManager)fColorManager,
231 fSmartyScanner = new SmartyCodeScanner((JavaColorManager) colorManager,
233 fSmartyDocScanner = new SmartyDocCodeScanner(
234 (JavaColorManager) colorManager, store);
236 fPartitionScanner = new FastJavaPartitionScanner();
238 // jspScriptScanner = new JSPScriptScanner();
239 // fPartitionScanner = new FastJavaPartitionScanner();
240 // fPartitionScanner = new PHPPartitionScanner();
242 // jspBracketScanner = new RuleBasedScanner();
243 // jspBracketScanner.setDefaultReturnToken(new
244 // Token(JSPScriptScanner.JSP_BRACKET));
245 // jspTextScanner = new RuleBasedScanner();
246 // jspTextScanner.setDefaultReturnToken(new
247 // Token(JSPScriptScanner.JSP_DEFAULT));
253 public XMLTextTools getXMLTextTools() {
258 * Disposes all the individual tools of this tools collection.
260 public void dispose() {
263 fMultilineCommentScanner = null;
264 fSinglelineCommentScanner = null;
265 // fStringDQScanner = null;
266 // fStringSQScanner = null;
267 fPHPDocScanner = null;
268 // fPartitionScanner = null;
270 if (colorManager != null) {
271 colorManager.dispose();
275 if (fPreferenceStore != null) {
276 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
277 fPreferenceStore = null;
279 if (fCorePreferenceStore != null) {
281 .removePropertyChangeListener(fPreferenceListener);
282 fCorePreferenceStore = null;
285 fPreferenceListener = null;
290 * Returns the color manager which is used to manage any Java-specific
291 * colors needed for such things like syntax highlighting.
293 * @return the color manager to be used for Java text viewers
295 public JavaColorManager getColorManager() {
296 return (JavaColorManager) colorManager;
300 * Returns a scanner which is configured to scan Java source code.
302 * @return a Java source code scanner
304 // public RuleBasedScanner getCodeScanner() {
305 // return fCodeScanner;
309 * Returns a scanner which is configured to scan Java multiline comments.
311 * @return a Java multiline comment scanner
315 // public RuleBasedScanner getMultilineCommentScanner() {
316 // return fMultilineCommentScanner;
320 * Returns a scanner which is configured to scan HTML code.
322 * @return a HTML scanner
326 // public RuleBasedScanner getHTMLScanner() {
327 // return fHTMLScanner;
330 * Returns a scanner which is configured to scan Smarty code.
332 * @return a Smarty scanner
336 // public RuleBasedScanner getSmartyScanner() {
337 // return fSmartyScanner;
341 * Returns a scanner which is configured to scan Smarty code.
343 * @return a Smarty scanner
347 // public RuleBasedScanner getSmartyDocScanner() {
348 // return fSmartyDocScanner;
352 * Returns a scanner which is configured to scan Java singleline comments.
354 * @return a Java singleline comment scanner
358 // public RuleBasedScanner getSinglelineCommentScanner() {
359 // return fSinglelineCommentScanner;
363 * Returns a scanner which is configured to scan Java strings.
365 * @return a Java string scanner
369 // public RuleBasedScanner getStringScanner() {
370 // return fStringDQScanner;
373 * Returns a scanner which is configured to scan JavaDoc compliant comments.
374 * Notes that the start sequence "/**" and the corresponding end sequence
375 * are part of the JavaDoc comment.
377 * @return a JavaDoc scanner
379 // public RuleBasedScanner getJavaDocScanner() {
380 // return fPHPDocScanner;
384 * Returns a scanner which is configured to scan Java-specific partitions,
385 * which are multi-line comments, JavaDoc comments, and regular Java source
388 * @return a Java partition scanner
390 // public IPartitionTokenScanner getPartitionScanner() {
391 // return fPartitionScanner;
394 * Factory method for creating a PHP-specific document partitioner using
395 * this object's partitions scanner. This method is a convenience method.
397 * @return a newly created Java document partitioner
399 public IDocumentPartitioner createDocumentPartitioner() {
400 return createDocumentPartitioner(".php");
404 * Factory method for creating a PHP-specific document partitioner using
405 * this object's partitions scanner. This method is a convenience method.
407 * @return a newly created Java document partitioner
409 public IDocumentPartitioner createDocumentPartitioner(String extension) {
413 // FastJavaPartitionScanner.JAVA_DOC,
414 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
415 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
416 // FastJavaPartitionScanner.JAVA_STRING };
418 // return new DefaultPartitioner(getPartitionScanner(), types);
419 IDocumentPartitioner partitioner = null;
420 // System.out.println(extension);
421 if (extension.equalsIgnoreCase(".html")
422 || extension.equalsIgnoreCase(".htm")) {
424 partitioner = createHTMLPartitioner();
425 partitioner = createJSPPartitioner();
426 } else if (extension.equalsIgnoreCase(".xml")) {
428 partitioner = createXMLPartitioner();
429 // } else if (extension.equalsIgnoreCase(".js")) {
431 // partitioner = createJavaScriptPartitioner();
432 // } else if (extension.equalsIgnoreCase(".css")) {
433 // // cascading style sheets
434 // partitioner = createCSSPartitioner();
435 } else if (extension.equalsIgnoreCase(".tpl")) {
437 partitioner = createSmartyPartitioner();
438 // } else if (extension.equalsIgnoreCase(".inc")) {
439 // // php include files ?
440 // partitioner = createIncludePartitioner();
443 if (partitioner == null) {
444 partitioner = createJSPPartitioner();
451 * Sets up the Java document partitioner for the given document for the
452 * given partitioning.
455 * the document to be set up
456 * @param partitioning
457 * the document partitioning
463 // public void setupJavaDocumentPartitioner(IDocument document, String
464 // partitioning, Object element) {
465 // IDocumentPartitioner partitioner = createDocumentPartitioner(".php");
467 // // if (document instanceof IDocumentExtension3) {
468 // // IDocumentExtension3 extension3= (IDocumentExtension3) document;
469 // // extension3.setDocumentPartitioner(partitioning, partitioner);
471 // document.setDocumentPartitioner(partitioner);
473 // partitioner.connect(document);
475 public void setupHTMLDocumentPartitioner(IDocument document,
476 String partitioning, Object element) {
477 IDocumentPartitioner partitioner = createDocumentPartitioner(".html");
479 // if (document instanceof IDocumentExtension3) {
480 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
481 // extension3.setDocumentPartitioner(partitioning, partitioner);
483 document.setDocumentPartitioner(partitioner);
485 partitioner.connect(document);
488 public void setupSmartyDocumentPartitioner(IDocument document,
489 String partitioning, Object element) {
490 IDocumentPartitioner partitioner = createDocumentPartitioner(".tpl");
492 // if (document instanceof IDocumentExtension3) {
493 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
494 // extension3.setDocumentPartitioner(partitioning, partitioner);
496 document.setDocumentPartitioner(partitioner);
498 partitioner.connect(document);
502 * Returns the names of the document position categories used by the
503 * document partitioners created by this object to manage their partition
504 * information. If the partitioners don't use document position categories,
505 * the returned result is <code>null</code>.
507 * @return the partition managing position categories or <code>null</code>
510 // public String[] getPartitionManagingPositionCategories() {
511 // return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
515 * Determines whether the preference change encoded by the given event
516 * changes the behavior of one its contained components.
519 * the event to be investigated
520 * @return <code>true</code> if event causes a behavioral change
522 * @deprecated As of 3.0, replaced by
523 * {@link net.sourceforge.phpdt.ui.text.JavaSourceViewerConfiguration#affectsTextPresentation(PropertyChangeEvent)}
525 // public boolean affectsBehavior(PropertyChangeEvent event) {
526 // return fCodeScanner.affectsBehavior(event)
527 // || fMultilineCommentScanner.affectsBehavior(event)
528 // || fSinglelineCommentScanner.affectsBehavior(event)
529 // || fStringDQScanner.affectsBehavior(event)
530 // || fPHPDocScanner.affectsBehavior(event);
533 * Adapts the behavior of the contained components to the change encoded in
537 * the event to which to adapt
540 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
541 if (fCodeScanner.affectsBehavior(event))
542 fCodeScanner.adaptToPreferenceChange(event);
543 if (fMultilineCommentScanner.affectsBehavior(event))
544 fMultilineCommentScanner.adaptToPreferenceChange(event);
545 if (fSinglelineCommentScanner.affectsBehavior(event))
546 fSinglelineCommentScanner.adaptToPreferenceChange(event);
547 // if (fStringDQScanner.affectsBehavior(event))
548 // fStringDQScanner.adaptToPreferenceChange(event);
549 if (fPHPDocScanner.affectsBehavior(event))
550 fPHPDocScanner.adaptToPreferenceChange(event);
551 // if (fHTMLScanner.affectsBehavior(event))
552 // fHTMLScanner.adaptToPreferenceChange(event);
553 if (fSmartyScanner.affectsBehavior(event))
554 fSmartyScanner.adaptToPreferenceChange(event);
555 if (fSmartyDocScanner.affectsBehavior(event))
556 fSmartyDocScanner.adaptToPreferenceChange(event);
557 // if (XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event))
559 // XMLPlugin.getDefault().getXMLTextTools().adaptToPreferenceChange(event);
564 * Return a partitioner for .html files.
566 public IDocumentPartitioner createHTMLPartitioner() {
567 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
568 return xmlTextTools.createXMLPartitioner();
571 // private static IDocumentPartitioner createIncludePartitioner() {
572 // // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
573 // return new DefaultPartitioner(getPHPPartitionScanner(),
574 // FastJavaPartitionScanner.PHP_PARTITION_TYPES);
578 // private static IDocumentPartitioner createJavaScriptPartitioner() {
579 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
583 * Return a partitioner for .php files.
585 public IDocumentPartitioner createPHPPartitioner() {
586 // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
587 return new /*DefaultPartitioner*/FastPartitioner(getPHPPartitionScanner(),
588 LEGAL_CONTENT_TYPES);
591 private IDocumentPartitioner createJSPPartitioner() {
592 return new PHPDocumentPartitioner(getJSPPartitionScanner());
593 // return new JSPDocumentPartitioner(getJSPPartitionScanner(),
594 // jspScriptScanner);
600 // public IPartitionTokenScanner getJSPScriptScanner() {
601 // return jspScriptScanner;
603 private IDocumentPartitioner createSmartyPartitioner() {
604 return new /*DefaultPartitioner*/FastPartitioner(getSmartyPartitionScanner(),
608 private IDocumentPartitioner createXMLPartitioner() {
609 // return new DefaultPartitioner(getXMLPartitionScanner(),
610 // XMLTextTools.TYPES);
611 return xmlTextTools.createXMLPartitioner();
614 // private IDocumentPartitioner createCSSPartitioner() {
615 // return new DefaultPartitioner(getHTMLPartitionScanner(),
616 // XMLTextTools.TYPES);
620 * Return a scanner for creating html partitions.
622 // private static XMLPartitionScanner getHTMLPartitionScanner() {
623 // // if (HTML_PARTITION_SCANNER == null)
624 // // HTML_PARTITION_SCANNER = new
625 // HTMLPartitionScanner(IPHPPartitions.HTML_FILE);
626 // // return HTML_PARTITION_SCANNER;^
627 // if (HTML_PARTITION_SCANNER == null)
628 // HTML_PARTITION_SCANNER = new XMLPartitionScanner(false);
629 // return HTML_PARTITION_SCANNER;
632 * Return a scanner for creating php partitions.
634 private FastJavaPartitionScanner getPHPPartitionScanner() {
635 // if (PHP_PARTITION_SCANNER == null)
636 // PHP_PARTITION_SCANNER = new FastJavaPartitionScanner(); //new
637 // PHPPartitionScanner(IPHPPartitions.PHP_FILE);
638 // return PHP_PARTITION_SCANNER;
639 return fPartitionScanner;
643 * Returns a scanner which is configured to scan plain text in JSP.
645 * @return a JSP text scanner
647 // public RuleBasedScanner getJSPTextScanner() {
648 // return jspTextScanner;
651 * Returns a scanner which is configured to scan plain text in JSP.
653 * @return a JSP text scanner
655 // public RuleBasedScanner getJSPBracketScanner() {
656 // return jspBracketScanner;
659 * Return a scanner for creating smarty partitions.
661 private static HTMLPartitionScanner getSmartyPartitionScanner() {
662 if (SMARTY_PARTITION_SCANNER == null)
663 SMARTY_PARTITION_SCANNER = new HTMLPartitionScanner(
664 IPHPPartitions.SMARTY_FILE);
665 return SMARTY_PARTITION_SCANNER;
669 * Return a scanner for creating xml partitions.
671 // private static XMLPartitionScanner getXMLPartitionScanner() {
672 // // if (XML_PARTITION_SCANNER == null)
673 // // XML_PARTITION_SCANNER = new
674 // HTMLPartitionScanner(IPHPPartitions.XML_FILE);
675 // // return XML_PARTITION_SCANNER;
676 // if (XML_PARTITION_SCANNER == null)
677 // XML_PARTITION_SCANNER = new XMLPartitionScanner(false);
678 // return XML_PARTITION_SCANNER;
680 private PHPPartitionScanner getJSPPartitionScanner() {
681 if (jspPartitionScanner == null)
682 jspPartitionScanner = new PHPPartitionScanner();
683 return jspPartitionScanner;
687 * Sets up the Java document partitioner for the given document for the
688 * default partitioning.
691 * the document to be set up
694 public void setupJavaDocumentPartitioner(IDocument document) {
695 setupJavaDocumentPartitioner(document,
696 IDocumentExtension3.DEFAULT_PARTITIONING);
700 * Sets up the Java document partitioner for the given document for the
701 * given partitioning.
704 * the document to be set up
705 * @param partitioning
706 * the document partitioning
709 public void setupJavaDocumentPartitioner(IDocument document,
710 String partitioning) {
711 IDocumentPartitioner partitioner = createDocumentPartitioner();
712 if (document instanceof IDocumentExtension3) {
713 IDocumentExtension3 extension3 = (IDocumentExtension3) document;
714 extension3.setDocumentPartitioner(partitioning, partitioner);
716 document.setDocumentPartitioner(partitioner);
718 partitioner.connect(document);
722 * Returns this text tool's preference store.
724 * @return the preference store
727 protected IPreferenceStore getPreferenceStore() {
728 return fPreferenceStore;
732 * Returns this text tool's core preference store.
734 * @return the core preference store
737 // protected Preferences getCorePreferenceStore() {
738 // return fCorePreferenceStore;