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.PHPCodeScanner;
14 import net.sourceforge.phpeclipse.phpeditor.php.SmartyCodeScanner;
15 import net.sourceforge.phpeclipse.phpeditor.php.SmartyDocCodeScanner;
17 import org.eclipse.jface.preference.IPreferenceStore;
18 import org.eclipse.jface.text.IDocumentPartitioner;
19 import org.eclipse.jface.text.rules.DefaultPartitioner;
20 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
21 import org.eclipse.jface.text.rules.RuleBasedScanner;
22 import org.eclipse.jface.util.IPropertyChangeListener;
23 import org.eclipse.jface.util.PropertyChangeEvent;
25 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
26 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
27 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
28 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
29 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
30 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
33 * Tools required to configure a Java text viewer.
34 * The color manager and all scanner exist only one time, i.e.
35 * the same instances are returned to all clients. Thus, clients
38 * This class may be instantiated; it is not intended to be subclassed.
41 public class JavaTextTools {
43 private class PreferenceListener implements IPropertyChangeListener {
44 public void propertyChange(PropertyChangeEvent event) {
45 adaptToPreferenceChange(event);
49 /** The color manager */
50 private JavaColorManager fColorManager;
51 /** The PHP source code scanner */
52 private PHPCodeScanner fCodeScanner;
53 /** The PHP multiline comment scanner */
54 private SingleTokenPHPScanner fMultilineCommentScanner;
55 /** The Java singleline comment scanner */
56 private SingleTokenPHPScanner fSinglelineCommentScanner;
57 /** The Java string scanner */
58 private SingleTokenPHPScanner fStringScanner;
59 /** The PHPDoc scanner */
60 private PHPDocCodeScanner fPHPDocScanner;
61 /** The HTML scanner */
62 private HTMLCodeScanner fHTMLScanner;
63 /** The Smarty scanner */
64 private SmartyCodeScanner fSmartyScanner;
65 /** The SmartyDoc scanner */
66 private SmartyDocCodeScanner fSmartyDocScanner;
67 /** The Java partitions scanner */
68 private FastJavaPartitionScanner fPartitionScanner;
70 /** The preference store */
71 private IPreferenceStore fPreferenceStore;
72 /** The preference change listener */
73 private PreferenceListener fPreferenceListener = new PreferenceListener();
76 * Creates a new Java text tools collection.
78 * @param store the preference store to initialize the text tools. The text tool
79 * instance installs a listener on the passed preference store to adapt itself to
80 * changes in the preference store. In general <code>PreferenceConstants.
81 * getPreferenceStore()</code> shoould be used to initialize the text tools.
83 * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
86 public JavaTextTools(IPreferenceStore store) {
87 fPreferenceStore = store;
88 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
90 fColorManager = new JavaColorManager();
91 fCodeScanner = new PHPCodeScanner(fColorManager, store);
92 fMultilineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
93 fSinglelineCommentScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
94 fStringScanner = new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
95 fPHPDocScanner = new PHPDocCodeScanner(fColorManager, store);
96 fHTMLScanner = new HTMLCodeScanner(fColorManager, store);
97 fSmartyScanner = new SmartyCodeScanner(fColorManager, store);
98 fSmartyDocScanner = new SmartyDocCodeScanner(fColorManager, store);
99 fPartitionScanner = new FastJavaPartitionScanner();
103 * Disposes all the individual tools of this tools collection.
105 public void dispose() {
108 fMultilineCommentScanner = null;
109 fSinglelineCommentScanner = null;
110 fStringScanner = null;
111 fPHPDocScanner = null;
112 fPartitionScanner = null;
114 if (fColorManager != null) {
115 fColorManager.dispose();
116 fColorManager = null;
119 if (fPreferenceStore != null) {
120 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
121 fPreferenceStore = null;
122 fPreferenceListener = null;
127 * Returns the color manager which is used to manage
128 * any Java-specific colors needed for such things like syntax highlighting.
130 * @return the color manager to be used for Java text viewers
132 public IColorManager getColorManager() {
133 return fColorManager;
137 * Returns a scanner which is configured to scan Java source code.
139 * @return a Java source code scanner
141 public RuleBasedScanner getCodeScanner() {
146 * Returns a scanner which is configured to scan Java multiline comments.
148 * @return a Java multiline comment scanner
152 public RuleBasedScanner getMultilineCommentScanner() {
153 return fMultilineCommentScanner;
157 * Returns a scanner which is configured to scan HTML code.
159 * @return a HTML scanner
163 public RuleBasedScanner getHTMLScanner() {
168 * Returns a scanner which is configured to scan Smarty code.
170 * @return a Smarty scanner
174 public RuleBasedScanner getSmartyScanner() {
175 return fSmartyScanner;
179 * Returns a scanner which is configured to scan Smarty code.
181 * @return a Smarty scanner
185 public RuleBasedScanner getSmartyDocScanner() {
186 return fSmartyDocScanner;
189 * Returns a scanner which is configured to scan Java singleline comments.
191 * @return a Java singleline comment scanner
195 public RuleBasedScanner getSinglelineCommentScanner() {
196 return fSinglelineCommentScanner;
200 * Returns a scanner which is configured to scan Java strings.
202 * @return a Java string scanner
206 public RuleBasedScanner getStringScanner() {
207 return fStringScanner;
211 * Returns a scanner which is configured to scan JavaDoc compliant comments.
212 * Notes that the start sequence "/**" and the corresponding end sequence
213 * are part of the JavaDoc comment.
215 * @return a JavaDoc scanner
217 public RuleBasedScanner getJavaDocScanner() {
218 return fPHPDocScanner;
222 * Returns a scanner which is configured to scan
223 * Java-specific partitions, which are multi-line comments,
224 * JavaDoc comments, and regular Java source code.
226 * @return a Java partition scanner
228 public IPartitionTokenScanner getPartitionScanner() {
229 return fPartitionScanner;
233 * Factory method for creating a Java-specific document partitioner
234 * using this object's partitions scanner. This method is a
235 * convenience method.
237 * @return a newly created Java document partitioner
239 public IDocumentPartitioner createDocumentPartitioner() {
243 FastJavaPartitionScanner.JAVA_DOC,
244 FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
245 FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
246 FastJavaPartitionScanner.JAVA_STRING };
248 return new DefaultPartitioner(getPartitionScanner(), types);
252 * Returns the names of the document position categories used by the document
253 * partitioners created by this object to manage their partition information.
254 * If the partitioners don't use document position categories, the returned
255 * result is <code>null</code>.
257 * @return the partition managing position categories or <code>null</code>
260 public String[] getPartitionManagingPositionCategories() {
261 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
265 * Determines whether the preference change encoded by the given event
266 * changes the behavior of one its contained components.
268 * @param event the event to be investigated
269 * @return <code>true</code> if event causes a behavioral change
273 public boolean affectsBehavior(PropertyChangeEvent event) {
274 return fCodeScanner.affectsBehavior(event)
275 || fMultilineCommentScanner.affectsBehavior(event)
276 || fSinglelineCommentScanner.affectsBehavior(event)
277 || fStringScanner.affectsBehavior(event)
278 || fPHPDocScanner.affectsBehavior(event);
282 * Adapts the behavior of the contained components to the change
283 * encoded in the given event.
285 * @param event the event to which to adapt
288 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
289 if (fCodeScanner.affectsBehavior(event))
290 fCodeScanner.adaptToPreferenceChange(event);
291 if (fMultilineCommentScanner.affectsBehavior(event))
292 fMultilineCommentScanner.adaptToPreferenceChange(event);
293 if (fSinglelineCommentScanner.affectsBehavior(event))
294 fSinglelineCommentScanner.adaptToPreferenceChange(event);
295 if (fStringScanner.affectsBehavior(event))
296 fStringScanner.adaptToPreferenceChange(event);
297 if (fPHPDocScanner.affectsBehavior(event))
298 fPHPDocScanner.adaptToPreferenceChange(event);