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.PHPeclipsePlugin;
14 import net.sourceforge.phpeclipse.phpeditor.php.HTMLPartitionScanner;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPDocumentPartitioner;
16 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
17 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
18 import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner;
19 import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner;
20 import net.sourceforge.phpeclipse.xml.ui.XMLPlugin;
21 import net.sourceforge.phpeclipse.xml.ui.internal.text.SingleTokenScanner;
22 import net.sourceforge.phpeclipse.xml.ui.internal.text.XMLPartitionScanner;
23 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
25 import org.eclipse.core.runtime.Preferences;
26 import org.eclipse.jface.preference.IPreferenceStore;
27 import org.eclipse.jface.text.IDocument;
28 import org.eclipse.jface.text.IDocumentExtension3;
29 import org.eclipse.jface.text.IDocumentPartitioner;
30 import org.eclipse.jface.text.rules.DefaultPartitioner;
31 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
32 import org.eclipse.jface.text.rules.RuleBasedScanner;
33 import org.eclipse.jface.text.rules.Token;
34 import org.eclipse.jface.util.IPropertyChangeListener;
35 import org.eclipse.jface.util.PropertyChangeEvent;
38 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
39 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
40 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
41 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
42 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
43 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
46 * Tools required to configure a Java text viewer. The color manager and all scanner exist only one time, i.e. the same instances
47 * are returned to all clients. Thus, clients share those tools.
49 * This class may be instantiated; it is not intended to be subclassed.
52 public class JavaTextTools implements IPHPPartitions {
53 // private static final String[] TOKENS = {
54 // JSPScriptScanner.JSP_DEFAULT,
55 // JSPScriptScanner.JSP_BRACKET };
56 private final static String[] LEGAL_CONTENT_TYPES = new String[] {
58 PHP_MULTILINE_COMMENT,
59 PHP_SINGLELINE_COMMENT,
63 private static XMLPartitionScanner HTML_PARTITION_SCANNER = null;
65 private static FastJavaPartitionScanner PHP_PARTITION_SCANNER = null;
67 private static HTMLPartitionScanner SMARTY_PARTITION_SCANNER = null;
69 private static XMLPartitionScanner XML_PARTITION_SCANNER = null;
71 // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC,
72 // PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
73 // private final static String[] TYPES = new String[] {
74 // IPHPPartitions.PHP_PARTITIONING,
75 // IPHPPartitions.PHP_PHPDOC_COMMENT,
76 // // IPHPPartitions.HTML,
77 // // IPHPPartitions.HTML_MULTILINE_COMMENT,
78 // IPHPPartitions.JAVASCRIPT,
79 // IPHPPartitions.CSS,
80 // IPHPPartitions.SMARTY,
81 // IPHPPartitions.SMARTY_MULTILINE_COMMENT };
84 * This tools' preference listener.
86 private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
87 public void propertyChange(PropertyChangeEvent event) {
88 adaptToPreferenceChange(event);
91 public void propertyChange(Preferences.PropertyChangeEvent event) {
92 adaptToPreferenceChange(new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event
97 // /** The color manager */
98 private JavaColorManager colorManager;
100 /** The PHP source code scanner */
101 private PHPCodeScanner fCodeScanner;
103 /** The PHP multiline comment scanner */
104 private SingleTokenPHPScanner fMultilineCommentScanner;
106 /** The Java singleline comment scanner */
107 private SingleTokenPHPScanner fSinglelineCommentScanner;
109 /** The Java string scanner */
110 private SingleTokenPHPScanner fStringScanner;
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 org.eclipse.jdt.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 org.eclipse.jdt.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 fStringScanner = new SingleTokenPHPScanner((JavaColorManager) colorManager, store, IPreferenceConstants.PHP_STRING);
205 fPHPDocScanner = new PHPDocCodeScanner((JavaColorManager) colorManager, store);
206 // fHTMLScanner = new HTMLCodeScanner((JavaColorManager)fColorManager, store);
207 fSmartyScanner = new SmartyCodeScanner((JavaColorManager) colorManager, store);
208 fSmartyDocScanner = new SmartyDocCodeScanner((JavaColorManager) colorManager, store);
210 fPartitionScanner = new FastJavaPartitionScanner();
212 // jspScriptScanner = new JSPScriptScanner();
213 // fPartitionScanner = new FastJavaPartitionScanner();
214 // fPartitionScanner = new PHPPartitionScanner();
216 // jspBracketScanner = new RuleBasedScanner();
217 // jspBracketScanner.setDefaultReturnToken(new Token(JSPScriptScanner.JSP_BRACKET));
218 // jspTextScanner = new RuleBasedScanner();
219 // jspTextScanner.setDefaultReturnToken(new Token(JSPScriptScanner.JSP_DEFAULT));
225 public XMLTextTools getXMLTextTools() {
230 * Disposes all the individual tools of this tools collection.
232 public void dispose() {
235 fMultilineCommentScanner = null;
236 fSinglelineCommentScanner = null;
237 fStringScanner = null;
238 fPHPDocScanner = null;
239 // fPartitionScanner = null;
241 if (colorManager != null) {
242 colorManager.dispose();
246 if (fPreferenceStore != null) {
247 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
248 fPreferenceStore = null;
250 if (fCorePreferenceStore != null) {
251 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
252 fCorePreferenceStore = null;
255 fPreferenceListener = null;
260 * Returns the color manager which is used to manage any Java-specific colors needed for such things like syntax highlighting.
262 * @return the color manager to be used for Java text viewers
264 public JavaColorManager getColorManager() {
265 return (JavaColorManager) colorManager;
269 * Returns a scanner which is configured to scan Java source code.
271 * @return a Java source code scanner
273 public RuleBasedScanner getCodeScanner() {
278 * Returns a scanner which is configured to scan Java multiline comments.
280 * @return a Java multiline comment scanner
284 public RuleBasedScanner getMultilineCommentScanner() {
285 return fMultilineCommentScanner;
289 * Returns a scanner which is configured to scan HTML code.
291 * @return a HTML scanner
295 // public RuleBasedScanner getHTMLScanner() {
296 // return fHTMLScanner;
299 * Returns a scanner which is configured to scan Smarty code.
301 * @return a Smarty scanner
305 public RuleBasedScanner getSmartyScanner() {
306 return fSmartyScanner;
310 * Returns a scanner which is configured to scan Smarty code.
312 * @return a Smarty scanner
316 public RuleBasedScanner getSmartyDocScanner() {
317 return fSmartyDocScanner;
321 * Returns a scanner which is configured to scan Java singleline comments.
323 * @return a Java singleline comment scanner
327 public RuleBasedScanner getSinglelineCommentScanner() {
328 return fSinglelineCommentScanner;
332 * Returns a scanner which is configured to scan Java strings.
334 * @return a Java string scanner
338 public RuleBasedScanner getStringScanner() {
339 return fStringScanner;
343 * Returns a scanner which is configured to scan JavaDoc compliant comments. Notes that the start sequence "/**" and the
344 * corresponding end sequence are part of the JavaDoc comment.
346 * @return a JavaDoc scanner
348 public RuleBasedScanner getJavaDocScanner() {
349 return fPHPDocScanner;
353 * Returns a scanner which is configured to scan Java-specific partitions, which are multi-line comments, JavaDoc comments, and
354 * regular Java source code.
356 * @return a Java partition scanner
358 // public IPartitionTokenScanner getPartitionScanner() {
359 // return fPartitionScanner;
362 * Factory method for creating a PHP-specific document partitioner using this object's partitions scanner. This method is a
363 * convenience method.
365 * @return a newly created Java document partitioner
367 public IDocumentPartitioner createDocumentPartitioner() {
368 return createDocumentPartitioner(".php");
372 * Factory method for creating a PHP-specific document partitioner using this object's partitions scanner. This method is a
373 * convenience method.
375 * @return a newly created Java document partitioner
377 public IDocumentPartitioner createDocumentPartitioner(String extension) {
381 // FastJavaPartitionScanner.JAVA_DOC,
382 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
383 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
384 // FastJavaPartitionScanner.JAVA_STRING };
386 // return new DefaultPartitioner(getPartitionScanner(), types);
387 IDocumentPartitioner partitioner = null;
388 // System.out.println(extension);
389 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
391 partitioner = createHTMLPartitioner();
392 partitioner = createJSPPartitioner();
393 } else if (extension.equalsIgnoreCase(".xml")) {
395 partitioner = createXMLPartitioner();
396 // } else if (extension.equalsIgnoreCase(".js")) {
398 // partitioner = createJavaScriptPartitioner();
399 // } else if (extension.equalsIgnoreCase(".css")) {
400 // // cascading style sheets
401 // partitioner = createCSSPartitioner();
402 } else if (extension.equalsIgnoreCase(".tpl")) {
404 partitioner = createSmartyPartitioner();
405 // } else if (extension.equalsIgnoreCase(".inc")) {
406 // // php include files ?
407 // partitioner = createIncludePartitioner();
410 if (partitioner == null) {
411 partitioner = createJSPPartitioner();
418 * Sets up the Java document partitioner for the given document for the given partitioning.
421 * the document to be set up
422 * @param partitioning
423 * the document partitioning
429 // public void setupJavaDocumentPartitioner(IDocument document, String partitioning, Object element) {
430 // IDocumentPartitioner partitioner = createDocumentPartitioner(".php");
432 // // if (document instanceof IDocumentExtension3) {
433 // // IDocumentExtension3 extension3= (IDocumentExtension3) document;
434 // // extension3.setDocumentPartitioner(partitioning, partitioner);
436 // document.setDocumentPartitioner(partitioner);
438 // partitioner.connect(document);
440 public void setupHTMLDocumentPartitioner(IDocument document, String partitioning, Object element) {
441 IDocumentPartitioner partitioner = createDocumentPartitioner(".html");
443 // if (document instanceof IDocumentExtension3) {
444 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
445 // extension3.setDocumentPartitioner(partitioning, partitioner);
447 document.setDocumentPartitioner(partitioner);
449 partitioner.connect(document);
452 public void setupSmartyDocumentPartitioner(IDocument document, String partitioning, Object element) {
453 IDocumentPartitioner partitioner = createDocumentPartitioner(".tpl");
455 // if (document instanceof IDocumentExtension3) {
456 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
457 // extension3.setDocumentPartitioner(partitioning, partitioner);
459 document.setDocumentPartitioner(partitioner);
461 partitioner.connect(document);
465 * Returns the names of the document position categories used by the document partitioners created by this object to manage their
466 * partition information. If the partitioners don't use document position categories, the returned result is <code>null</code>.
468 * @return the partition managing position categories or <code>null</code> if there is none
470 public String[] getPartitionManagingPositionCategories() {
471 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
475 * Determines whether the preference change encoded by the given event changes the behavior of one its contained components.
478 * the event to be investigated
479 * @return <code>true</code> if event causes a behavioral change
481 * @deprecated As of 3.0, replaced by
482 * {@link org.eclipse.jdt.ui.text.JavaSourceViewerConfiguration#affectsTextPresentation(PropertyChangeEvent)}
484 // public boolean affectsBehavior(PropertyChangeEvent event) {
485 // return fCodeScanner.affectsBehavior(event)
486 // || fMultilineCommentScanner.affectsBehavior(event)
487 // || fSinglelineCommentScanner.affectsBehavior(event)
488 // || fStringScanner.affectsBehavior(event)
489 // || fPHPDocScanner.affectsBehavior(event);
492 * Adapts the behavior of the contained components to the change encoded in the given event.
495 * the event to which to adapt
498 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
499 if (fCodeScanner.affectsBehavior(event))
500 fCodeScanner.adaptToPreferenceChange(event);
501 if (fMultilineCommentScanner.affectsBehavior(event))
502 fMultilineCommentScanner.adaptToPreferenceChange(event);
503 if (fSinglelineCommentScanner.affectsBehavior(event))
504 fSinglelineCommentScanner.adaptToPreferenceChange(event);
505 // if (fStringScanner.affectsBehavior(event))
506 // fStringScanner.adaptToPreferenceChange(event);
507 if (fPHPDocScanner.affectsBehavior(event))
508 fPHPDocScanner.adaptToPreferenceChange(event);
509 // if (fHTMLScanner.affectsBehavior(event))
510 // fHTMLScanner.adaptToPreferenceChange(event);
511 if (fSmartyScanner.affectsBehavior(event))
512 fSmartyScanner.adaptToPreferenceChange(event);
513 if (fSmartyDocScanner.affectsBehavior(event))
514 fSmartyDocScanner.adaptToPreferenceChange(event);
515 // if (XMLPlugin.getDefault().getXMLTextTools().affectsBehavior(event)) {
516 // XMLPlugin.getDefault().getXMLTextTools().adaptToPreferenceChange(event);
521 * Return a partitioner for .html files.
523 public IDocumentPartitioner createHTMLPartitioner() {
524 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
525 return xmlTextTools.createXMLPartitioner();
528 // private static IDocumentPartitioner createIncludePartitioner() {
529 // // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
530 // return new DefaultPartitioner(getPHPPartitionScanner(), FastJavaPartitionScanner.PHP_PARTITION_TYPES);
534 // private static IDocumentPartitioner createJavaScriptPartitioner() {
535 // return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
539 * Return a partitioner for .php files.
541 public IDocumentPartitioner createPHPPartitioner() {
542 // return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
543 return new DefaultPartitioner(getPHPPartitionScanner(), LEGAL_CONTENT_TYPES);
546 private IDocumentPartitioner createJSPPartitioner() {
547 return new PHPDocumentPartitioner(getJSPPartitionScanner(), getPHPPartitionScanner());
548 // return new JSPDocumentPartitioner(getJSPPartitionScanner(), jspScriptScanner);
554 // public IPartitionTokenScanner getJSPScriptScanner() {
555 // return jspScriptScanner;
557 private IDocumentPartitioner createSmartyPartitioner() {
558 return new DefaultPartitioner(getSmartyPartitionScanner(), XMLTextTools.TYPES);
561 private IDocumentPartitioner createXMLPartitioner() {
562 // return new DefaultPartitioner(getXMLPartitionScanner(), XMLTextTools.TYPES);
563 return xmlTextTools.createXMLPartitioner();
566 // private IDocumentPartitioner createCSSPartitioner() {
567 // return new DefaultPartitioner(getHTMLPartitionScanner(), XMLTextTools.TYPES);
571 * Return a scanner for creating html partitions.
573 // private static XMLPartitionScanner getHTMLPartitionScanner() {
574 // // if (HTML_PARTITION_SCANNER == null)
575 // // HTML_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.HTML_FILE);
576 // // return HTML_PARTITION_SCANNER;^
577 // if (HTML_PARTITION_SCANNER == null)
578 // HTML_PARTITION_SCANNER = new XMLPartitionScanner(false);
579 // return HTML_PARTITION_SCANNER;
582 * Return a scanner for creating php partitions.
584 private FastJavaPartitionScanner getPHPPartitionScanner() {
585 // if (PHP_PARTITION_SCANNER == null)
586 // PHP_PARTITION_SCANNER = new FastJavaPartitionScanner(); //new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
587 // return PHP_PARTITION_SCANNER;
588 return fPartitionScanner;
592 * Returns a scanner which is configured to scan plain text in JSP.
594 * @return a JSP text scanner
596 // public RuleBasedScanner getJSPTextScanner() {
597 // return jspTextScanner;
600 * Returns a scanner which is configured to scan plain text in JSP.
602 * @return a JSP text scanner
604 // public RuleBasedScanner getJSPBracketScanner() {
605 // return jspBracketScanner;
608 * Return a scanner for creating smarty partitions.
610 private static HTMLPartitionScanner getSmartyPartitionScanner() {
611 if (SMARTY_PARTITION_SCANNER == null)
612 SMARTY_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.SMARTY_FILE);
613 return SMARTY_PARTITION_SCANNER;
617 * Return a scanner for creating xml partitions.
619 private static XMLPartitionScanner getXMLPartitionScanner() {
620 // if (XML_PARTITION_SCANNER == null)
621 // XML_PARTITION_SCANNER = new HTMLPartitionScanner(IPHPPartitions.XML_FILE);
622 // return XML_PARTITION_SCANNER;
623 if (XML_PARTITION_SCANNER == null)
624 XML_PARTITION_SCANNER = new XMLPartitionScanner(false);
625 return XML_PARTITION_SCANNER;
628 private PHPPartitionScanner getJSPPartitionScanner() {
629 if (jspPartitionScanner == null)
630 jspPartitionScanner = new PHPPartitionScanner();
631 return jspPartitionScanner;
635 * Sets up the Java document partitioner for the given document for the default partitioning.
638 * the document to be set up
641 public void setupJavaDocumentPartitioner(IDocument document) {
642 setupJavaDocumentPartitioner(document, IDocumentExtension3.DEFAULT_PARTITIONING);
646 * Sets up the Java document partitioner for the given document for the given partitioning.
649 * the document to be set up
650 * @param partitioning
651 * the document partitioning
654 public void setupJavaDocumentPartitioner(IDocument document, String partitioning) {
655 IDocumentPartitioner partitioner = createDocumentPartitioner();
656 if (document instanceof IDocumentExtension3) {
657 IDocumentExtension3 extension3 = (IDocumentExtension3) document;
658 extension3.setDocumentPartitioner(partitioning, partitioner);
660 document.setDocumentPartitioner(partitioner);
662 partitioner.connect(document);
666 * Returns this text tool's preference store.
668 * @return the preference store
671 protected IPreferenceStore getPreferenceStore() {
672 return fPreferenceStore;
676 * Returns this text tool's core preference store.
678 * @return the core preference store
681 protected Preferences getCorePreferenceStore() {
682 return fCorePreferenceStore;