added a builder to parse files with eclipse's build mechanisms
[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.HTMLCodeScanner;
12 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
13
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;
21 //
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;
28
29
30 /**
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
34  * share those tools.
35  * <p>
36  * This class may be instantiated; it is not intended to be subclassed.
37  * </p>
38  */
39 public class JavaTextTools {
40         
41         private class PreferenceListener implements IPropertyChangeListener {
42                 public void propertyChange(PropertyChangeEvent event) {
43                         adaptToPreferenceChange(event);
44                 }
45         };
46                 
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;     
63         
64         /** The preference store */
65         private IPreferenceStore fPreferenceStore;
66         /** The preference change listener */
67         private PreferenceListener fPreferenceListener= new PreferenceListener();
68
69         
70         /**
71          * Creates a new Java text tools collection.
72          * 
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.
77          * 
78          * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
79          * @since 2.0
80          */
81         public JavaTextTools(IPreferenceStore store) {
82                 fPreferenceStore= store;
83                 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
84                 
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();
93         }
94         
95         /**
96          * Disposes all the individual tools of this tools collection.
97          */
98         public void dispose() {
99                 
100                 fCodeScanner= null;
101                 fMultilineCommentScanner= null;
102                 fSinglelineCommentScanner= null;
103                 fStringScanner= null;
104                 fJavaDocScanner= null;
105                 fPartitionScanner= null;
106                 
107                 if (fColorManager != null) {
108                         fColorManager.dispose();
109                         fColorManager= null;
110                 }
111                 
112                 if (fPreferenceStore != null) {
113                         fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
114                         fPreferenceStore= null;
115                         fPreferenceListener= null;
116                 }
117         }
118         
119         /**
120          * Returns the color manager which is used to manage
121          * any Java-specific colors needed for such things like syntax highlighting.
122          *
123          * @return the color manager to be used for Java text viewers
124          */
125         public JavaColorManager getColorManager() {
126                 return fColorManager;
127         }
128         
129         /**
130          * Returns a scanner which is configured to scan Java source code.
131          *
132          * @return a Java source code scanner
133          */
134         public RuleBasedScanner getCodeScanner() {
135                 return fCodeScanner;
136         }
137         
138         /**
139          * Returns a scanner which is configured to scan Java multiline comments.
140          *
141          * @return a Java multiline comment scanner
142          * 
143          * @since 2.0
144          */
145         public RuleBasedScanner getMultilineCommentScanner() {
146                 return fMultilineCommentScanner;
147         }
148
149   /**
150    * Returns a scanner which is configured to scan HTML code.
151    *
152    * @return a HTML scanner
153    * 
154    * @since 2.0
155    */
156   public RuleBasedScanner getHTMLScanner() {
157     return fHTMLScanner;
158   } 
159   
160         /**
161          * Returns a scanner which is configured to scan Java singleline comments.
162          *
163          * @return a Java singleline comment scanner
164          * 
165          * @since 2.0
166          */
167         public RuleBasedScanner getSinglelineCommentScanner() {
168                 return fSinglelineCommentScanner;
169         }
170         
171         /**
172          * Returns a scanner which is configured to scan Java strings.
173          *
174          * @return a Java string scanner
175          * 
176          * @since 2.0
177          */
178         public RuleBasedScanner getStringScanner() {
179                 return fStringScanner;
180         }
181         
182         /**
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.
186          *
187          * @return a JavaDoc scanner
188          */
189         public RuleBasedScanner getJavaDocScanner() {
190                 return fJavaDocScanner;
191         }
192         
193         /**
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.
197          *
198          * @return a Java partition scanner
199          */
200         public IPartitionTokenScanner getPartitionScanner() {
201                 return fPartitionScanner;
202         }
203         
204         /**
205          * Factory method for creating a Java-specific document partitioner
206          * using this object's partitions scanner. This method is a 
207          * convenience method.
208          *
209          * @return a newly created Java document partitioner
210          */
211         public IDocumentPartitioner createDocumentPartitioner() {
212                 
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
218                 };
219
220                 return new DefaultPartitioner(getPartitionScanner(), types);
221         }
222         
223         /**
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>.
228          *
229          * @return the partition managing position categories or <code>null</code> 
230          *                      if there is none
231          */
232         public String[] getPartitionManagingPositionCategories() {
233                 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
234         }
235         
236         /**
237          * Determines whether the preference change encoded by the given event
238          * changes the behavior of one its contained components.
239          * 
240          * @param event the event to be investigated
241          * @return <code>true</code> if event causes a behavioral change
242          * 
243          * @since 2.0
244          */
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);
251         }
252         
253         /**
254          * Adapts the behavior of the contained components to the change
255          * encoded in the given event.
256          * 
257          * @param event the event to which to adapt
258          * @since 2.0
259          */
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);
271         }
272 }