bibliothek.gui.dock
Class DockSituation

java.lang.Object
  extended by bibliothek.gui.dock.DockSituation
Direct Known Subclasses:
PredefinedDockSituation

public class DockSituation
extends java.lang.Object

A DockSituation is a converter: the relationship of DockStations and Dockables, the position of Dockables and other information are converted into a stream of bytes. The other direction, read a stream and create Dockables and DockStations, is also possible.

Author:
Benjamin Sigg

Field Summary
private  java.util.Map<java.lang.String,DockFactory<?>> factories
          the factories used to create new elements
private  DockSituationIgnore ignore
          a filter for elements which should be ignored
 
Constructor Summary
DockSituation()
          Constructs a new DockSituation.
DockSituation(DockFactory<?>... factories)
          Constructs a new DockSituation and sets some factories which are used to create new DockElements
 
Method Summary
 void add(DockFactory<?> factory)
          Adds a factory
protected  DockFactory<? extends DockElement> getFactory(java.lang.String id)
          Gets the factory which has the given id.
protected  java.lang.String getID(DockElement dockable)
          Gets the id of the factory which is needed to write (and later read) the element dockable.
protected  java.lang.String getID(DockFactory<?> factory)
          Gets the id of factory.
 DockSituationIgnore getIgnore()
          Gets the filter which decides, which elements are stored.
protected  boolean ignoreChildren(DockStation station)
          Tells whether to ignore the children of the station when saving or not.
protected  boolean ignoreElement(DockElement element)
          Tells whether to ignore this element when saving.
 java.util.Map<java.lang.String,DockStation> read(byte[] data)
          Reads data as stream and returns the roots of the DockElements which were found.
 java.util.Map<java.lang.String,DockStation> read(java.io.DataInputStream in)
          Reads in and returns the roots of the DockElements which were found.
private  java.util.Map<java.lang.Integer,Dockable> readDockables(java.io.DataInputStream in)
          Reads the next list of Dockables.
private  DockElement readElement(java.io.DataInputStream in)
          Reads the contents of one DockElement.
 void setIgnore(DockSituationIgnore ignore)
          Sets a filter which decides, which elements (stations and dockables) are stored.
 byte[] write(java.util.Map<java.lang.String,DockStation> stations)
          Writes all locations and relationships of the DockStations stations and their children into an array of bytes.
 void write(java.util.Map<java.lang.String,DockStation> stations, java.io.DataOutputStream out)
          Writes all locations and relationships of the DockStations stations and their children into the stream out.
private  void writeElement(DockElement element, java.io.DataOutputStream out)
          Writes the contents of element into out.
private  java.util.Map<Dockable,java.lang.Integer> writeStation(DockStation station, boolean ignoreChildren, java.io.DataOutputStream out)
          Writes the children of station and returns a map which contains all children and a unique id for each child.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

factories

private java.util.Map<java.lang.String,DockFactory<?>> factories
the factories used to create new elements


ignore

private DockSituationIgnore ignore
a filter for elements which should be ignored

Constructor Detail

DockSituation

public DockSituation(DockFactory<?>... factories)
Constructs a new DockSituation and sets some factories which are used to create new DockElements

Parameters:
factories - the factories

DockSituation

public DockSituation()
Constructs a new DockSituation. Factories for DefaultDockable, SplitDockStation, StackDockStation and FlapDockStation will be preinstalled.

Method Detail

setIgnore

public void setIgnore(DockSituationIgnore ignore)
Sets a filter which decides, which elements (stations and dockables) are stored.

Parameters:
ignore - the filter or null

getIgnore

public DockSituationIgnore getIgnore()
Gets the filter which decides, which elements are stored.

Returns:
the filter or null

add

public void add(DockFactory<?> factory)
Adds a factory

Parameters:
factory - the additional factory

write

public byte[] write(java.util.Map<java.lang.String,DockStation> stations)
             throws java.io.IOException
Writes all locations and relationships of the DockStations stations and their children into an array of bytes.

