aaec612961c58b884415cc65ca0e74ec4bd1c3ef
[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.IPHPPartitionScannerConstants;
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.IDocument;
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
25 public class ContentAssistPreference {
26         
27         /** Preference key for content assist auto activation */
28         private final static String AUTOACTIVATION=  PreferenceConstants.CODEASSIST_AUTOACTIVATION;
29         /** Preference key for content assist auto activation delay */
30         private final static String AUTOACTIVATION_DELAY=  PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY;
31         /** Preference key for content assist proposal color */
32         private final static String PROPOSALS_FOREGROUND=  PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND;
33         /** Preference key for content assist proposal color */
34         private final static String PROPOSALS_BACKGROUND=  PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND;
35         /** Preference key for content assist parameters color */
36         private final static String PARAMETERS_FOREGROUND=  PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND;
37         /** Preference key for content assist parameters color */
38         private final static String PARAMETERS_BACKGROUND=  PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND;
39         /** Preference key for content assist completion replacement color */
40         private final static String COMPLETION_REPLACEMENT_FOREGROUND=  PreferenceConstants.CODEASSIST_REPLACEMENT_FOREGROUND;
41         /** Preference key for content assist completion replacement color */
42         private final static String COMPLETION_REPLACEMENT_BACKGROUND=  PreferenceConstants.CODEASSIST_REPLACEMENT_BACKGROUND;
43         /** Preference key for content assist auto insert */
44         private final static String AUTOINSERT= PreferenceConstants.CODEASSIST_AUTOINSERT;
45         
46         /** Preference key for php content assist auto activation triggers */
47         private final static String AUTOACTIVATION_TRIGGERS_JAVA= PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA;
48         /** Preference key for phpdoc content assist auto activation triggers */
49         private final static String AUTOACTIVATION_TRIGGERS_JAVADOC= PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC;
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         
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 void configureJavaProcessor(ContentAssistant assistant, IPreferenceStore store) {
92                 PHPCompletionProcessor jcp= getJavaProcessor(assistant);
93                 if (jcp == null)
94                         return;
95                         
96                 String triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVA);
97                 if (triggers != null)
98                         jcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
99                         
100 //              boolean enabled= store.getBoolean(SHOW_VISIBLE_PROPOSALS);
101 //              jcp.restrictProposalsToVisibility(enabled);
102 //              
103 //              enabled= store.getBoolean(CASE_SENSITIVITY);
104 //              jcp.restrictProposalsToMatchingCases(enabled);
105 //              
106 //              enabled= store.getBoolean(ORDER_PROPOSALS);
107 //              jcp.orderProposalsAlphabetically(enabled);
108 //              
109 //              enabled= store.getBoolean(ADD_IMPORT);
110 //              jcp.allowAddingImports(enabled);                
111         }
112         
113         private static void configureJavaDocProcessor(ContentAssistant assistant, IPreferenceStore store) {
114                 PHPDocCompletionProcessor jdcp= getJavaDocProcessor(assistant);
115                 if (jdcp == null)
116                         return;
117                         
118                 String triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
119                 if (triggers != null)
120                         jdcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
121                         
122                 boolean enabled= store.getBoolean(CASE_SENSITIVITY);
123                 jdcp.restrictProposalsToMatchingCases(enabled);
124                 
125                 enabled= store.getBoolean(ORDER_PROPOSALS);
126                 jdcp.orderProposalsAlphabetically(enabled);
127         }
128         
129         /**
130          * Configure the given content assistant from the given store.
131          */
132         public static void configure(ContentAssistant assistant, IPreferenceStore store) {
133                 
134                 JavaTextTools textTools= PHPeclipsePlugin.getDefault().getJavaTextTools();
135                 IColorManager manager= textTools.getColorManager();             
136                 
137                 
138                 boolean enabled= store.getBoolean(AUTOACTIVATION);
139                 assistant.enableAutoActivation(enabled);
140                 
141                 int delay= store.getInt(AUTOACTIVATION_DELAY);
142                 assistant.setAutoActivationDelay(delay);
143                 
144                 Color c= getColor(store, PROPOSALS_FOREGROUND, manager);
145                 assistant.setProposalSelectorForeground(c);
146                 
147                 c= getColor(store, PROPOSALS_BACKGROUND, manager);
148                 assistant.setProposalSelectorBackground(c);
149                 
150                 c= getColor(store, PARAMETERS_FOREGROUND, manager);
151                 assistant.setContextInformationPopupForeground(c);
152                 assistant.setContextSelectorForeground(c);
153                 
154                 c= getColor(store, PARAMETERS_BACKGROUND, manager);
155                 assistant.setContextInformationPopupBackground(c);
156                 assistant.setContextSelectorBackground(c);
157                 
158                 enabled= store.getBoolean(AUTOINSERT);
159                 assistant.enableAutoInsert(enabled);
160
161                 configureJavaProcessor(assistant, store);
162                 configureJavaDocProcessor(assistant, store);
163         }
164         
165         
166         private static void changeJavaProcessor(ContentAssistant assistant, IPreferenceStore store, String key) {
167                 PHPCompletionProcessor jcp= getJavaProcessor(assistant);
168                 if (jcp == null)
169                         return;
170                         
171                 if (AUTOACTIVATION_TRIGGERS_JAVA.equals(key)) {
172                         String triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVA);
173                         if (triggers != null)
174                                 jcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
175                 } 
176 //              else if (SHOW_VISIBLE_PROPOSALS.equals(key)) {
177 //                      boolean enabled= store.getBoolean(SHOW_VISIBLE_PROPOSALS);
178 //                      jcp.restrictProposalsToVisibility(enabled);
179 //              } else if (CASE_SENSITIVITY.equals(key)) {
180 //                      boolean enabled= store.getBoolean(CASE_SENSITIVITY);
181 //                      jcp.restrictProposalsToMatchingCases(enabled);
182 //              } else if (ORDER_PROPOSALS.equals(key)) {
183 //                      boolean enable= store.getBoolean(ORDER_PROPOSALS);
184 //                      jcp.orderProposalsAlphabetically(enable);
185 //              } else if (ADD_IMPORT.equals(key)) {
186 //                      boolean enabled= store.getBoolean(ADD_IMPORT);
187 //                      jcp.allowAddingImports(enabled);
188 //              }
189         }
190         
191         private static void changeJavaDocProcessor(ContentAssistant assistant, IPreferenceStore store, String key) {
192                 PHPDocCompletionProcessor jdcp= getJavaDocProcessor(assistant);
193                 if (jdcp == null)
194                         return;
195                         
196                 if (AUTOACTIVATION_TRIGGERS_JAVADOC.equals(key)) {
197                         String triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
198                         if (triggers != null)
199                                 jdcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
200                 } else if (CASE_SENSITIVITY.equals(key)) {
201                         boolean enabled= store.getBoolean(CASE_SENSITIVITY);
202                         jdcp.restrictProposalsToMatchingCases(enabled);
203                 } else if (ORDER_PROPOSALS.equals(key)) {
204                         boolean enable= store.getBoolean(ORDER_PROPOSALS);
205                         jdcp.orderProposalsAlphabetically(enable);
206                 }
207         }
208         
209         /**
210          * Changes the configuration of the given content assistant according to the given property
211          * change event and the given preference store.
212          */
213         public static void changeConfiguration(ContentAssistant assistant, IPreferenceStore store, PropertyChangeEvent event) {
214                 
215                 String p= event.getProperty();
216                 
217                 if (AUTOACTIVATION.equals(p)) {
218                         boolean enabled= store.getBoolean(AUTOACTIVATION);
219                         assistant.enableAutoActivation(enabled);
220                 } else if (AUTOACTIVATION_DELAY.equals(p)) {
221                         int delay= store.getInt(AUTOACTIVATION_DELAY);
222                         assistant.setAutoActivationDelay(delay);
223                 } else if (PROPOSALS_FOREGROUND.equals(p)) {
224                         Color c= getColor(store, PROPOSALS_FOREGROUND);
225                         assistant.setProposalSelectorForeground(c);
226                 } else if (PROPOSALS_BACKGROUND.equals(p)) {
227                         Color c= getColor(store, PROPOSALS_BACKGROUND);
228                         assistant.setProposalSelectorBackground(c);
229                 } else if (PARAMETERS_FOREGROUND.equals(p)) {
230                         Color c= getColor(store, PARAMETERS_FOREGROUND);
231                         assistant.setContextInformationPopupForeground(c);
232                         assistant.setContextSelectorForeground(c);
233                 } else if (PARAMETERS_BACKGROUND.equals(p)) {
234                         Color c= getColor(store, PARAMETERS_BACKGROUND);
235                         assistant.setContextInformationPopupBackground(c);
236                         assistant.setContextSelectorBackground(c);
237                 } else if (AUTOINSERT.equals(p)) {
238                         boolean enabled= store.getBoolean(AUTOINSERT);
239                         assistant.enableAutoInsert(enabled);
240                 }
241                 
242                 changeJavaProcessor(assistant, store, p);
243                 changeJavaDocProcessor(assistant, store, p);
244         }
245
246         public static boolean fillArgumentsOnMethodCompletion(IPreferenceStore store) {
247                 return store.getBoolean(FILL_METHOD_ARGUMENTS);
248         }
249 }
250