001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.preferences;
011:
012: import java.util.Map;
013:
014: import org.eclipse.core.runtime.IStatus;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.SelectionAdapter;
018: import org.eclipse.swt.events.SelectionEvent;
019: import org.eclipse.swt.events.SelectionListener;
020: import org.eclipse.swt.graphics.Point;
021: import org.eclipse.swt.layout.GridData;
022: import org.eclipse.swt.layout.GridLayout;
023: import org.eclipse.swt.widgets.Button;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Group;
027: import org.eclipse.swt.widgets.Label;
028: import org.eclipse.swt.widgets.Link;
029: import org.eclipse.swt.widgets.Text;
030:
031: import org.eclipse.jface.preference.IPreferenceStore;
032:
033: import org.eclipse.ui.dialogs.PreferencesUtil;
034: import org.eclipse.ui.preferences.IWorkbenchPreferenceContainer;
035:
036: import org.eclipse.jdt.core.JavaCore;
037:
038: import org.eclipse.jdt.internal.corext.util.Messages;
039:
040: import org.eclipse.jdt.ui.PreferenceConstants;
041:
042: import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
043: import org.eclipse.jdt.internal.ui.text.java.ProposalSorterHandle;
044: import org.eclipse.jdt.internal.ui.text.java.ProposalSorterRegistry;
045: import org.eclipse.jdt.internal.ui.util.PixelConverter;
046: import org.eclipse.jdt.internal.ui.wizards.IStatusChangeListener;
047:
048: /**
049: * Configures the content assist preferences.
050: *
051: * @since 3.0
052: */
053: class CodeAssistConfigurationBlock extends OptionsConfigurationBlock {
054:
055: private static final Key PREF_CODEASSIST_AUTOACTIVATION = getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION);
056: private static final Key PREF_CODEASSIST_AUTOACTIVATION_DELAY = getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY);
057: private static final Key PREF_CODEASSIST_AUTOINSERT = getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOINSERT);
058: private static final Key PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA = getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA);
059: private static final Key PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC = getJDTUIKey(PreferenceConstants.CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC);
060: private static final Key PREF_CODEASSIST_SHOW_VISIBLE_PROPOSALS = getJDTUIKey(PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS);
061: private static final Key PREF_CODEASSIST_SORTER = getJDTUIKey(PreferenceConstants.CODEASSIST_SORTER);
062: private static final Key PREF_CODEASSIST_CASE_SENSITIVITY = getJDTUIKey(PreferenceConstants.CODEASSIST_CASE_SENSITIVITY);
063: private static final Key PREF_CODEASSIST_ADDIMPORT = getJDTUIKey(PreferenceConstants.CODEASSIST_ADDIMPORT);
064: private static final Key PREF_CODEASSIST_SUGGEST_STATIC_IMPORTS = getJDTCoreKey(JavaCore.CODEASSIST_SUGGEST_STATIC_IMPORTS);
065: private static final Key PREF_CODEASSIST_INSERT_COMPLETION = getJDTUIKey(PreferenceConstants.CODEASSIST_INSERT_COMPLETION);
066: private static final Key PREF_CODEASSIST_FILL_ARGUMENT_NAMES = getJDTUIKey(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES);
067: private static final Key PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS = getJDTUIKey(PreferenceConstants.CODEASSIST_GUESS_METHOD_ARGUMENTS);
068: private static final Key PREF_CODEASSIST_PREFIX_COMPLETION = getJDTUIKey(PreferenceConstants.CODEASSIST_PREFIX_COMPLETION);
069: private static final Key PREF_CODEASSIST_FORBIDDEN_REFERENCE_CHECK = getJDTCoreKey(JavaCore.CODEASSIST_FORBIDDEN_REFERENCE_CHECK);
070: private static final Key PREF_CODEASSIST_DISCOURAGED_REFERENCE_CHECK = getJDTCoreKey(JavaCore.CODEASSIST_DISCOURAGED_REFERENCE_CHECK);
071: private static final Key PREF_CODEASSIST_DEPRECATION_CHECK = getJDTCoreKey(JavaCore.CODEASSIST_DEPRECATION_CHECK);
072: private static final Key PREF_CODEASSIST_CAMEL_CASE_MATCH = getJDTCoreKey(JavaCore.CODEASSIST_CAMEL_CASE_MATCH);
073:
074: private static Key[] getAllKeys() {
075: return new Key[] { PREF_CODEASSIST_AUTOACTIVATION,
076: PREF_CODEASSIST_AUTOACTIVATION_DELAY,
077: PREF_CODEASSIST_AUTOINSERT,
078: PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA,
079: PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC,
080: PREF_CODEASSIST_SHOW_VISIBLE_PROPOSALS,
081: PREF_CODEASSIST_SORTER,
082: PREF_CODEASSIST_CASE_SENSITIVITY,
083: PREF_CODEASSIST_ADDIMPORT,
084: PREF_CODEASSIST_SUGGEST_STATIC_IMPORTS,
085: PREF_CODEASSIST_INSERT_COMPLETION,
086: PREF_CODEASSIST_FILL_ARGUMENT_NAMES,
087: PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS,
088: PREF_CODEASSIST_PREFIX_COMPLETION,
089: PREF_CODEASSIST_FORBIDDEN_REFERENCE_CHECK,
090: PREF_CODEASSIST_DISCOURAGED_REFERENCE_CHECK,
091: PREF_CODEASSIST_DEPRECATION_CHECK,
092: PREF_CODEASSIST_CAMEL_CASE_MATCH, };
093: }
094:
095: private static final String[] trueFalse = new String[] {
096: IPreferenceStore.TRUE, IPreferenceStore.FALSE };
097: private static final String[] enabledDisabled = new String[] {
098: JavaCore.ENABLED, JavaCore.DISABLED };
099:
100: private Button fCompletionInsertsRadioButton;
101: private Button fCompletionOverwritesRadioButton;
102:
103: public CodeAssistConfigurationBlock(
104: IStatusChangeListener statusListener,
105: IWorkbenchPreferenceContainer workbenchcontainer) {
106: super (statusListener, null, getAllKeys(), workbenchcontainer);
107: }
108:
109: protected Control createContents(Composite parent) {
110: ScrolledPageContent scrolled = new ScrolledPageContent(parent,
111: SWT.H_SCROLL | SWT.V_SCROLL);
112: scrolled.setExpandHorizontal(true);
113: scrolled.setExpandVertical(true);
114:
115: Composite control = new Composite(scrolled, SWT.NONE);
116: GridLayout layout = new GridLayout();
117: layout.marginHeight = 0;
118: layout.marginWidth = 0;
119: control.setLayout(layout);
120:
121: Composite composite;
122:
123: composite = createSubsection(
124: control,
125: PreferencesMessages.CodeAssistConfigurationBlock_insertionSection_title);
126: addInsertionSection(composite);
127:
128: composite = createSubsection(
129: control,
130: PreferencesMessages.CodeAssistConfigurationBlock_sortingSection_title);
131: addSortingSection(composite);
132:
133: composite = createSubsection(
134: control,
135: PreferencesMessages.CodeAssistConfigurationBlock_autoactivationSection_title);
136: addAutoActivationSection(composite);
137:
138: initialize();
139:
140: scrolled.setContent(control);
141: final Point size = control
142: .computeSize(SWT.DEFAULT, SWT.DEFAULT);
143: scrolled.setMinSize(size.x, size.y);
144: return scrolled;
145: }
146:
147: protected Composite createSubsection(Composite parent, String label) {
148: Group group = new Group(parent, SWT.SHADOW_NONE);
149: group.setText(label);
150: GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
151: group.setLayoutData(data);
152: GridLayout layout = new GridLayout();
153: layout.numColumns = 3;
154: group.setLayout(layout);
155:
156: return group;
157: }
158:
159: private void addInsertionSection(Composite composite) {
160: addCompletionRadioButtons(composite);
161:
162: String label;
163: label = PreferencesMessages.JavaEditorPreferencePage_insertSingleProposalsAutomatically;
164: addCheckBox(composite, label, PREF_CODEASSIST_AUTOINSERT,
165: trueFalse, 0);
166:
167: label = PreferencesMessages.JavaEditorPreferencePage_completePrefixes;
168: addCheckBox(composite, label,
169: PREF_CODEASSIST_PREFIX_COMPLETION, trueFalse, 0);
170:
171: label = PreferencesMessages.JavaEditorPreferencePage_automaticallyAddImportInsteadOfQualifiedName;
172: Button master = addCheckBox(composite, label,
173: PREF_CODEASSIST_ADDIMPORT, trueFalse, 0);
174:
175: label = PreferencesMessages.JavaEditorPreferencePage_suggestStaticImports;
176: Button slave = addCheckBox(composite, label,
177: PREF_CODEASSIST_SUGGEST_STATIC_IMPORTS,
178: enabledDisabled, 20);
179: createSelectionDependency(master, slave);
180:
181: label = PreferencesMessages.JavaEditorPreferencePage_fillArgumentNamesOnMethodCompletion;
182: master = addCheckBox(composite, label,
183: PREF_CODEASSIST_FILL_ARGUMENT_NAMES, trueFalse, 0);
184:
185: label = PreferencesMessages.JavaEditorPreferencePage_guessArgumentNamesOnMethodCompletion;
186: slave = addCheckBox(composite, label,
187: PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS, trueFalse, 20);
188: createSelectionDependency(master, slave);
189: }
190:
191: /**
192: * Creates a selection dependency between a master and a slave control.
193: *
194: * @param master
195: * The master button that controls the state of the slave
196: * @param slave
197: * The slave control that is enabled only if the master is
198: * selected
199: */
200: protected static void createSelectionDependency(
201: final Button master, final Control slave) {
202:
203: master.addSelectionListener(new SelectionListener() {
204:
205: public void widgetDefaultSelected(SelectionEvent event) {
206: // Do nothing
207: }
208:
209: public void widgetSelected(SelectionEvent event) {
210: slave.setEnabled(master.getSelection());
211: }
212: });
213: slave.setEnabled(master.getSelection());
214: }
215:
216: private void addSortingSection(Composite composite) {
217: String label;
218: label = PreferencesMessages.JavaEditorPreferencePage_presentProposalsInAlphabeticalOrder;
219: ProposalSorterHandle[] sorters = ProposalSorterRegistry
220: .getDefault().getSorters();
221: String[] labels = new String[sorters.length];
222: String[] values = new String[sorters.length];
223: for (int i = 0; i < sorters.length; i++) {
224: ProposalSorterHandle handle = sorters[i];
225: labels[i] = handle.getName();
226: values[i] = handle.getId();
227: }
228:
229: addComboBox(composite, label, PREF_CODEASSIST_SORTER, values,
230: labels, 0);
231:
232: label = PreferencesMessages.JavaEditorPreferencePage_showOnlyProposalsVisibleInTheInvocationContext;
233: addCheckBox(composite, label,
234: PREF_CODEASSIST_SHOW_VISIBLE_PROPOSALS, trueFalse, 0);
235:
236: label = PreferencesMessages.CodeAssistConfigurationBlock_matchCamelCase_label;
237: addCheckBox(composite, label, PREF_CODEASSIST_CAMEL_CASE_MATCH,
238: enabledDisabled, 0);
239:
240: label = PreferencesMessages.CodeAssistConfigurationBlock_restricted_link;
241: Map targetInfo = new java.util.HashMap(2);
242: targetInfo.put(
243: ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_KEY,
244: JavaCore.COMPILER_PB_FORBIDDEN_REFERENCE);
245: targetInfo
246: .put(
247: ProblemSeveritiesPreferencePage.DATA_SELECT_OPTION_QUALIFIER,
248: JavaCore.PLUGIN_ID);
249: createPreferencePageLink(composite, label, targetInfo);
250:
251: label = PreferencesMessages.CodeAssistConfigurationBlock_hideForbidden_label;
252: addCheckBox(composite, label,
253: PREF_CODEASSIST_FORBIDDEN_REFERENCE_CHECK,
254: enabledDisabled, 0);
255:
256: label = PreferencesMessages.CodeAssistConfigurationBlock_hideDiscouraged_label;
257: addCheckBox(composite, label,
258: PREF_CODEASSIST_DISCOURAGED_REFERENCE_CHECK,
259: enabledDisabled, 0);
260:
261: label = PreferencesMessages.CodeAssistConfigurationBlock_hideDeprecated_label;
262: addCheckBox(composite, label,
263: PREF_CODEASSIST_DEPRECATION_CHECK, enabledDisabled, 0);
264: }
265:
266: private void createPreferencePageLink(Composite composite,
267: String label, final Map targetInfo) {
268: final Link link = new Link(composite, SWT.NONE);
269: link.setText(label);
270: link.addSelectionListener(new SelectionAdapter() {
271: public void widgetSelected(SelectionEvent e) {
272: PreferencesUtil.createPreferenceDialogOn(link
273: .getShell(), e.text, null, targetInfo);
274: }
275: });
276: }
277:
278: private void addAutoActivationSection(Composite composite) {
279: String label;
280: label = PreferencesMessages.JavaEditorPreferencePage_enableAutoActivation;
281: final Button autoactivation = addCheckBox(composite, label,
282: PREF_CODEASSIST_AUTOACTIVATION, trueFalse, 0);
283: autoactivation.addSelectionListener(new SelectionAdapter() {
284: public void widgetSelected(SelectionEvent e) {
285: updateAutoactivationControls();
286: }
287: });
288:
289: label = PreferencesMessages.JavaEditorPreferencePage_autoActivationDelay;
290: addLabelledTextField(composite, label,
291: PREF_CODEASSIST_AUTOACTIVATION_DELAY, 4, 0, true);
292:
293: label = PreferencesMessages.JavaEditorPreferencePage_autoActivationTriggersForJava;
294: addLabelledTextField(composite, label,
295: PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA, 4, 0,
296: false);
297:
298: label = PreferencesMessages.JavaEditorPreferencePage_autoActivationTriggersForJavaDoc;
299: addLabelledTextField(composite, label,
300: PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC, 4, 0,
301: false);
302: }
303:
304: protected Text addLabelledTextField(Composite parent, String label,
305: Key key, int textlimit, int indent, boolean dummy) {
306: PixelConverter pixelConverter = new PixelConverter(parent);
307:
308: Label labelControl = new Label(parent, SWT.WRAP);
309: labelControl.setText(label);
310: labelControl.setLayoutData(new GridData());
311:
312: Text textBox = new Text(parent, SWT.BORDER | SWT.SINGLE);
313: textBox.setData(key);
314: textBox.setLayoutData(new GridData());
315:
316: fLabels.put(textBox, labelControl);
317:
318: String currValue = getValue(key);
319: if (currValue != null) {
320: textBox.setText(currValue);
321: }
322: textBox.addModifyListener(getTextModifyListener());
323:
324: GridData data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
325: if (textlimit != 0) {
326: textBox.setTextLimit(textlimit);
327: data.widthHint = pixelConverter
328: .convertWidthInCharsToPixels(textlimit + 1);
329: }
330: data.horizontalIndent = indent;
331: data.horizontalSpan = 2;
332: textBox.setLayoutData(data);
333:
334: fTextBoxes.add(textBox);
335: return textBox;
336: }
337:
338: private void addCompletionRadioButtons(
339: Composite contentAssistComposite) {
340: Composite completionComposite = new Composite(
341: contentAssistComposite, SWT.NONE);
342: GridData ccgd = new GridData();
343: ccgd.horizontalSpan = 2;
344: completionComposite.setLayoutData(ccgd);
345: GridLayout ccgl = new GridLayout();
346: ccgl.marginWidth = 0;
347: ccgl.numColumns = 2;
348: completionComposite.setLayout(ccgl);
349:
350: SelectionListener completionSelectionListener = new SelectionAdapter() {
351: public void widgetSelected(SelectionEvent e) {
352: boolean insert = fCompletionInsertsRadioButton
353: .getSelection();
354: setValue(PREF_CODEASSIST_INSERT_COMPLETION, insert);
355: }
356: };
357:
358: fCompletionInsertsRadioButton = new Button(completionComposite,
359: SWT.RADIO | SWT.LEFT);
360: fCompletionInsertsRadioButton
361: .setText(PreferencesMessages.JavaEditorPreferencePage_completionInserts);
362: fCompletionInsertsRadioButton.setLayoutData(new GridData());
363: fCompletionInsertsRadioButton
364: .addSelectionListener(completionSelectionListener);
365:
366: fCompletionOverwritesRadioButton = new Button(
367: completionComposite, SWT.RADIO | SWT.LEFT);
368: fCompletionOverwritesRadioButton
369: .setText(PreferencesMessages.JavaEditorPreferencePage_completionOverwrites);
370: fCompletionOverwritesRadioButton.setLayoutData(new GridData());
371: fCompletionOverwritesRadioButton
372: .addSelectionListener(completionSelectionListener);
373:
374: Label label = new Label(completionComposite, SWT.NONE);
375: label
376: .setText(PreferencesMessages.JavaEditorPreferencePage_completionToggleHint);
377: GridData gd = new GridData();
378: gd.horizontalIndent = 20;
379: gd.horizontalSpan = 2;
380: label.setLayoutData(gd);
381: }
382:
383: public void initialize() {
384: initializeFields();
385: }
386:
387: private void initializeFields() {
388: boolean completionInserts = getBooleanValue(PREF_CODEASSIST_INSERT_COMPLETION);
389: fCompletionInsertsRadioButton.setSelection(completionInserts);
390: fCompletionOverwritesRadioButton
391: .setSelection(!completionInserts);
392:
393: updateAutoactivationControls();
394: }
395:
396: private void updateAutoactivationControls() {
397: boolean autoactivation = getBooleanValue(PREF_CODEASSIST_AUTOACTIVATION);
398: setControlEnabled(PREF_CODEASSIST_AUTOACTIVATION_DELAY,
399: autoactivation);
400: setControlEnabled(PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVA,
401: autoactivation);
402: setControlEnabled(
403: PREF_CODEASSIST_AUTOACTIVATION_TRIGGERS_JAVADOC,
404: autoactivation);
405: setControlEnabled(PREF_CODEASSIST_GUESS_METHOD_ARGUMENTS,
406: getBooleanValue(PREF_CODEASSIST_FILL_ARGUMENT_NAMES));
407: }
408:
409: public void performDefaults() {
410: super .performDefaults();
411: initializeFields();
412: }
413:
414: protected String[] getFullBuildDialogStrings(
415: boolean workspaceSettings) {
416: return null;
417: }
418:
419: /**
420: * Validates that the specified number is positive.
421: *
422: * @param number
423: * The number to validate
424: * @return The status of the validation
425: */
426: protected static IStatus validatePositiveNumber(final String number) {
427:
428: final StatusInfo status = new StatusInfo();
429: if (number.length() == 0) {
430: status
431: .setError(PreferencesMessages.SpellingPreferencePage_empty_threshold);
432: } else {
433: try {
434: final int value = Integer.parseInt(number);
435: if (value < 0) {
436: status
437: .setError(Messages
438: .format(
439: PreferencesMessages.SpellingPreferencePage_invalid_threshold,
440: number));
441: }
442: } catch (NumberFormatException exception) {
443: status
444: .setError(Messages
445: .format(
446: PreferencesMessages.SpellingPreferencePage_invalid_threshold,
447: number));
448: }
449: }
450: return status;
451: }
452:
453: protected void validateSettings(Key key, String oldValue,
454: String newValue) {
455: if (key == null
456: || PREF_CODEASSIST_AUTOACTIVATION_DELAY.equals(key))
457: fContext
458: .statusChanged(validatePositiveNumber(getValue(PREF_CODEASSIST_AUTOACTIVATION_DELAY)));
459: }
460:
461: protected void setControlEnabled(Key key, boolean enabled) {
462: Control control = getControl(key);
463: control.setEnabled(enabled);
464: Label label = (Label) fLabels.get(control);
465: if (label != null)
466: label.setEnabled(enabled);
467: }
468:
469: private Control getControl(Key key) {
470: for (int i = fComboBoxes.size() - 1; i >= 0; i--) {
471: Control curr = (Control) fComboBoxes.get(i);
472: ControlData data = (ControlData) curr.getData();
473: if (key.equals(data.getKey())) {
474: return curr;
475: }
476: }
477: for (int i = fCheckBoxes.size() - 1; i >= 0; i--) {
478: Control curr = (Control) fCheckBoxes.get(i);
479: ControlData data = (ControlData) curr.getData();
480: if (key.equals(data.getKey())) {
481: return curr;
482: }
483: }
484: for (int i = fTextBoxes.size() - 1; i >= 0; i--) {
485: Control curr = (Control) fTextBoxes.get(i);
486: Key currKey = (Key) curr.getData();
487: if (key.equals(currKey)) {
488: return curr;
489: }
490: }
491: return null;
492: }
493: }
|