3409576b82b1dc3c28c4e8c46abab99b8ef11683
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ui / text / JavaTextTools.java
1 package net.sourceforge.phpdt.ui.text;
2
3 /*
4  * (c) Copyright IBM Corp. 2000, 2001.
5  * All Rights Reserved.
6  */
7
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;
12
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;
20 //
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;
27
28
29 /**
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
33  * share those tools.
34  * <p>
35  * This class may be instantiated; it is not intended to be subclassed.
36  * </p>
37  */
38 public class JavaTextTools {
39         
40         private class PreferenceListener implements IPropertyChangeListener {
41                 public void propertyChange(PropertyChangeEvent event) {
42                         adaptToPreferenceChange(event);
43                 }
44         };
45                 
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;     
60         
61         /** The preference store */
62         private IPreferenceStore fPreferenceStore;
63         /** The preference change listener */
64         private PreferenceListener fPreferenceListener= new PreferenceListener();
65
66         
67         /**
68          * Creates a new Java text tools collection.
69          * 
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.
74          * 
75          * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
76          * @since 2.0
77          */
78         public JavaTextTools(IPreferenceStore store) {
79                 fPreferenceStore= store;
80                 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
81                 
82                 fColorManager= new JavaColorManager();
83                 fCodeScanner= new PHPCodeScanner(fColorManager, store);
84                 fMultilineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.PHP_MULTI_LINE_COMMENT);
85                 fSinglelineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.PHP_SINGLE_LINE_COMMENT);
86                 fStringScanner= new SingleTokenPHPScanner(fColorManager, store, IJavaColorConstants.PHP_STRING);
87                 fJavaDocScanner= new PHPDocCodeScanner(fColorManager, store);
88                 fPartitionScanner= new FastJavaPartitionScanner();
89         }
90         
91         /**
92          * Disposes all the individual tools of this tools collection.
93          */
94         public void dispose() {
95                 
96                 fCodeScanner= null;
97                 fMultilineCommentScanner= null;
98                 fSinglelineCommentScanner= null;
99                 fStringScanner= null;
100                 fJavaDocScanner= null;
101                 fPartitionScanner= null;
102                 
103                 if (fColorManager != null) {
104                         fColorManager.dispose();
105                         fColorManager= null;
106                 }
107                 
108                 if (fPreferenceStore != null) {
109                         fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
110                         fPreferenceStore= null;
111                         fPreferenceListener= null;
112                 }
113         }
114         
115         /**
116          * Returns the color manager which is used to manage
117          * any Java-specific colors needed for such things like syntax highlighting.
118          *
119          * @return the color manager to be used for Java text viewers
120          */
121         public JavaColorManager getColorManager() {
122                 return fColorManager;
123         }
124         
125         /**
126          * Returns a scanner which is configured to scan Java source code.
127          *
128          * @return a Java source code scanner
129          */
130         public RuleBasedScanner getCodeScanner() {
131                 return fCodeScanner;
132         }
133         
134         /**
135          * Returns a scanner which is configured to scan Java multiline comments.
136          *
137          * @return a Java multiline comment scanner
138          * 
139          * @since 2.0
140          */
141         public RuleBasedScanner getMultilineCommentScanner() {
142                 return fMultilineCommentScanner;
143         }
144
145         /**
146          * Returns a scanner which is configured to scan Java singleline comments.
147          *
148          * @return a Java singleline comment scanner
149          * 
150          * @since 2.0
151          */
152         public RuleBasedScanner getSinglelineCommentScanner() {
153                 return fSinglelineCommentScanner;
154         }
155         
156         /**
157          * Returns a scanner which is configured to scan Java strings.
158          *
159          * @return a Java string scanner
160          * 
161          * @since 2.0
162          */
163         public RuleBasedScanner getStringScanner() {
164                 return fStringScanner;
165         }
166         
167         /**
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.
171          *
172          * @return a JavaDoc scanner
173          */
174         public RuleBasedScanner getJavaDocScanner() {
175                 return fJavaDocScanner;
176         }
177         
178         /**
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.
182          *
183          * @return a Java partition scanner
184          */
185         public IPartitionTokenScanner getPartitionScanner() {
186                 return fPartitionScanner;
187         }
188         
189         /**
190          * Factory method for creating a Java-specific document partitioner
191          * using this object's partitions scanner. This method is a 
192          * convenience method.
193          *
194          * @return a newly created Java document partitioner
195          */
196         public IDocumentPartitioner createDocumentPartitioner() {
197                 
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
203                 };
204
205                 return new DefaultPartitioner(getPartitionScanner(), types);
206         }
207         
208         /**
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>.
213          *
214          * @return the partition managing position categories or <code>null</code> 
215          *                      if there is none
216          */
217         public String[] getPartitionManagingPositionCategories() {
218                 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
219         }
220         
221         /**
222          * Determines whether the preference change encoded by the given event
223          * changes the behavior of one its contained components.
224          * 
225          * @param event the event to be investigated
226          * @return <code>true</code> if event causes a behavioral change
227          * 
228          * @since 2.0
229          */
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);
236         }
237         
238         /**
239          * Adapts the behavior of the contained components to the change
240          * encoded in the given event.
241          * 
242          * @param event the event to which to adapt
243          * @since 2.0
244          */
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);
256         }
257 }