KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > preferences > CodeAssistConfigurationBlock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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
12 package org.eclipse.jdt.internal.ui.preferences;
13
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.core.runtime.IStatus;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.SelectionListener;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Button;
26 import org.eclipse.swt.widgets.Composite;
27 import org.eclipse.swt.widgets.Control;
28 import org.eclipse.swt.widgets.Group;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Link;
31 import org.eclipse.swt.widgets.Text;
32
33 import org.eclipse.jface.preference.IPreferenceStore;
34
35 import org.eclipse.ui.dialogs.PreferencesUtil;
36 import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
37
38 import org.eclipse.jdt.core.JavaCore;
39
40 import org.eclipse.jdt.internal.corext.util.Messages;
41
42 import org.eclipse.jdt.ui.PreferenceConstants;
43
44 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
45 import org.eclipse.jdt.internal.ui.text.java.ProposalSorterHandle;
46 import org.eclipse.jdt.internal.ui.text.java.ProposalSorterRegistry;
47 import org.eclipse.jdt.internal.ui.util.PixelConverter;
48 import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
49
50 /**
51  * Configures the content assist preferences.
52  *
53  * @since 3.0
54  */

55 class CodeAssistConfigurationBlock extends OptionsConfigurationBlock {
56     
57     private static final Key PREF_CODEASSIST_AUTOACTIVATION= getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION);
58     private static final Key PREF_CODEASSIST_AUTOACTIVATION_DELAY= getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY);
59     private static final Key PREF_CODEASSIST_AUTOINSERT= getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOINSERT);
60     private static final Key PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA= getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA);
61     private static final Key PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC= getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC);
62     private static final Key PREF_CODEASSIST_SHOW_VISIBLE_PROPOSALS= getJDTUIKey(PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS);
63     private static final Key PREF_CODEASSIST_SORTER= getJDTUIKey(PreferenceConstants.CODEASSIST_SORTER);
64     private static final Key PREF_CODEASSIST_CASE_SENSITIVITY= getJDTUIKey(PreferenceConstants.CODEASSIST_CASE_SENSITIVITY);
65     private static final Key PREF_CODEASSIST_ADDIMPORT= getJDTUIKey(PreferenceConstants.CODEASSIST_ADDIMPORT);
66     private static final Key PREF_CODEASSIST_SUGGEST_STATIC_IMPORTS= getJDTCoreKey(JavaCore.CODEASSIST_SUGGEST_STATIC_IMPORTS);
67     private static final Key PREF_CODEASSIST_INSERT_COMPLETION= getJDTUIKey(PreferenceConstants.CODEASSIST_INSERT_COMPLETION);
68     private static final Key PREF_CODEASSIST_FILL_ARGUMENT_NAMES= getJDTUIKey(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES);
69     private static final Key PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS= getJDTUIKey(PreferenceConstants.CODEASSIST_GUESS_METHOD_ARGUMENTS);
70     private static final Key PREF_CODEASSIST_PREFIX_COMPLETION= getJDTUIKey(PreferenceConstants.CODEASSIST_PREFIX_COMPLETION);
71     private static final Key PREF_CODEASSIST_FORBIDDEN_REFERENCE_CHECK= getJDTCoreKey(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK);
72     private static final Key PREF_CODEASSIST_DISCOURAGED_REFERENCE_CHECK= getJDTCoreKey(JavaCore.CODEASSIST_DISCOURAGED_REFERENCE_CHECK);
73     private static final Key PREF_CODEASSIST_DEPRECATION_CHECK= getJDTCoreKey(JavaCore.CODEASSIST_DEPRECATION_CHECK);
74     private static final Key PREF_CODEASSIST_CAMEL_CASE_MATCH= getJDTCoreKey(JavaCore.CODEASSIST_CAMEL_CASE_MATCH);
75
76     private static Key[] getAllKeys() {
77         return new Key[] {
78                 PREF_CODEASSIST_AUTOACTIVATION,
79                 PREF_CODEASSIST_AUTOACTIVATION_DELAY,
80                 PREF_CODEASSIST_AUTOINSERT,
81                 PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA,
82                 PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC,
83                 PREF_CODEASSIST_SHOW_VISIBLE_PROPOSALS,
84                 PREF_CODEASSIST_SORTER,
85                 PREF_CODEASSIST_CASE_SENSITIVITY,
86                 PREF_CODEASSIST_ADDIMPORT,
87                 PREF_CODEASSIST_SUGGEST_STATIC_IMPORTS,
88                 PREF_CODEASSIST_INSERT_COMPLETION,
89                 PREF_CODEASSIST_FILL_ARGUMENT_NAMES,
90                 PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS,
91                 PREF_CODEASSIST_PREFIX_COMPLETION,
92                 PREF_CODEASSIST_FORBIDDEN_REFERENCE_CHECK,
93                 PREF_CODEASSIST_DISCOURAGED_REFERENCE_CHECK,
94                 PREF_CODEASSIST_DEPRECATION_CHECK,
95                 PREF_CODEASSIST_CAMEL_CASE_MATCH,
96         };
97     }
98     
99     private static final String JavaDoc[] trueFalse= new String JavaDoc[] { IPreferenceStore.TRUE, IPreferenceStore.FALSE };
100     private static final String JavaDoc[] enabledDisabled= new String JavaDoc[] { JavaCore.ENABLED, JavaCore.DISABLED };
101
102     private Button fCompletionInsertsRadioButton;
103     private Button fCompletionOverwritesRadioButton;
104
105     public CodeAssistConfigurationBlock(IStatusChangeListener statusListener, IWorkbenchPreferenceContainer workbenchcontainer) {
106         super(statusListener, null, getAllKeys(), workbenchcontainer);
107     }
108
109     protected Control createContents(Composite parent) {
110         ScrolledPageContent scrolled= new ScrolledPageContent(parent, SWT.H_SCROLL | SWT.V_SCROLL);
111         scrolled.setExpandHorizontal(true);
112         scrolled.setExpandVertical(true);
113         
114         Composite control= new Composite(scrolled, SWT.NONE);
115         GridLayout layout= new GridLayout();
116         layout.marginHeight= 0;
117         layout.marginWidth= 0;
118         control.setLayout(layout);
119
120         Composite composite;
121
122         composite= createSubsection(control, PreferencesMessages.CodeAssistConfigurationBlock_insertionSection_title);
123         addInsertionSection(composite);
124         
125         composite= createSubsection(control, PreferencesMessages.CodeAssistConfigurationBlock_sortingSection_title);
126         addSortingSection(composite);
127         
128         composite= createSubsection(control, PreferencesMessages.CodeAssistConfigurationBlock_autoactivationSection_title);
129         addAutoActivationSection(composite);
130         
131         initialize();
132         
133         scrolled.setContent(control);
134         final Point size= control.computeSize(SWT.DEFAULT, SWT.DEFAULT);
135         scrolled.setMinSize(size.x, size.y);
136         return scrolled;
137     }
138
139     protected Composite createSubsection(Composite parent, String JavaDoc label) {
140         Group group= new Group(parent, SWT.SHADOW_NONE);
141         group.setText(label);
142         GridData data= new GridData(SWT.FILL, SWT.CENTER, true, false);
143         group.setLayoutData(data);
144         GridLayout layout= new GridLayout();
145         layout.numColumns= 3;
146         group.setLayout(layout);
147
148         return group;
149     }
150
151     private void addInsertionSection(Composite composite) {
152         addCompletionRadioButtons(composite);
153         
154         String JavaDoc label;
155         label= PreferencesMessages.JavaEditorPreferencePage_insertSingleProposalsAutomatically;
156         addCheckBox(composite, label, PREF_CODEASSIST_AUTOINSERT, trueFalse, 0);
157         
158         label= PreferencesMessages.JavaEditorPreferencePage_completePrefixes;
159         addCheckBox(composite, label, PREF_CODEASSIST_PREFIX_COMPLETION, trueFalse, 0);
160         
161         label= PreferencesMessages.JavaEditorPreferencePage_automaticallyAddImportInsteadOfQualifiedName;
162         Button master= addCheckBox(composite, label, PREF_CODEASSIST_ADDIMPORT, trueFalse, 0);
163         
164         label= PreferencesMessages.JavaEditorPreferencePage_suggestStaticImports;
165         Button slave= addCheckBox(composite, label, PREF_CODEASSIST_SUGGEST_STATIC_IMPORTS, enabledDisabled, 20);
166         createSelectionDependency(master, slave);
167         
168         
169         label= PreferencesMessages.JavaEditorPreferencePage_fillArgumentNamesOnMethodCompletion;
170         master= addCheckBox(composite, label, PREF_CODEASSIST_FILL_ARGUMENT_NAMES, trueFalse, 0);
171         
172         label= PreferencesMessages.JavaEditorPreferencePage_guessArgumentNamesOnMethodCompletion;
173         slave= addCheckBox(composite, label, PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS, trueFalse, 20);
174         createSelectionDependency(master, slave);
175     }
176
177     /**
178      * Creates a selection dependency between a master and a slave control.
179      *
180      * @param master
181      * The master button that controls the state of the slave
182      * @param slave
183      * The slave control that is enabled only if the master is
184      * selected
185      */

