⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 preferenceaccessor.java

📁 eclipseme的最新版本的source,欢迎j2me程序员使用
💻 JAVA
字号:
/**
 * Copyright (c) 2004 Craig Setera
 * All Rights Reserved.
 * Licensed under the Eclipse Public License - v 1.0
 * For more information see http://www.eclipse.org/legal/epl-v10.html
 */
package eclipseme.core.internal;

import java.util.StringTokenizer;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ProjectScope;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Preferences;
import org.eclipse.core.runtime.preferences.DefaultScope;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

import eclipseme.core.IEclipseMECoreConstants;
import eclipseme.core.model.API;
import eclipseme.core.model.ApplicationDescriptor;
import eclipseme.core.model.ILibrary;
import eclipseme.core.model.IMidletSuiteProject;
import eclipseme.core.model.MidletSuiteFactory;
import eclipseme.core.model.Version;
import eclipseme.core.model.device.IDevice;

/**
 * A helper wrapper around the more complex preferences supported
 * by EclipseME.
 * <p />
 * Copyright (c) 2004 Craig Setera<br>
 * All Rights Reserved.<br>
 * Licensed under the Eclipse Public License - v 1.0<p/>
 * <br>
 * $Revision: 1.5 $
 * <br>
 * $Date: 2006/03/06 00:55:13 $
 * <br>
 * @author Craig Setera
 */
public class PreferenceAccessor {
	/** The singleton instance of the Obfuscation Preferences */
	public static final PreferenceAccessor instance = new PreferenceAccessor();

	/** Separator for multi-valued preferences */
	public static final String MULTI_VALUE_SEPARATOR = "|";
	
	// Scope search contexts
	private IEclipsePreferences defaultPreferences;
	private IEclipsePreferences instancePreferences;
	
	/**
	 * Construct a new preference wrapper.
	 * 
	 * @param preferenceStore
	 */
	private PreferenceAccessor() {
		super();
		
		defaultPreferences = new DefaultScope().getNode(IEclipseMECoreConstants.PLUGIN_ID);
		instancePreferences = new InstanceScope().getNode(IEclipseMECoreConstants.PLUGIN_ID);
	}
	
	/**
	 * Return the default Proguard keep expressions from the preferences.
	 * 
	 * @return
	 */
	public String[] getDefaultProguardKeepExpressions() {
		String keepString = getPreferences().getDefaultString(IEclipseMECoreConstants.PREF_PROGUARD_KEEP);
		return getProguardKeepExpressions(keepString);
	}
	
	/**
	 * @return Returns the defaultProguardOptions.
	 */
	public String getDefaultProguardOptions() {
		return getPreferences().getDefaultString(IEclipseMECoreConstants.PREF_PROGUARD_OPTIONS);
	}

	/**
	 * Return the excluded manifest properties based on the specified project.
	 * 
	 * @param project
	 * @return
	 */
	public String[] getExcludedManifestProperties(IProject project) {
		IEclipsePreferences preferences = getProjectPreferences(
				project, 
				IEclipseMECoreConstants.PREF_PKG_USE_PROJECT,
				IEclipseMECoreConstants.PREF_PKG_EXCLUDED_PROPS);
		String value = preferences.get(
				IEclipseMECoreConstants.PREF_PKG_EXCLUDED_PROPS, 
				EclipseMECorePreferenceInitializer.PREF_DEF_PKG_EXCLUDED_PROPS);
		
		return parseMultiValuedPreferenceValue(value);
	}
	
	/**
	 * Return a boolean indicating whether or not automatic versioning
	 * should be done while packaging.
	 * 
	 * @param project
	 * @return
	 */
	public boolean getAutoversionPackage(IProject project) {
		IEclipsePreferences preferences = getProjectPreferences(
				project, 
				IEclipseMECoreConstants.PREF_PKG_USE_PROJECT,
				IEclipseMECoreConstants.PREF_PKG_AUTOVERSION); 
		
		return preferences.getBoolean(
			IEclipseMECoreConstants.PREF_PKG_AUTOVERSION, 
			EclipseMECorePreferenceInitializer.PREF_DEF_PKG_AUTOVERSION);
	}
	
	/**
	 * Return the configuration to be used for preverification.
	 * 
	 * @param project
	 * @return
	 * @throws CoreException 
	 */
	public Version getPreverificationConfigurationVersion(IProject project) 
		throws CoreException 
	{
		Version version = null;
		
		IJavaProject javaProject = JavaCore.create(project);
		IMidletSuiteProject suite = MidletSuiteFactory.getMidletSuiteProject(javaProject);
	
		// Figure out which preference store to use
		IEclipsePreferences preferences = getProjectPreferences(
				project, 
				IEclipseMECoreConstants.PREF_PREVERIFY_USE_PROJECT,
				IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION);
		
		// Figure out where to extract the configuration from
		String location = preferences.get(
				IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION, 
				EclipseMECorePreferenceInitializer.PREF_DEF_PREVERIFY_CONFIG_LOCATION);

		// Pull the configuration from the specified place
		if (IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION_JAD.equals(location)) {
			ApplicationDescriptor jad = suite.getApplicationDescriptor();
			version = jad.getConfigurationSpecificationVersion();
		} else if (IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_LOCATION_PLATFORM.equals(location)) {
			version = new Version("1.0");
			
			IDevice device = suite.getDevice();
			if (device != null) {
				ILibrary configurationLibrary = device.getConfigurationLibrary();
				API api = configurationLibrary.getConfiguration();
				if (api != null) {
					version = api.getVersion();
				}
			}
		} else {
			String identifierOrVersion = preferences.get(
				IEclipseMECoreConstants.PREF_PREVERIFY_CONFIG_VALUE,
				EclipseMECorePreferenceInitializer.PREF_DEF_PREVERIFY_CONFIG_VALUE);
			
			// This handles the transition to the case where configurations
			// have been dropped from the plug-in extension points.
			if (identifierOrVersion.startsWith("CLDC")) {
				String[] split = identifierOrVersion.split("-");
				if (split.length == 2) {
					identifierOrVersion = split[1];
				}
			}
			
			version = new Version(identifierOrVersion);
		}

		// Fall back if we don't have a specification yet
		if (version == null) {
			version = new Version("1.0");
		}
		
		return version;
	}
	
