1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 21:05:37 +02:00

Similar preference API between cdt.core and cdt.ui

This commit is contained in:
Sergey Prigogin 2012-10-16 14:46:15 -07:00
parent e8c0bd4d43
commit d5f793f01d
2 changed files with 226 additions and 116 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2000, 2011 QNX Software Systems and others.
* Copyright (c) 2000, 2012 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@ -14,12 +14,20 @@
*******************************************************************************/
package org.eclipse.cdt.core;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.preferences.ConfigurationScope;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;
import org.eclipse.core.runtime.preferences.InstanceScope;
/**
* @noextend This class is not intended to be subclassed by clients.
* @noinstantiate This class is not intended to be instantiated by clients.
*/
public class CCorePreferenceConstants {
/**
* <pre>
* RECOGNIZED OPTIONS:
@ -166,4 +174,97 @@ public class CCorePreferenceConstants {
public static final String PREF_BUILD_CONFIGS_RESOURCE_CHANGES = "build.proj.ref.configs.enabled"; //$NON-NLS-1$
/**
* Returns the node in the preference in the given context.
* @param key The preference key.
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should
* be avoided.
* @return Returns the node matching the given context.
*/
private static IEclipsePreferences getPreferenceNode(String key, ICProject project) {
IEclipsePreferences node = null;
if (project != null) {
node = new ProjectScope(project.getProject()).getNode(CCorePlugin.PLUGIN_ID);
if (node.get(key, null) != null) {
return node;
}
}
node = InstanceScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID);
if (node.get(key, null) != null) {
return node;
}
node = ConfigurationScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID);
if (node.get(key, null) != null) {
return node;
}
return DefaultScope.INSTANCE.getNode(CCorePlugin.PLUGIN_ID);
}
/**
* Returns the string value for the given key in the given context.
* @param key The preference key
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @return Returns the current value for the string.
* @since 5.5
*/
public static String getPreference(String key, ICProject project) {
return getPreference(key, project, null);
}
/**
* Returns the string value for the given key in the given context.
* @param key The preference key
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value of the preference.
* @since 5.5
*/
public static String getPreference(String key, ICProject project, String defaultValue) {
return getPreferenceNode(key, project).get(key, defaultValue);
}
/**
* Returns the integer value for the given key in the given context.
* @param key The preference key
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value of the preference.
* @since 5.5
*/
public static int getPreference(String key, ICProject project, int defaultValue) {
return getPreferenceNode(key, project).getInt(key, defaultValue);
}
/**
* Returns the boolean value for the given key in the given context.
* @param key The preference key
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value of the preference.
* @since 5.5
*/
public static boolean getPreference(String key, ICProject project, boolean defaultValue) {
return getPreferenceNode(key, project).getBoolean(key, defaultValue);
}
/**
* Returns the scopes for preference lookup.
*
* @param project a project or {@code null}
* @return the scopes for preference lookup.
* @since 5.5
*/
public static IScopeContext[] getPreferenceScopes(IProject project) {
return project != null ?
new IScopeContext[] { new ProjectScope(project), InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE } :
new IScopeContext[] { InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE };
}
}

View file

@ -29,6 +29,7 @@ import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.ui.texteditor.AbstractDecoratedTextEditorPreferenceConstants;
import org.eclipse.cdt.core.CCorePreferenceConstants;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.ui.text.ICColorConstants;
@ -2173,9 +2174,8 @@ public class PreferenceConstants {
/**
* Returns the node in the preference in the given context.
* @param key The preference key.
* @param project The current context or <code>null</code> if no context is available and
* the workspace setting should be taken. Note that passing <code>null</code> should
* be avoided.
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @return Returns the node matching the given context.
*/
private static IEclipsePreferences getPreferenceNode(String key, ICProject project) {
@ -2203,21 +2203,33 @@ public class PreferenceConstants {
/**
* Returns the string value for the given key in the given context.
* @param key The preference key
* @param project The current context or <code>null</code> if no context is available and
* the workspace setting should be taken. Note that passing <code>null</code> should
* be avoided.
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @return Returns the current value for the string.
* @since 5.0
*/
public static String getPreference(String key, ICProject project) {
return getPreferenceNode(key, project).get(key, null);
return getPreference(key, project, null);
}
/**
* Returns the string value for the given key in the given context.
* @param key The preference key
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value of the preference.
* @since 5.5
*/
public static String getPreference(String key, ICProject project, String defaultValue) {
return getPreferenceNode(key, project).get(key, defaultValue);
}
/**
* Returns the integer value for the given key in the given context.
* @param key The preference key
* @param project The current context or <code>null</code> if no context is available and
* the workspace setting should be taken. Note that passing <code>null</code> should
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should
* be avoided.
* @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value for the string.
@ -2230,9 +2242,8 @@ public class PreferenceConstants {
/**
* Returns the boolean value for the given key in the given context.
* @param key The preference key
* @param project The current context or <code>null</code> if no context is available and
* the workspace setting should be taken. Note that passing <code>null</code> should
* be avoided.
* @param project The current context or {@code null} if no context is available and
* the workspace setting should be taken. Note that passing {@code null} should be avoided.
* @param defaultValue The default value if not specified in the preferences.
* @return Returns the current value for the string.
* @since 5.1
@ -2244,13 +2255,11 @@ public class PreferenceConstants {
/**
* Returns the scopes for preference lookup.
*
* @param project a project or <code>null</code>
* @param project a project or {@code null}
* @return the scopes for preference lookup.
* @since 5.4
*/
public static IScopeContext[] getPreferenceScopes(IProject project) {
return project != null ?
new IScopeContext[] { new ProjectScope(project), InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE } :
new IScopeContext[] { InstanceScope.INSTANCE, ConfigurationScope.INSTANCE, DefaultScope.INSTANCE };
return CCorePreferenceConstants.getPreferenceScopes(project);
}
}