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