1 package net.sourceforge.phpdt.ui.text;
4 * (c) Copyright IBM Corp. 2000, 2001.
8 import net.sourceforge.phpdt.internal.ui.text.IPHPPartitions;
9 import net.sourceforge.phpdt.internal.ui.text.JavaColorManager;
10 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCodeScanner;
11 import net.sourceforge.phpeclipse.IPreferenceConstants;
12 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCodeScanner;
13 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
14 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
15 import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner;
16 import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.Preferences;
20 import org.eclipse.jface.preference.IPreferenceStore;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentExtension3;
23 import org.eclipse.jface.text.IDocumentPartitioner;
24 import org.eclipse.jface.text.rules.DefaultPartitioner;
25 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
26 import org.eclipse.jface.text.rules.RuleBasedScanner;
27 import org.eclipse.jface.util.IPropertyChangeListener;
28 import org.eclipse.jface.util.PropertyChangeEvent;
29 import org.eclipse.ui.part.FileEditorInput;
31 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
32 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
33 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
34 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
35 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
36 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
39 * Tools required to configure a Java text viewer.
40 * The color manager and all scanner exist only one time, i.e.
41 * the same instances are returned to all clients. Thus, clients
44 * This class may be instantiated; it is not intended to be subclassed.
47 public class JavaTextTools {
49 private static PHPPartitionScanner HTML_PARTITION_SCANNER = null;
50 private static PHPPartitionScanner PHP_PARTITION_SCANNER = null;
51 private static PHPPartitionScanner SMARTY_PARTITION_SCANNER = null;
53 // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC, PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
54 private final static String[] TYPES =
56 IPHPPartitions.PHP_PARTITIONING,
57 IPHPPartitions.PHP_PHPDOC_COMMENT,
59 IPHPPartitions.HTML_MULTILINE_COMMENT,
60 IPHPPartitions.JAVASCRIPT,
62 IPHPPartitions.SMARTY,
63 IPHPPartitions.SMARTY_MULTILINE_COMMENT };
64 private static PHPPartitionScanner XML_PARTITION_SCANNER = null;
67 * This tools' preference listener.
69 private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
70 public void propertyChange(PropertyChangeEvent event) {
71 adaptToPreferenceChange(event);
73 public void propertyChange(Preferences.PropertyChangeEvent event) {
74 adaptToPreferenceChange(
75 new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue()));
79 /** The color manager */
80 private JavaColorManager fColorManager;
81 /** The PHP source code scanner */
82 private PHPCodeScanner fCodeScanner;
83 /** The PHP multiline comment scanner */
84 private SingleTokenPHPScanner fMultilineCommentScanner;
85 /** The Java singleline comment scanner */
86 private SingleTokenPHPScanner fSinglelineCommentScanner;
87 /** The Java string scanner */
88 private SingleTokenPHPScanner fStringScanner;
89 /** The PHPDoc scanner */
90 private PHPDocCodeScanner fPHPDocScanner;
91 /** The HTML scanner */
92 private HTMLCodeScanner fHTMLScanner;
93 /** The Smarty scanner */
94 private SmartyCodeScanner fSmartyScanner;
95 /** The SmartyDoc scanner */
96 private SmartyDocCodeScanner fSmartyDocScanner;
97 /** The Java partitions scanner */
98 private PHPPartitionScanner fPartitionScanner;
100 /** The preference store */
101 private IPreferenceStore fPreferenceStore;
103 * The core preference store.
106 private Preferences fCorePreferenceStore;
107 /** The preference change listener */
108 private PreferenceListener fPreferenceListener = new PreferenceListener();
111 * Creates a new Java text tools collection.
113 * @param store the preference store to initialize the text tools. The text tool
114 * instance installs a listener on the passed preference store to adapt itself to
115 * changes in the preference store. In general <code>PreferenceConstants.
116 * getPreferenceStore()</code> shoould be used to initialize the text tools.
118 * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
121 // public JavaTextTools(IPreferenceStore store) {
122 // fPreferenceStore = store;
123 // fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
125 // fColorManager = new JavaColorManager();
126 // fCodeScanner = new PHPCodeScanner(fColorManager, store);
127 // fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
128 // fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
129 // fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
130 // fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
131 // fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
132 // fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
133 // fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
134 //// fPartitionScanner = new FastJavaPartitionScanner();
135 // fPartitionScanner = new PHPPartitionScanner();
138 * Creates a new Java text tools collection.
139 * @param store the preference store to initialize the text tools. The text tool
140 * instance installs a listener on the passed preference store to adapt itself to
141 * changes in the preference store. In general <code>PreferenceConstants.
142 * getPreferenceStore()</code> should be used to initialize the text tools.
143 * @param coreStore optional preference store to initialize the text tools. The text tool
144 * instance installs a listener on the passed preference store to adapt itself to
145 * changes in the preference store.
146 * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
149 public JavaTextTools(IPreferenceStore store, Preferences coreStore) {
150 this(store, coreStore, true);
153 * Creates a new Java text tools collection.
155 * @param store the preference store to initialize the text tools. The text tool
156 * instance installs a listener on the passed preference store to adapt itself to
157 * changes in the preference store. In general <code>PreferenceConstants.
158 * getPreferenceStore()</code> shoould be used to initialize the text tools.
159 * @param coreStore optional preference store to initialize the text tools. The text tool
160 * instance installs a listener on the passed preference store to adapt itself to
161 * changes in the preference store.
162 * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
163 * automatically disposes all managed colors when the current display gets disposed
164 * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
165 * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
168 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
169 fPreferenceStore = store;
170 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
172 fCorePreferenceStore = coreStore;
173 if (fCorePreferenceStore != null)
174 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
176 fColorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
178 fCodeScanner = new PHPCodeScanner(fColorManager, store);
179 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
180 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
181 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
182 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
183 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
184 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
185 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
186 // fPartitionScanner = new FastJavaPartitionScanner();
187 fPartitionScanner = new PHPPartitionScanner();
191 * Disposes all the individual tools of this tools collection.
193 public void dispose() {
196 fMultilineCommentScanner = null;
197 fSinglelineCommentScanner = null;
198 fStringScanner = null;
199 fPHPDocScanner = null;
200 fPartitionScanner = null;
202 if (fColorManager != null) {
203 fColorManager.dispose();
204 fColorManager = null;
207 if (fPreferenceStore != null) {
208 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
209 fPreferenceStore = null;
211 if (fCorePreferenceStore != null) {
212 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
213 fCorePreferenceStore = null;
216 fPreferenceListener = null;
221 * Returns the color manager which is used to manage
222 * any Java-specific colors needed for such things like syntax highlighting.
224 * @return the color manager to be used for Java text viewers
226 public IColorManager getColorManager() {
227 return fColorManager;
231 * Returns a scanner which is configured to scan Java source code.
233 * @return a Java source code scanner
235 public RuleBasedScanner getCodeScanner() {
240 * Returns a scanner which is configured to scan Java multiline comments.
242 * @return a Java multiline comment scanner
246 public RuleBasedScanner getMultilineCommentScanner() {
247 return fMultilineCommentScanner;
251 * Returns a scanner which is configured to scan HTML code.
253 * @return a HTML scanner
257 public RuleBasedScanner getHTMLScanner() {
262 * Returns a scanner which is configured to scan Smarty code.
264 * @return a Smarty scanner
268 public RuleBasedScanner getSmartyScanner() {
269 return fSmartyScanner;
273 * Returns a scanner which is configured to scan Smarty code.
275 * @return a Smarty scanner
279 public RuleBasedScanner getSmartyDocScanner() {
280 return fSmartyDocScanner;
283 * Returns a scanner which is configured to scan Java singleline comments.
285 * @return a Java singleline comment scanner
289 public RuleBasedScanner getSinglelineCommentScanner() {
290 return fSinglelineCommentScanner;
294 * Returns a scanner which is configured to scan Java strings.
296 * @return a Java string scanner
300 public RuleBasedScanner getStringScanner() {
301 return fStringScanner;
305 * Returns a scanner which is configured to scan JavaDoc compliant comments.
306 * Notes that the start sequence "/**" and the corresponding end sequence
307 * are part of the JavaDoc comment.
309 * @return a JavaDoc scanner
311 public RuleBasedScanner getJavaDocScanner() {
312 return fPHPDocScanner;
316 * Returns a scanner which is configured to scan
317 * Java-specific partitions, which are multi-line comments,
318 * JavaDoc comments, and regular Java source code.
320 * @return a Java partition scanner
322 public IPartitionTokenScanner getPartitionScanner() {
323 return fPartitionScanner;
327 * Factory method for creating a PHP-specific document partitioner
328 * using this object's partitions scanner. This method is a
329 * convenience method.
331 * @return a newly created Java document partitioner
333 public IDocumentPartitioner createDocumentPartitioner() {
334 return createDocumentPartitioner(".php");
338 * Factory method for creating a PHP-specific document partitioner
339 * using this object's partitions scanner. This method is a
340 * convenience method.
342 * @return a newly created Java document partitioner
344 public IDocumentPartitioner createDocumentPartitioner(String extension) {
348 // FastJavaPartitionScanner.JAVA_DOC,
349 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
350 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
351 // FastJavaPartitionScanner.JAVA_STRING };
353 // return new DefaultPartitioner(getPartitionScanner(), types);
354 IDocumentPartitioner partitioner = null;
355 // System.out.println(extension);
356 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
358 partitioner = createHTMLPartitioner();
359 } else if (extension.equalsIgnoreCase(".xml")) {
361 partitioner = createXMLPartitioner();
362 } else if (extension.equalsIgnoreCase(".js")) {
364 partitioner = createJavaScriptPartitioner();
365 } else if (extension.equalsIgnoreCase(".css")) {
366 // cascading style sheets
367 partitioner = createCSSPartitioner();
368 } else if (extension.equalsIgnoreCase(".tpl")) {
370 partitioner = createSmartyPartitioner();
371 } else if (extension.equalsIgnoreCase(".inc")) {
372 // php include files ?
373 partitioner = createIncludePartitioner();
376 if (partitioner == null) {
377 partitioner = createPHPPartitioner();
383 * Sets up the Java document partitioner for the given document for the default partitioning.
385 * @param document the document to be set up
388 public void setupJavaDocumentPartitioner(IDocument document) {
389 setupJavaDocumentPartitioner(document, IDocumentExtension3.DEFAULT_PARTITIONING,null);
392 * Sets up the Java document partitioner for the given document for the given partitioning.
393 * @param document the document to be set up
394 * @param partitioning the document partitioning
395 * @param element TODO
399 public void setupJavaDocumentPartitioner(IDocument document, String partitioning, Object element) {
400 IDocumentPartitioner partitioner = createDocumentPartitioner(".php");
402 // if (document instanceof IDocumentExtension3) {
403 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
404 // extension3.setDocumentPartitioner(partitioning, partitioner);
406 document.setDocumentPartitioner(partitioner);
408 partitioner.connect(document);
410 public void setupHTMLDocumentPartitioner(IDocument document, String partitioning, Object element) {
411 IDocumentPartitioner partitioner = createDocumentPartitioner(".html");
413 // if (document instanceof IDocumentExtension3) {
414 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
415 // extension3.setDocumentPartitioner(partitioning, partitioner);
417 document.setDocumentPartitioner(partitioner);
419 partitioner.connect(document);
421 public void setupSmartyDocumentPartitioner(IDocument document, String partitioning, Object element) {
422 IDocumentPartitioner partitioner = createDocumentPartitioner(".tpl");
424 // if (document instanceof IDocumentExtension3) {
425 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
426 // extension3.setDocumentPartitioner(partitioning, partitioner);
428 document.setDocumentPartitioner(partitioner);
430 partitioner.connect(document);
433 * Returns the names of the document position categories used by the document
434 * partitioners created by this object to manage their partition information.
435 * If the partitioners don't use document position categories, the returned
436 * result is <code>null</code>.
438 * @return the partition managing position categories or <code>null</code>
441 public String[] getPartitionManagingPositionCategories() {
442 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
446 * Determines whether the preference change encoded by the given event
447 * changes the behavior of one its contained components.
449 * @param event the event to be investigated
450 * @return <code>true</code> if event causes a behavioral change
454 public boolean affectsBehavior(PropertyChangeEvent event) {
455 return fCodeScanner.affectsBehavior(event)
456 || fMultilineCommentScanner.affectsBehavior(event)
457 || fSinglelineCommentScanner.affectsBehavior(event)
458 || fStringScanner.affectsBehavior(event)
459 || fPHPDocScanner.affectsBehavior(event);
463 * Adapts the behavior of the contained components to the change
464 * encoded in the given event.
466 * @param event the event to which to adapt
469 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
470 if (fCodeScanner.affectsBehavior(event))
471 fCodeScanner.adaptToPreferenceChange(event);
472 if (fMultilineCommentScanner.affectsBehavior(event))
473 fMultilineCommentScanner.adaptToPreferenceChange(event);
474 if (fSinglelineCommentScanner.affectsBehavior(event))
475 fSinglelineCommentScanner.adaptToPreferenceChange(event);
476 if (fStringScanner.affectsBehavior(event))
477 fStringScanner.adaptToPreferenceChange(event);
478 if (fPHPDocScanner.affectsBehavior(event))
479 fPHPDocScanner.adaptToPreferenceChange(event);
480 if (fHTMLScanner.affectsBehavior(event))
481 fHTMLScanner.adaptToPreferenceChange(event);
482 if (fSmartyScanner.affectsBehavior(event))
483 fSmartyScanner.adaptToPreferenceChange(event);
484 if (fSmartyDocScanner.affectsBehavior(event))
485 fSmartyDocScanner.adaptToPreferenceChange(event);
489 * Return a partitioner for .html files.
491 private static IDocumentPartitioner createHTMLPartitioner() {
492 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
495 private static IDocumentPartitioner createIncludePartitioner() {
496 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
499 private static IDocumentPartitioner createJavaScriptPartitioner() {
500 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
504 * Return a partitioner for .php files.
506 private static IDocumentPartitioner createPHPPartitioner() {
507 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
510 private static IDocumentPartitioner createSmartyPartitioner() {
511 return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
514 private static IDocumentPartitioner createXMLPartitioner() {
515 return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
518 private static IDocumentPartitioner createCSSPartitioner() {
519 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
522 * Return a scanner for creating html partitions.
524 private static PHPPartitionScanner getHTMLPartitionScanner() {
525 if (HTML_PARTITION_SCANNER == null)
526 HTML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.HTML_FILE);
527 return HTML_PARTITION_SCANNER;
530 * Return a scanner for creating php partitions.
532 private static PHPPartitionScanner getPHPPartitionScanner() {
533 if (PHP_PARTITION_SCANNER == null)
534 PHP_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
535 return PHP_PARTITION_SCANNER;
539 * Return a scanner for creating smarty partitions.
541 private static PHPPartitionScanner getSmartyPartitionScanner() {
542 if (SMARTY_PARTITION_SCANNER == null)
543 SMARTY_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.SMARTY_FILE);
544 return SMARTY_PARTITION_SCANNER;
548 * Return a scanner for creating xml partitions.
550 private static PHPPartitionScanner getXMLPartitionScanner() {
551 if (XML_PARTITION_SCANNER == null)
552 XML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.XML_FILE);
553 return XML_PARTITION_SCANNER;