bibliothek.extension.gui.dock.preference
Class PreferenceStorage

java.lang.Object
  extended by bibliothek.extension.gui.dock.preference.PreferenceStorage

public class PreferenceStorage
extends Object

A preference storage is a container storing the values of artificial preferences of one or more PreferenceModels. Clients can use store(PreferenceModel) to transfer values from model to storage, or load(PreferenceModel, boolean) to transfer values from storage to model. Further mode the entire storage can be persistently written to a file using one of the write-methods. It can later be loaded using one of the read-methods.
The static read-methods and the static write-methods can be used to write and read the contents of a PreferenceModel directly.

Author:
Benjamin Sigg

Constructor Summary
PreferenceStorage()
          Creates a new preference storage with some default factories set.
 
Method Summary
 void addFactory(Path type, PreferenceFactory<?> factory)
          Adds a new factory to this storage, the factory will be responsible to write or read some kind of preferences.
 void clear()
          Removes all preferences from this storage
 void load(PreferenceModel model, boolean missingToNull)
          Gets through all the preferences of model and changes their values according to the values stored in this storage.
 void read(DataInputStream in)
          Reads the contents of this storage from a stream.
static void read(PreferenceModel model, DataInputStream in)
          Reads preferences from in and transfers them into model.
static void readXML(PreferenceModel model, XElement element)
          Reads some preferences from element and stores them in model.
 void readXML(XElement element)
          Reads the contents of this storage from element.
 void store(PreferenceModel model)
          Stores all the preferences of model in this storage.
 void write(DataOutputStream out)
          Writes all values currently stored in this storage to out.
static void write(PreferenceModel model, DataOutputStream out)
          Writes the current preferences of model into out.
static void writeXML(PreferenceModel model, XElement element)
          Writes the preferences of model into element.
 void writeXML(XElement element)
          Writes the contents of this storage into element, adds new elements to element but does not change the attributes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PreferenceStorage

public PreferenceStorage()
Creates a new preference storage with some default factories set.

Method Detail

write

public static void write(PreferenceModel model,
                         DataOutputStream out)
                  throws IOException
Writes the current preferences of model into out.

Parameters:
model - the model to store
out - the stream to write into
Throws:
IOException - if the stream is not writeable

read

public static void read(PreferenceModel model,
                        DataInputStream in)
                 throws IOException
Reads preferences from in and transfers them into model. Missing preferences will be replaced by null.

Parameters:
model - the model to write into.
in - the stream to read from
Throws:
IOException - if the stream cannot be read

writeXML

public static void writeXML(PreferenceModel model,
                            XElement element)
Writes the preferences of model into element. This method will add new children to element but not change its attributes.

Parameters:
model - the model to store
element - the element to write into

readXML

public static void readXML(PreferenceModel model,
                           XElement element)
Reads some preferences from element and stores them in model.

Parameters:
model - the model to write into
element - the element to read
Throws:
XException - if element is incorrect

addFactory

public void addFactory(Path type,
                       PreferenceFactory<?> factory)
Adds a new factory to this storage, the factory will be responsible to write or read some kind of preferences. If there is already a factory for type, then the old factory will be replaced by factory

Parameters:
type - the type of values factory handles, this path is most times just the name of some class. Node: there is a set of standard paths defined in Path
factory - the new factory

store

public void store(PreferenceModel model)
Stores all the preferences of model in this storage. This storage uses the paths of the preferences to store each value individually. If there is already a value for some path, then that value gets replaced.

Parameters:
model - the model to read out

load

public void load(PreferenceModel model,
                 boolean missingToNull)
Gets through all the preferences of model and changes their values according to the values stored in this storage.

Parameters:
missingToNull - whether missing values should be set to null. If not set, then missing values remain just unchanged.
model - the model to write into

write

public void write(DataOutputStream out)
           throws IOException
Writes all values currently stored in this storage to out.

Parameters:
out - the stream to write into
Throws:
IOException - if the stream is not writable or if there is a factory missing for some type

read

public void read(DataInputStream in)
          throws IOException
Reads the contents of this storage from a stream. Note that this method does not clear the storage, if there are values in this storage that are not in the stream, then these values remain.

Parameters:
in - the stream to read from
Throws:
IOException - if in can't be read

writeXML

public void writeXML(XElement element)
Writes the contents of this storage into element, adds new elements to element but does not change the attributes. If a factory for some element is missing, then this element will not be stored.

Parameters:
element - the element to write into

readXML

public void readXML(XElement element)
Reads the contents of this storage from element. Note that this method does not clear the storage, if there are values in this storage that are not in element, then these values remain. If there is a PreferenceFactory missing for some type, then this value will silently be left out.

Parameters:
element - the element to read from
Throws:
XException - if element is not correct

clear

public void clear()
Removes all preferences from this storage