bibliothek.gui.dock.station.split
Class SplitDockGrid

java.lang.Object
  extended by bibliothek.gui.dock.station.split.SplitDockGrid

public class SplitDockGrid
extends Object

A class that provides a grid for Dockables. The grid can be transformed into a SplitDockTree which has values that would layout the components as they are in the grid. The algorithms used in this class can handle overlapping elements and holes, however results are much better if there are no disturbances in the grid.
There is also a possibility to tell the tree, where dividers should be made.

Author:
Benjamin Sigg
See Also:
toTree(), SplitDockStation.dropTree(SplitDockTree)

Nested Class Summary
protected static class SplitDockGrid.Line
          Represents a dividing line in the grid.
protected static class SplitDockGrid.Node
          Represents a node in the tree which will be built.
 
Constructor Summary
SplitDockGrid()
          Creates a new, empty grid.
SplitDockGrid(String layout, Map<Character,Dockable[]> dockables)
          Creates a grid by reading a string which represents a grid.
 
Method Summary
 void addDockable(double x, double y, double width, double height, Dockable... dockables)
          Adds dockable to the grid.
 void addHorizontalDivider(double x1, double x2, double y)
          Adds a horizonal dividing line.
 void addVerticalDivider(double x, double y1, double y2)
          Adds a vertical dividing line.
protected  SplitDockGrid.Line bestFittingLine(double x, double y, double w, double h, boolean horizontal, double split)
          Searches the line that divides the rectangle x, y, width, height best.
protected  SplitDockGrid.Node combine(SplitDockGrid.Node a, SplitDockGrid.Node b)
          Creates a combination of a and b.
protected  double diff(SplitDockGrid.Node a, SplitDockGrid.Node b)
          Tells whether the two nodes could be merged or not.
protected  List<SplitDockGrid.Line> getLines()
          Gets a list containing all lines of this grid.
protected  List<SplitDockGrid.Node> getNodes()
          Gets a list containing all nodes of this grid.
protected  double penalty(double x, double y, double w, double h, SplitDockGrid.Line line)
          Used by diff to calculate add a penalty if a line hits a rectangle.
 void setSelected(double x, double y, double width, double height, Dockable dockable)
          Marks dockable as selected in the stack of elements that are on position x, y, width, height.
 SplitDockTree toTree()
          Converts the current grid into a tree.
protected  SplitDockGrid.Node tree()
          Transforms the grid into a tree and returns the root.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SplitDockGrid

public SplitDockGrid()
Creates a new, empty grid.


SplitDockGrid

public SplitDockGrid(String layout,
                     Map<Character,Dockable[]> dockables)
Creates a grid by reading a string which represents a grid.
The argument layout is a string divided by newline "\n". Every line represents a y-coordinate, the position of a character in a line represents a x-coordinate. The minimal and the maximal x- and y-coordinates for a character is searched, and used to call addDockable, where the Dockable-array is taken from the Map dockables.

Parameters:
layout - the layout, a string divided by newlines
dockables - the Dockables to add, only entries whose character is in the String layout.
Method Detail

addDockable

public void addDockable(double x,
                        double y,
                        double width,
                        double height,
                        Dockable... dockables)
Adds dockable to the grid. The coordinates are not absolute, only the relative location and size matters. If there are already dockables at the exact same location, then the dockables are stacked.

Parameters:
x - the x-coordinate
y - the y-coordinate
width - the width, more than 0
height - the height, more than 0
dockables - the Dockables to add

setSelected

public void setSelected(double x,
                        double y,
                        double width,
                        double height,
                        Dockable dockable)
Marks dockable as selected in the stack of elements that are on position x, y, width, height. This method requires that add was called with the exact same coordinates and with dockable.

Parameters:
x - the x coordinate
y - the y coordinate
width - the width of the elements
height - the height of the elements
dockable - the element to select, not null
Throws:
IllegalArgumentException - if width or height are below 0, if dockable is null, if add was never called with the arguments

addVerticalDivider

public void addVerticalDivider(double x,
                               double y1,
                               double y2)
Adds a vertical dividing line.

Parameters:
x - the x-coordinate of the line
y1 - the y-coordinate of the first endpoint
y2 - the y-coordinate of the second endpoint

addHorizontalDivider

public void addHorizontalDivider(double x1,
                                 double x2,
                                 double y)
Adds a horizonal dividing line.

Parameters:
x1 - the x-coordinate of the first endpoint
x2 - the x-coordinate of the second endpoint
y - the y-coordinate of the line

toTree

public SplitDockTree toTree()
Converts the current grid into a tree.

Returns:
the tree which represents this grid
See Also:
SplitDockStation.dropTree(SplitDockTree)

getLines

protected List<SplitDockGrid.Line> getLines()
Gets a list containing all lines of this grid.

Returns:
the list

getNodes

protected List<SplitDockGrid.Node> getNodes()
Gets a list containing all nodes of this grid.

Returns:
the nodes

tree

protected SplitDockGrid.Node tree()
Transforms the grid into a tree and returns the root.

Returns:
the root, can be null

combine

protected SplitDockGrid.Node combine(SplitDockGrid.Node a,
                                     SplitDockGrid.Node b)
Creates a combination of a and b.

Parameters:
a - the first node
b - the second node
Returns:
a node which has a and b as children

diff

protected double diff(SplitDockGrid.Node a,
                      SplitDockGrid.Node b)
Tells whether the two nodes could be merged or not.

Parameters:
a - the first node
b - the second node
Returns:
how likely the two nodes can be merged, a small result indicates that merging would be a good idea.

bestFittingLine

protected SplitDockGrid.Line bestFittingLine(double x,
                                             double y,
                                             double w,
                                             double h,
                                             boolean horizontal,
                                             double split)
Searches the line that divides the rectangle x, y, width, height best.

Parameters:
x - the x-coordinate of the rectangle
y - the y-coordinate of the rectangle
w - the width of the rectangle
h - the height of the rectangle
horizontal - whether the line should be horizontal or not
split - the preferred value of SplitDockGrid.Line.alpha.
Returns:
a line or null

penalty

protected double penalty(double x,
                         double y,
                         double w,
                         double h,
                         SplitDockGrid.Line line)
Used by diff to calculate add a penalty if a line hits a rectangle.

Parameters:
x - the x-coordinate of the rectangle
y - the y-coordinate of the rectangle
w - the width of the rectangle
h - the height of the rectangle
line - the line which may hit the rectangle
Returns:
the penalty, a value that will be added to the result of diff.