	/**
	 * Return the Proguard keep expressions from the preferences.
	 * 
	 * @param project
	 * @return
	 */
	public String[] getProguardKeepExpressions(IProject project) {
		IEclipsePreferences preferences = getProjectPreferences(
				project, 
				IEclipseMECoreConstants.PREF_OBFUSCATION_USE_PROJECT,
				IEclipseMECoreConstants.PREF_PROGUARD_KEEP);
		String value = preferences.get(
				IEclipseMECoreConstants.PREF_PROGUARD_KEEP, 
				EclipseMECorePreferenceInitializer.PREF_DEF_PROGUARD_KEEP);
		
		return parseMultiValuedPreferenceValue(value);
	}
	
	/**
	 * Return the specified proguard options.
	 * 
	 * @param project
	 * @return Returns the specifiedOptions.
	 */
	public String getSpecifiedProguardOptions(IProject project) {
		IEclipsePreferences preferences = getProjectPreferences(
				project, 
				IEclipseMECoreConstants.PREF_OBFUSCATION_USE_PROJECT,
				IEclipseMECoreConstants.PREF_PROGUARD_OPTIONS);
		return preferences.get(
				IEclipseMECoreConstants.PREF_PROGUARD_OPTIONS, 
				EclipseMECorePreferenceInitializer.PREF_DEF_PROGUARD_OPTIONS);
	}

	/**
	 * Return whether or not to use the built-in preverifier for
	 * the specified project.
	 * 
	 * @param project
	 * @return
	 */
	public boolean getUseBuiltInPreverifier(IProject project) {
		IEclipsePreferences preferences = getProjectPreferences(
				project, 
				IEclipseMECoreConstants.PREF_PREVERIFY_USE_PROJECT,
				IEclipseMECoreConstants.PREF_PREVERIFY_BUILT_IN);
		return preferences.getBoolean(
				IEclipseMECoreConstants.PREF_PREVERIFY_BUILT_IN, 
				EclipseMECorePreferenceInitializer.PREF_DEF_PREVERIFY_BUILT_IN);
	}
	
	/**
	 * Return whether to use the specified proguard options.
	 * 
	 * @param project
	 * @return Returns the useSpecifiedOptions.
	 */
	public boolean isUseSpecifiedProguardOptions(IProject project) {
		IEclipsePreferences preferences = getProjectPreferences(
				project, 
				IEclipseMECoreConstants.PREF_OBFUSCATION_USE_PROJECT,
				IEclipseMECoreConstants.PREF_PROGUARD_USE_SPECIFIED); 
		
		return preferences.getBoolean(
			IEclipseMECoreConstants.PREF_PROGUARD_USE_SPECIFIED, 
			EclipseMECorePreferenceInitializer.PREF_DEF_PROGUARD_USE_SPECIFIED);
	}

	/**
	 * Get the preferences store from the core.
	 * 
	 * @return
	 */
	private Preferences getPreferences() {
		return EclipseMECorePlugin.getDefault().getPluginPreferences();
	}
	
	/**
	 * Return the correct preferences based on the specified project and any
	 * groups of keys that tell whether the preference should be pulled from
	 * the project or instance preferences.
	 * 
	 * @param project
	 * @param selectionKeys
	 * @return
	 */
	private IEclipsePreferences getProjectPreferences(
			IProject project, 
			String projectSpecificKey,
			String preferenceKey) 
	{
		ProjectScope projectScope = new ProjectScope(project);
		IEclipsePreferences prefNode = 
			projectScope.getNode(IEclipseMECoreConstants.PLUGIN_ID);
		
		boolean useProjectSpecific = prefNode.getBoolean(projectSpecificKey, false);
		prefNode = useProjectSpecific ? prefNode : instancePreferences;
				
		if (prefNode.get(preferenceKey, null) == null) {
			prefNode = defaultPreferences;
		}

		return prefNode;
	}
	
	/**
	 * Return the Proguard keep expressions from the specified value
	 * 
	 * @return
	 */
	private String[] getProguardKeepExpressions(String expressionsString) {
		return getMultiValuedPreference(expressionsString);
	}

	/**
	 * Return the property names that are excluded during packaging
	 * from the preferences.
	 * 
	 * @return
	 */
	public String[] getMultiValuedPreference(String preferenceName) {
		Preferences prefs = EclipseMECorePlugin.getDefault().getPluginPreferences();
		String propNames = prefs.getString(preferenceName);
		return parseMultiValuedPreferenceValue(propNames);
	}

	/**
	 * Return the parsed multi-value preference.
	 * 
	 * @param value
	 * @return
	 */
	public String[] parseMultiValuedPreferenceValue(String value) {
		StringTokenizer st = new StringTokenizer(value, MULTI_VALUE_SEPARATOR);
		
		int count = st.countTokens();
		String[] names = new String[count];
		for (int i = 0; i < count; i++) {
			names[i] = st.nextToken();
		}
	
		return names;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -