Class LayoutParser


  • public class LayoutParser
    extends java.lang.Object
    The parser that analyzes the Ascii art drawing. Typically used internally by AsciiArtGridBagLayout, no need to use it directly.
    Example of a drawing :
    
       String aa =
         "+--------------------------------------------+\n"+
         "|                                            |\n"+
         "|                     L                      |\n"+
         "|                                            |\n"+
         "+--------------+--------------+--------------+\n"+
         "|              |              |              |\n"+
         "|      S1      |<     B      >|      S2      |\n"+
         "|              |              |              |\n"+
         "+--------------+--------------+--------------+\n";
     
    See the documentation (layout-definition.md) for more explanations of the conventions used in the drawings. Note that if you don't give a name to a rectangle, it will be automatically set to "_space" followed by a sequential number, and will later contain a JLabel with a String of just a space " ". This is necessary to get a correct dispatch of the layout's geometry.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static boolean debugParser
      set to true to emit messages on System.out while parsing
      static int FILL_HORIZ
      value that corresponds to the presence of horizontal filling chars ('<', '>')
      static int FILL_VERT
      value that corresponds to the presence of a vertical filling char ('^')
    • Constructor Summary

      Constructors 
      Constructor Description
      LayoutParser()
      Default constructor
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int evalFlags​(java.lang.String spec)
      Process the flags : if '^' is encountered, add FILL_VERT, if '<' or '>' is encountered, add FILL_HORIZ.
      CRect findCRect​(int px, int py)
      find a CRect whose last line is just above px and py
      java.util.ArrayList<CRect> getCRects()
      Return the CRects
      boolean isNameChar​(char c)
      Is c compatible with a name
      boolean isNameStart​(char c)
      Is c compatible with the start of a name
      boolean isNameValid​(java.lang.String str)
      Is the name a valid name for a rectangle
      char[] normalize​(java.lang.String line)
      Normalize format string : - Trim the line - Replace all '-' and '|' with '+'
      void parseSpec​(java.util.List<char[]> la)
      Parse Ascii Art specification
      java.util.ArrayList<char[]> readSpec​(java.io.BufferedReader specInput)
      Step one : read all lines, ignoring the empty ones, and trimming the others.
      java.lang.String removeFlags​(java.lang.String spec)
      Remove the flags from the spec line.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FILL_HORIZ

        public static final int FILL_HORIZ
        value that corresponds to the presence of horizontal filling chars ('<', '>')
        See Also:
        Constant Field Values
      • FILL_VERT

        public static final int FILL_VERT
        value that corresponds to the presence of a vertical filling char ('^')
        See Also:
        Constant Field Values
      • debugParser

        public static boolean debugParser
        set to true to emit messages on System.out while parsing
    • Constructor Detail

      • LayoutParser

        public LayoutParser()
        Default constructor
    • Method Detail

      • getCRects

        public java.util.ArrayList<CRect> getCRects()
        Return the CRects
        Returns:
        The list of CRects
      • normalize

        public char[] normalize​(java.lang.String line)
        Normalize format string : - Trim the line - Replace all '-' and '|' with '+'
        Parameters:
        line - The line that must be normalized
        Returns:
        The normalized line as an array of chars
      • readSpec

        public java.util.ArrayList<char[]> readSpec​(java.io.BufferedReader specInput)
                                             throws LayoutParseException
        Step one : read all lines, ignoring the empty ones, and trimming the others.
        Parameters:
        specInput - A BufferedReader that will be used to read the specification
        Returns:
        A list of char arrays
        Throws:
        LayoutParseException - If non-empty lines are not the same length
      • findCRect

        public CRect findCRect​(int px,
                               int py)
        find a CRect whose last line is just above px and py
        Parameters:
        px - position x
        py - position y
        Returns:
        the CRect that is just above position or null if none found
      • isNameStart

        public boolean isNameStart​(char c)
        Is c compatible with the start of a name
        Parameters:
        c - The char to test
        Returns:
        true if c is OK for the start of a name
      • isNameChar

        public boolean isNameChar​(char c)
        Is c compatible with a name
        Parameters:
        c - The char to test
        Returns:
        true if c is OK for a name
      • evalFlags

        public int evalFlags​(java.lang.String spec)
        Process the flags : if '^' is encountered, add FILL_VERT, if '<' or '>' is encountered, add FILL_HORIZ.
        Parameters:
        spec - The spec to process
        Returns:
        the evaluated flags (0, FILL_VERT, FILL_HORIZ, FILL_VERT|FILL_HORIZ)
      • removeFlags

        public java.lang.String removeFlags​(java.lang.String spec)
        Remove the flags from the spec line.
        Parameters:
        spec - The spec line to process
        Returns:
        The spec with the flags '^', '<', '>' replaced with ' '
      • isNameValid

        public boolean isNameValid​(java.lang.String str)
        Is the name a valid name for a rectangle
        Parameters:
        str - String to test
        Returns:
        true if str is a valid name
      • parseSpec

        public void parseSpec​(java.util.List<char[]> la)
                       throws LayoutParseException
        Parse Ascii Art specification
        Parameters:
        la - A list of cleaned lines to parse
        Throws:
        LayoutParseException - If the line does not start/end with a '+' delimiter, if a line is less than 3 chars wide, if a rectangle width changes