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;
51 private static PHPPartitionScanner PHP_PARTITION_SCANNER = null;
52 private static PHPPartitionScanner SMARTY_PARTITION_SCANNER = null;
54 // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC, PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
55 private final static String[] TYPES =
57 IPHPPartitions.PHP_PARTITIONING,
58 IPHPPartitions.PHP_PHPDOC_COMMENT,
60 IPHPPartitions.HTML_MULTILINE_COMMENT,
61 IPHPPartitions.JAVASCRIPT,
63 IPHPPartitions.SMARTY,
64 IPHPPartitions.SMARTY_MULTILINE_COMMENT };
65 private static PHPPartitionScanner XML_PARTITION_SCANNER = null;
68 * This tools' preference listener.
70 private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
71 public void propertyChange(PropertyChangeEvent event) {
72 adaptToPreferenceChange(event);
74 public void propertyChange(Preferences.PropertyChangeEvent event) {
75 adaptToPreferenceChange(
76 new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue()));
80 /** The color manager */
81 private JavaColorManager fColorManager;
82 /** The PHP source code scanner */
83 private PHPCodeScanner fCodeScanner;
84 /** The PHP multiline comment scanner */
85 private SingleTokenPHPScanner fMultilineCommentScanner;
86 /** The Java singleline comment scanner */
87 private SingleTokenPHPScanner fSinglelineCommentScanner;
88 /** The Java string scanner */
89 private SingleTokenPHPScanner fStringScanner;
90 /** The PHPDoc scanner */
91 private PHPDocCodeScanner fPHPDocScanner;
92 /** The HTML scanner */
93 private HTMLCodeScanner fHTMLScanner;
94 /** The Smarty scanner */
95 private SmartyCodeScanner fSmartyScanner;
96 /** The SmartyDoc scanner */
97 private SmartyDocCodeScanner fSmartyDocScanner;
98 /** The Java partitions scanner */
99 private PHPPartitionScanner fPartitionScanner;
101 /** The preference store */
102 private IPreferenceStore fPreferenceStore;
104 * The core preference store.
107 private Preferences fCorePreferenceStore;
108 /** The preference change listener */
109 private PreferenceListener fPreferenceListener = new PreferenceListener();
112 * Creates a new Java text tools collection.
114 * @param store the preference store to initialize the text tools. The text tool
115 * instance installs a listener on the passed preference store to adapt itself to
116 * changes in the preference store. In general <code>PreferenceConstants.
117 * getPreferenceStore()</code> shoould be used to initialize the text tools.
119 * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
122 public JavaTextTools(IPreferenceStore store) {
123 fPreferenceStore = store;
124 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
126 fColorManager = new JavaColorManager();
127 fCodeScanner = new PHPCodeScanner(fColorManager, store);
128 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
129 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
130 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
131 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
132 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
133 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
134 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
135 // fPartitionScanner = new FastJavaPartitionScanner();
136 fPartitionScanner = new PHPPartitionScanner();
140 * Creates a new Java text tools collection.
142 * @param store the preference store to initialize the text tools. The text tool
143 * instance installs a listener on the passed preference store to adapt itself to
144 * changes in the preference store. In general <code>PreferenceConstants.
145 * getPreferenceStore()</code> shoould be used to initialize the text tools.
146 * @param coreStore optional preference store to initialize the text tools. The text tool
147 * instance installs a listener on the passed preference store to adapt itself to
148 * changes in the preference store.
149 * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
150 * automatically disposes all managed colors when the current display gets disposed
151 * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
152 * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
155 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
156 fPreferenceStore = store;
157 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
159 fCorePreferenceStore = coreStore;
160 if (fCorePreferenceStore != null)
161 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
163 fColorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
164 fCodeScanner = new PHPCodeScanner(fColorManager, store);
165 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
166 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
167 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
168 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
169 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
170 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
171 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
172 // fPartitionScanner = new FastJavaPartitionScanner();
173 fPartitionScanner = new PHPPartitionScanner();
177 * Disposes all the individual tools of this tools collection.
179 public void dispose() {
182 fMultilineCommentScanner = null;
183 fSinglelineCommentScanner = null;
184 fStringScanner = null;
185 fPHPDocScanner = null;
186 fPartitionScanner = null;
188 if (fColorManager != null) {
189 fColorManager.dispose();
190 fColorManager = null;
193 if (fPreferenceStore != null) {
194 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
195 fPreferenceStore = null;
197 if (fCorePreferenceStore != null) {
198 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
199 fCorePreferenceStore = null;
202 fPreferenceListener = null;
207 * Returns the color manager which is used to manage
208 * any Java-specific colors needed for such things like syntax highlighting.
210 * @return the color manager to be used for Java text viewers
212 public IColorManager getColorManager() {
213 return fColorManager;
217 * Returns a scanner which is configured to scan Java source code.
219 * @return a Java source code scanner
221 public RuleBasedScanner getCodeScanner() {
226 * Returns a scanner which is configured to scan Java multiline comments.
228 * @return a Java multiline comment scanner
232 public RuleBasedScanner getMultilineCommentScanner() {
233 return fMultilineCommentScanner;
237 * Returns a scanner which is configured to scan HTML code.
239 * @return a HTML scanner
243 public RuleBasedScanner getHTMLScanner() {
248 * Returns a scanner which is configured to scan Smarty code.
250 * @return a Smarty scanner
254 public RuleBasedScanner getSmartyScanner() {
255 return fSmartyScanner;
259 * Returns a scanner which is configured to scan Smarty code.
261 * @return a Smarty scanner
265 public RuleBasedScanner getSmartyDocScanner() {
266 return fSmartyDocScanner;
269 * Returns a scanner which is configured to scan Java singleline comments.
271 * @return a Java singleline comment scanner
275 public RuleBasedScanner getSinglelineCommentScanner() {
276 return fSinglelineCommentScanner;
280 * Returns a scanner which is configured to scan Java strings.
282 * @return a Java string scanner
286 public RuleBasedScanner getStringScanner() {
287 return fStringScanner;
291 * Returns a scanner which is configured to scan JavaDoc compliant comments.
292 * Notes that the start sequence "/**" and the corresponding end sequence
293 * are part of the JavaDoc comment.
295 * @return a JavaDoc scanner
297 public RuleBasedScanner getJavaDocScanner() {
298 return fPHPDocScanner;
302 * Returns a scanner which is configured to scan
303 * Java-specific partitions, which are multi-line comments,
304 * JavaDoc comments, and regular Java source code.
306 * @return a Java partition scanner
308 public IPartitionTokenScanner getPartitionScanner() {
309 return fPartitionScanner;
313 * Factory method for creating a PHP-specific document partitioner
314 * using this object's partitions scanner. This method is a
315 * convenience method.
317 * @return a newly created Java document partitioner
319 public IDocumentPartitioner createDocumentPartitioner() {
320 return createDocumentPartitioner(".php");
324 * Factory method for creating a PHP-specific document partitioner
325 * using this object's partitions scanner. This method is a
326 * convenience method.
328 * @return a newly created Java document partitioner
330 public IDocumentPartitioner createDocumentPartitioner(String extension) {
334 // FastJavaPartitionScanner.JAVA_DOC,
335 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
336 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
337 // FastJavaPartitionScanner.JAVA_STRING };
339 // return new DefaultPartitioner(getPartitionScanner(), types);
340 IDocumentPartitioner partitioner = null;
341 // System.out.println(extension);
342 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
344 partitioner = createHTMLPartitioner();
345 } else if (extension.equalsIgnoreCase(".xml")) {
347 partitioner = createXMLPartitioner();
348 } else if (extension.equalsIgnoreCase(".js")) {
350 partitioner = createJavaScriptPartitioner();
351 } else if (extension.equalsIgnoreCase(".css")) {
352 // cascading style sheets
353 partitioner = createCSSPartitioner();
354 } else if (extension.equalsIgnoreCase(".tpl")) {
356 partitioner = createSmartyPartitioner();
357 } else if (extension.equalsIgnoreCase(".inc")) {
358 // php include files ?
359 partitioner = createIncludePartitioner();
362 if (partitioner == null) {
363 partitioner = createPHPPartitioner();
369 * Sets up the Java document partitioner for the given document for the default partitioning.
371 * @param document the document to be set up
374 public void setupJavaDocumentPartitioner(IDocument document) {
375 setupJavaDocumentPartitioner(document, IDocumentExtension3.DEFAULT_PARTITIONING,null);
378 * Sets up the Java document partitioner for the given document for the given partitioning.
379 * @param document the document to be set up
380 * @param partitioning the document partitioning
381 * @param element TODO
385 public void setupJavaDocumentPartitioner(IDocument document, String partitioning, Object element) {
386 IDocumentPartitioner partitioner = createDocumentPartitioner(".php");
388 // if (document instanceof IDocumentExtension3) {
389 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
390 // extension3.setDocumentPartitioner(partitioning, partitioner);
392 document.setDocumentPartitioner(partitioner);
394 partitioner.connect(document);
396 public void setupHTMLDocumentPartitioner(IDocument document, String partitioning, Object element) {
397 IDocumentPartitioner partitioner = createDocumentPartitioner(".html");
399 // if (document instanceof IDocumentExtension3) {
400 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
401 // extension3.setDocumentPartitioner(partitioning, partitioner);
403 document.setDocumentPartitioner(partitioner);
405 partitioner.connect(document);
407 public void setupSmartyDocumentPartitioner(IDocument document, String partitioning, Object element) {
408 IDocumentPartitioner partitioner = createDocumentPartitioner(".tpl");
410 // if (document instanceof IDocumentExtension3) {
411 // IDocumentExtension3 extension3= (IDocumentExtension3) document;
412 // extension3.setDocumentPartitioner(partitioning, partitioner);
414 document.setDocumentPartitioner(partitioner);
416 partitioner.connect(document);
419 * Returns the names of the document position categories used by the document
420 * partitioners created by this object to manage their partition information.
421 * If the partitioners don't use document position categories, the returned
422 * result is <code>null</code>.
424 * @return the partition managing position categories or <code>null</code>
427 public String[] getPartitionManagingPositionCategories() {
428 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
432 * Determines whether the preference change encoded by the given event
433 * changes the behavior of one its contained components.
435 * @param event the event to be investigated
436 * @return <code>true</code> if event causes a behavioral change
440 public boolean affectsBehavior(PropertyChangeEvent event) {
441 return fCodeScanner.affectsBehavior(event)
442 || fMultilineCommentScanner.affectsBehavior(event)
443 || fSinglelineCommentScanner.affectsBehavior(event)
444 || fStringScanner.affectsBehavior(event)
445 || fPHPDocScanner.affectsBehavior(event);
449 * Adapts the behavior of the contained components to the change
450 * encoded in the given event.
452 * @param event the event to which to adapt
455 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
456 if (fCodeScanner.affectsBehavior(event))
457 fCodeScanner.adaptToPreferenceChange(event);
458 if (fMultilineCommentScanner.affectsBehavior(event))
459 fMultilineCommentScanner.adaptToPreferenceChange(event);
460 if (fSinglelineCommentScanner.affectsBehavior(event))
461 fSinglelineCommentScanner.adaptToPreferenceChange(event);
462 if (fStringScanner.affectsBehavior(event))
463 fStringScanner.adaptToPreferenceChange(event);
464 if (fPHPDocScanner.affectsBehavior(event))
465 fPHPDocScanner.adaptToPreferenceChange(event);
469 * Return a partitioner for .html files.
471 private static IDocumentPartitioner createHTMLPartitioner() {
472 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
475 private static IDocumentPartitioner createIncludePartitioner() {
476 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
479 private static IDocumentPartitioner createJavaScriptPartitioner() {
480 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
484 * Return a partitioner for .php files.
486 private static IDocumentPartitioner createPHPPartitioner() {
487 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
490 private static IDocumentPartitioner createSmartyPartitioner() {
491 return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
494 private static IDocumentPartitioner createXMLPartitioner() {
495 return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
498 private static IDocumentPartitioner createCSSPartitioner() {
499 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
502 * Return a scanner for creating html partitions.
504 private static PHPPartitionScanner getHTMLPartitionScanner() {
505 if (HTML_PARTITION_SCANNER == null)
506 HTML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.HTML_FILE);
507 return HTML_PARTITION_SCANNER;
510 * Return a scanner for creating php partitions.
512 private static PHPPartitionScanner getPHPPartitionScanner() {
513 if (PHP_PARTITION_SCANNER == null)
514 PHP_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
515 return PHP_PARTITION_SCANNER;
519 * Return a scanner for creating smarty partitions.
521 private static PHPPartitionScanner getSmartyPartitionScanner() {
522 if (SMARTY_PARTITION_SCANNER == null)
523 SMARTY_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.SMARTY_FILE);
524 return SMARTY_PARTITION_SCANNER;
528 * Return a scanner for creating xml partitions.
530 private static PHPPartitionScanner getXMLPartitionScanner() {
531 if (XML_PARTITION_SCANNER == null)
532 XML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.XML_FILE);
533 return XML_PARTITION_SCANNER;