001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.text;
011:
012: import org.eclipse.swt.graphics.Color;
013: import org.eclipse.swt.graphics.RGB;
014:
015: import org.eclipse.jface.preference.IPreferenceStore;
016: import org.eclipse.jface.preference.PreferenceConverter;
017: import org.eclipse.jface.text.IDocument;
018: import org.eclipse.jface.text.contentassist.ContentAssistant;
019: import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
020: import org.eclipse.jface.util.PropertyChangeEvent;
021:
022: import org.eclipse.jdt.ui.PreferenceConstants;
023: import org.eclipse.jdt.ui.text.IColorManager;
024: import org.eclipse.jdt.ui.text.IJavaPartitions;
025: import org.eclipse.jdt.ui.text.JavaTextTools;
026:
027: import org.eclipse.jdt.internal.ui.JavaPlugin;
028: import org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor;
029: import org.eclipse.jdt.internal.ui.text.javadoc.JavadocCompletionProcessor;
030:
031: public class ContentAssistPreference {
032:
033: /** Preference key for content assist auto activation */
034: private final static String AUTOACTIVATION = PreferenceConstants.CODEASSIST_AUTOACTIVATION;
035: /** Preference key for content assist auto activation delay */
036: private final static String AUTOACTIVATION_DELAY = PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY;
037: /** Preference key for content assist proposal color */
038: private final static String PROPOSALS_FOREGROUND = PreferenceConstants.CODEASSIST_PROPOSALS_FOREGROUND;
039: /** Preference key for content assist proposal color */
040: private final static String PROPOSALS_BACKGROUND = PreferenceConstants.CODEASSIST_PROPOSALS_BACKGROUND;
041: /** Preference key for content assist parameters color */
042: private final static String PARAMETERS_FOREGROUND = PreferenceConstants.CODEASSIST_PARAMETERS_FOREGROUND;
043: /** Preference key for content assist parameters color */
044: private final static String PARAMETERS_BACKGROUND = PreferenceConstants.CODEASSIST_PARAMETERS_BACKGROUND;
045: /** Preference key for content assist auto insert */
046: private final static String AUTOINSERT = PreferenceConstants.CODEASSIST_AUTOINSERT;
047:
048: /** Preference key for java content assist auto activation triggers */
049: private final static String AUTOACTIVATION_TRIGGERS_JAVA = PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA;
050: /** Preference key for javadoc content assist auto activation triggers */
051: private final static String AUTOACTIVATION_TRIGGERS_JAVADOC = PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC;
052:
053: /** Preference key for visibility of proposals */
054: private final static String SHOW_VISIBLE_PROPOSALS = PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS;
055: /** Preference key for case sensitivity of proposals */
056: private final static String CASE_SENSITIVITY = PreferenceConstants.CODEASSIST_CASE_SENSITIVITY;
057: /** Preference key for adding imports on code assist */
058: /** Preference key for filling argument names on method completion */
059: private static final String FILL_METHOD_ARGUMENTS = PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES;
060: /** Preference key for prefix completion. */
061: private static final String PREFIX_COMPLETION = PreferenceConstants.CODEASSIST_PREFIX_COMPLETION;
062:
063: private static Color getColor(IPreferenceStore store, String key,
064: IColorManager manager) {
065: RGB rgb = PreferenceConverter.getColor(store, key);
066: return manager.getColor(rgb);
067: }
068:
069: private static Color getColor(IPreferenceStore store, String key) {
070: JavaTextTools textTools = JavaPlugin.getDefault()
071: .getJavaTextTools();
072: return getColor(store, key, textTools.getColorManager());
073: }
074:
075: private static JavaCompletionProcessor getJavaProcessor(
076: ContentAssistant assistant) {
077: IContentAssistProcessor p = assistant
078: .getContentAssistProcessor(IDocument.DEFAULT_CONTENT_TYPE);
079: if (p instanceof JavaCompletionProcessor)
080: return (JavaCompletionProcessor) p;
081: return null;
082: }
083:
084: private static JavadocCompletionProcessor getJavaDocProcessor(
085: ContentAssistant assistant) {
086: IContentAssistProcessor p = assistant
087: .getContentAssistProcessor(IJavaPartitions.JAVA_DOC);
088: if (p instanceof JavadocCompletionProcessor)
089: return (JavadocCompletionProcessor) p;
090: return null;
091: }
092:
093: private static void configureJavaProcessor(
094: ContentAssistant assistant, IPreferenceStore store) {
095: JavaCompletionProcessor jcp = getJavaProcessor(assistant);
096: if (jcp == null)
097: return;
098:
099: String triggers = store.getString(AUTOACTIVATION_TRIGGERS_JAVA);
100: if (triggers != null)
101: jcp.setCompletionProposalAutoActivationCharacters(triggers
102: .toCharArray());
103:
104: boolean enabled = store.getBoolean(SHOW_VISIBLE_PROPOSALS);
105: jcp.restrictProposalsToVisibility(enabled);
106:
107: enabled = store.getBoolean(CASE_SENSITIVITY);
108: jcp.restrictProposalsToMatchingCases(enabled);
109: }
110:
111: private static void configureJavaDocProcessor(
112: ContentAssistant assistant, IPreferenceStore store) {
113: JavadocCompletionProcessor jdcp = getJavaDocProcessor(assistant);
114: if (jdcp == null)
115: return;
116:
117: String triggers = store
118: .getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
119: if (triggers != null)
120: jdcp.setCompletionProposalAutoActivationCharacters(triggers
121: .toCharArray());
122:
123: boolean enabled = store.getBoolean(CASE_SENSITIVITY);
124: jdcp.restrictProposalsToMatchingCases(enabled);
125: }
126:
127: /**
128: * Configure the given content assistant from the given store.
129: */
130: public static void configure(ContentAssistant assistant,
131: IPreferenceStore store) {
132:
133: JavaTextTools textTools = JavaPlugin.getDefault()
134: .getJavaTextTools();
135: IColorManager manager = textTools.getColorManager();
136:
137: boolean enabled = store.getBoolean(AUTOACTIVATION);
138: assistant.enableAutoActivation(enabled);
139:
140: int delay = store.getInt(AUTOACTIVATION_DELAY);
141: assistant.setAutoActivationDelay(delay);
142:
143: Color c = getColor(store, PROPOSALS_FOREGROUND, manager);
144: assistant.setProposalSelectorForeground(c);
145:
146: c = getColor(store, PROPOSALS_BACKGROUND, manager);
147: assistant.setProposalSelectorBackground(c);
148:
149: c = getColor(store, PARAMETERS_FOREGROUND, manager);
150: assistant.setContextInformationPopupForeground(c);
151: assistant.setContextSelectorForeground(c);
152:
153: c = getColor(store, PARAMETERS_BACKGROUND, manager);
154: assistant.setContextInformationPopupBackground(c);
155: assistant.setContextSelectorBackground(c);
156:
157: enabled = store.getBoolean(AUTOINSERT);
158: assistant.enableAutoInsert(enabled);
159:
160: enabled = store.getBoolean(PREFIX_COMPLETION);
161: assistant.enablePrefixCompletion(enabled);
162:
163: configureJavaProcessor(assistant, store);
164: configureJavaDocProcessor(assistant, store);
165: }
166:
167: private static void changeJavaProcessor(ContentAssistant assistant,
168: IPreferenceStore store, String key) {
169: JavaCompletionProcessor jcp = getJavaProcessor(assistant);
170: if (jcp == null)
171: return;
172:
173: if (AUTOACTIVATION_TRIGGERS_JAVA.equals(key)) {
174: String triggers = store
175: .getString(AUTOACTIVATION_TRIGGERS_JAVA);
176: if (triggers != null)
177: jcp
178: .setCompletionProposalAutoActivationCharacters(triggers
179: .toCharArray());
180: } else if (SHOW_VISIBLE_PROPOSALS.equals(key)) {
181: boolean enabled = store.getBoolean(SHOW_VISIBLE_PROPOSALS);
182: jcp.restrictProposalsToVisibility(enabled);
183: } else if (CASE_SENSITIVITY.equals(key)) {
184: boolean enabled = store.getBoolean(CASE_SENSITIVITY);
185: jcp.restrictProposalsToMatchingCases(enabled);
186: }
187: }
188:
189: private static void changeJavaDocProcessor(
190: ContentAssistant assistant, IPreferenceStore store,
191: String key) {
192: JavadocCompletionProcessor jdcp = getJavaDocProcessor(assistant);
193: if (jdcp == null)
194: return;
195:
196: if (AUTOACTIVATION_TRIGGERS_JAVADOC.equals(key)) {
197: String triggers = store
198: .getString(AUTOACTIVATION_TRIGGERS_JAVADOC);
199: if (triggers != null)
200: jdcp
201: .setCompletionProposalAutoActivationCharacters(triggers
202: .toCharArray());
203: } else if (CASE_SENSITIVITY.equals(key)) {
204: boolean enabled = store.getBoolean(CASE_SENSITIVITY);
205: jdcp.restrictProposalsToMatchingCases(enabled);
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,
214: IPreferenceStore store, PropertyChangeEvent event) {
215:
216: String p = event.getProperty();
217:
218: if (AUTOACTIVATION.equals(p)) {
219: boolean enabled = store.getBoolean(AUTOACTIVATION);
220: assistant.enableAutoActivation(enabled);
221: } else if (AUTOACTIVATION_DELAY.equals(p)) {
222: int delay = store.getInt(AUTOACTIVATION_DELAY);
223: assistant.setAutoActivationDelay(delay);
224: } else if (PROPOSALS_FOREGROUND.equals(p)) {
225: Color c = getColor(store, PROPOSALS_FOREGROUND);
226: assistant.setProposalSelectorForeground(c);
227: } else if (PROPOSALS_BACKGROUND.equals(p)) {
228: Color c = getColor(store, PROPOSALS_BACKGROUND);
229: assistant.setProposalSelectorBackground(c);
230: } else if (PARAMETERS_FOREGROUND.equals(p)) {
231: Color c = getColor(store, PARAMETERS_FOREGROUND);
232: assistant.setContextInformationPopupForeground(c);
233: assistant.setContextSelectorForeground(c);
234: } else if (PARAMETERS_BACKGROUND.equals(p)) {
235: Color c = getColor(store, PARAMETERS_BACKGROUND);
236: assistant.setContextInformationPopupBackground(c);
237: assistant.setContextSelectorBackground(c);
238: } else if (AUTOINSERT.equals(p)) {
239: boolean enabled = store.getBoolean(AUTOINSERT);
240: assistant.enableAutoInsert(enabled);
241: } else if (PREFIX_COMPLETION.equals(p)) {
242: boolean enabled = store.getBoolean(PREFIX_COMPLETION);
243: assistant.enablePrefixCompletion(enabled);
244: }
245:
246: changeJavaProcessor(assistant, store, p);
247: changeJavaDocProcessor(assistant, store, p);
248: }
249:
250: public static boolean fillArgumentsOnMethodCompletion(
251: IPreferenceStore store) {
252: return store.getBoolean(FILL_METHOD_ARGUMENTS);
253: }
254: }
|