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.PHPCodeScanner;
13 import org.eclipse.jface.preference.IPreferenceStore;
14 import org.eclipse.jface.text.IDocumentPartitioner;
15 import org.eclipse.jface.text.rules.DefaultPartitioner;
16 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
17 import org.eclipse.jface.text.rules.RuleBasedScanner;
18 import org.eclipse.jface.util.IPropertyChangeListener;
19 import org.eclipse.jface.util.PropertyChangeEvent;
21 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
22 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
23 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
24 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
25 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
26 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
30 * Tools required to configure a Java text viewer.
31 * The color manager and all scanner exist only one time, i.e.
32 * the same instances are returned to all clients. Thus, clients
35 * This class may be instantiated; it is not intended to be subclassed.
38 public class JavaTextTools {
40 private class PreferenceListener implements IPropertyChangeListener {
41 public void propertyChange(PropertyChangeEvent event) {
42 adaptToPreferenceChange(event);
46 /** The color manager */
47 private JavaColorManager fColorManager;
48 /** The Java source code scanner */
49 private PHPCodeScanner fCodeScanner;
50 /** The Java multiline comment scanner */
51 private SingleTokenPHPScanner fMultilineCommentScanner;
52 /** The Java singleline comment scanner */
53 private SingleTokenPHPScanner fSinglelineCommentScanner;
54 /** The Java string scanner */
55 private SingleTokenPHPScanner fStringScanner;
56 /** The JavaDoc scanner */
57 private PHPDocCodeScanner fJavaDocScanner;
58 /** The Java partitions scanner */
59 private FastJavaPartitionScanner fPartitionScanner;
61 /** The preference store */
62 private IPreferenceStore fPreferenceStore;
63 /** The preference change listener */
64 private PreferenceListener fPreferenceListener= new PreferenceListener();
68 * Creates a new Java text tools collection.
70 * @param store the preference store to initialize the text tools. The text tool
71 * instance installs a listener on the passed preference store to adapt itself to
72 * changes in the preference store. In general <code>PreferenceConstants.
73 * getPreferenceStore()</code> shoould be used to initialize the text tools.
75 * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
78 public JavaTextTools(IPreferenceStore store) {
79 fPreferenceStore= store;
80 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
82 fColorManager= new JavaColorManager();
83 fCodeScanner= new PHPCodeScanner(fColorManager, store);
84 fMultilineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.JAVA_MULTI_LINE_COMMENT);
85 fSinglelineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.JAVA_SINGLE_LINE_COMMENT);
86 fStringScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.JAVA_STRING);
87 fJavaDocScanner= new PHPDocCodeScanner(fColorManager, store);
88 fPartitionScanner= new FastJavaPartitionScanner();
92 * Disposes all the individual tools of this tools collection.
94 public void dispose() {
97 fMultilineCommentScanner= null;
98 fSinglelineCommentScanner= null;
100 fJavaDocScanner= null;
101 fPartitionScanner= null;
103 if (fColorManager != null) {
104 fColorManager.dispose();
108 if (fPreferenceStore != null) {
109 fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
110 fPreferenceStore= null;
111 fPreferenceListener= null;
116 * Returns the color manager which is used to manage
117 * any Java-specific colors needed for such things like syntax highlighting.
119 * @return the color manager to be used for Java text viewers
121 public JavaColorManager getColorManager() {
122 return fColorManager;
126 * Returns a scanner which is configured to scan Java source code.
128 * @return a Java source code scanner
130 public RuleBasedScanner getCodeScanner() {
135 * Returns a scanner which is configured to scan Java multiline comments.
137 * @return a Java multiline comment scanner
141 public RuleBasedScanner getMultilineCommentScanner() {
142 return fMultilineCommentScanner;
146 * Returns a scanner which is configured to scan Java singleline comments.
148 * @return a Java singleline comment scanner
152 public RuleBasedScanner getSinglelineCommentScanner() {
153 return fSinglelineCommentScanner;
157 * Returns a scanner which is configured to scan Java strings.
159 * @return a Java string scanner
163 public RuleBasedScanner getStringScanner() {
164 return fStringScanner;
168 * Returns a scanner which is configured to scan JavaDoc compliant comments.
169 * Notes that the start sequence "/**" and the corresponding end sequence
170 * are part of the JavaDoc comment.
172 * @return a JavaDoc scanner
174 public RuleBasedScanner getJavaDocScanner() {
175 return fJavaDocScanner;
179 * Returns a scanner which is configured to scan
180 * Java-specific partitions, which are multi-line comments,
181 * JavaDoc comments, and regular Java source code.
183 * @return a Java partition scanner
185 public IPartitionTokenScanner getPartitionScanner() {
186 return fPartitionScanner;
190 * Factory method for creating a Java-specific document partitioner
191 * using this object's partitions scanner. This method is a
192 * convenience method.
194 * @return a newly created Java document partitioner
196 public IDocumentPartitioner createDocumentPartitioner() {
198 String[] types= new String[] {
199 FastJavaPartitionScanner.JAVA_DOC,
200 FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
201 FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
202 FastJavaPartitionScanner.JAVA_STRING
205 return new DefaultPartitioner(getPartitionScanner(), types);
209 * Returns the names of the document position categories used by the document
210 * partitioners created by this object to manage their partition information.
211 * If the partitioners don't use document position categories, the returned
212 * result is <code>null</code>.
214 * @return the partition managing position categories or <code>null</code>
217 public String[] getPartitionManagingPositionCategories() {
218 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
222 * Determines whether the preference change encoded by the given event
223 * changes the behavior of one its contained components.
225 * @param event the event to be investigated
226 * @return <code>true</code> if event causes a behavioral change
230 public boolean affectsBehavior(PropertyChangeEvent event) {
231 return // fCodeScanner.affectsBehavior(event) ||
232 fMultilineCommentScanner.affectsBehavior(event) ||
233 fSinglelineCommentScanner.affectsBehavior(event) ||
234 fStringScanner.affectsBehavior(event); // ||
235 // fJavaDocScanner.affectsBehavior(event);
239 * Adapts the behavior of the contained components to the change
240 * encoded in the given event.
242 * @param event the event to which to adapt
245 protected void adaptToPreferenceChange(PropertyChangeEvent event) {
246 // if (fCodeScanner.affectsBehavior(event))
247 // fCodeScanner.adaptToPreferenceChange(event);
248 if (fMultilineCommentScanner.affectsBehavior(event))
249 fMultilineCommentScanner.adaptToPreferenceChange(event);
250 if (fSinglelineCommentScanner.affectsBehavior(event))
251 fSinglelineCommentScanner.adaptToPreferenceChange(event);
252 if (fStringScanner.affectsBehavior(event))
253 fStringScanner.adaptToPreferenceChange(event);
254 // if (fJavaDocScanner.affectsBehavior(event))
255 // fJavaDocScanner.adaptToPreferenceChange(event);