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.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.IPHPPartitionScannerConstants;
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.IDocumentPartitioner;
22 import org.eclipse.jface.text.rules.DefaultPartitioner;
23 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
24 import org.eclipse.jface.text.rules.RuleBasedScanner;
25 import org.eclipse.jface.util.IPropertyChangeListener;
26 import org.eclipse.jface.util.PropertyChangeEvent;
28 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
29 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
30 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
31 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
32 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
33 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
36 * Tools required to configure a Java text viewer.
37 * The color manager and all scanner exist only one time, i.e.
38 * the same instances are returned to all clients. Thus, clients
41 * This class may be instantiated; it is not intended to be subclassed.
44 public class JavaTextTools {
46 private static PHPPartitionScanner HTML_PARTITION_SCANNER = null;
48 private static PHPPartitionScanner PHP_PARTITION_SCANNER = null;
49 private static PHPPartitionScanner SMARTY_PARTITION_SCANNER = null;
51 // private final static String[] TYPES= new String[] { PHPPartitionScanner.PHP, PHPPartitionScanner.JAVA_DOC, PHPPartitionScanner.JAVA_MULTILINE_COMMENT };
52 private final static String[] TYPES =
54 IPHPPartitionScannerConstants.PHP,
55 IPHPPartitionScannerConstants.PHP_MULTILINE_COMMENT,
56 IPHPPartitionScannerConstants.HTML,
57 IPHPPartitionScannerConstants.HTML_MULTILINE_COMMENT,
58 IPHPPartitionScannerConstants.JAVASCRIPT,
59 IPHPPartitionScannerConstants.CSS,
60 IPHPPartitionScannerConstants.SMARTY,
61 IPHPPartitionScannerConstants.SMARTY_MULTILINE_COMMENT };
62 private static PHPPartitionScanner XML_PARTITION_SCANNER = null;
65 * This tools' preference listener.
67 private class PreferenceListener implements IPropertyChangeListener, Preferences.IPropertyChangeListener {
68 public void propertyChange(PropertyChangeEvent event) {
69 adaptToPreferenceChange(event);
71 public void propertyChange(Preferences.PropertyChangeEvent event) {
72 adaptToPreferenceChange(
73 new PropertyChangeEvent(event.getSource(), event.getProperty(), event.getOldValue(), event.getNewValue()));
77 /** The color manager */
78 private JavaColorManager fColorManager;
79 /** The PHP source code scanner */
80 private PHPCodeScanner fCodeScanner;
81 /** The PHP multiline comment scanner */
82 private SingleTokenPHPScanner fMultilineCommentScanner;
83 /** The Java singleline comment scanner */
84 private SingleTokenPHPScanner fSinglelineCommentScanner;
85 /** The Java string scanner */
86 private SingleTokenPHPScanner fStringScanner;
87 /** The PHPDoc scanner */
88 private PHPDocCodeScanner fPHPDocScanner;
89 /** The HTML scanner */
90 private HTMLCodeScanner fHTMLScanner;
91 /** The Smarty scanner */
92 private SmartyCodeScanner fSmartyScanner;
93 /** The SmartyDoc scanner */
94 private SmartyDocCodeScanner fSmartyDocScanner;
95 /** The Java partitions scanner */
96 private FastJavaPartitionScanner fPartitionScanner;
98 /** The preference store */
99 private IPreferenceStore fPreferenceStore;
101 * The core preference store.
104 private Preferences fCorePreferenceStore;
105 /** The preference change listener */
106 private PreferenceListener fPreferenceListener = new PreferenceListener();
109 * Creates a new Java text tools collection.
111 * @param store the preference store to initialize the text tools. The text tool
112 * instance installs a listener on the passed preference store to adapt itself to
113 * changes in the preference store. In general <code>PreferenceConstants.
114 * getPreferenceStore()</code> shoould be used to initialize the text tools.
116 * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
119 public JavaTextTools(IPreferenceStore store) {
120 fPreferenceStore = store;
121 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
123 fColorManager = new JavaColorManager();
124 fCodeScanner = new PHPCodeScanner(fColorManager, store);
125 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
126 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
127 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
128 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
129 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
130 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
131 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
132 fPartitionScanner = new FastJavaPartitionScanner();
136 * Creates a new Java text tools collection.
138 * @param store the preference store to initialize the text tools. The text tool
139 * instance installs a listener on the passed preference store to adapt itself to
140 * changes in the preference store. In general <code>PreferenceConstants.
141 * getPreferenceStore()</code> shoould be used to initialize the text tools.
142 * @param coreStore optional 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.
145 * @param autoDisposeOnDisplayDispose if <code>true</code> the color manager
146 * automatically disposes all managed colors when the current display gets disposed
147 * and all calls to {@link org.eclipse.jface.text.source.ISharedTextColors#dispose()} are ignored.
148 * @see org.eclipse.jdt.ui.PreferenceConstants#getPreferenceStore()
151 public JavaTextTools(IPreferenceStore store, Preferences coreStore, boolean autoDisposeOnDisplayDispose) {
152 fPreferenceStore = store;
153 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
155 fCorePreferenceStore = coreStore;
156 if (fCorePreferenceStore != null)
157 fCorePreferenceStore.addPropertyChangeListener(fPreferenceListener);
159 fColorManager = new JavaColorManager(autoDisposeOnDisplayDispose);
160 fCodeScanner = new PHPCodeScanner(fColorManager, store);
161 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
162 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
163 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
164 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
165 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
166 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
167 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
168 fPartitionScanner = new FastJavaPartitionScanner();
172 * Disposes all the individual tools of this tools collection.
174 public void dispose() {
177 fMultilineCommentScanner = null;
178 fSinglelineCommentScanner = null;
179 fStringScanner = null;
180 fPHPDocScanner = null;
181 fPartitionScanner = null;
183 if (fColorManager != null) {
184 fColorManager.dispose();
185 fColorManager = null;
188 if (fPreferenceStore != null) {
189 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
190 fPreferenceStore = null;
192 if (fCorePreferenceStore != null) {
193 fCorePreferenceStore.removePropertyChangeListener(fPreferenceListener);
194 fCorePreferenceStore = null;
197 fPreferenceListener = null;
202 * Returns the color manager which is used to manage
203 * any Java-specific colors needed for such things like syntax highlighting.
205 * @return the color manager to be used for Java text viewers
207 public IColorManager getColorManager() {
208 return fColorManager;
212 * Returns a scanner which is configured to scan Java source code.
214 * @return a Java source code scanner
216 public RuleBasedScanner getCodeScanner() {
221 * Returns a scanner which is configured to scan Java multiline comments.
223 * @return a Java multiline comment scanner
227 public RuleBasedScanner getMultilineCommentScanner() {
228 return fMultilineCommentScanner;
232 * Returns a scanner which is configured to scan HTML code.
234 * @return a HTML scanner
238 public RuleBasedScanner getHTMLScanner() {
243 * Returns a scanner which is configured to scan Smarty code.
245 * @return a Smarty scanner
249 public RuleBasedScanner getSmartyScanner() {
250 return fSmartyScanner;
254 * Returns a scanner which is configured to scan Smarty code.
256 * @return a Smarty scanner
260 public RuleBasedScanner getSmartyDocScanner() {
261 return fSmartyDocScanner;
264 * Returns a scanner which is configured to scan Java singleline comments.
266 * @return a Java singleline comment scanner
270 public RuleBasedScanner getSinglelineCommentScanner() {
271 return fSinglelineCommentScanner;
275 * Returns a scanner which is configured to scan Java strings.
277 * @return a Java string scanner
281 public RuleBasedScanner getStringScanner() {
282 return fStringScanner;
286 * Returns a scanner which is configured to scan JavaDoc compliant comments.
287 * Notes that the start sequence "/**" and the corresponding end sequence
288 * are part of the JavaDoc comment.
290 * @return a JavaDoc scanner
292 public RuleBasedScanner getJavaDocScanner() {
293 return fPHPDocScanner;
297 * Returns a scanner which is configured to scan
298 * Java-specific partitions, which are multi-line comments,
299 * JavaDoc comments, and regular Java source code.
301 * @return a Java partition scanner
303 public IPartitionTokenScanner getPartitionScanner() {
304 return fPartitionScanner;
308 * Factory method for creating a PHP-specific document partitioner
309 * using this object's partitions scanner. This method is a
310 * convenience method.
312 * @return a newly created Java document partitioner
314 public IDocumentPartitioner createDocumentPartitioner() {
315 return createDocumentPartitioner(".php");
319 * Factory method for creating a PHP-specific document partitioner
320 * using this object's partitions scanner. This method is a
321 * convenience method.
323 * @return a newly created Java document partitioner
325 public IDocumentPartitioner createDocumentPartitioner(String extension) {
329 // FastJavaPartitionScanner.JAVA_DOC,
330 // FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
331 // FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
332 // FastJavaPartitionScanner.JAVA_STRING };
334 // return new DefaultPartitioner(getPartitionScanner(), types);
335 IDocumentPartitioner partitioner = null;
336 // System.out.println(extension);
337 if (extension.equalsIgnoreCase(".html") || extension.equalsIgnoreCase(".htm")) {
339 partitioner = createHTMLPartitioner();
340 } else if (extension.equalsIgnoreCase(".xml")) {
342 partitioner = createXMLPartitioner();
343 } else if (extension.equalsIgnoreCase(".js")) {
345 partitioner = createJavaScriptPartitioner();
346 } else if (extension.equalsIgnoreCase(".css")) {
347 // cascading style sheets
348 partitioner = createCSSPartitioner();
349 } else if (extension.equalsIgnoreCase(".tpl")) {
351 partitioner = createSmartyPartitioner();
352 } else if (extension.equalsIgnoreCase(".inc")) {
353 // php include files ?
354 partitioner = createIncludePartitioner();
357 if (partitioner == null) {
358 partitioner = createPHPPartitioner();
365 * Returns the names of the document position categories used by the document
366 * partitioners created by this object to manage their partition information.
367 * If the partitioners don't use document position categories, the returned
368 * result is <code>null</code>.
370 * @return the partition managing position categories or <code>null</code>
373 public String[] getPartitionManagingPositionCategories() {
374 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
378 * Determines whether the preference change encoded by the given event
379 * changes the behavior of one its contained components.
381 * @param event the event to be investigated
382 * @return <code>true</code> if event causes a behavioral change
386 public boolean affectsBehavior(PropertyChangeEvent event) {
387 return fCodeScanner.affectsBehavior(event)
388 || fMultilineCommentScanner.affectsBehavior(event)
389 || fSinglelineCommentScanner.affectsBehavior(event)
390 || fStringScanner.affectsBehavior(event)
391 || fPHPDocScanner.affectsBehavior(event);
395 * Adapts the behavior of the contained components to the change
396 * encoded in the given event.
398 * @param event the event to which to adapt
401 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
402 if (fCodeScanner.affectsBehavior(event))
403 fCodeScanner.adaptToPreferenceChange(event);
404 if (fMultilineCommentScanner.affectsBehavior(event))
405 fMultilineCommentScanner.adaptToPreferenceChange(event);
406 if (fSinglelineCommentScanner.affectsBehavior(event))
407 fSinglelineCommentScanner.adaptToPreferenceChange(event);
408 if (fStringScanner.affectsBehavior(event))
409 fStringScanner.adaptToPreferenceChange(event);
410 if (fPHPDocScanner.affectsBehavior(event))
411 fPHPDocScanner.adaptToPreferenceChange(event);
415 * Return a partitioner for .html files.
417 private static IDocumentPartitioner createHTMLPartitioner() {
418 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
421 private static IDocumentPartitioner createIncludePartitioner() {
422 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
425 private static IDocumentPartitioner createJavaScriptPartitioner() {
426 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
430 * Return a partitioner for .php files.
432 private static IDocumentPartitioner createPHPPartitioner() {
433 return new DefaultPartitioner(getPHPPartitionScanner(), TYPES);
436 private static IDocumentPartitioner createSmartyPartitioner() {
437 return new DefaultPartitioner(getSmartyPartitionScanner(), TYPES);
440 private static IDocumentPartitioner createXMLPartitioner() {
441 return new DefaultPartitioner(getXMLPartitionScanner(), TYPES);
444 private static IDocumentPartitioner createCSSPartitioner() {
445 return new DefaultPartitioner(getHTMLPartitionScanner(), TYPES);
448 * Return a scanner for creating html partitions.
450 private static PHPPartitionScanner getHTMLPartitionScanner() {
451 if (HTML_PARTITION_SCANNER == null)
452 HTML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.HTML_FILE);
453 return HTML_PARTITION_SCANNER;
456 * Return a scanner for creating php partitions.
458 private static PHPPartitionScanner getPHPPartitionScanner() {
459 if (PHP_PARTITION_SCANNER == null)
460 PHP_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.PHP_FILE);
461 return PHP_PARTITION_SCANNER;
465 * Return a scanner for creating smarty partitions.
467 private static PHPPartitionScanner getSmartyPartitionScanner() {
468 if (SMARTY_PARTITION_SCANNER == null)
469 SMARTY_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.SMARTY_FILE);
470 return SMARTY_PARTITION_SCANNER;
474 * Return a scanner for creating xml partitions.
476 private static PHPPartitionScanner getXMLPartitionScanner() {
477 if (XML_PARTITION_SCANNER == null)
478 XML_PARTITION_SCANNER = new PHPPartitionScanner(IPHPPartitionScannerConstants.XML_FILE);
479 return XML_PARTITION_SCANNER;