186     protected static void createSelectionDependency(final Button master, final Control slave) {
187
188         master.addSelectionListener(new SelectionListener() {
189
190             public void widgetDefaultSelected(SelectionEvent event) {
191                 // Do nothing
192
}
193
194             public void widgetSelected(SelectionEvent event) {
195                 slave.setEnabled(master.getSelection());
196             }
197         });
198         slave.setEnabled(master.getSelection());
199     }
200     
201     private void addSortingSection(Composite composite) {
202         String JavaDoc label;
203         label= PreferencesMessages.JavaEditorPreferencePage_presentProposalsInAlphabeticalOrder;
204         ProposalSorterHandle[] sorters= ProposalSorterRegistry.getDefault().getSorters();
205         String JavaDoc[] labels= new String JavaDoc[sorters.length];
206         String JavaDoc[] values= new String JavaDoc[sorters.length];
207         for (int i= 0; i < sorters.length; i++) {
208             ProposalSorterHandle handle= sorters[i];
209             labels[i]= handle.getName();
210             values[i]= handle.getId();
211         }
212         
213         addComboBox(composite, label, PREF_CODEASSIST_SORTER, values, labels, 0);
214         
215         label= PreferencesMessages.JavaEditorPreferencePage_showOnlyProposalsVisibleInTheInvocationContext;
216         addCheckBox(composite, label, PREF_CODEASSIST_SHOW_VISIBLE_PROPOSALS, trueFalse, 0);
217         
218         label= PreferencesMessages.CodeAssistConfigurationBlock_matchCamelCase_label;
219         addCheckBox(composite, label, PREF_CODEASSIST_CAMEL_CASE_MATCH, enabledDisabled, 0);
220
221         label= PreferencesMessages.CodeAssistConfigurationBlock_restricted_link;
222         Map JavaDoc targetInfo= new java.util.HashMap JavaDoc(2);
223         targetInfo.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_KEY, JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE);
224         targetInfo.put(ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_QUALIFIER, JavaCore.PLUGIN_ID);
225         createPreferencePageLink(composite, label, targetInfo);
226         
227         
228         label= PreferencesMessages.CodeAssistConfigurationBlock_hideForbidden_label;
229         addCheckBox(composite, label, PREF_CODEASSIST_FORBIDDEN_REFERENCE_CHECK, enabledDisabled, 0);
230         
231         label= PreferencesMessages.CodeAssistConfigurationBlock_hideDiscouraged_label;
232         addCheckBox(composite, label, PREF_CODEASSIST_DISCOURAGED_REFERENCE_CHECK, enabledDisabled, 0);
233         
234         label= PreferencesMessages.CodeAssistConfigurationBlock_hideDeprecated_label;
235         addCheckBox(composite, label, PREF_CODEASSIST_DEPRECATION_CHECK, enabledDisabled, 0);
236     }
237
238     private void createPreferencePageLink(Composite composite, String JavaDoc label, final Map JavaDoc targetInfo) {
239         final Link link= new Link(composite, SWT.NONE);
240         link.setText(label);
241         link.addSelectionListener(new SelectionAdapter() {
242             public void widgetSelected(SelectionEvent e) {
243                 PreferencesUtil.createPreferenceDialogOn(link.getShell(), e.text, null, targetInfo);
244             }
245         });
246     }
247     
248     private void addAutoActivationSection(Composite composite) {
249         String JavaDoc label;
250         label= PreferencesMessages.JavaEditorPreferencePage_enableAutoActivation;
251         final Button autoactivation= addCheckBox(composite, label, PREF_CODEASSIST_AUTOACTIVATION, trueFalse, 0);
252         autoactivation.addSelectionListener(new SelectionAdapter(){
253             public void widgetSelected(SelectionEvent e) {
254                 updateAutoactivationControls();
255             }
256         });
257         
258         label= PreferencesMessages.JavaEditorPreferencePage_autoActivationDelay;
259         addLabelledTextField(composite, label, PREF_CODEASSIST_AUTOACTIVATION_DELAY, 4, 0, true);
260         
261         label= PreferencesMessages.JavaEditorPreferencePage_autoActivationTriggersForJava;
262         addLabelledTextField(composite, label, PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA, 4, 0, false);
263         
264         label= PreferencesMessages.JavaEditorPreferencePage_autoActivationTriggersForJavaDoc;
265         addLabelledTextField(composite, label, PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC, 4, 0, false);
266     }
267     
268     protected Text addLabelledTextField(Composite parent, String JavaDoc label, Key key, int textlimit, int indent, boolean dummy) {
269         PixelConverter pixelConverter= new PixelConverter(parent);
270         
271         Label labelControl= new Label(parent, SWT.WRAP);
272         labelControl.setText(label);
273         labelControl.setLayoutData(new GridData());
274                 
275         Text textBox= new Text(parent, SWT.BORDER | SWT.SINGLE);
276         textBox.setData(key);
277         textBox.setLayoutData(new GridData());
278         
279         fLabels.put(textBox, labelControl);
280         
281         String JavaDoc currValue= getValue(key);
282         if (currValue != null) {
283             textBox.setText(currValue);
284         }
285         textBox.addModifyListener(getTextModifyListener());
286
287         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_FILL);
288         if (textlimit != 0) {
289             textBox.setTextLimit(textlimit);
290             data.widthHint= pixelConverter.convertWidthInCharsToPixels(textlimit + 1);
291         }
292         data.horizontalIndent= indent;
293         data.horizontalSpan= 2;
294         textBox.setLayoutData(data);
295
296         fTextBoxes.add(textBox);
297         return textBox;
298     }
299
300     private void addCompletionRadioButtons(Composite contentAssistComposite) {
301         Composite completionComposite= new Composite(contentAssistComposite, SWT.NONE);
302         GridData ccgd= new GridData();
303         ccgd.horizontalSpan= 2;
304         completionComposite.setLayoutData(ccgd);
305         GridLayout ccgl= new GridLayout();
306         ccgl.marginWidth= 0;
307         ccgl.numColumns= 2;
308         completionComposite.setLayout(ccgl);
309         
310         SelectionListener completionSelectionListener= new SelectionAdapter() {
311             public void widgetSelected(SelectionEvent e) {
312                 boolean insert= fCompletionInsertsRadioButton.getSelection();
313                 setValue(PREF_CODEASSIST_INSERT_COMPLETION, insert);
314             }
315         };
316         
317         fCompletionInsertsRadioButton= new Button(completionComposite, SWT.RADIO | SWT.LEFT);
318         fCompletionInsertsRadioButton.setText(PreferencesMessages.JavaEditorPreferencePage_completionInserts);
319         fCompletionInsertsRadioButton.setLayoutData(new GridData());
320         fCompletionInsertsRadioButton.addSelectionListener(completionSelectionListener);
321         
322         fCompletionOverwritesRadioButton= new Button(completionComposite, SWT.RADIO | SWT.LEFT);
323         fCompletionOverwritesRadioButton.setText(PreferencesMessages.JavaEditorPreferencePage_completionOverwrites);
324         fCompletionOverwritesRadioButton.setLayoutData(new GridData());
325         fCompletionOverwritesRadioButton.addSelectionListener(completionSelectionListener);
326         
327         Label label= new Label(completionComposite, SWT.NONE);
328         label.setText(PreferencesMessages.JavaEditorPreferencePage_completionToggleHint);
329         GridData gd= new GridData();
330         gd.horizontalIndent= 20;
331         gd.horizontalSpan= 2;
332         label.setLayoutData(gd);
333     }
334     
335     public void initialize() {
336         initializeFields();
337     }
338
339     private void initializeFields() {
340         boolean completionInserts= getBooleanValue(PREF_CODEASSIST_INSERT_COMPLETION);
341         fCompletionInsertsRadioButton.setSelection(completionInserts);
342         fCompletionOverwritesRadioButton.setSelection(!completionInserts);
343         
344         updateAutoactivationControls();
345     }
346     
347     private void updateAutoactivationControls() {
348         boolean autoactivation= getBooleanValue(PREF_CODEASSIST_AUTOACTIVATION);
349         setControlEnabled(PREF_CODEASSIST_AUTOACTIVATION_DELAY, autoactivation);
350         setControlEnabled(PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA, autoactivation);
351         setControlEnabled(PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC, autoactivation);
352         setControlEnabled(PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS, getBooleanValue(PREF_CODEASSIST_FILL_ARGUMENT_NAMES));
353     }
354
355     
356     public void performDefaults() {
357         super.performDefaults();
358         initializeFields();
359     }
360     
361     protected String JavaDoc[] getFullBuildDialogStrings(boolean workspaceSettings) {
362         return null;
363     }
364     
365     /**
366      * Validates that the specified number is positive.
367      *
368      * @param number
369      * The number to validate
370      * @return The status of the validation
371      */

