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_MULTILINE_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 FastJavaPartitionScanner 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();
138 * Creates a new Java text tools collection.
140 * @param store the preference store to initialize the text tools. The text tool
141 * instance installs a listener on the passed preference store to adapt itself to
142 * changes in the preference store. In general <code>PreferenceConstants.
143 * getPreferenceStore()</code> shoould be used to initialize the text tools.
144 * @param coreStore optional preference store to initialize the text tools. The text tool
145 * instance installs a listener on the passed preference store to adapt itself to
146 * changes in the preference store.
147 * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
148 * automatically disposes all managed colors when the current display gets disposed
149 * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
150 * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
153 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
154 fPreferenceStore = store;
155 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
157 fCorePreferenceStore = coreStore;
158 if (fCorePreferenceStore != null)
159 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
161 fColorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
162 fCodeScanner = new PHPCodeScanner(fColorManager, store);
163 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
164 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
165 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
166 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
167 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
168 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
169 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
170 fPartitionScanner = new FastJavaPartitionScanner();
174 * Disposes all the individual tools of this tools collection.
176 public void dispose() {
179 fMultilineCommentScanner = null;
180 fSinglelineCommentScanner = null;
181 fStringScanner = null;
182 fPHPDocScanner = null;
183 fPartitionScanner = null;
185 if (fColorManager != null) {
186 fColorManager.dispose();
187 fColorManager = null;
190 if (fPreferenceStore != null) {
191 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
192 fPreferenceStore = null;
194 if (fCorePreferenceStore != null) {
195 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
196 fCorePreferenceStore = null;
199 fPreferenceListener = null;
204 * Returns the color manager which is used to manage
205 * any Java-specific colors needed for such things like syntax highlighting.
207 * @return the color manager to be used for Java text viewers
209 public IColorManager getColorManager() {
210 return fColorManager;
214 * Returns a scanner which is configured to scan Java source code.
216 * @return a Java source code scanner
218 public RuleBasedScanner getCodeScanner() {
223 * Returns a scanner which is configured to scan Java multiline comments.
225 * @return a Java multiline comment scanner
229 public RuleBasedScanner getMultilineCommentScanner() {
230 return fMultilineCommentScanner;
234 * Returns a scanner which is configured to scan HTML code.
236 * @return a HTML scanner
240 public RuleBasedScanner getHTMLScanner() {
245 * Returns a scanner which is configured to scan Smarty code.
247 * @return a Smarty scanner
251 public RuleBasedScanner getSmartyScanner() {
252 return fSmartyScanner;
256 * Returns a scanner which is configured to scan Smarty code.
258 * @return a Smarty scanner
262 public RuleBasedScanner getSmartyDocScanner() {
263 return fSmartyDocScanner;
266 * Returns a scanner which is configured to scan Java singleline comments.
268 * @return a Java singleline comment scanner
272 public RuleBasedScanner getSinglelineCommentScanner() {
273 return fSinglelineCommentScanner;
277 * Returns a scanner which is configured to scan Java strings.
279 * @return a Java string scanner
283 public RuleBasedScanner getStringScanner() {
284 return fStringScanner;
288 * Returns a scanner which is configured to scan JavaDoc compliant comments.
289 * Notes that the start sequence "/**" and the corresponding end sequence
290 * are part of the JavaDoc comment.
292 * @return a JavaDoc scanner
294 public RuleBasedScanner getJavaDocScanner() {
295 return fPHPDocScanner;
299 * Returns a scanner which is configured to scan
300 * Java-specific partitions, which are multi-line comments,
301 * JavaDoc comments, and regular Java source code.
303 * @return a Java partition scanner
305 public IPartitionTokenScanner getPartitionScanner() {
306 return fPartitionScanner;
310 * Factory method for creating a PHP-specific document partitioner
311 * using this object's partitions scanner. This method is a
312 * convenience method.
314 * @return a newly created Java document partitioner
316 public IDocumentPartitioner createDocumentPartitioner() {
317 return createDocumentPartitioner(".php");
321 * Factory method for creating a PHP-specific document partitioner
322 * using this object's partitions scanner. This method is a
323 * convenience method.
325 * @return a newly created Java document partitioner
327 public IDocumentPartitioner createDocumentPartitioner(String extension) {
331 // FastJavaPartitionScanner.JAVA_DOC,
332 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
333 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
334 // FastJavaPartitionScanner.JAVA_STRING };
336 // return new DefaultPartitioner(getPartitionScanner(), types);
337 IDocumentPartitioner partitioner = null;
338 // System.out.println(extension);
339 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
341 partitioner = createHTMLPartitioner();
342 } else if (extension.equalsIgnoreCase(".xml")) {
344 partitioner = createXMLPartitioner();
345 } else if (extension.equalsIgnoreCase(".js")) {
347 partitioner = createJavaScriptPartitioner();
348 } else if (extension.equalsIgnoreCase(".css")) {
349 // cascading style sheets
350 partitioner = createCSSPartitioner();
351 } else if (extension.equalsIgnoreCase(".tpl")) {
353 partitioner = createSmartyPartitioner();
354 } else if (extension.equalsIgnoreCase(".inc")) {
355 // php include files ?
356 partitioner = createIncludePartitioner();
359 if (partitioner == null) {
360 partitioner = createPHPPartitioner();
367 * Sets up the Java document partitioner for the given document for the given partitioning.
369 * @param document the document to be set up
370 * @param partitioning the document partitioning
373 public void setupJavaDocumentPartitioner(IDocument document, String partitioning) {
374 IDocumentPartitioner partitioner= createDocumentPartitioner();
375 if (document instanceof IDocumentExtension3) {
376 IDocumentExtension3 extension3= (IDocumentExtension3) document;
377 extension3.setDocumentPartitioner(partitioning, partitioner);
379 document.setDocumentPartitioner(partitioner);
381 partitioner.connect(document);
385 * Returns the names of the document position categories used by the document
386 * partitioners created by this object to manage their partition information.
387 * If the partitioners don't use document position categories, the returned
388 * result is <code>null</code>.
390 * @return the partition managing position categories or <code>null</code>
393 public String[] getPartitionManagingPositionCategories() {
394 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
398 * Determines whether the preference change encoded by the given event
399 * changes the behavior of one its contained components.
401 * @param event the event to be investigated
402 * @return <code>true</code> if event causes a behavioral change
406 public boolean affectsBehavior(PropertyChangeEvent event) {
407 return fCodeScanner.affectsBehavior(event)
408 || fMultilineCommentScanner.affectsBehavior(event)
409 || fSinglelineCommentScanner.affectsBehavior(event)
410 || fStringScanner.affectsBehavior(event)
411 || fPHPDocScanner.affectsBehavior(event);
415 * Adapts the behavior of the contained components to the change
416 * encoded in the given event.
418 * @param event the event to which to adapt
421 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
422 if (fCodeScanner.affectsBehavior(event))
423 fCodeScanner.adaptToPreferenceChange(event);
424 if (fMultilineCommentScanner.affectsBehavior(event))
425 fMultilineCommentScanner.adaptToPreferenceChange(event);
426 if (fSinglelineCommentScanner.affectsBehavior(event))
427 fSinglelineCommentScanner.adaptToPreferenceChange(event);
428 if (fStringScanner.affectsBehavior(event))
429 fStringScanner.adaptToPreferenceChange(event);
430 if (fPHPDocScanner.affectsBehavior(event))
431 fPHPDocScanner.adaptToPreferenceChange(event);
435 * Return a partitioner for .html files.
437 private static IDocumentPartitioner createHTMLPartitioner() {
438 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
441 private static IDocumentPartitioner createIncludePartitioner() {
442 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
445 private static IDocumentPartitioner createJavaScriptPartitioner() {
446 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
450 * Return a partitioner for .php files.
452 private static IDocumentPartitioner createPHPPartitioner() {
453 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
456 private static IDocumentPartitioner createSmartyPartitioner() {
457 return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
460 private static IDocumentPartitioner createXMLPartitioner() {
461 return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
464 private static IDocumentPartitioner createCSSPartitioner() {
465 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
468 * Return a scanner for creating html partitions.
470 private static PHPPartitionScanner getHTMLPartitionScanner() {
471 if (HTML_PARTITION_SCANNER == null)
472 HTML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.HTML_FILE);
473 return HTML_PARTITION_SCANNER;
476 * Return a scanner for creating php partitions.
478 private static PHPPartitionScanner getPHPPartitionScanner() {
479 if (PHP_PARTITION_SCANNER == null)
480 PHP_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.PHP_FILE);
481 return PHP_PARTITION_SCANNER;
485 * Return a scanner for creating smarty partitions.
487 private static PHPPartitionScanner getSmartyPartitionScanner() {
488 if (SMARTY_PARTITION_SCANNER == null)
489 SMARTY_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.SMARTY_FILE);
490 return SMARTY_PARTITION_SCANNER;
494 * Return a scanner for creating xml partitions.
496 private static PHPPartitionScanner getXMLPartitionScanner() {
497 if (XML_PARTITION_SCANNER == null)
498 XML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitions.XML_FILE);
499 return XML_PARTITION_SCANNER;