KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > ContentAssistPreference


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.text;
12
13 import org.eclipse.swt.graphics.Color;
14 import org.eclipse.swt.graphics.RGB;
15
16 import org.eclipse.jface.preference.IPreferenceStore;
17 import org.eclipse.jface.preference.PreferenceConverter;
18 import org.eclipse.jface.text.IDocument;
19 import org.eclipse.jface.text.contentassist.ContentAssistant;
20 import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
21 import org.eclipse.jface.util.PropertyChangeEvent;
22
23 import org.eclipse.jdt.ui.PreferenceConstants;
24 import org.eclipse.jdt.ui.text.IColorManager;
25 import org.eclipse.jdt.ui.text.IJavaPartitions;
26 import org.eclipse.jdt.ui.text.JavaTextTools;
27
28 import org.eclipse.jdt.internal.ui.JavaPlugin;
29 import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor;
30 import org.eclipse.jdt.internal.ui.text.javadoc.JavadocCompletionProcessor;
31
32
33 public class ContentAssistPreference {
34
35     /** Preference key for content assist auto activation */
36     private final static String JavaDoc AUTOACTIVATION= PreferenceConstants.CODEASSIST_AUTOACTIVATION;
37     /** Preference key for content assist auto activation delay */
38     private final static String JavaDoc AUTOACTIVATION_DELAY= PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY;
39     /** Preference key for content assist proposal color */
40     private final static String JavaDoc PROPOSALS_FOREGROUND= PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND;
41     /** Preference key for content assist proposal color */
42     private final static String JavaDoc PROPOSALS_BACKGROUND= PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND;
43     /** Preference key for content assist parameters color */
44     private final static String JavaDoc PARAMETERS_FOREGROUND= PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND;
45     /** Preference key for content assist parameters color */
46     private final static String JavaDoc PARAMETERS_BACKGROUND= PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND;
47     /** Preference key for content assist auto insert */
48     private final static String JavaDoc AUTOINSERT= PreferenceConstants.CODEASSIST_AUTOINSERT;
49
50     /** Preference key for java content assist auto activation triggers */
51     private final static String JavaDoc AUTOACTIVATION_TRIGGERS_JAVA= PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA;
52     /** Preference key for javadoc content assist auto activation triggers */
53     private final static String JavaDoc AUTOACTIVATION_TRIGGERS_JAVADOC= PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC;
54
55     /** Preference key for visibility of proposals */
56     private final static String JavaDoc SHOW_VISIBLE_PROPOSALS= PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS;
57     /** Preference key for case sensitivity of proposals */
58     private final static String JavaDoc CASE_SENSITIVITY= PreferenceConstants.CODEASSIST_CASE_SENSITIVITY;
59     /** Preference key for adding imports on code assist */
60     /** Preference key for filling argument names on method completion */
61     private static final String JavaDoc FILL_METHOD_ARGUMENTS= PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES;
62     /** Preference key for prefix completion. */
63     private static final String JavaDoc PREFIX_COMPLETION= PreferenceConstants.CODEASSIST_PREFIX_COMPLETION;
64
65
66     private static Color getColor(IPreferenceStore store, String JavaDoc 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 JavaDoc key) {
72         JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
73         return getColor(store, key, textTools.getColorManager());
74     }
75
76     private static JavaCompletionProcessor getJavaProcessor(ContentAssistant assistant) {
77         IContentAssistProcessor p= assistant.getContentAssistProcessor(IDocument.DEFAULT_CONTENT_TYPE);
78         if (p instanceof JavaCompletionProcessor)
79             return (JavaCompletionProcessor) p;
80         return null;
81     }
82
83     private static JavadocCompletionProcessor getJavaDocProcessor(ContentAssistant assistant) {
84         IContentAssistProcessor p= assistant.getContentAssistProcessor(IJavaPartitions.JAVA_DOC);
85         if (p instanceof JavadocCompletionProcessor)
86             return (JavadocCompletionProcessor) p;
87         return null;
88     }
89
90     private static void configureJavaProcessor(ContentAssistant assistant, IPreferenceStore store) {
91         JavaCompletionProcessor jcp= getJavaProcessor(assistant);
92         if (jcp == null)
93             return;
94
95         String JavaDoc triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVA);
96         if (triggers != null)
97             jcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
98
99         boolean enabled= store.getBoolean(SHOW_VISIBLE_PROPOSALS);
100         jcp.restrictProposalsToVisibility(enabled);
101
102         enabled= store.getBoolean(CASE_SENSITIVITY);
103         jcp.restrictProposalsToMatchingCases(enabled);
104     }
105
106     private static void configureJavaDocProcessor(ContentAssistant assistant, IPreferenceStore store) {
107         JavadocCompletionProcessor jdcp= getJavaDocProcessor(assistant);
108         if (jdcp == null)
109             return;
110
111         String JavaDoc triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
112         if (triggers != null)
113             jdcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
114
115         boolean enabled= store.getBoolean(CASE_SENSITIVITY);
116         jdcp.restrictProposalsToMatchingCases(enabled);
117     }
118
119     /**
120      * Configure the given content assistant from the given store.
121      */

