Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » PreferenceStore not supported?
PreferenceStore not supported? [message #55246] Wed, 24 October 2007 17:11 Go to next message
Eclipse UserFriend
Originally posted by: Gordon.Hu.infor.com

Hi,
I am trying to conver a RCP app to RAP and encountered a problem with a
method in AbstractUIPlugin class.
Does RAP support getPreferenceStore method? I have looked at the code
and found it commented out in
org.eclipse.rap.ui.workbench_1.0.0.20071010-2241.
If not, how should developers get around using it in their code?
Thanks, Gordon
Re: PreferenceStore not supported? [message #55489 is a reply to message #55246] Thu, 25 October 2007 20:25 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jkrause.innoopract.com

Hi Gordon,

The PreferenceStore is not supported with RAP 1.0. We implemented a
transient PreferenceStore (with session scope) for evaluation purposes,
but decided that we want to provide also some persistence support before
making it API.

It is actually not very hard to implement a transient PreferenceStore
yourself. I could also provide a patch with a simple implementation.

Jochen

Gordon Hu wrote:
> Hi,
> I am trying to conver a RCP app to RAP and encountered a problem with a
> method in AbstractUIPlugin class.
> Does RAP support getPreferenceStore method? I have looked at the code
> and found it commented out in
> org.eclipse.rap.ui.workbench_1.0.0.20071010-2241.
> If not, how should developers get around using it in their code?
> Thanks, Gordon
Re: PreferenceStore not supported? [message #55992 is a reply to message #55489] Mon, 29 October 2007 01:57 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: mascht.hotmail.com

Hi Jochen,

I would be very interested in that patch. Thanks in advance!

kindly regards,
Martin

Jochen Krause wrote:

> Hi Gordon,

> The PreferenceStore is not supported with RAP 1.0. We implemented a
> transient PreferenceStore (with session scope) for evaluation purposes,
> but decided that we want to provide also some persistence support before
> making it API.

> It is actually not very hard to implement a transient PreferenceStore
> yourself. I could also provide a patch with a simple implementation.

> Jochen

> Gordon Hu wrote:
>> Hi,
>> I am trying to conver a RCP app to RAP and encountered a problem with a
>> method in AbstractUIPlugin class.
>> Does RAP support getPreferenceStore method? I have looked at the code
>> and found it commented out in
>> org.eclipse.rap.ui.workbench_1.0.0.20071010-2241.
>> If not, how should developers get around using it in their code?
>> Thanks, Gordon
Re: PreferenceStore not supported? [message #56052 is a reply to message #55992] Mon, 29 October 2007 08:53 Go to previous message
Eclipse UserFriend
Originally posted by: jkrause.innoopract.com

This is a multi-part message in MIME format.
--------------060404040407040207080704
Content-Type: text/plain; charset=ISO-8859-15; format=flowed
Content-Transfer-Encoding: 7bit

Please note:
This implementation will NOT be booked into CVS, it was just written for
evaluation purposes. It does not use the Extension Point mechanism for
contributing scopes to the preference store, it does not use the ICU
i18n and the preferences are not persisted. Accessing the
PreferenceStore from non-ui threads will result in a return value of null.


Having said all that, attached are three classes that implement the
PreferenceStore, and the altered version of the getPreferenceStore
method in AbstractUIPlugin:

public IPreferenceStore getPreferenceStore() {
ScopedPreferenceStore preferenceStore = null;
try{
ISessionStore session = RWT.getSessionStore();
preferenceStore = ( ScopedPreferenceStore )session.getAttribute(
"ScopedPreferenceStore" );
// Create the preference store lazily.
if (preferenceStore == null) {
preferenceStore = new ScopedPreferenceStore(new
SessionScopeContext( session.getId() ),getBundle().getSymbolicName());
session.setAttribute( "ScopedPreferenceStore",
preferenceStore );
}
} catch (Exception e) {
WorkbenchPlugin.log( "NO CONTEXT AVAILABLE WHILE ACCESSING
PREFERENCE STORE", e );
}
return preferenceStore;
}


Martin Claus wrote:
> Hi Jochen,
>
> I would be very interested in that patch. Thanks in advance!
>
> kindly regards,
> Martin
>
> Jochen Krause wrote:
>
>> Hi Gordon,
>
>> The PreferenceStore is not supported with RAP 1.0. We implemented a
>> transient PreferenceStore (with session scope) for evaluation
>> purposes, but decided that we want to provide also some persistence
>> support before making it API.
>
>> It is actually not very hard to implement a transient PreferenceStore
>> yourself. I could also provide a patch with a simple implementation.
>
>> Jochen
>
>> Gordon Hu wrote:
>>> Hi,
>>> I am trying to conver a RCP app to RAP and encountered a problem with
>>> a method in AbstractUIPlugin class.
>>> Does RAP support getPreferenceStore method? I have looked at the code
>>> and found it commented out in
>>> org.eclipse.rap.ui.workbench_1.0.0.20071010-2241.
>>> If not, how should developers get around using it in their code?
>>> Thanks, Gordon
>
>


--------------060404040407040207080704
Content-Type: text/plain;
name="SessionScopeContext.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="SessionScopeContext.java"

/*********************************************************** ********************
* Copyright (c) 2002-2006 Innoopract Informationssysteme GmbH.
* 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:
* Innoopract Informationssysteme GmbH - initial API and implementation
************************************************************ ******************/

package org.eclipse.ui.preferences;

import org.eclipse.core.internal.preferences.EclipsePreferences;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IScopeContext;


public class SessionScopeContext implements IScopeContext {

String name = null;
ServerScope pref = null;

private SessionScopeContext(){
// inhibit default constructor instantiation
}

public SessionScopeContext( String name ) {
this.name = name;
pref = new ServerScope();
}

public IPath getLocation() {
return null;
}

public String getName() {
return name;
}

public IEclipsePreferences getNode( String arg0 ) {
return pref;
}
}

--------------060404040407040207080704
Content-Type: text/plain;
name="ScopedPreferenceStore.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ScopedPreferenceStore.java"

/*********************************************************** ********************
* Copyright (c) 2004, 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.ui.preferences;

import java.io.IOException;

import org.eclipse.core.commands.common.EventManager;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Plugin;
import org.eclipse.core.runtime.SafeRunner;
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.IEclipsePreferences.INo deChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.Nod eChangeEvent;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.Pre ferenceChangeEvent;
import org.eclipse.jface.preference.IPersistentPreferenceStore;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.jface.util.SafeRunnable;
import org.eclipse.ui.internal.WorkbenchMessages;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
* The ScopedPreferenceStore is an IPreferenceStore that uses the scopes
* provided in org.eclipse.core.runtime.preferences.
* <p>
* A ScopedPreferenceStore does the lookup of a preference based on it's search
* scopes and sets the value of the preference based on its store scope.
* </p>
* <p>
* The default scope is always included in the search scopes when searching for
* preference values.
* </p>
*
* @see org.eclipse.core.runtime.preferences
* @since 3.1
*/
public class ScopedPreferenceStore extends EventManager implements
IPreferenceStore, IPersistentPreferenceStore {

/**
* The storeContext is the context where values will stored with the
* setValue methods. If there are no searchContexts this will be the search
* context. (along with the "default" context)
*/
private IScopeContext storeContext;

/**
* The searchContext is the array of contexts that will be used by the get
* methods for searching for values.
*/
private IScopeContext[] searchContexts;

/**
* A boolean to indicate the property changes should not be propagated.
*/
protected boolean silentRunning = false;

/**
* The listener on the IEclipsePreferences. This is used to forward updates
* to the property change listeners on the preference store.
*/
IEclipsePreferences.IPreferenceChangeListener preferencesListener;

/**
* The default context is the context where getDefault and setDefault
* methods will search. This context is also used in the search.
*/
private IScopeContext defaultContext = new DefaultScope();

/**
* The nodeQualifer is the string used to look up the node in the contexts.
*/
String nodeQualifier;

/**
* The defaultQualifier is the string used to look up the default node.
*/
String defaultQualifier;

/**
* Boolean value indicating whether or not this store has changes to be
* saved.
*/
private boolean dirty;

/**
* Create a new instance of the receiver. Store the values in context in the
* node looked up by qualifier. <strong>NOTE:</strong> Any instance of
* ScopedPreferenceStore should call
*
* @param context
* the scope to store to
* @param qualifier
* the qualifier used to look up the preference node
* @param defaultQualifierPath
* the qualifier used when looking up the defaults
*/
public ScopedPreferenceStore(IScopeContext context, String qualifier,
String defaultQualifierPath) {
this(context, qualifier);
this.defaultQualifier = defaultQualifierPath;
}

/**
* Create a new instance of the receiver. Store the values in context in the
* node looked up by qualifier.
*
* @param context
* the scope to store to
* @param qualifier
* the qualifer used to look up the preference node
*/
public ScopedPreferenceStore(IScopeContext context, String qualifier) {
storeContext = context;
this.nodeQualifier = qualifier;
this.defaultQualifier = qualifier;

Preferences parent = getStorePreferences().parent();
if( parent != null ) {
((IEclipsePreferences) parent)
.addNodeChangeListener(getNodeChangeListener());
}
}

/**
* Return a node change listener that adds a removes the receiver when nodes
* change.
*
* @return INodeChangeListener
*/
private INodeChangeListener getNodeChangeListener() {
return new IEclipsePreferences.INodeChangeListener() {
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INo deChangeListener#added(org.eclipse.core.runtime.preferences. IEclipsePreferences.NodeChangeEvent)
*/
public void added(NodeChangeEvent event) {
if (nodeQualifier.equals(event.getChild().name())
&& isListenerAttached()) {
getStorePreferences().addPreferenceChangeListener(
preferencesListener);
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences.INo deChangeListener#removed(org.eclipse.core.runtime.preference s.IEclipsePreferences.NodeChangeEvent)
*/
public void removed(NodeChangeEvent event) {
// Do nothing as there are no events from removed node
}
};
}

/**
* Initialize the preferences listener.
*/
private void initializePreferencesListener() {
if (preferencesListener == null) {
preferencesListener = new IEclipsePreferences.IPreferenceChangeListener() {
/*
* (non-Javadoc)
*
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPr eferenceChangeListener#preferenceChange(org.eclipse.core.run time.preferences.IEclipsePreferences.PreferenceChangeEvent)
*/
public void preferenceChange(PreferenceChangeEvent event) {

if (silentRunning) {
return;
}

Object oldValue = event.getOldValue();
Object newValue = event.getNewValue();
String key = event.getKey();
if (newValue == null) {
newValue = getDefault(key, oldValue);
} else if (oldValue == null) {
oldValue = getDefault(key, newValue);
}
firePropertyChangeEvent(event.getKey(), oldValue, newValue);
}
};
getStorePreferences().addPreferenceChangeListener(
preferencesListener);
}

}

/**
* Does its best at determining the default value for the given key. Checks
* the given object's type and then looks in the list of defaults to see if
* a value exists. If not or if there is a problem converting the value, the
* default default value for that type is returned.
*
* @param key
* the key to search
* @param obj
* the object who default we are looking for
* @return Object or <code>null</code>
*/
Object getDefault(String key, Object obj) {
IEclipsePreferences defaults = getDefaultPreferences();
if (obj instanceof String) {
return defaults.get(key, STRING_DEFAULT_DEFAULT);
} else if (obj instanceof Integer) {
return new Integer(defaults.getInt(key, INT_DEFAULT_DEFAULT));
} else if (obj instanceof Double) {
return new Double(defaults.getDouble(key, DOUBLE_DEFAULT_DEFAULT));
} else if (obj instanceof Float) {
return new Float(defaults.getFloat(key, FLOAT_DEFAULT_DEFAULT));
} else if (obj instanceof Long) {
return new Long(defaults.getLong(key, LONG_DEFAULT_DEFAULT));
} else if (obj instanceof Boolean) {
return defaults.getBoolean(key, BOOLEAN_DEFAULT_DEFAULT) ? Boolean.TRUE : Boolean.FALSE;
} else {
return null;
}
}

/**
* Return the IEclipsePreferences node associated with this store.
*
* @return the preference node for this store
*/
IEclipsePreferences getStorePreferences() {
return storeContext.getNode(nodeQualifier);
}

/**
* Return the default IEclipsePreferences for this store.
*
* @return this store's default preference node
*/
private IEclipsePreferences getDefaultPreferences() {
return defaultContext.getNode(defaultQualifier);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#addPropertyCha ngeListener(org.eclipse.jface.util.IPropertyChangeListener)
*/
public void addPropertyChangeListener(IPropertyChangeListener listener) {
initializePreferencesListener();// Create the preferences listener if it
// does not exist
addListenerObject(listener);
}

/**
* Return the preference path to search preferences on. This is the list of
* preference nodes based on the scope contexts for this store. If there are
* no search contexts set, then return this store's context.
* <p>
* Whether or not the default context should be included in the resulting
* list is specified by the <code>includeDefault</code> parameter.
* </p>
*
* @param includeDefault
* <code>true</code> if the default context should be included
* and <code>false</code> otherwise
* @return IEclipsePreferences[]
*/
private IEclipsePreferences[] getPreferenceNodes(boolean includeDefault) {
// if the user didn't specify a search order, then return the scope that
// this store was created on. (and optionally the default)
if (searchContexts == null) {
if (includeDefault) {
return new IEclipsePreferences[] { getStorePreferences(),
getDefaultPreferences() };
}
return new IEclipsePreferences[] { getStorePreferences() };
}
// otherwise the user specified a search order so return the appropriate
// nodes based on it
int length = searchContexts.length;
if (includeDefault) {
length++;
}
IEclipsePreferences[] preferences = new IEclipsePreferences[length];
for (int i = 0; i < searchContexts.length; i++) {
preferences[i] = searchContexts[i].getNode(nodeQualifier);
}
if (includeDefault) {
preferences[length - 1] = getDefaultPreferences();
}
return preferences;
}

/**
* Set the search contexts to scopes. When searching for a value the seach
* will be done in the order of scope contexts and will not search the
* storeContext unless it is in this list.
* <p>
* If the given list is <code>null</code>, then clear this store's search
* contexts. This means that only this store's scope context and default
* scope will be used during preference value searching.
* </p>
* <p>
* The defaultContext will be added to the end of this list automatically
* and <em>MUST NOT</em> be included by the user.
* </p>
*
* @param scopes
* a list of scope contexts to use when searching, or
* <code>null</code>
*/
public void setSearchContexts(IScopeContext[] scopes) {
this.searchContexts = scopes;
if (scopes == null) {
return;
}

// Assert that the default was not included (we automatically add it to
// the end)
for (int i = 0; i < scopes.length; i++) {
if (scopes[i].equals(defaultContext)) {
Assert
.isTrue(
false,
WorkbenchMessages.ScopedPreferenceStore_DefaultAddedError);
}
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#contains(java. lang.String)
*/
public boolean contains(String name) {
if (name == null) {
return false;
}
return (Platform.getPreferencesService().get(name, null,
getPreferenceNodes(true))) != null;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#firePropertyCh angeEvent(java.lang.String,
* java.lang.Object, java.lang.Object)
*/
public void firePropertyChangeEvent(String name, Object oldValue,
Object newValue) {
// important: create intermediate array to protect against listeners
// being added/removed during the notification
final Object[] list = getListeners();
if (list.length == 0) {
return;
}
final PropertyChangeEvent event = new PropertyChangeEvent(this, name,
oldValue, newValue);
for (int i = 0; i < list.length; i++) {
final IPropertyChangeListener listener = (IPropertyChangeListener) list[i];
SafeRunner.run(new SafeRunnable(JFaceResources
.getString("PreferenceStore.changeError")) { //$NON-NLS-1$
public void run() {
listener.propertyChange(event);
}
});
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getBoolean(jav a.lang.String)
*/
public boolean getBoolean(String name) {
String value = internalGet(name);
return value == null ? BOOLEAN_DEFAULT_DEFAULT : Boolean.valueOf(value)
.booleanValue();
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getDefaultBool ean(java.lang.String)
*/
public boolean getDefaultBoolean(String name) {
return getDefaultPreferences()
.getBoolean(name, BOOLEAN_DEFAULT_DEFAULT);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getDefaultDoub le(java.lang.String)
*/
public double getDefaultDouble(String name) {
return getDefaultPreferences().getDouble(name, DOUBLE_DEFAULT_DEFAULT);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getDefaultFloa t(java.lang.String)
*/
public float getDefaultFloat(String name) {
return getDefaultPreferences().getFloat(name, FLOAT_DEFAULT_DEFAULT);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getDefaultInt( java.lang.String)
*/
public int getDefaultInt(String name) {
return getDefaultPreferences().getInt(name, INT_DEFAULT_DEFAULT);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getDefaultLong (java.lang.String)
*/
public long getDefaultLong(String name) {
return getDefaultPreferences().getLong(name, LONG_DEFAULT_DEFAULT);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getDefaultStri ng(java.lang.String)
*/
public String getDefaultString(String name) {
return getDefaultPreferences().get(name, STRING_DEFAULT_DEFAULT);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getDouble(java .lang.String)
*/
public double getDouble(String name) {
String value = internalGet(name);
if (value == null) {
return DOUBLE_DEFAULT_DEFAULT;
}
try {
return Double.parseDouble(value);
} catch (NumberFormatException e) {
return DOUBLE_DEFAULT_DEFAULT;
}
}

/**
* Return the string value for the specified key. Look in the nodes which
* are specified by this object's list of search scopes. If the value does
* not exist then return <code>null</code>.
*
* @param key
* the key to search with
* @return String or <code>null</code> if the value does not exist.
*/
private String internalGet(String key) {
return Platform.getPreferencesService().get(key, null,
getPreferenceNodes(true));
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getFloat(java. lang.String)
*/
public float getFloat(String name) {
String value = internalGet(name);
if (value == null) {
return FLOAT_DEFAULT_DEFAULT;
}
try {
return Float.parseFloat(value);
} catch (NumberFormatException e) {
return FLOAT_DEFAULT_DEFAULT;
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getInt(java.la ng.String)
*/
public int getInt(String name) {
String value = internalGet(name);
if (value == null) {
return INT_DEFAULT_DEFAULT;
}
try {
return Integer.parseInt(value);
} catch (NumberFormatException e) {
return INT_DEFAULT_DEFAULT;
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getLong(java.l ang.String)
*/
public long getLong(String name) {
String value = internalGet(name);
if (value == null) {
return LONG_DEFAULT_DEFAULT;
}
try {
return Long.parseLong(value);
} catch (NumberFormatException e) {
return LONG_DEFAULT_DEFAULT;
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#getString(java .lang.String)
*/
public String getString(String name) {
String value = internalGet(name);
return value == null ? STRING_DEFAULT_DEFAULT : value;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#isDefault(java .lang.String)
*/
public boolean isDefault(String name) {
if (name == null) {
return false;
}
return (Platform.getPreferencesService().get(name, null,
getPreferenceNodes(false))) == null;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#needsSaving()
*/
public boolean needsSaving() {
return dirty;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#putValue(java. lang.String,
* java.lang.String)
*/
public void putValue(String name, String value) {
try {
// Do not notify listeners
silentRunning = true;
getStorePreferences().put(name, value);
} finally {
// Be sure that an exception does not stop property updates
silentRunning = false;
dirty = true;
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#removeProperty ChangeListener(org.eclipse.jface.util.IPropertyChangeListene r)
*/
public void removePropertyChangeListener(IPropertyChangeListener listener) {
removeListenerObject(listener);
if (!isListenerAttached()) {
disposePreferenceStoreListener();
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setDefault(jav a.lang.String,
* double)
*/
public void setDefault(String name, double value) {
getDefaultPreferences().putDouble(name, value);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setDefault(jav a.lang.String,
* float)
*/
public void setDefault(String name, float value) {
getDefaultPreferences().putFloat(name, value);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setDefault(jav a.lang.String,
* int)
*/
public void setDefault(String name, int value) {
getDefaultPreferences().putInt(name, value);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setDefault(jav a.lang.String,
* long)
*/
public void setDefault(String name, long value) {
getDefaultPreferences().putLong(name, value);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setDefault(jav a.lang.String,
* java.lang.String)
*/
public void setDefault(String name, String defaultObject) {
getDefaultPreferences().put(name, defaultObject);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setDefault(jav a.lang.String,
* boolean)
*/
public void setDefault(String name, boolean value) {
getDefaultPreferences().putBoolean(name, value);
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setToDefault(j ava.lang.String)
*/
public void setToDefault(String name) {

String oldValue = getString(name);
String defaultValue = getDefaultString(name);
try {
silentRunning = true;// Turn off updates from the store
// removing a non-existing preference is a no-op so call the Core
// API directly
getStorePreferences().remove(name);
dirty = true;
firePropertyChangeEvent(name, oldValue, defaultValue);
} finally {
silentRunning = false;// Restart listening to preferences
}

}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setValue(java. lang.String,
* double)
*/
public void setValue(String name, double value) {
double oldValue = getDouble(name);
if (oldValue == value) {
return;
}
try {
silentRunning = true;// Turn off updates from the store
if (getDefaultDouble(name) == value) {
getStorePreferences().remove(name);
} else {
getStorePreferences().putDouble(name, value);
}
dirty = true;
firePropertyChangeEvent(name, new Double(oldValue), new Double(
value));
} finally {
silentRunning = false;// Restart listening to preferences
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setValue(java. lang.String,
* float)
*/
public void setValue(String name, float value) {
float oldValue = getFloat(name);
if (oldValue == value) {
return;
}
try {
silentRunning = true;// Turn off updates from the store
if (getDefaultFloat(name) == value) {
getStorePreferences().remove(name);
} else {
getStorePreferences().putFloat(name, value);
}
dirty = true;
firePropertyChangeEvent(name, new Float(oldValue), new Float(value));
} finally {
silentRunning = false;// Restart listening to preferences
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setValue(java. lang.String,
* int)
*/
public void setValue(String name, int value) {
int oldValue = getInt(name);
if (oldValue == value) {
return;
}
try {
silentRunning = true;// Turn off updates from the store
if (getDefaultInt(name) == value) {
getStorePreferences().remove(name);
} else {
getStorePreferences().putInt(name, value);
}
dirty = true;
firePropertyChangeEvent(name, new Integer(oldValue), new Integer(
value));
} finally {
silentRunning = false;// Restart listening to preferences
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setValue(java. lang.String,
* long)
*/
public void setValue(String name, long value) {
long oldValue = getLong(name);
if (oldValue == value) {
return;
}
try {
silentRunning = true;// Turn off updates from the store
if (getDefaultLong(name) == value) {
getStorePreferences().remove(name);
} else {
getStorePreferences().putLong(name, value);
}
dirty = true;
firePropertyChangeEvent(name, new Long(oldValue), new Long(value));
} finally {
silentRunning = false;// Restart listening to preferences
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setValue(java. lang.String,
* java.lang.String)
*/
public void setValue(String name, String value) {
// Do not turn on silent running here as Strings are propagated
if (getDefaultString(name).equals(value)) {
getStorePreferences().remove(name);
} else {
getStorePreferences().put(name, value);
}
dirty = true;
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPreferenceStore#setValue(java. lang.String,
* boolean)
*/
public void setValue(String name, boolean value) {
boolean oldValue = getBoolean(name);
if (oldValue == value) {
return;
}
try {
silentRunning = true;// Turn off updates from the store
if (getDefaultBoolean(name) == value) {
getStorePreferences().remove(name);
} else {
getStorePreferences().putBoolean(name, value);
}
dirty = true;
firePropertyChangeEvent(name, oldValue ? Boolean.TRUE : Boolean.FALSE,
value ? Boolean.TRUE : Boolean.FALSE);
} finally {
silentRunning = false;// Restart listening to preferences
}
}

/*
* (non-Javadoc)
*
* @see org.eclipse.jface.preference.IPersistentPreferenceStore#save ()
*/
public void save() throws IOException {
try {
getStorePreferences().flush();
dirty = false;
} catch (BackingStoreException e) {
throw new IOException(e.getMessage());
}

}

/**
* Dispose the receiver.
*/
private void disposePreferenceStoreListener() {

IEclipsePreferences root = (IEclipsePreferences) Platform
.getPreferencesService().getRootNode().node(
Plugin.PLUGIN_PREFERENCE_SCOPE);
try {
if (!(root.nodeExists(nodeQualifier))) {
return;
}
} catch (BackingStoreException e) {
return;// No need to report here as the node won't have the
// listener
}

IEclipsePreferences preferences = getStorePreferences();
if (preferences == null) {
return;
}
if (preferencesListener != null) {
preferences.removePreferenceChangeListener(preferencesListen er);
preferencesListener = null;
}
}

}

--------------060404040407040207080704
Content-Type: text/plain;
name="ServerScope.java"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="ServerScope.java"

/*********************************************************** ********************
* Copyright (c) 2005 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
************************************************************ *******************/
package org.eclipse.ui.preferences;

import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.core.runtime.preferences.*;
import org.osgi.service.prefs.BackingStoreException;
import org.osgi.service.prefs.Preferences;

/**
* @since 3.1
*/
public class ServerScope implements IScope, IEclipsePreferences {

public static final String SCOPE = "server"; //$NON-NLS-1$
private ListenerList nodeChangeListeners;
private ListenerList preferenceChangeListeners;
protected boolean removed = false;
protected static final IEclipsePreferences[] EMPTY_NODE_ARRAY = new IEclipsePreferences[0];
protected static final String[] EMPTY_STRING_ARRAY = new String[0];
protected Map children;
protected HashMap properties = new HashMap();
protected boolean dirty = false;
private static final String FALSE = "false"; //$NON-NLS-1$
private static final String TRUE = "true"; //$NON-NLS-1$


/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IScope#create(org.eclip se.core.runtime.preferences.IEclipsePreferences, java.lang.String)
*/
public IEclipsePreferences create(IEclipsePreferences parent, String name) {
return new ServerScope(parent, name);
}

private String name;
private IEclipsePreferences parent;

public ServerScope() {
super();
}

public ServerScope(IEclipsePreferences parent, String name) {
super();
this.name = name;
this.parent = parent;
}

/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences#add NodeChangeListener(org.eclipse.core.runtime.preferences.IEcl ipsePreferences.INodeChangeListener)
*/
public void addNodeChangeListener(INodeChangeListener listener) {
checkRemoved();
if (nodeChangeListeners == null)
nodeChangeListeners = new ListenerList();
nodeChangeListeners.add(listener);
}

/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences#rem oveNodeChangeListener(org.eclipse.core.runtime.preferences.I EclipsePreferences.INodeChangeListener)
*/
public void removeNodeChangeListener(INodeChangeListener listener) {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences#add PreferenceChangeListener(org.eclipse.core.runtime.preference s.IEclipsePreferences.IPreferenceChangeListener)
*/
public void addPreferenceChangeListener(IPreferenceChangeListener listener) {
checkRemoved();
if (preferenceChangeListeners == null)
preferenceChangeListeners = new ListenerList();
preferenceChangeListeners.add(listener);
}

/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences#rem ovePreferenceChangeListener(org.eclipse.core.runtime.prefere nces.IEclipsePreferences.IPreferenceChangeListener)
*/
public void removePreferenceChangeListener(IPreferenceChangeListener listener) {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#removeNode()
*/
public void removeNode() throws BackingStoreException {
// illegal state if this node has been removed
checkRemoved();
// clear all the property values. do it "the long way" so
// everyone gets notification
String[] keys = keys();
for (int i = 0; i < keys.length; i++)
remove(keys[i]);
IEclipsePreferences[] childNodes = getChildren(false);
for (int i = 0; i < childNodes.length; i++)
try {
childNodes[i].removeNode();
} catch (IllegalStateException e) {
// ignore since we only get this exception if we have already
// been removed. no work to do.
}
}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#node(java.lang.String)
*/
public Preferences node(String path) {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.eclipse.core.runtime.preferences.IEclipsePreferences#acc ept(org.eclipse.core.runtime.preferences.IPreferenceNodeVisi tor)
*/
public void accept(IPreferenceNodeVisitor visitor) throws BackingStoreException {
// TODO Auto-generated method stub

}

/*
* @see org.osgi.service.prefs.Preferences#put(java.lang.String, java.lang.String)
*/
public void put(String key, String newValue) {
if (key == null || newValue == null)
throw new NullPointerException();
String oldValue = internalPut(key, newValue);
if (!newValue.equals(oldValue)) {
makeDirty();
firePreferenceEvent(key, oldValue, newValue);
}
}

/*
* @see org.osgi.service.prefs.Preferences#putBoolean(java.lang.Stri ng, boolean)
*/
public void putBoolean(String key, boolean value) {
if (key == null)
throw new NullPointerException();
String newValue = value ? TRUE : FALSE;
String oldValue = internalPut(key, newValue);
if (!newValue.equals(oldValue)) {
makeDirty();
firePreferenceEvent(key, oldValue, newValue);
}
}

/*
* @see org.osgi.service.prefs.Preferences#putByteArray(java.lang.St ring, byte[])
*/
public void putByteArray(String key, byte[] value) {
if (key == null || value == null)
throw new NullPointerException();
String newValue = new String( value );
String oldValue = internalPut(key, newValue);
if (!newValue.equals(oldValue)) {
makeDirty();
firePreferenceEvent(key, oldValue, newValue);
}
}

/*
* @see org.osgi.service.prefs.Preferences#putDouble(java.lang.Strin g, double)
*/
public void putDouble(String key, double value) {
if (key == null)
throw new NullPointerException();
String newValue = Double.toString(value);
String oldValue = internalPut(key, newValue);
if (!newValue.equals(oldValue)) {
makeDirty();
firePreferenceEvent(key, oldValue, newValue);
}
}

/*
* @see org.osgi.service.prefs.Preferences#putFloat(java.lang.String , float)
*/
public void putFloat(String key, float value) {
if (key == null)
throw new NullPointerException();
String newValue = Float.toString(value);
String oldValue = internalPut(key, newValue);
if (!newValue.equals(oldValue)) {
makeDirty();
firePreferenceEvent(key, oldValue, newValue);
}
}

/*
* @see org.osgi.service.prefs.Preferences#putInt(java.lang.String, int)
*/
public void putInt(String key, int value) {
if (key == null)
throw new NullPointerException();
String newValue = Integer.toString(value);
String oldValue = internalPut(key, newValue);
if (!newValue.equals(oldValue)) {
makeDirty();
firePreferenceEvent(key, oldValue, newValue);
}
}

/*
* @see org.osgi.service.prefs.Preferences#putLong(java.lang.String, long)
*/
public void putLong(String key, long value) {
if (key == null)
throw new NullPointerException();
String newValue = Long.toString(value);
String oldValue = internalPut(key, newValue);
if (!newValue.equals(oldValue)) {
makeDirty();
firePreferenceEvent(key, oldValue, newValue);
}
}

/*
* @see org.osgi.service.prefs.Preferences#remove(java.lang.String)
*/
public void remove(String key) {
String oldValue = ( String )properties.get(key);
if (oldValue == null)
return;
properties.remove( key );
makeDirty();
firePreferenceEvent(key, oldValue, null);
}

/*
* @see org.osgi.service.prefs.Preferences#get(java.lang.String, java.lang.String)
*/
public String get(String key, String defaultValue) {
String value = internalGet(key);
return value == null ? defaultValue : value;
}

/*
* @see org.osgi.service.prefs.Preferences#getBoolean(java.lang.Stri ng, boolean)
*/
public boolean getBoolean(String key, boolean defaultValue) {
String value = internalGet(key);
return value == null ? defaultValue : TRUE.equalsIgnoreCase(value);
}

/*
* @see org.osgi.service.prefs.Preferences#getByteArray(java.lang.St ring, byte[])
*/
public byte[] getByteArray(String key, byte[] defaultValue) {
String value = internalGet(key);
return value == null ? defaultValue : value.getBytes();
}

/*
* @see org.osgi.service.prefs.Preferences#clear()
*/
public void clear() {
// illegal state if this node has been removed
checkRemoved();
// call each one separately (instead of Properties.clear) so
// clients get change notification
String[] keys = ( String[] )properties.keySet().toArray();
for (int i = 0; i < keys.length; i++)
remove(keys[i]);
makeDirty();
}

protected void makeDirty() {
dirty = true;
}


/*
* @see org.osgi.service.prefs.Preferences#getDouble(java.lang.Strin g, double)
*/
public double getDouble(String key, double defaultValue) {
String value = internalGet(key);
double result = defaultValue;
if (value != null)
try {
result = Double.parseDouble(value);
} catch (NumberFormatException e) {
// use default
}
return result;
}

/*
* @see org.osgi.service.prefs.Preferences#getFloat(java.lang.String , float)
*/
public float getFloat(String key, float defaultValue) {
String value = internalGet(key);
float result = defaultValue;
if (value != null)
try {
result = Float.parseFloat(value);
} catch (NumberFormatException e) {
// use default
}
return result;
}

/*
* @see org.osgi.service.prefs.Preferences#getInt(java.lang.String, int)
*/
public int getInt(String key, int defaultValue) {
String value = internalGet(key);
int result = defaultValue;
if (value != null)
try {
result = Integer.parseInt(value);
} catch (NumberFormatException e) {
// use default
}
return result;
}

/*
* @see org.osgi.service.prefs.Preferences#getLong(java.lang.String, long)
*/
public long getLong(String key, long defaultValue) {
String value = internalGet(key);
long result = defaultValue;
if (value != null)
try {
result = Long.parseLong(value);
} catch (NumberFormatException e) {
// use default
}
return result;
}


/*
* @see org.osgi.service.prefs.Preferences#keys()
*/
public String[] keys() {
// illegal state if this node has been removed
checkRemoved();
return ( String[] )properties.keySet().toArray();
}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#childrenNames()
*/
public String[] childrenNames() throws BackingStoreException {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#parent()
*/
public Preferences parent() {
return this.parent;
}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#nodeExists(java.lang.Stri ng)
*/
public boolean nodeExists(String pathName) throws BackingStoreException {
// TODO Auto-generated method stub
return false;
}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#name()
*/
public String name() {
return this.name;
}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#absolutePath()
*/
public String absolutePath() {
// TODO Auto-generated method stub
return null;
}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#flush()
*/
public void flush() throws BackingStoreException {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* @see org.osgi.service.prefs.Preferences#sync()
*/
public void sync() throws BackingStoreException {
// TODO Auto-generated method stub

}

/*
* Convenience method for throwing an exception when methods
* are called on a removed node.
*/
protected void checkRemoved() {
if (removed)
throw new IllegalStateException(name);
}

/**
* Returns the existing value at the given key, or null if
* no such value exists.
*/
protected String internalGet(String key) {
// throw NPE if key is null
if (key == null)
throw new NullPointerException();
// illegal state if this node has been removed
checkRemoved();
String result = ( String )properties.get(key);
return result;
}


/**
* Thread safe way to obtain all children of this node. Never returns null.
*/
protected IEclipsePreferences[] getChildren(boolean create) {
ArrayList result = new ArrayList();
String[] names = internalChildNames();
for (int i = 0; i < names.length; i++) {
IEclipsePreferences child = getChild(names[i], null, create);
if (child != null)
result.add(child);
}
return (IEclipsePreferences[]) result.toArray(EMPTY_NODE_ARRAY);
}

protected String[] internalChildNames() {
Map temp = children;
if (temp == null || temp.size() == 0)
return EMPTY_STRING_ARRAY;
return (String[]) temp.keySet().toArray(EMPTY_STRING_ARRAY);
}

/**
* Thread safe way to obtain a child for a given key. Returns the child
* that matches the given key, or null if there is no matching child.
*/
protected IEclipsePreferences getChild(String key, Object context, boolean create) {
synchronized (this) {
if (children == null)
return null;
Object value = children.get(key);
if (value == null)
return null;
if (value instanceof IEclipsePreferences)
return (IEclipsePreferences) value;
// if we aren't supposed to create this node, then
// just return null
if (!create)
return null;
}
return addChild(key, create(this, key, context));
}

protected synchronized IEclipsePreferences addChild(String childName, IEclipsePreferences child) {
//Thread safety: synchronize method to protect modification of children field
if (children == null)
children = Collections.synchronizedMap(new HashMap());
children.put(childName, child == null ? (Object) childName : child);
return child;
}

protected ServerScope internalCreate(ServerScope nodeParent, String nodeName, Object context) {
return new ServerScope(nodeParent, nodeName);
}

protected IEclipsePreferences getLoadLevel() {
return null;
}

public IEclipsePreferences create(ServerScope nodeParent, String nodeName, Object context) {
ServerScope result = internalCreate(nodeParent, nodeName, context);
nodeParent.addChild(nodeName, result);
IEclipsePreferences loadLevel = result.getLoadLevel();

// if this node or a parent node is not the load level then return
if (loadLevel == null)
return result;

// if the result node is not a load level, then a child must be
if (result != loadLevel)
return result;

// the result node is a load level
if (isAlreadyLoaded(result) )
return result;
return result;
}

/*
* Subclasses to over-ride.
*/
protected boolean isAlreadyLoaded(IEclipsePreferences node) {
return true;
}

/**
* Stores the given (key,value) pair, performing lazy initialization of the
* properties field if necessary. Returns the old value for the given key,
* or null if no value existed.
*/
protected String internalPut(String key, String newValue) {
// illegal state if this node has been removed
checkRemoved();
String oldValue = ( String )properties.get(key);
if (oldValue != null && oldValue.equals(newValue))
return oldValue;
properties.put(key, newValue);
return oldValue;
}

/*
* Convenience method for notifying preference change listeners.
*/
protected void firePreferenceEvent(String key, Object oldValue, Object newValue) {
if (preferenceChangeListeners == null)
return;
Object[] listeners = preferenceChangeListeners.getListeners();
final PreferenceChangeEvent event = new PreferenceChangeEvent(this, key, oldValue, newValue);
for (int i = 0; i < listeners.length; i++) {
final IPreferenceChangeListener listener = (IPreferenceChangeListener) listeners[i];
ISafeRunnable job = new ISafeRunnable() {
public void handleException(Throwable exception) {
// already logged in Platform#run()
}

public void run() throws Exception {
listener.preferenceChange(event);
}
};
SafeRunner.run(job);
}
}


}

--------------060404040407040207080704--
Previous Topic:insert in tableviewer
Next Topic:Exception when shutting down Equinox service
Goto Forum:
  


Current Time: Sat Jun 10 20:33:31 GMT 2023

Powered by FUDForum. Page generated in 0.02389 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top