372     protected static IStatus validatePositiveNumber(final String JavaDoc number) {
373
374         final StatusInfo status= new StatusInfo();
375         if (number.length() == 0) {
376             status.setError(PreferencesMessages.SpellingPreferencePage_empty_threshold);
377         } else {
378             try {
379                 final int value= Integer.parseInt(number);
380                 if (value < 0) {
381                     status.setError(Messages.format(PreferencesMessages.SpellingPreferencePage_invalid_threshold, number));
382                 }
383             } catch (NumberFormatException JavaDoc exception) {
384                 status.setError(Messages.format(PreferencesMessages.SpellingPreferencePage_invalid_threshold, number));
385             }
386         }
387         return status;
388     }
389     
390     protected void validateSettings(Key key, String JavaDoc oldValue, String JavaDoc newValue) {
391         if (key == null || PREF_CODEASSIST_AUTOACTIVATION_DELAY.equals(key))
392             fContext.statusChanged(validatePositiveNumber(getValue(PREF_CODEASSIST_AUTOACTIVATION_DELAY)));
393     }
394
395     protected void setControlEnabled(Key key, boolean enabled) {
396         Control control= getControl(key);
397         control.setEnabled(enabled);
398         Label label= (Label) fLabels.get(control);
399         if (label != null)
400             label.setEnabled(enabled);
401     }
402
403     private Control getControl(Key key) {
404         for (int i= fComboBoxes.size() - 1; i >= 0; i--) {
405             Control curr= (Control) fComboBoxes.get(i);
406             ControlData data= (ControlData) curr.getData();
407             if (key.equals(data.getKey())) {
408                 return curr;
409             }
410         }
411         for (int i= fCheckBoxes.size() - 1; i >= 0; i--) {
412             Control curr= (Control) fCheckBoxes.get(i);
413             ControlData data= (ControlData) curr.getData();
414             if (key.equals(data.getKey())) {
415                 return curr;
416             }
417         }
418         for (int i= fTextBoxes.size() - 1; i >= 0; i--) {
419             Control curr= (Control) fTextBoxes.get(i);
420             Key currKey= (Key) curr.getData();
421             if (key.equals(currKey)) {
422                 return curr;
423             }
424         }
425         return null;
426     }
427 }
428
Popular Tags