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.HTMLCodeScanner;
14 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
15 import net.sourceforge.phpeclipse.phpeditor.php.PHPPartitionScanner;
16 import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner;
17 import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner;
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;
30 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
31 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
32 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
33 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
34 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
35 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
38 * Tools required to configure a Java text viewer.
39 * The color manager and all scanner exist only one time, i.e.
40 * the same instances are returned to all clients. Thus, clients
43 * This class may be instantiated; it is not intended to be subclassed.
46 public class JavaTextTools {
48 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();
139 * Creates a new Java text tools collection.
141 * @param store the preference store to initialize the text tools. The text tool
142 * instance installs a listener on the passed preference store to adapt itself to
143 * changes in the preference store. In general <code>PreferenceConstants.
144 * getPreferenceStore()</code> shoould be used to initialize the text tools.
145 * @param coreStore optional preference store to initialize the text tools. The text tool
146 * instance installs a listener on the passed preference store to adapt itself to
147 * changes in the preference store.
148 * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
149 * automatically disposes all managed colors when the current display gets disposed
150 * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
151 * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
154 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
155 fPreferenceStore = store;
156 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
158 fCorePreferenceStore = coreStore;
159 if (fCorePreferenceStore != null)
160 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
162 fColorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
163 fCodeScanner = new PHPCodeScanner(fColorManager, store);
164 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
165 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
166 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
167 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
168 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
169 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
170 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
171 // fPartitionScanner = new FastJavaPartitionScanner();
172 fPartitionScanner = new PHPPartitionScanner();
176 * Disposes all the individual tools of this tools collection.
178 public void dispose() {
181 fMultilineCommentScanner = null;
182 fSinglelineCommentScanner = null;
183 fStringScanner = null;
184 fPHPDocScanner = null;
185 fPartitionScanner = null;
187 if (fColorManager != null) {
188 fColorManager.dispose();
189 fColorManager = null;
192 if (fPreferenceStore != null) {
193 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
194 fPreferenceStore = null;
196 if (fCorePreferenceStore != null) {
197 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
198 fCorePreferenceStore = null;
201 fPreferenceListener = null;
206 * Returns the color manager which is used to manage
207 * any Java-specific colors needed for such things like syntax highlighting.
209 * @return the color manager to be used for Java text viewers
211 public IColorManager getColorManager() {
212 return fColorManager;
216 * Returns a scanner which is configured to scan Java source code.
218 * @return a Java source code scanner
220 public RuleBasedScanner getCodeScanner() {
225 * Returns a scanner which is configured to scan Java multiline comments.
227 * @return a Java multiline comment scanner
231 public RuleBasedScanner getMultilineCommentScanner() {
232 return fMultilineCommentScanner;
236 * Returns a scanner which is configured to scan HTML code.
238 * @return a HTML scanner
242 public RuleBasedScanner getHTMLScanner() {
247 * Returns a scanner which is configured to scan Smarty code.
249 * @return a Smarty scanner
253 public RuleBasedScanner getSmartyScanner() {
254 return fSmartyScanner;
258 * Returns a scanner which is configured to scan Smarty code.
260 * @return a Smarty scanner
264 public RuleBasedScanner getSmartyDocScanner() {
265 return fSmartyDocScanner;
268 * Returns a scanner which is configured to scan Java singleline comments.
270 * @return a Java singleline comment scanner
274 public RuleBasedScanner getSinglelineCommentScanner() {
275 return fSinglelineCommentScanner;
279 * Returns a scanner which is configured to scan Java strings.
281 * @return a Java string scanner
285 public RuleBasedScanner getStringScanner() {
286 return fStringScanner;
290 * Returns a scanner which is configured to scan JavaDoc compliant comments.
291 * Notes that the start sequence "/**" and the corresponding end sequence
292 * are part of the JavaDoc comment.
294 * @return a JavaDoc scanner
296 public RuleBasedScanner getJavaDocScanner() {
297 return fPHPDocScanner;
301 * Returns a scanner which is configured to scan
302 * Java-specific partitions, which are multi-line comments,
303 * JavaDoc comments, and regular Java source code.
305 * @return a Java partition scanner
307 public IPartitionTokenScanner getPartitionScanner() {
308 return fPartitionScanner;
312 * Factory method for creating a PHP-specific document partitioner
313 * using this object's partitions scanner. This method is a
314 * convenience method.
316 * @return a newly created Java document partitioner
318 public IDocumentPartitioner createDocumentPartitioner() {
319 return createDocumentPartitioner(".php");
323 * Factory method for creating a PHP-specific document partitioner
324 * using this object's partitions scanner. This method is a
325 * convenience method.
327 * @return a newly created Java document partitioner
329 public IDocumentPartitioner createDocumentPartitioner(String extension) {
333 // FastJavaPartitionScanner.JAVA_DOC,
334 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
335 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
336 // FastJavaPartitionScanner.JAVA_STRING };
338 // return new DefaultPartitioner(getPartitionScanner(), types);
339 IDocumentPartitioner partitioner = null;
340 // System.out.println(extension);
341 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
343 partitioner = createHTMLPartitioner();
344 } else if (extension.equalsIgnoreCase(".xml")) {
346 partitioner = createXMLPartitioner();
347 } else if (extension.equalsIgnoreCase(".js")) {
349 partitioner = createJavaScriptPartitioner();
350 } else if (extension.equalsIgnoreCase(".css")) {
351 // cascading style sheets
352 partitioner = createCSSPartitioner();
353 } else if (extension.equalsIgnoreCase(".tpl")) {
355 partitioner = createSmartyPartitioner();
356 } else if (extension.equalsIgnoreCase(".inc")) {
357 // php include files ?
358 partitioner = createIncludePartitioner();
361 if (partitioner == null) {
362 partitioner = createPHPPartitioner();
369 * Sets up the Java document partitioner for the given document for the given partitioning.
371 * @param document the document to be set up
372 * @param partitioning the document partitioning
375 public void setupJavaDocumentPartitioner(IDocument document, String partitioning) {
376 IDocumentPartitioner partitioner= createDocumentPartitioner();
377 if (document instanceof IDocumentExtension3) {
378 IDocumentExtension3 extension3= (IDocumentExtension3) document;
379 extension3.setDocumentPartitioner(partitioning, partitioner);
381 document.setDocumentPartitioner(partitioner);
383 partitioner.connect(document);
387 * Returns the names of the document position categories used by the document
388 * partitioners created by this object to manage their partition information.
389 * If the partitioners don't use document position categories, the returned
390 * result is <code>null</code>.
392 * @return the partition managing position categories or <code>null</code>
395 public String[] getPartitionManagingPositionCategories() {
396 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
400 * Determines whether the preference change encoded by the given event
401 * changes the behavior of one its contained components.
403 * @param event the event to be investigated
404 * @return <code>true</code> if event causes a behavioral change
408 public boolean affectsBehavior(PropertyChangeEvent event) {
409 return fCodeScanner.affectsBehavior(event)
410 || fMultilineCommentScanner.affectsBehavior(event)
411 || fSinglelineCommentScanner.affectsBehavior(event)
412 || fStringScanner.affectsBehavior(event)
413 || fPHPDocScanner.affectsBehavior(event);
417 * Adapts the behavior of the contained components to the change
418 * encoded in the given event.
420 * @param event the event to which to adapt
423 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
424 if (fCodeScanner.affectsBehavior(event))
425 fCodeScanner.adaptToPreferenceChange(event);
426 if (fMultilineCommentScanner.affectsBehavior(event))
427 fMultilineCommentScanner.adaptToPreferenceChange(event);
428 if (fSinglelineCommentScanner.affectsBehavior(event))
429 fSinglelineCommentScanner.adaptToPreferenceChange(event);
430 if (fStringScanner.affectsBehavior(event))
431 fStringScanner.adaptToPreferenceChange(event);
432 if (fPHPDocScanner.affectsBehavior(event))
433 fPHPDocScanner.adaptToPreferenceChange(event);
437 * Return a partitioner for .html files.
439 private static IDocumentPartitioner createHTMLPartitioner() {
440 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
443 private static IDocumentPartitioner createIncludePartitioner() {
444 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
447 private static IDocumentPartitioner createJavaScriptPartitioner() {
448 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
452 * Return a partitioner for .php files.
454 private static IDocumentPartitioner createPHPPartitioner() {
455 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
458 private static IDocumentPartitioner createSmartyPartitioner() {
459 return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
462 private static IDocumentPartitioner createXMLPartitioner() {
463 return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
466 private static IDocumentPartitioner createCSSPartitioner() {
467 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
470 * Return a scanner for creating html partitions.
472 private static PHPPartitionScanner getHTMLPartitionScanner() {
473 if (HTML_PARTITION_SCANNER == null)
474 HTML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.HTML_FILE);
475 return HTML_PARTITION_SCANNER;
478 * Return a scanner for creating php partitions.
480 private static PHPPartitionScanner getPHPPartitionScanner() {
481 if (PHP_PARTITION_SCANNER == null)
482 PHP_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
483 return PHP_PARTITION_SCANNER;
487 * Return a scanner for creating smarty partitions.
489 private static PHPPartitionScanner getSmartyPartitionScanner() {
490 if (SMARTY_PARTITION_SCANNER == null)
491 SMARTY_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.SMARTY_FILE);
492 return SMARTY_PARTITION_SCANNER;
496 * Return a scanner for creating xml partitions.
498 private static PHPPartitionScanner getXMLPartitionScanner() {
499 if (XML_PARTITION_SCANNER == null)
500 XML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.XML_FILE);
501 return XML_PARTITION_SCANNER;