CODEASSIST_VISIBILITY_CHECK support reviewed
diff --git a/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/core/DLTKCore.java b/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/core/DLTKCore.java
index 4670e5c..17da531 100644
--- a/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/core/DLTKCore.java
+++ b/core/plugins/org.eclipse.dltk.core/model/org/eclipse/dltk/core/DLTKCore.java
@@ -212,10 +212,20 @@
 	public static final String CORE_INCOMPLETE_BUILDPATH = PLUGIN_ID
 			+ ".incompleteBuildpath"; //$NON-NLS-1$
 
-	/*
-	 * Possible configurable option ID.
-	 * 
-	 * @see #getDefaultOptions()
+	/**
+	 * Code assist option ID: Activate Visibility Sensitive Completion.
+	 * <p>
+	 * When active, completion doesn't show that you can not see (for example,
+	 * you can not see private methods of a super class). This option is
+	 * language specific.
+	 * <dl>
+	 * <dt>Option id:</dt>
+	 * <dd><code>{@value #CODEASSIST_VISIBILITY_CHECK}</code></dd>
+	 * <dt>Possible values:</dt>
+	 * <dd><code>{ {@value #ENABLED}, {@value #DISABLED} }</code></dd>
+	 * <dt>Default:</dt>
+	 * <dd><code>{@value #DISABLED}</code></dd>
+	 * </dl>
 	 */
 	public static final String CODEASSIST_VISIBILITY_CHECK = PLUGIN_ID
 			+ ".codeComplete.visibilityCheck"; //$NON-NLS-1$
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeAssistConfigurationBlock.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeAssistConfigurationBlock.java
index 221e699..4a46023 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeAssistConfigurationBlock.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/CodeAssistConfigurationBlock.java
@@ -13,8 +13,12 @@
 package org.eclipse.dltk.ui.preferences;
 
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.eclipse.dltk.ui.PreferenceConstants;
+import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.jface.preference.PreferencePage;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionAdapter;
@@ -28,12 +32,15 @@
 import org.eclipse.swt.widgets.Label;
 
 public class CodeAssistConfigurationBlock extends AbstractConfigurationBlock {
+	private final Set<String> optionalPreferences;
 	private Button fCompletionInsertsRadioButton;
 	private Button fCompletionOverwritesRadioButton;
 
 	public CodeAssistConfigurationBlock(PreferencePage mainPreferencePage,
-			OverlayPreferenceStore store) {
+			OverlayPreferenceStore store, String... optionalPreferences) {
 		super(store, mainPreferencePage);
+		this.optionalPreferences = new HashSet<String>();
+		Collections.addAll(this.optionalPreferences, optionalPreferences);
 		getPreferenceStore().addKeys(createOverlayStoreKeys());
 	}
 
@@ -66,6 +73,12 @@
 		overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
 				OverlayPreferenceStore.BOOLEAN,
 				PreferenceConstants.CODEASSIST_SORTER));
