📄 preferencestore.java
字号:
/******************************************************************************* * Copyright (c) 2000, 2006 IBM Corporation 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 * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/package org.eclipse.jface.preference;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.PrintStream;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Enumeration;import java.util.Properties;import org.eclipse.core.commands.common.EventManager;import org.eclipse.jface.resource.JFaceResources;import org.eclipse.jface.util.Assert;import org.eclipse.jface.util.IPropertyChangeListener;import org.eclipse.jface.util.PropertyChangeEvent;import org.eclipse.jface.util.SafeRunnable;/** * A concrete preference store implementation based on an internal * <code>java.util.Properties</code> object, with support for persisting the * non-default preference values to files or streams. * <p> * This class was not designed to be subclassed. * </p> * * @see IPreferenceStore */public class PreferenceStore extends EventManager implements IPersistentPreferenceStore { /** * The mapping from preference name to preference value (represented as * strings). */ private Properties properties; /** * The mapping from preference name to default preference value (represented * as strings); <code>null</code> if none. */ private Properties defaultProperties; /** * Indicates whether a value as been changed by <code>setToDefault</code> * or <code>setValue</code>; initially <code>false</code>. */ private boolean dirty = false; /** * The file name used by the <code>load</code> method to load a property * file. This filename is used to save the properties file when * <code>save</code> is called. */ private String filename; /** * Creates an empty preference store. * <p> * Use the methods <code>load(InputStream)</code> and * <code>save(InputStream)</code> to load and store this preference store. * </p> * * @see #load(InputStream) * @see #save(OutputStream, String) */ public PreferenceStore() { defaultProperties = new Properties(); properties = new Properties(defaultProperties); } /** * Creates an empty preference store that loads from and saves to the a * file. * <p> * Use the methods <code>load()</code> and <code>save()</code> to load * and store this preference store. * </p> * * @param filename * the file name * @see #load() * @see #save() */ public PreferenceStore(String filename) { this(); Assert.isNotNull(filename); this.filename = filename; } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public void addPropertyChangeListener(IPropertyChangeListener listener) { addListenerObject(listener); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public boolean contains(String name) { return (properties.containsKey(name) || defaultProperties .containsKey(name)); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) { final Object[] finalListeners = getListeners(); // Do we need to fire an event. if (finalListeners.length > 0 && (oldValue == null || !oldValue.equals(newValue))) { final PropertyChangeEvent pe = new PropertyChangeEvent(this, name, oldValue, newValue); for (int i = 0; i < finalListeners.length; ++i) { final IPropertyChangeListener l = (IPropertyChangeListener) finalListeners[i]; SafeRunnable.run(new SafeRunnable(JFaceResources.getString("PreferenceStore.changeError")) { //$NON-NLS-1$ public void run() { l.propertyChange(pe); } }); } } } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public boolean getBoolean(String name) { return getBoolean(properties, name); } /** * Helper function: gets boolean for a given name. * * @param p * @param name * @return boolean */ private boolean getBoolean(Properties p, String name) { String value = p != null ? p.getProperty(name) : null; if (value == null) { return BOOLEAN_DEFAULT_DEFAULT; } if (value.equals(IPreferenceStore.TRUE)) { return true; } return false; } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public boolean getDefaultBoolean(String name) { return getBoolean(defaultProperties, name); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public double getDefaultDouble(String name) { return getDouble(defaultProperties, name); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public float getDefaultFloat(String name) { return getFloat(defaultProperties, name); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public int getDefaultInt(String name) { return getInt(defaultProperties, name); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public long getDefaultLong(String name) { return getLong(defaultProperties, name); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public String getDefaultString(String name) { return getString(defaultProperties, name); } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public double getDouble(String name) { return getDouble(properties, name); } /** * Helper function: gets double for a given name. * * @param p * @param name * @return double */ private double getDouble(Properties p, String name) { String value = p != null ? p.getProperty(name) : null; if (value == null) { return DOUBLE_DEFAULT_DEFAULT; } double ival = DOUBLE_DEFAULT_DEFAULT; try { ival = new Double(value).doubleValue(); } catch (NumberFormatException e) { } return ival; } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public float getFloat(String name) { return getFloat(properties, name); } /** * Helper function: gets float for a given name. * @param p * @param name * @return float */ private float getFloat(Properties p, String name) { String value = p != null ? p.getProperty(name) : null; if (value == null) { return FLOAT_DEFAULT_DEFAULT; } float ival = FLOAT_DEFAULT_DEFAULT; try { ival = new Float(value).floatValue(); } catch (NumberFormatException e) { } return ival; } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public int getInt(String name) { return getInt(properties, name); } /** * Helper function: gets int for a given name. * @param p * @param name * @return int */ private int getInt(Properties p, String name) { String value = p != null ? p.getProperty(name) : null; if (value == null) { return INT_DEFAULT_DEFAULT; } int ival = 0; try { ival = Integer.parseInt(value); } catch (NumberFormatException e) { } return ival; } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public long getLong(String name) { return getLong(properties, name); } /** * Helper function: gets long for a given name. * @param p * @param name * @return */ private long getLong(Properties p, String name) { String value = p != null ? p.getProperty(name) : null; if (value == null) { return LONG_DEFAULT_DEFAULT; } long ival = LONG_DEFAULT_DEFAULT; try { ival = Long.parseLong(value); } catch (NumberFormatException e) { } return ival; } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public String getString(String name) { return getString(properties, name); } /** * Helper function: gets string for a given name. * @param p * @param name * @return */ private String getString(Properties p, String name) { String value = p != null ? p.getProperty(name) : null; if (value == null) { return STRING_DEFAULT_DEFAULT; } return value; } /* * (non-Javadoc) Method declared on IPreferenceStore. */ public boolean isDefault(String name) { return (!properties.containsKey(name) && defaultProperties .containsKey(name)); } /** * Prints the contents of this preference store to the given print stream.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -