Package org.hkmi2.aagbl
Class AsciiArtGridBagLayout
- java.lang.Object
-
- java.awt.GridBagLayout
-
- org.hkmi2.aagbl.AsciiArtGridBagLayout
-
- All Implemented Interfaces:
java.awt.LayoutManager,java.awt.LayoutManager2,java.io.Serializable
public class AsciiArtGridBagLayout extends java.awt.GridBagLayoutA variant ofGridBagLayoutthat can be initialized using ascii art. This is the main class to use. Under the hood, it uses aGridBagLayoutBuilderobject. To use :- Make a String that represents the Ascii art of your layout (see the doc for this)
- Tweak some values (Weightx for example) for some rectangles if needed
- For each of you components, call
setConstraints(String, Component)with the name given in your Ascii art and your component - Use this layout, for example in a
JPanel(look in the tests dir for more examples) - Add all your components to the container. As the container has hour layout, it will know how to manage each component as it is added.
//create our layout aagbl = new AsciiArtGridBagLayout(aa); //associate the constraint rectangles with our components aagbl.setConstraints("B", B); aagbl.setConstraints("L", L); aagbl.setConstraints("S1", S1); aagbl.setConstraints("S2", S2); //Alternative method to set constraints : //aagbl.setConstraints(AsciiArtGridBagLayout.makeMap(new Object[] {"B",B,"L",L,"S1",S1,"S2",S2})); //now set this as our layout setLayout(aagbl); //and add all the components aagbl.addAllComponentsTo(getContentPane());- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description AsciiArtGridBagLayout()Simple no-args constructorAsciiArtGridBagLayout(java.lang.String asciiArt)Main constructor, with the layout specification as an ASCII art drawing.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAllComponentsTo(java.awt.Container cr)Add all our components to the givenContainerobject.java.util.List<java.lang.String>getAllComponentNames()Get all the component namesjava.util.List<java.awt.Component>getAllComponents()Get all the components that were declared.java.lang.StringgetAllComponentsAsCsNameList()Return a String that contains a comma-separated list of all component namesjava.util.List<java.lang.String>getAllConstraintNames()Get all the constraint namesjava.util.List<java.lang.String>getAllCRectNames()Return a list of all non-empty CRect names.java.lang.StringgetAsciiArt()Return the current ASCII art drawingjava.awt.ComponentgetComponent(java.lang.String name)Get the component that has the given namejava.awt.GridBagConstraintsgetConstraints(java.lang.String name)Get theGridBagConstraintsfor the given namejava.util.List<CRect>getCRects()Return a list of all the declared CRectsGridBagLayoutBuildergetGridBagLayoutBuilder()Get the builder that is used.java.awt.InsetsgetInsetsToUse()Gets the Insets to use for future calls tosetConstraints(String, Component).intgetMaxX()Get Max X which is the maximum char number used in the drawing, without the final column.intgetMaxY()Get Max Y which is the maximum line number used in the drawing, without the final line.static java.util.HashMap<java.lang.String,java.awt.Component>makeMap(java.lang.Object[] objs)Using an array of objects, make a HashMap.voidsetAsciiArt(java.lang.String asciiArt)Set the ASCII art drawing, and compute the layoutvoidsetConstraints(java.lang.String componentName, java.awt.Component comp)Set the constraints for the given component.voidsetConstraints(java.util.Map<java.lang.String,java.awt.Component> cm)Set the constraints for all the components of the map.voidsetInsetsToUse(java.awt.Insets newInsets)Set theInsetsto use as default instead of the default ones in CRect.voidsetIpadx(java.lang.String rectNameList, int val)Set the x ipadding for all the components in the list (comma-separated list, no whitespace) and re-applies all the constraints for the components in the listvoidsetIpady(java.lang.String rectNameList, int val)Set the y ipadding for all the components in the list (comma-separated list, no whitespace) and re-applies all the constraints for the components in the listvoidsetWeightx(java.lang.String rectNameList, double val)Set the x weight for all the components in the list (comma-separated list, no whitespace) Sets the x-weight for all the components in the list, and re-applies all the constraints for the components in the listvoidsetWeighty(java.lang.String rectNameList, double val)Sets the y-weight for all the components in the list (comma-separated list, no whitespace), and re-applies all the constraints for the components in the list-
Methods inherited from class java.awt.GridBagLayout
addLayoutComponent, addLayoutComponent, adjustForGravity, AdjustForGravity, arrangeGrid, ArrangeGrid, getConstraints, getLayoutAlignmentX, getLayoutAlignmentY, getLayoutDimensions, getLayoutInfo, GetLayoutInfo, getLayoutOrigin, getLayoutWeights, getMinSize, GetMinSize, invalidateLayout, layoutContainer, location, lookupConstraints, maximumLayoutSize, minimumLayoutSize, preferredLayoutSize, removeLayoutComponent, setConstraints, toString
-
-
-
-
Constructor Detail
-
AsciiArtGridBagLayout
public AsciiArtGridBagLayout(java.lang.String asciiArt) throws LayoutParseExceptionMain constructor, with the layout specification as an ASCII art drawing.- Parameters:
asciiArt- The layout (see docs for the format)- Throws:
LayoutParseException- If layout drawing is incorrect
-
AsciiArtGridBagLayout
public AsciiArtGridBagLayout()
Simple no-args constructor
-
-
Method Detail
-
makeMap
public static java.util.HashMap<java.lang.String,java.awt.Component> makeMap(java.lang.Object[] objs)
Using an array of objects, make a HashMap. This allows for a more compact initialization. Ex :Object[] objs = ["B0", B0, "L1", L1]; HashMap<String, Component> hm = AsciiArtGridBagLayout.makeMap(objs);
- Parameters:
objs- An array of String, Component, String, Component ...- Returns:
- The HashMap of Components by name
-
getAsciiArt
public java.lang.String getAsciiArt()
Return the current ASCII art drawing- Returns:
- the current ASCII art drawing
-
setAsciiArt
public void setAsciiArt(java.lang.String asciiArt) throws LayoutParseExceptionSet the ASCII art drawing, and compute the layout- Parameters:
asciiArt- the ASCII art drawing- Throws:
LayoutParseException- if layout drawing is incorrect
-
setConstraints
public void setConstraints(java.lang.String componentName, java.awt.Component comp)Set the constraints for the given component. Used also to "declare" the components that will be affected by the layout constraints.- Parameters:
componentName- The name the component has in the layoutcomp- The actual component that will get drawn
-
setConstraints
public void setConstraints(java.util.Map<java.lang.String,java.awt.Component> cm)
Set the constraints for all the components of the map. You can set the constraints for all your components at once using this method.- Parameters:
cm- The component by name map, each name must be the name used in the layout drawing.- See Also:
for an easy way to build a map
-
getMaxX
public int getMaxX()
Get Max X which is the maximum char number used in the drawing, without the final column. Ex :+-+-+ |A|B| +-+-+ | Z | +---+
Here MaxX will be 4.- Returns:
- The maximum X
-
getMaxY
public int getMaxY()
Get Max Y which is the maximum line number used in the drawing, without the final line.- Returns:
- The maximum Y
-
getComponent
public java.awt.Component getComponent(java.lang.String name)
Get the component that has the given name- Parameters:
name- The name that was used whensetConstraints(String, Component)was called.- Returns:
- The component or null if there is no component with that name
-
getConstraints
public java.awt.GridBagConstraints getConstraints(java.lang.String name)
Get theGridBagConstraintsfor the given name- Parameters:
name- The name that was used whensetConstraints(String, Component)was called.- Returns:
- The
GridBagConstraintsobject or null if there is no rectangle with that name
-
getAllComponents
public java.util.List<java.awt.Component> getAllComponents()
Get all the components that were declared.- Returns:
- A list with all the components
-
getAllComponentNames
public java.util.List<java.lang.String> getAllComponentNames()
Get all the component names- Returns:
- A list of all the component names
-
getAllComponentsAsCsNameList
public java.lang.String getAllComponentsAsCsNameList()
Return a String that contains a comma-separated list of all component names- Returns:
- The list, e.g. "a,b,x"
-
getAllConstraintNames
public java.util.List<java.lang.String> getAllConstraintNames()
Get all the constraint names- Returns:
- A list of all the constraint names
-
addAllComponentsTo
public void addAllComponentsTo(java.awt.Container cr)
Add all our components to the givenContainerobject. For each component,Container.add(Component, Object)is called, with the corresponding constraints object. Adds all components the the container, from the list of components. Useful if you already have a list of component names.- Parameters:
cr- The container that will get all our objects
-
setWeightx
public void setWeightx(java.lang.String rectNameList, double val)Set the x weight for all the components in the list (comma-separated list, no whitespace) Sets the x-weight for all the components in the list, and re-applies all the constraints for the components in the list- Parameters:
rectNameList- A comma-separated list of names, example "a,x,y"val- The new weight
-
setWeighty
public void setWeighty(java.lang.String rectNameList, double val)Sets the y-weight for all the components in the list (comma-separated list, no whitespace), and re-applies all the constraints for the components in the list- Parameters:
rectNameList- the list, example "a,x,y"val- the y weight to set to
-
setIpadx
public void setIpadx(java.lang.String rectNameList, int val)Set the x ipadding for all the components in the list (comma-separated list, no whitespace) and re-applies all the constraints for the components in the list- Parameters:
rectNameList- the list, example "a,x,y"val- the x ipadding to set to
-
setIpady
public void setIpady(java.lang.String rectNameList, int val)Set the y ipadding for all the components in the list (comma-separated list, no whitespace) and re-applies all the constraints for the components in the list- Parameters:
rectNameList- the list, example "a,x,y"val- the y ipadding to set to
-
getCRects
public java.util.List<CRect> getCRects()
Return a list of all the declared CRects- Returns:
- A list of all the CRects
-
getAllCRectNames
public java.util.List<java.lang.String> getAllCRectNames()
Return a list of all non-empty CRect names. This is useful if you already have a map, that contains components that uses the same names as the CRects (but also other names). A typical usage is in Groovy, where undeclared variables go into the "binding" :gbl.allCRectNames.each { gbl.setConstraints(it, binding[it]) }- Returns:
- A list of names (never null, but can be empty)
-
getGridBagLayoutBuilder
public GridBagLayoutBuilder getGridBagLayoutBuilder()
Get the builder that is used.- Returns:
- The builder instance
-
getInsetsToUse
public java.awt.Insets getInsetsToUse()
Gets the Insets to use for future calls tosetConstraints(String, Component). Calls getInsetsToUse() on the internalGridBagLayoutBuilder. If theGridBagLayoutBuilderis null, returns null, else returns the currentInsetsto use.- Returns:
- The Insets to use, if null default value in CRect(s) will be kept
-
setInsetsToUse
public void setInsetsToUse(java.awt.Insets newInsets)
Set theInsetsto use as default instead of the default ones in CRect. Call this before callingsetConstraints- Parameters:
newInsets- The newInsetsobject
-
-