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.phpeditor.php.HTMLCodeScanner;
12 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
14 import org.eclipse.jface.preference.IPreferenceStore;
15 import org.eclipse.jface.text.IDocumentPartitioner;
16 import org.eclipse.jface.text.rules.DefaultPartitioner;
17 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
18 import org.eclipse.jface.text.rules.RuleBasedScanner;
19 import org.eclipse.jface.util.IPropertyChangeListener;
20 import org.eclipse.jface.util.PropertyChangeEvent;
22 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
23 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
24 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
25 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
26 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
27 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
31 * Tools required to configure a Java text viewer.
32 * The color manager and all scanner exist only one time, i.e.
33 * the same instances are returned to all clients. Thus, clients
36 * This class may be instantiated; it is not intended to be subclassed.
39 public class JavaTextTools {
41 private class PreferenceListener implements IPropertyChangeListener {
42 public void propertyChange(PropertyChangeEvent event) {
43 adaptToPreferenceChange(event);
47 /** The color manager */
48 private JavaColorManager fColorManager;
49 /** The PHP source code scanner */
50 private PHPCodeScanner fCodeScanner;
51 /** The PHP multiline comment scanner */
52 private SingleTokenPHPScanner fMultilineCommentScanner;
53 /** The Java singleline comment scanner */
54 private SingleTokenPHPScanner fSinglelineCommentScanner;
55 /** The Java string scanner */
56 private SingleTokenPHPScanner fStringScanner;
57 /** The PHPDoc scanner */
58 private PHPDocCodeScanner fJavaDocScanner;
59 /** The HTML scanner */
60 private HTMLCodeScanner fHTMLScanner;
61 /** The Java partitions scanner */
62 private FastJavaPartitionScanner fPartitionScanner;
64 /** The preference store */
65 private IPreferenceStore fPreferenceStore;
66 /** The preference change listener */
67 private PreferenceListener fPreferenceListener= new PreferenceListener();
71 * Creates a new Java text tools collection.
73 * @param store the preference store to initialize the text tools. The text tool
74 * instance installs a listener on the passed preference store to adapt itself to
75 * changes in the preference store. In general <code>PreferenceConstants.
76 * getPreferenceStore()</code> shoould be used to initialize the text tools.
78 * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
81 public JavaTextTools(IPreferenceStore store) {
82 fPreferenceStore= store;
83 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
85 fColorManager= new JavaColorManager();
86 fCodeScanner= new PHPCodeScanner(fColorManager, store);
87 fMultilineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.PHP_MULTI_LINE_COMMENT);
88 fSinglelineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.PHP_SINGLE_LINE_COMMENT);
89 fStringScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.PHP_STRING);
90 fJavaDocScanner= new PHPDocCodeScanner(fColorManager, store);
91 fHTMLScanner= new HTMLCodeScanner(fColorManager, store);
92 fPartitionScanner= new FastJavaPartitionScanner();
96 * Disposes all the individual tools of this tools collection.
98 public void dispose() {
101 fMultilineCommentScanner= null;
102 fSinglelineCommentScanner= null;
103 fStringScanner= null;
104 fJavaDocScanner= null;
105 fPartitionScanner= null;
107 if (fColorManager != null) {
108 fColorManager.dispose();
112 if (fPreferenceStore != null) {
113 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
114 fPreferenceStore= null;
115 fPreferenceListener= null;
120 * Returns the color manager which is used to manage
121 * any Java-specific colors needed for such things like syntax highlighting.
123 * @return the color manager to be used for Java text viewers
125 public JavaColorManager getColorManager() {
126 return fColorManager;
130 * Returns a scanner which is configured to scan Java source code.
132 * @return a Java source code scanner
134 public RuleBasedScanner getCodeScanner() {
139 * Returns a scanner which is configured to scan Java multiline comments.
141 * @return a Java multiline comment scanner
145 public RuleBasedScanner getMultilineCommentScanner() {
146 return fMultilineCommentScanner;
150 * Returns a scanner which is configured to scan HTML code.
152 * @return a HTML scanner
156 public RuleBasedScanner getHTMLScanner() {
161 * Returns a scanner which is configured to scan Java singleline comments.
163 * @return a Java singleline comment scanner
167 public RuleBasedScanner getSinglelineCommentScanner() {
168 return fSinglelineCommentScanner;
172 * Returns a scanner which is configured to scan Java strings.
174 * @return a Java string scanner
178 public RuleBasedScanner getStringScanner() {
179 return fStringScanner;
183 * Returns a scanner which is configured to scan JavaDoc compliant comments.
184 * Notes that the start sequence "/**" and the corresponding end sequence
185 * are part of the JavaDoc comment.
187 * @return a JavaDoc scanner
189 public RuleBasedScanner getJavaDocScanner() {
190 return fJavaDocScanner;
194 * Returns a scanner which is configured to scan
195 * Java-specific partitions, which are multi-line comments,
196 * JavaDoc comments, and regular Java source code.
198 * @return a Java partition scanner
200 public IPartitionTokenScanner getPartitionScanner() {
201 return fPartitionScanner;
205 * Factory method for creating a Java-specific document partitioner
206 * using this object's partitions scanner. This method is a
207 * convenience method.
209 * @return a newly created Java document partitioner
211 public IDocumentPartitioner createDocumentPartitioner() {
213 String[] types= new String[] {
214 FastJavaPartitionScanner.JAVA_DOC,
215 FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
216 FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
217 FastJavaPartitionScanner.JAVA_STRING
220 return new DefaultPartitioner(getPartitionScanner(), types);
224 * Returns the names of the document position categories used by the document
225 * partitioners created by this object to manage their partition information.
226 * If the partitioners don't use document position categories, the returned
227 * result is <code>null</code>.
229 * @return the partition managing position categories or <code>null</code>
232 public String[] getPartitionManagingPositionCategories() {
233 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
237 * Determines whether the preference change encoded by the given event
238 * changes the behavior of one its contained components.
240 * @param event the event to be investigated
241 * @return <code>true</code> if event causes a behavioral change
245 public boolean affectsBehavior(PropertyChangeEvent event) {
246 return fCodeScanner.affectsBehavior(event) ||
247 fMultilineCommentScanner.affectsBehavior(event) ||
248 fSinglelineCommentScanner.affectsBehavior(event) ||
249 fStringScanner.affectsBehavior(event) ||
250 fJavaDocScanner.affectsBehavior(event);
254 * Adapts the behavior of the contained components to the change
255 * encoded in the given event.
257 * @param event the event to which to adapt
260 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
261 if (fCodeScanner.affectsBehavior(event))
262 fCodeScanner.adaptToPreferenceChange(event);
263 if (fMultilineCommentScanner.affectsBehavior(event))
264 fMultilineCommentScanner.adaptToPreferenceChange(event);
265 if (fSinglelineCommentScanner.affectsBehavior(event))
266 fSinglelineCommentScanner.adaptToPreferenceChange(event);
267 if (fStringScanner.affectsBehavior(event))
268 fStringScanner.adaptToPreferenceChange(event);
269 if (fJavaDocScanner.affectsBehavior(event))
270 fJavaDocScanner.adaptToPreferenceChange(event);