Class AsciiArtGridBagLayout

  • All Implemented Interfaces:
    java.awt.LayoutManager, java.awt.LayoutManager2, java.io.Serializable

    public class AsciiArtGridBagLayout
    extends java.awt.GridBagLayout
    A variant of GridBagLayout that can be initialized using ascii art. This is the main class to use. Under the hood, it uses a GridBagLayoutBuilder object. 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.
    Example code (taken from the HelloWorld class (a subclass of JFrame) in the tests) :
       //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
    • Field Summary

      • Fields inherited from class java.awt.GridBagLayout

        columnWeights, columnWidths, comptable, defaultConstraints, layoutInfo, MAXGRIDSIZE, MINSIZE, PREFERREDSIZE, rowHeights, rowWeights
    • Constructor Summary

      Constructors 
      Constructor Description
      AsciiArtGridBagLayout()
      Simple no-args constructor
      AsciiArtGridBagLayout​(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
      void addAllComponentsTo​(java.awt.Container cr)
      Add all our components to the given Container object.
      java.util.List<java.lang.String> getAllComponentNames()
      Get all the component names
      java.util.List<java.awt.Component> getAllComponents()
      Get all the components that were declared.
      java.lang.String getAllComponentsAsCsNameList()
      Return a String that contains a comma-separated list of all component names
      java.util.List<java.lang.String> getAllConstraintNames()
      Get all the constraint names
      java.util.List<java.lang.String> getAllCRectNames()
      Return a list of all non-empty CRect names.
      java.lang.String getAsciiArt()
      Return the current ASCII art drawing
      java.awt.Component getComponent​(java.lang.String name)
      Get the component that has the given name
      java.awt.GridBagConstraints getConstraints​(java.lang.String name)
      Get the GridBagConstraints for the given name
      java.util.List<CRect> getCRects()
      Return a list of all the declared CRects
      GridBagLayoutBuilder getGridBagLayoutBuilder()
      Get the builder that is used.
      java.awt.Insets getInsetsToUse()
      Gets the Insets to use for future calls to setConstraints(String, Component).
      int getMaxX()
      Get Max X which is the maximum char number used in the drawing, without the final column.
      int getMaxY()
      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.
      void setAsciiArt​(java.lang.String asciiArt)
      Set the ASCII art drawing, and compute the layout
      void setConstraints​(java.lang.String componentName, java.awt.Component comp)
      Set the constraints for the given component.
      void setConstraints​(java.util.Map<java.lang.String,​java.awt.Component> cm)
      Set the constraints for all the components of the map.
      void setInsetsToUse​(java.awt.Insets newInsets)
      Set the Insets to use as default instead of the default ones in CRect.
      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
      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
      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
      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
      • 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
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AsciiArtGridBagLayout

        public AsciiArtGridBagLayout​(java.lang.String asciiArt)
                              throws LayoutParseException
        Main 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 LayoutParseException
        Set 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 layout
        comp - 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 when setConstraints(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 the GridBagConstraints for the given name
        Parameters:
        name - The name that was used when setConstraints(String, Component) was called.
        Returns:
        The GridBagConstraints object 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 given Container object. 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
      • setInsetsToUse

        public void setInsetsToUse​(java.awt.Insets newInsets)
        Set the Insets to use as default instead of the default ones in CRect. Call this before calling setConstraints
        Parameters:
        newInsets - The new Insets object