Improved CodeCompletion with information from the project.index file.
[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.IPreferenceConstants;
12 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCodeScanner;
13 import net.sourceforge.phpeclipse.phpeditor.php.PHPCodeScanner;
14
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.text.IDocumentPartitioner;
17 import org.eclipse.jface.text.rules.DefaultPartitioner;
18 import org.eclipse.jface.text.rules.IPartitionTokenScanner;
19 import org.eclipse.jface.text.rules.RuleBasedScanner;
20 import org.eclipse.jface.util.IPropertyChangeListener;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22 //
23 //import org.phpeclipse.phpdt.internal.ui.text.FastJavaPartitionScanner;
24 //import org.phpeclipse.phpdt.internal.ui.text.JavaColorManager;
25 //import org.phpeclipse.phpdt.internal.ui.text.JavaPartitionScanner;
26 //import org.phpeclipse.phpdt.internal.ui.text.SingleTokenJavaScanner;
27 //import org.phpeclipse.phpdt.internal.ui.text.php.JavaCodeScanner;
28 //import org.phpeclipse.phpdt.internal.ui.text.phpdoc.JavaDocScanner;
29
30
31 /**
32  * Tools required to configure a Java text viewer. 
33  * The color manager and all scanner exist only one time, i.e.
34  * the same instances are returned to all clients. Thus, clients
35  * share those tools.
36  * <p>
37  * This class may be instantiated; it is not intended to be subclassed.
38  * </p>
39  */
40 public class JavaTextTools {
41         
42         private class PreferenceListener implements IPropertyChangeListener {
43                 public void propertyChange(PropertyChangeEvent event) {
44                         adaptToPreferenceChange(event);
45                 }
46         };
47                 
48         /** The color manager */
49         private JavaColorManager fColorManager;
50         /** The PHP source code scanner */
51         private PHPCodeScanner fCodeScanner;
52         /** The PHP multiline comment scanner */
53         private SingleTokenPHPScanner fMultilineCommentScanner;
54         /** The Java singleline comment scanner */
55         private SingleTokenPHPScanner fSinglelineCommentScanner;
56         /** The Java string scanner */
57         private SingleTokenPHPScanner fStringScanner;
58         /** The PHPDoc scanner */
59         private PHPDocCodeScanner fJavaDocScanner;
60   /** The HTML scanner */
61   private HTMLCodeScanner fHTMLScanner;
62         /** The Java partitions scanner */
63         private FastJavaPartitionScanner fPartitionScanner;     
64         
65         /** The preference store */
66         private IPreferenceStore fPreferenceStore;
67         /** The preference change listener */
68         private PreferenceListener fPreferenceListener= new PreferenceListener();
69
70         
71         /**
72          * Creates a new Java text tools collection.
73          * 
74          * @param store the preference store to initialize the text tools. The text tool
75          * instance installs a listener on the passed preference store to adapt itself to 
76          * changes in the preference store. In general <code>PreferenceConstants.
77          * getPreferenceStore()</code> shoould be used to initialize the text tools.
78          * 
79          * @see org.phpeclipse.phpdt.ui.PreferenceConstants#getPreferenceStore()
80          * @since 2.0
81          */
82         public JavaTextTools(IPreferenceStore store) {
83                 fPreferenceStore= store;
84                 fPreferenceStore.addPropertyChangeListener(fPreferenceListener);
85                 
86                 fColorManager= new JavaColorManager();
87                 fCodeScanner= new PHPCodeScanner(fColorManager, store);
88                 fMultilineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_MULTILINE_COMMENT);
89                 fSinglelineCommentScanner= new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_SINGLELINE_COMMENT);
90                 fStringScanner= new SingleTokenPHPScanner(fColorManager, store, IPreferenceConstants.PHP_STRING);
91                 fJavaDocScanner= new PHPDocCodeScanner(fColorManager, store);
92     fHTMLScanner= new HTMLCodeScanner(fColorManager, store);
93                 fPartitionScanner= new FastJavaPartitionScanner();
94         }
95         
96         /**
97          * Disposes all the individual tools of this tools collection.
98          */
99         public void dispose() {
100                 
101                 fCodeScanner= null;
102                 fMultilineCommentScanner= null;
103                 fSinglelineCommentScanner= null;
104                 fStringScanner= null;
105                 fJavaDocScanner= null;
106                 fPartitionScanner= null;
107                 
108                 if (fColorManager != null) {
109                         fColorManager.dispose();
110                         fColorManager= null;
111                 }
112                 
113                 if (fPreferenceStore != null) {
114                         fPreferenceStore.removePropertyChangeListener(fPreferenceListener);
115                         fPreferenceStore= null;
116                         fPreferenceListener= null;
117                 }
118         }
119         
120         /**
121          * Returns the color manager which is used to manage
122          * any Java-specific colors needed for such things like syntax highlighting.
123          *
124          * @return the color manager to be used for Java text viewers
125          */
126         public JavaColorManager getColorManager() {
127                 return fColorManager;
128         }
129         
130         /**
131          * Returns a scanner which is configured to scan Java source code.
132          *
133          * @return a Java source code scanner
134          */
135         public RuleBasedScanner getCodeScanner() {
136                 return fCodeScanner;
137         }
138         
139         /**
140          * Returns a scanner which is configured to scan Java multiline comments.
141          *
142          * @return a Java multiline comment scanner
143          * 
144          * @since 2.0
145          */
146         public RuleBasedScanner getMultilineCommentScanner() {
147                 return fMultilineCommentScanner;
148         }
149
150   /**
151    * Returns a scanner which is configured to scan HTML code.
152    *
153    * @return a HTML scanner
154    * 
155    * @since 2.0
156    */
157   public RuleBasedScanner getHTMLScanner() {
158     return fHTMLScanner;
159   } 
160   
161         /**
162          * Returns a scanner which is configured to scan Java singleline comments.
163          *
164          * @return a Java singleline comment scanner
165          * 
166          * @since 2.0
167          */
168         public RuleBasedScanner getSinglelineCommentScanner() {
169                 return fSinglelineCommentScanner;
170         }
171         
172         /**
173          * Returns a scanner which is configured to scan Java strings.
174          *
175          * @return a Java string scanner
176          * 
177          * @since 2.0
178          */
179         public RuleBasedScanner getStringScanner() {
180                 return fStringScanner;
181         }
182         
183         /**
184          * Returns a scanner which is configured to scan JavaDoc compliant comments.
185          * Notes that the start sequence "/**" and the corresponding end sequence
186          * are part of the JavaDoc comment.
187          *
188          * @return a JavaDoc scanner
189          */
190         public RuleBasedScanner getJavaDocScanner() {
191                 return fJavaDocScanner;
192         }
193         
194         /**
195          * Returns a scanner which is configured to scan 
196          * Java-specific partitions, which are multi-line comments,
197          * JavaDoc comments, and regular Java source code.
198          *
199          * @return a Java partition scanner
200          */
201         public IPartitionTokenScanner getPartitionScanner() {
202                 return fPartitionScanner;
203         }
204         
205         /**
206          * Factory method for creating a Java-specific document partitioner
207          * using this object's partitions scanner. This method is a 
208          * convenience method.
209          *
210          * @return a newly created Java document partitioner
211          */
212         public IDocumentPartitioner createDocumentPartitioner() {
213                 
214                 String[] types= new String[] {
215       FastJavaPartitionScanner.JAVA_DOC,
216       FastJavaPartitionScanner.JAVA_MULTI_LINE_COMMENT,
217       FastJavaPartitionScanner.JAVA_SINGLE_LINE_COMMENT,
218       FastJavaPartitionScanner.JAVA_STRING
219                 };
220
221                 return new DefaultPartitioner(getPartitionScanner(), types);
222         }
223         
224         /**
225          * Returns the names of the document position categories used by the document
226          * partitioners created by this object to manage their partition information.
227          * If the partitioners don't use document position categories, the returned
228          * result is <code>null</code>.
229          *
230          * @return the partition managing position categories or <code>null</code> 
231          *                      if there is none
232          */
233         public String[] getPartitionManagingPositionCategories() {
234                 return new String[] { DefaultPartitioner.CONTENT_TYPES_CATEGORY };
235         }
236         
237         /**
238          * Determines whether the preference change encoded by the given event
239          * changes the behavior of one its contained components.
240          * 
241          * @param event the event to be investigated
242          * @return <code>true</code> if event causes a behavioral change
243          * 
244          * @since 2.0
245          */
246         public boolean affectsBehavior(PropertyChangeEvent event) {
247                 return fCodeScanner.affectsBehavior(event) ||
248                                         fMultilineCommentScanner.affectsBehavior(event) ||
249                                         fSinglelineCommentScanner.affectsBehavior(event) ||
250                                         fStringScanner.affectsBehavior(event) ||
251                                         fJavaDocScanner.affectsBehavior(event);
252         }
253         
254         /**
255          * Adapts the behavior of the contained components to the change
256          * encoded in the given event.
257          * 
258          * @param event the event to which to adapt
259          * @since 2.0
260          */
261         protected void adaptToPreferenceChange(PropertyChangeEvent event) {
262                 if (fCodeScanner.affectsBehavior(event))
263                         fCodeScanner.adaptToPreferenceChange(event);
264                 if (fMultilineCommentScanner.affectsBehavior(event))
265                         fMultilineCommentScanner.adaptToPreferenceChange(event);
266                 if (fSinglelineCommentScanner.affectsBehavior(event))
267                         fSinglelineCommentScanner.adaptToPreferenceChange(event);
268                 if (fStringScanner.affectsBehavior(event))
269                         fStringScanner.adaptToPreferenceChange(event);
270                 if (fJavaDocScanner.affectsBehavior(event))
271                         fJavaDocScanner.adaptToPreferenceChange(event);
272         }
273 }