122     public static void configure(ContentAssistant assistant, IPreferenceStore store) {
123
124         JavaTextTools textTools= JavaPlugin.getDefault().getJavaTextTools();
125         IColorManager manager= textTools.getColorManager();
126
127
128         boolean enabled= store.getBoolean(AUTOACTIVATION);
129         assistant.enableAutoActivation(enabled);
130
131         int delay= store.getInt(AUTOACTIVATION_DELAY);
132         assistant.setAutoActivationDelay(delay);
133
134         Color c= getColor(store, PROPOSALS_FOREGROUND, manager);
135         assistant.setProposalSelectorForeground(c);
136
137         c= getColor(store, PROPOSALS_BACKGROUND, manager);
138         assistant.setProposalSelectorBackground(c);
139
140         c= getColor(store, PARAMETERS_FOREGROUND, manager);
141         assistant.setContextInformationPopupForeground(c);
142         assistant.setContextSelectorForeground(c);
143
144         c= getColor(store, PARAMETERS_BACKGROUND, manager);
145         assistant.setContextInformationPopupBackground(c);
146         assistant.setContextSelectorBackground(c);
147
148         enabled= store.getBoolean(AUTOINSERT);
149         assistant.enableAutoInsert(enabled);
150
151         enabled= store.getBoolean(PREFIX_COMPLETION);
152         assistant.enablePrefixCompletion(enabled);
153
154         configureJavaProcessor(assistant, store);
155         configureJavaDocProcessor(assistant, store);
156     }
157
158
159     private static void changeJavaProcessor(ContentAssistant assistant, IPreferenceStore store, String JavaDoc key) {
160         JavaCompletionProcessor jcp= getJavaProcessor(assistant);
161         if (jcp == null)
162             return;
163
164         if (AUTOACTIVATION_TRIGGERS_JAVA.equals(key)) {
165             String JavaDoc triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVA);
166             if (triggers != null)
167                 jcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
168         } else if (SHOW_VISIBLE_PROPOSALS.equals(key)) {
169             boolean enabled= store.getBoolean(SHOW_VISIBLE_PROPOSALS);
170             jcp.restrictProposalsToVisibility(enabled);
171         } else if (CASE_SENSITIVITY.equals(key)) {
172             boolean enabled= store.getBoolean(CASE_SENSITIVITY);
173             jcp.restrictProposalsToMatchingCases(enabled);
174         }
175     }
176
177     private static void changeJavaDocProcessor(ContentAssistant assistant, IPreferenceStore store, String JavaDoc key) {
178         JavadocCompletionProcessor jdcp= getJavaDocProcessor(assistant);
179         if (jdcp == null)
180             return;
181
182         if (AUTOACTIVATION_TRIGGERS_JAVADOC.equals(key)) {
183             String JavaDoc triggers= store.getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
184             if (triggers != null)
185                 jdcp.setCompletionProposalAutoActivationCharacters(triggers.toCharArray());
186         } else if (CASE_SENSITIVITY.equals(key)) {
187             boolean enabled= store.getBoolean(CASE_SENSITIVITY);
188             jdcp.restrictProposalsToMatchingCases(enabled);
189         }
190     }
191
192     /**
193      * Changes the configuration of the given content assistant according to the given property
194      * change event and the given preference store.
195      */

196     public static void changeConfiguration(ContentAssistant assistant, IPreferenceStore store, PropertyChangeEvent event) {
197
198         String JavaDoc p= event.getProperty();
199
200         if (AUTOACTIVATION.equals(p)) {
201             boolean enabled= store.getBoolean(AUTOACTIVATION);
202             assistant.enableAutoActivation(enabled);
203         } else if (AUTOACTIVATION_DELAY.equals(p)) {
204             int delay= store.getInt(AUTOACTIVATION_DELAY);
205             assistant.setAutoActivationDelay(delay);
206         } else if (PROPOSALS_FOREGROUND.equals(p)) {
207             Color c= getColor(store, PROPOSALS_FOREGROUND);
208             assistant.setProposalSelectorForeground(c);
209         } else if (PROPOSALS_BACKGROUND.equals(p)) {
210             Color c= getColor(store, PROPOSALS_BACKGROUND);
211             assistant.setProposalSelectorBackground(c);
212         } else if (PARAMETERS_FOREGROUND.equals(p)) {
213             Color c= getColor(store, PARAMETERS_FOREGROUND);
214             assistant.setContextInformationPopupForeground(c);
215             assistant.setContextSelectorForeground(c);
216         } else if (PARAMETERS_BACKGROUND.equals(p)) {
217             Color c= getColor(store, PARAMETERS_BACKGROUND);
218             assistant.setContextInformationPopupBackground(c);
219             assistant.setContextSelectorBackground(c);
220         } else if (AUTOINSERT.equals(p)) {
221             boolean enabled= store.getBoolean(AUTOINSERT);
222             assistant.enableAutoInsert(enabled);
223         } else if (PREFIX_COMPLETION.equals(p)) {
224             boolean enabled= store.getBoolean(PREFIX_COMPLETION);
225             assistant.enablePrefixCompletion(enabled);
226         }
227
228         changeJavaProcessor(assistant, store, p);
229         changeJavaDocProcessor(assistant, store, p);
230     }
231
232     public static boolean fillArgumentsOnMethodCompletion(IPreferenceStore store) {
233         return store.getBoolean(FILL_METHOD_ARGUMENTS);
234     }
235 }
236
237
Popular Tags