+		if (optionalPreferences
+				.contains(PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS)) {
+			overlayKeys.add(new OverlayPreferenceStore.OverlayKey(
+					OverlayPreferenceStore.BOOLEAN,
+					PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS));
+		}
 	}
 
 	/**
@@ -87,13 +100,21 @@
 				control,
 				null,
 				PreferencesMessages.CodeAssistConfigurationBlock_insertionSection_title);
-
-		GridLayout layout = new GridLayout();
-		layout.numColumns = 2;
+		final GridLayout layout = GridLayoutFactory.swtDefaults().numColumns(2)
+				.create();
 		composite.setLayout(layout);
-
 		addInsertionSection(composite);
 
+		if (optionalPreferences
+				.contains(PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS)) {
+			composite = createSubsection(
+					control,
+					null,
+					PreferencesMessages.CodeAssistConfigurationBlock_filteringSection_title);
+			composite.setLayout(layout);
+			addFilteringSection(composite);
+		}
+
 		composite = createSubsection(
 				control,
 				null,
@@ -105,30 +126,32 @@
 
 		return control;
 	}
-	
-//	protected void addSortingSection(Composite composite) {
-//		String label;
-//
-//		label= PreferencesMessages.DLTKEditorPreferencePage_presentProposalsInAlphabeticalOrder;
-//		ProposalSorterHandle[] sorters= ProposalSorterRegistry.getDefault().getSorters();
-//		String[] labels= new String[sorters.length];
-//		String[] values= new String[sorters.length];
-//		for (int i= 0; i < sorters.length; i++) {
-//			ProposalSorterHandle handle= sorters[i];
-//			labels[i]= handle.getName();
-//			values[i]= handle.getId();
-//		}
-//		
-//		addComboBox(composite, label, PreferenceConstants.CODEASSIST_SORTER, values, labels);
-//	}
 
+	// protected void addSortingSection(Composite composite) {
+	// String label;
+	//
+	// label=
+	// PreferencesMessages.DLTKEditorPreferencePage_presentProposalsInAlphabeticalOrder;
+	// ProposalSorterHandle[] sorters=
+	// ProposalSorterRegistry.getDefault().getSorters();
+	// String[] labels= new String[sorters.length];
+	// String[] values= new String[sorters.length];
+	// for (int i= 0; i < sorters.length; i++) {
+	// ProposalSorterHandle handle= sorters[i];
+	// labels[i]= handle.getName();
+	// values[i]= handle.getId();
+	// }
+	//
+	// addComboBox(composite, label, PreferenceConstants.CODEASSIST_SORTER,
+	// values, labels);
+	// }
 
-	Control autoActivation;
+	private Control autoActivation;
 
 	protected void addAutoActivationSection(Composite composite) {
-		String label;
-		label = PreferencesMessages.DLTKEditorPreferencePage_enableAutoActivation;
-		final Button autoactivation = addCheckBox(composite, label,
+		final Button autoactivation = addCheckBox(
+				composite,
+				PreferencesMessages.DLTKEditorPreferencePage_enableAutoActivation,
 				PreferenceConstants.CODEASSIST_AUTOACTIVATION, 2);
 		autoactivation.addSelectionListener(new SelectionAdapter() {
 			public void widgetSelected(SelectionEvent e) {
@@ -136,8 +159,9 @@
 			}
 		});
 
-		label = PreferencesMessages.DLTKEditorPreferencePage_autoActivationDelay;
-		Control[] ctrl = addLabelledTextField(composite, label,
+		Control[] ctrl = addLabelledTextField(
+				composite,
+				PreferencesMessages.DLTKEditorPreferencePage_autoActivationDelay,
 				PreferenceConstants.CODEASSIST_AUTOACTIVATION_DELAY, 4, 2, true);
 		autoActivation = ctrl[1];
 	}
@@ -153,9 +177,9 @@
 	protected void addInsertionSection(Composite composite) {
 		addCompletionRadioButtons(composite);
 
-		String label;
-		label = PreferencesMessages.DLTKEditorPreferencePage_insertSingleProposalsAutomatically;
-		addCheckBox(composite, label,
+		addCheckBox(
+				composite,
+				PreferencesMessages.DLTKEditorPreferencePage_insertSingleProposalsAutomatically,
 				PreferenceConstants.CODEASSIST_AUTOINSERT, 2);
 	}
 
@@ -203,6 +227,13 @@
 		label.setLayoutData(gd);
 	}
 
+	protected void addFilteringSection(Composite composite) {
+		addCheckBox(
+				composite,
+				PreferencesMessages.DLTKEditorPreferencePage_showOnlyProposalsVisibleInTheInvocationContext,
+				PreferenceConstants.CODEASSIST_SHOW_VISIBLE_PROPOSALS, 2);
+	}
+
 	protected void initializeFields() {
 		super.initializeFields();
 		boolean completionInserts = getPreferenceStore().getBoolean(
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.java
index 1146e07..48968c0 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.java
@@ -258,6 +258,7 @@
 	public static String SmartTypingConfigurationBlock_pasting_title;
 	public static String SmartTypingConfigurationBlock_strings_title;
 	public static String CodeAssistConfigurationBlock_sortingSection_title;
+	public static String CodeAssistConfigurationBlock_filteringSection_title;
 	public static String CodeAssistConfigurationBlock_autoactivationSection_title;
 	public static String CodeAssistConfigurationBlock_insertionSection_title;
 	public static String DLTKEditorPreferencePage_coloring_category_DLTK;
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.properties b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.properties
index 6242a3a..6cce6f7 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.properties
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/preferences/PreferencesMessages.properties
@@ -232,6 +232,7 @@
 # code assist block
 CodeAssistConfigurationBlock_insertionSection_title=Insertion
 CodeAssistConfigurationBlock_sortingSection_title=Sorting and Filtering
+CodeAssistConfigurationBlock_filteringSection_title=Filtering
 #CodeAssistConfigurationBlock_matchCamelCase_label=Show ca&mel case matches
 CodeAssistConfigurationBlock_autoactivationSection_title=Auto-Activation
 # do not translate the href arguments (org.eclipse.jdt.ui.preferences.ProblemSeveritiesPreferencePage)
diff --git a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/text/completion/ScriptCompletionProcessor.java b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/text/completion/ScriptCompletionProcessor.java
index e5e1cf3..5347983 100644
--- a/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/text/completion/ScriptCompletionProcessor.java
+++ b/core/plugins/org.eclipse.dltk.ui/src/org/eclipse/dltk/ui/text/completion/ScriptCompletionProcessor.java
@@ -9,11 +9,14 @@
  *******************************************************************************/
 package org.eclipse.dltk.ui.text.completion;
 
