Fixing some bugs and making the plug-in compatible with Java 1.4
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / text / ContentAssistPreference.java
1 /*
2  * (c) Copyright IBM Corp. 2000, 2001.
3  * All Rights Reserved.
4  */
5 package net.sourceforge.phpdt.internal.ui.text;
6
7 import net.sourceforge.phpdt.internal.ui.text.phpdoc.PHPDocCompletionProcessor;
8 import net.sourceforge.phpdt.ui.PreferenceConstants;
9 import net.sourceforge.phpdt.ui.text.IColorManager;
10 import net.sourceforge.phpdt.ui.text.JavaTextTools;
11 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
12 import net.sourceforge.phpeclipse.phpeditor.php.HTMLCompletionProcessor;
13 import net.sourceforge.phpeclipse.phpeditor.php.PHPCompletionProcessor;
14
15 import org.eclipse.jface.preference.IPreferenceStore;
16 import org.eclipse.jface.preference.PreferenceConverter;
17 import org.eclipse.jface.text.contentassist.ContentAssistant;
18 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
19 import org.eclipse.jface.util.PropertyChangeEvent;
20 import org.eclipse.swt.graphics.Color;
21 import org.eclipse.swt.graphics.RGB;
22
23 public class ContentAssistPreference {
24
25         /** Preference key for content assist auto activation */
26         private final static String AUTOACTIVATION = PreferenceConstants.CODEASSIST_AUTOACTIVATION;
27
28         /** Preference key for content assist auto activation delay */
29         private final static String AUTOACTIVATION_DELAY = PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY;
30
31         /** Preference key for content assist proposal color */
32         private final static String PROPOSALS_FOREGROUND = PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND;
33
34         /** Preference key for content assist proposal color */
35         private final static String PROPOSALS_BACKGROUND = PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND;
36
37         /** Preference key for content assist parameters color */
38         private final static String PARAMETERS_FOREGROUND = PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND;
39
40         /** Preference key for content assist parameters color */
41         private final static String PARAMETERS_BACKGROUND = PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND;
42
43         /** Preference key for content assist completion replacement color */
44         private final static String COMPLETION_REPLACEMENT_FOREGROUND = PreferenceConstants.CODEASSIST_REPLACEMENT_FOREGROUND;
45
46         /** Preference key for content assist completion replacement color */
47         private final static String COMPLETION_REPLACEMENT_BACKGROUND = PreferenceConstants.CODEASSIST_REPLACEMENT_BACKGROUND;
48
49         /** Preference key for content assist auto insert */
50         private final static String AUTOINSERT = PreferenceConstants.CODEASSIST_AUTOINSERT;
51
52         /** Preference key for php content assist auto activation triggers */
53         private final static String AUTOACTIVATION_TRIGGERS_JAVA = PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA;
54
55         /** Preference key for phpdoc content assist auto activation triggers */
56         private final static String AUTOACTIVATION_TRIGGERS_JAVADOC = PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC;
57
58         /** Preference key for html content assist auto activation triggers */
59         private final static String AUTOACTIVATION_TRIGGERS_HTML = PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_HTML;
60
61         /** Preference key for visibility of proposals */
62         private final static String SHOW_VISIBLE_PROPOSALS = PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS;
63
64         /** Preference key for alphabetic ordering of proposals */
65         private final static String ORDER_PROPOSALS = PreferenceConstants.CODEASSIST_ORDER_PROPOSALS;
66
67         /** Preference key for case sensitivity of propsals */
68         private final static String CASE_SENSITIVITY = PreferenceConstants.CODEASSIST_CASE_SENSITIVITY;
69
70         /** Preference key for adding imports on code assist */
71         private final static String ADD_IMPORT = PreferenceConstants.CODEASSIST_ADDIMPORT;
72
73         /** Preference key for inserting content assist */
74         private static final String INSERT_COMPLETION = PreferenceConstants.CODEASSIST_INSERT_COMPLETION;
75
76         /** Preference key for filling argument names on method completion */
77         private static final String FILL_METHOD_ARGUMENTS = PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES;
78
79         /** Preference key for guessing argument names on method completion */
80         private static final String GUESS_METHOD_ARGUMENTS = PreferenceConstants.CODEASSIST_GUESS_METHOD_ARGUMENTS;
81
82         private static Color getColor(IPreferenceStore store, String key,
83                         IColorManager manager) {
84                 RGB rgb = PreferenceConverter.getColor(store, key);
85                 return manager.getColor(rgb);
86         }
87
88         private static Color getColor(IPreferenceStore store, String key) {
89                 JavaTextTools textTools = PHPeclipsePlugin.getDefault()
90                                 .getJavaTextTools();
91                 return getColor(store, key, textTools.getColorManager());
92         }
93
94         private static PHPCompletionProcessor getJavaProcessor(
95                         ContentAssistant assistant) {
96                 IContentAssistProcessor p = assistant
97                                 .getContentAssistProcessor(IPHPPartitions.PHP_PARTITIONING);
98                 if (p instanceof PHPCompletionProcessor)
99                         return (PHPCompletionProcessor) p;
100                 return null;
101         }
102
103         private static PHPDocCompletionProcessor getJavaDocProcessor(
104                         ContentAssistant assistant) {
105                 IContentAssistProcessor p = assistant
106                                 .getContentAssistProcessor(IPHPPartitions.PHP_PHPDOC_COMMENT);
107                 if (p instanceof PHPDocCompletionProcessor)
108                         return (PHPDocCompletionProcessor) p;
109                 return null;
110         }
111
112         private static HTMLCompletionProcessor getHTMLProcessor(
113                         ContentAssistant assistant) {
114                 IContentAssistProcessor p = assistant
115                                 .getContentAssistProcessor(IPHPPartitions.HTML);
116                 if (p instanceof HTMLCompletionProcessor)
117                         return (HTMLCompletionProcessor) p;
118                 return null;
119         }
120
121         private static void configureJavaProcessor(ContentAssistant assistant,
122                         IPreferenceStore store) {
123                 PHPCompletionProcessor pcp = getJavaProcessor(assistant);
124                 if (pcp == null)
125                         return;
126
127                 String triggers = store.getString(AUTOACTIVATION_TRIGGERS_JAVA);
128                 if (triggers != null)
129                         pcp.setCompletionProposalAutoActivationCharacters(triggers
130                                         .toCharArray());
131                 boolean enabled;
132                 // boolean enabled= store.getBoolean(SHOW_VISIBLE_PROPOSALS);
133                 // jcp.restrictProposalsToVisibility(enabled);
134                 //              
135                 // enabled= store.getBoolean(CASE_SENSITIVITY);
136                 // jcp.restrictProposalsToMatchingCases(enabled);
137                 //              
138                 enabled = store.getBoolean(ORDER_PROPOSALS);
139                 pcp.orderProposalsAlphabetically(enabled);
140                 //              
141                 // enabled= store.getBoolean(ADD_IMPORT);
142                 // jcp.allowAddingImports(enabled);
143         }
144
145         private static void configureJavaDocProcessor(ContentAssistant assistant,
146                         IPreferenceStore store) {
147                 PHPDocCompletionProcessor pdcp = getJavaDocProcessor(assistant);
148                 if (pdcp == null)
149                         return;
150
151                 String triggers = store.getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
152                 if (triggers != null)
153                         pdcp.setCompletionProposalAutoActivationCharacters(triggers
154                                         .toCharArray());
155
156                 boolean enabled = store.getBoolean(CASE_SENSITIVITY);
157                 pdcp.restrictProposalsToMatchingCases(enabled);
158
159                 enabled = store.getBoolean(ORDER_PROPOSALS);
160                 pdcp.orderProposalsAlphabetically(enabled);
161         }
162
163         private static void configureHTMLProcessor(ContentAssistant assistant,
164                         IPreferenceStore store) {
165                 HTMLCompletionProcessor hcp = getHTMLProcessor(assistant);
166                 if (hcp == null)
167                         return;
168
169                 String triggers = store.getString(AUTOACTIVATION_TRIGGERS_HTML);
170                 if (triggers != null)
171                         hcp.setCompletionProposalAutoActivationCharacters(triggers
172                                         .toCharArray());
173
174                 boolean enabled;
175                 // boolean enabled = store.getBoolean(CASE_SENSITIVITY);
176                 // jdcp.restrictProposalsToMatchingCases(enabled);
177
178                 enabled = store.getBoolean(ORDER_PROPOSALS);
179                 hcp.orderProposalsAlphabetically(enabled);
180         }
181
182         /**
183          * Configure the given content assistant from the given store.
184          */
185         public static void configure(ContentAssistant assistant,
186                         IPreferenceStore store) {
187
188                 JavaTextTools textTools = PHPeclipsePlugin.getDefault()
189                                 .getJavaTextTools();
190                 IColorManager manager = textTools.getColorManager();
191
192                 boolean enabled = store.getBoolean(AUTOACTIVATION);
193                 assistant.enableAutoActivation(enabled);
194
195                 int delay = store.getInt(AUTOACTIVATION_DELAY);
196                 assistant.setAutoActivationDelay(delay);
197
198                 Color c = getColor(store, PROPOSALS_FOREGROUND, manager);
199                 assistant.setProposalSelectorForeground(c);
200
201                 c = getColor(store, PROPOSALS_BACKGROUND, manager);
202                 assistant.setProposalSelectorBackground(c);
203
204                 c = getColor(store, PARAMETERS_FOREGROUND, manager);
205                 assistant.setContextInformationPopupForeground(c);
206                 assistant.setContextSelectorForeground(c);
207
208                 c = getColor(store, PARAMETERS_BACKGROUND, manager);
209                 assistant.setContextInformationPopupBackground(c);
210                 assistant.setContextSelectorBackground(c);
211
212                 enabled = store.getBoolean(AUTOINSERT);
213                 assistant.enableAutoInsert(enabled);
214
215                 configureJavaProcessor(assistant, store);
216                 configureJavaDocProcessor(assistant, store);
217                 configureHTMLProcessor(assistant, store);
218         }
219
220         private static void changeJavaProcessor(ContentAssistant assistant,
221                         IPreferenceStore store, String key) {
222                 PHPCompletionProcessor jcp = getJavaProcessor(assistant);
223                 if (jcp == null)
224                         return;
225
226                 if (AUTOACTIVATION_TRIGGERS_JAVA.equals(key)) {
227                         String triggers = store.getString(AUTOACTIVATION_TRIGGERS_JAVA);
228                         if (triggers != null)
229                                 jcp.setCompletionProposalAutoActivationCharacters(triggers
230                                                 .toCharArray());
231                 }
232                 // else if (SHOW_VISIBLE_PROPOSALS.equals(key)) {
233                 // boolean enabled= store.getBoolean(SHOW_VISIBLE_PROPOSALS);
234                 // jcp.restrictProposalsToVisibility(enabled);
235                 // } else if (CASE_SENSITIVITY.equals(key)) {
236                 // boolean enabled= store.getBoolean(CASE_SENSITIVITY);
237                 // jcp.restrictProposalsToMatchingCases(enabled); }
238                 else if (ORDER_PROPOSALS.equals(key)) {
239                         boolean enable = store.getBoolean(ORDER_PROPOSALS);
240                         jcp.orderProposalsAlphabetically(enable);
241                         // } else if (ADD_IMPORT.equals(key)) {
242                         // boolean enabled= store.getBoolean(ADD_IMPORT);
243                         // jcp.allowAddingImports(enabled);
244                 }
245         }
246
247         private static void changeJavaDocProcessor(ContentAssistant assistant,
248                         IPreferenceStore store, String key) {
249                 PHPDocCompletionProcessor jdcp = getJavaDocProcessor(assistant);
250                 if (jdcp == null)
251                         return;
252
253                 if (AUTOACTIVATION_TRIGGERS_JAVADOC.equals(key)) {
254                         String triggers = store.getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
255                         if (triggers != null)
256                                 jdcp.setCompletionProposalAutoActivationCharacters(triggers
257                                                 .toCharArray());
258                 } else if (CASE_SENSITIVITY.equals(key)) {
259                         boolean enabled = store.getBoolean(CASE_SENSITIVITY);
260                         jdcp.restrictProposalsToMatchingCases(enabled);
261                 } else if (ORDER_PROPOSALS.equals(key)) {
262                         boolean enable = store.getBoolean(ORDER_PROPOSALS);
263                         jdcp.orderProposalsAlphabetically(enable);
264                 }
265         }
266
267         private static void changeHTMLProcessor(ContentAssistant assistant,
268                         IPreferenceStore store, String key) {
269                 HTMLCompletionProcessor jdcp = getHTMLProcessor(assistant);
270                 if (jdcp == null)
271                         return;
272
273                 if (AUTOACTIVATION_TRIGGERS_HTML.equals(key)) {
274                         String triggers = store.getString(AUTOACTIVATION_TRIGGERS_HTML);
275                         if (triggers != null)
276                                 jdcp.setCompletionProposalAutoActivationCharacters(triggers
277                                                 .toCharArray());
278                         // } else if (CASE_SENSITIVITY.equals(key)) {
279                         // boolean enabled = store.getBoolean(CASE_SENSITIVITY);
280                         // jdcp.restrictProposalsToMatchingCases(enabled);
281                 } else if (ORDER_PROPOSALS.equals(key)) {
282                         boolean enable = store.getBoolean(ORDER_PROPOSALS);
283                         jdcp.orderProposalsAlphabetically(enable);
284                 }
285         }
286
287         /**
288          * Changes the configuration of the given content assistant according to the
289          * given property change event and the given preference store.
290          */
291         public static void changeConfiguration(ContentAssistant assistant,
292                         IPreferenceStore store, PropertyChangeEvent event) {
293
294                 String p = event.getProperty();
295
296                 if (AUTOACTIVATION.equals(p)) {
297                         boolean enabled = store.getBoolean(AUTOACTIVATION);
298                         assistant.enableAutoActivation(enabled);
299                 } else if (AUTOACTIVATION_DELAY.equals(p)) {
300                         int delay = store.getInt(AUTOACTIVATION_DELAY);
301                         assistant.setAutoActivationDelay(delay);
302                 } else if (PROPOSALS_FOREGROUND.equals(p)) {
303                         Color c = getColor(store, PROPOSALS_FOREGROUND);
304                         assistant.setProposalSelectorForeground(c);
305                 } else if (PROPOSALS_BACKGROUND.equals(p)) {
306                         Color c = getColor(store, PROPOSALS_BACKGROUND);
307                         assistant.setProposalSelectorBackground(c);
308                 } else if (PARAMETERS_FOREGROUND.equals(p)) {
309                         Color c = getColor(store, PARAMETERS_FOREGROUND);
310                         assistant.setContextInformationPopupForeground(c);
311                         assistant.setContextSelectorForeground(c);
312                 } else if (PARAMETERS_BACKGROUND.equals(p)) {
313                         Color c = getColor(store, PARAMETERS_BACKGROUND);
314                         assistant.setContextInformationPopupBackground(c);
315                         assistant.setContextSelectorBackground(c);
316                 } else if (AUTOINSERT.equals(p)) {
317                         boolean enabled = store.getBoolean(AUTOINSERT);
318                         assistant.enableAutoInsert(enabled);
319                 }
320
321                 changeJavaProcessor(assistant, store, p);
322                 changeJavaDocProcessor(assistant, store, p);
323                 changeHTMLProcessor(assistant, store, p);
324         }
325
326         public static boolean fillArgumentsOnMethodCompletion(IPreferenceStore store) {
327                 return store.getBoolean(FILL_METHOD_ARGUMENTS);
328         }
329 }