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