-import java.util.Hashtable;
 import java.util.List;
 
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.preferences.IEclipsePreferences;
+import org.eclipse.core.runtime.preferences.InstanceScope;
 import org.eclipse.dltk.core.DLTKCore;
+import org.eclipse.dltk.core.DLTKLanguageManager;
+import org.eclipse.dltk.core.IDLTKLanguageToolkit;
 import org.eclipse.dltk.ui.DLTKUILanguageManager;
 import org.eclipse.dltk.ui.DLTKUIPlugin;
 import org.eclipse.dltk.ui.IDLTKUILanguageToolkit;
@@ -26,18 +29,13 @@
 import org.eclipse.jface.text.contentassist.IContextInformationPresenter;
 import org.eclipse.jface.text.contentassist.IContextInformationValidator;
 import org.eclipse.ui.IEditorPart;
+import org.osgi.service.prefs.BackingStoreException;
 
 /**
  * Script completion processor.
  */
 public abstract class ScriptCompletionProcessor extends ContentAssistProcessor {
 
-	private final static String VISIBILITY = DLTKCore.CODEASSIST_VISIBILITY_CHECK;
-
-	private final static String ENABLED = "enabled"; //$NON-NLS-1$
-
-	private final static String DISABLED = "disabled"; //$NON-NLS-1$
-
 	private IContextInformationValidator fValidator;
 
 	protected final IEditorPart fEditor;
@@ -56,13 +54,29 @@
 	 *            <code>true</code> if proposals should be restricted
 	 */
 	public void restrictProposalsToVisibility(boolean restrict) {
-		Hashtable options = DLTKCore.getOptions();
-		Object value = options.get(VISIBILITY);
-		if (value instanceof String) {
-			String newValue = restrict ? ENABLED : DISABLED;
-			if (!newValue.equals(value)) {
-				options.put(VISIBILITY, newValue);
-				DLTKCore.setOptions(options);
+		final IDLTKLanguageToolkit toolkit = DLTKLanguageManager
+				.getLanguageToolkit(getNatureId());
+		if (toolkit == null) {
+			return;
+		}
+		final String preferenceQualifier = toolkit.getPreferenceQualifier();
+		if (preferenceQualifier == null) {
+			return;
+		}
+		final IEclipsePreferences node = new InstanceScope()
+				.getNode(preferenceQualifier);
+		if (node == null) {
+			return;
+		}
+		final String value = node.get(DLTKCore.CODEASSIST_VISIBILITY_CHECK,
+				null);
+		final String newValue = restrict ? DLTKCore.ENABLED : DLTKCore.DISABLED;
+		if (!newValue.equals(value)) {
+			node.put(DLTKCore.CODEASSIST_VISIBILITY_CHECK, newValue);
+			try {
+				node.flush();
+			} catch (BackingStoreException e) {
+				DLTKUIPlugin.log(e);
 			}
 		}
 	}