AnyLayout is a Java layout manager that delegates all layout tasks to user-provided constraints.
It makes a few concessions to its simplicity to be as easy to use as it can be. The main aim is to take the magic out of layout, to prevent surprises.
Here is a sample constraint implementation:
Constraint centre=new Constraint()
{
public int getLeft(LayoutContext context)
{
return (context.getParentSize()-context.getPreferredSize())/2;
}
public int getY(LayoutContext context)
{
return (context.getParentSize()-context.getPreferredSize())/2;
}
public int getWidth(LayoutContext context)
{
return context.getPreferredSize();
}
public int getHeight(LayoutContext context)
{
return context.getPreferredSize();
}
public void close()
{
}
};
You would expect most constraints to have identical implementations for getWidth and getHeight, so one of the optional extras to the API lets you define a constraint in terms of just X and Y (XYConstraint) and then expand it to make a Constraint from the XYConstraint, adding in normal implementations for getWidth and getHeight. This is done in anylayout.extras.ConstraintUtility.expand.
Also, the code duplication in the above example can be eliminated by using anylayout.extras.ConstraintBuilder, e.g.:
Get<LayoutContext,Integer> nearEdge=new Get<LayoutContext,Integer>()
{
public Integer get(LayoutContext context)
{
return (context.getParentSize()-context.getPreferredSize())/2;
}
};
Get<LayoutContext,Integer> preferredSize=new Get<LayoutContext,Integer>()
{
public Integer get(LayoutContext context)
{
return context.getPreferredSize();
}
};
Constraint centre=ConstraintBuilder.buildConstraint(nearEdge,nearEdge,size,size);
This project will be evolved to make it as simple to use as possible, but to retain the simplicity that is in its core now (the anylayout top-level package).
----
To run the examples provided, check out the source code and run:
ant examples
It makes a few concessions to its simplicity to be as easy to use as it can be. The main aim is to take the magic out of layout, to prevent surprises.
Here is a sample constraint implementation:
Constraint centre=new Constraint()
{
public int getLeft(LayoutContext context)
{
return (context.getParentSize()-context.getPreferredSize())/2;
}
public int getY(LayoutContext context)
{
return (context.getParentSize()-context.getPreferredSize())/2;
}
public int getWidth(LayoutContext context)
{
return context.getPreferredSize();
}
public int getHeight(LayoutContext context)
{
return context.getPreferredSize();
}
public void close()
{
}
};
You would expect most constraints to have identical implementations for getWidth and getHeight, so one of the optional extras to the API lets you define a constraint in terms of just X and Y (XYConstraint) and then expand it to make a Constraint from the XYConstraint, adding in normal implementations for getWidth and getHeight. This is done in anylayout.extras.ConstraintUtility.expand.
Also, the code duplication in the above example can be eliminated by using anylayout.extras.ConstraintBuilder, e.g.:
Get<LayoutContext,Integer> nearEdge=new Get<LayoutContext,Integer>()
{
public Integer get(LayoutContext context)
{
return (context.getParentSize()-context.getPreferredSize())/2;
}
};
Get<LayoutContext,Integer> preferredSize=new Get<LayoutContext,Integer>()
{
public Integer get(LayoutContext context)
{
return context.getPreferredSize();
}
};
Constraint centre=ConstraintBuilder.buildConstraint(nearEdge,nearEdge,size,size);
This project will be evolved to make it as simple to use as possible, but to retain the simplicity that is in its core now (the anylayout top-level package).
----
To run the examples provided, check out the source code and run:
ant examples