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