Parameters:
stations - The stations to store, a call to read(byte[]) would return the same map. Only the roots are needed.
Returns:
the information as an array of bytes
Throws:
java.io.IOException - if the information can't be written

write

public void write(java.util.Map<java.lang.String,DockStation> stations,
                  java.io.DataOutputStream out)
           throws java.io.IOException
Writes all locations and relationships of the DockStations stations and their children into the stream out.

Parameters:
stations - The stations to store, only the roots are needed.
out - the stream to write in
Throws:
java.io.IOException - if the stream throws an exception

writeStation

private java.util.Map<Dockable,java.lang.Integer> writeStation(DockStation station,
                                                               boolean ignoreChildren,
                                                               java.io.DataOutputStream out)
                                                        throws java.io.IOException
Writes the children of station and returns a map which contains all children and a unique id for each child.

Parameters:
station - the station whose children are written
ignoreChildren - true if no information about the children should be saved.
out - the stream to write into
Returns:
a map containing all children of station
Throws:
java.io.IOException - if the stream throws an exception

writeElement

private void writeElement(DockElement element,
                          java.io.DataOutputStream out)
                   throws java.io.IOException
Writes the contents of element into out.

Parameters:
element - the element to store
out - the stream to write into
Throws:
java.io.IOException - if the stream throws an exception or the element can't be converted

read

public java.util.Map<java.lang.String,DockStation> read(byte[] data)
                                                 throws java.io.IOException
Reads data as stream and returns the roots of the DockElements which were found.

Parameters:
data - the array to read
Returns:
the root stations which were found
Throws:
java.io.IOException - if data can't be read

read

public java.util.Map<java.lang.String,DockStation> read(java.io.DataInputStream in)
                                                 throws java.io.IOException
Reads in and returns the roots of the DockElements which were found.

Parameters:
in - the stream to read
Returns:
the roots of all elements that were found
Throws:
java.io.IOException - if the stream can't be read

readElement

private DockElement readElement(java.io.DataInputStream in)
                         throws java.io.IOException
Reads the contents of one DockElement. Note if there is a DockFactory missing, null will be returned.

Parameters:
in - the stream to read
Returns:
the read element or null
Throws:
java.io.IOException - if the element can't be read because of a malfunction of in

readDockables

private java.util.Map<java.lang.Integer,Dockable> readDockables(java.io.DataInputStream in)
                                                         throws java.io.IOException
Reads the next list of Dockables.

Parameters:
in - the stream to read
Returns:
a map containing some instances of Dockable
Throws:
java.io.IOException - if the stream throws an exception

ignoreElement

protected boolean ignoreElement(DockElement element)
Tells whether to ignore this element when saving. If an element is ignored, no factory is needed for it. This implementation forwards the call to the DockSituationIgnore of this situation.

Parameters:
element - the element which might not be saved
Returns:
true if the element should not be saved

ignoreChildren

protected boolean ignoreChildren(DockStation station)
Tells whether to ignore the children of the station when saving or not. If the children are ignored, no factories are needed for them. This implementation forwards the call to the DockSituationIgnore of this situation.

Parameters:
station - the station whose children might be ignored
Returns:
true if the station is saved as having no children

getID

protected java.lang.String getID(DockElement dockable)
Gets the id of the factory which is needed to write (and later read) the element dockable.

Parameters:
dockable - the dockable to write
Returns:
the id of the factory
See Also:
getID(DockFactory), getFactory(String)

getID

protected java.lang.String getID(DockFactory<?> factory)
Gets the id of factory. The default behavior is just to return DockFactory.getID(). Note that this method should be a bijection to getFactory(String).

Parameters:
factory - the factory whose id is needed
Returns:
the id of the factory

getFactory

protected DockFactory<? extends DockElement> getFactory(java.lang.String id)
Gets the factory which has the given id. Note that this method should be a bijection to getID(DockFactory). The default behavior compares id with the getID(DockFactory).

Parameters:
id - the name of the factory
Returns:
the factory or null if no factory has this id