Class HTMLTag

java.lang.Object
com.amalgamasimulation.desktop.html.HTMLTag
All Implemented Interfaces:
HTMLConvertable
Direct Known Subclasses:
HTMLChart, HTMLHeader, HTMLHeatmapChart, HTMLImage, HTMLLink, HTMLLinkAction, HTMLTable, HTMLTextBlock

public class HTMLTag extends Object implements HTMLConvertable
The HTMLTag class represents an HTML tag that can contain children, styles, and attributes. It implements the HTMLConvertable interface, providing a method to convert the tag and its contents to an HTML string.
Author:
Alexey Glazyrin
  • Constructor Details

    • HTMLTag

      public HTMLTag(String tag)
      Constructs a new HTMLTag instance with the specified tag name.
      Parameters:
      tag - the name of the HTML tag (e.g., "div", "span")
  • Method Details

    • tag

      public HTMLTag tag(String tag)
      Sets the HTML tag name.
      Parameters:
      tag - new tag name to use
      Returns:
      this HTMLTag instance for method chaining
    • append

      public HTMLTag append(HTMLConvertable... children)
      Appends one or more children to this HTML tag.
      Parameters:
      children - the children to append
      Returns:
      this HTMLTag instance for method chaining
    • removeChildren

      protected void removeChildren(Collection<HTMLConvertable> children)
    • style

      public HTMLTag style(String style)
      Adds a CSS style to this HTML tag.
      Parameters:
      style - the CSS style to add
      Returns:
      this HTMLTag instance for method chaining
    • attr

      public HTMLTag attr(String key, String value)
      Adds an attribute to this HTML tag.
      Parameters:
      key - the attribute key (e.g., "id", "class")
      value - the attribute value
      Returns:
      this HTMLTag instance for method chaining
    • relativeWidth

      public HTMLTag relativeWidth(double width)
      Sets the relative width of this HTML tag as a percentage.
      Parameters:
      width - the width as a percentage (e.g., 0.5 for 50%)
      Returns:
      this HTMLTag instance for method chaining
    • relativeHeight

      public HTMLTag relativeHeight(double height)
      Sets the relative height of this HTML tag as a percentage.
      Parameters:
      height - the height as a percentage (e.g., 0.5 for 50%)
      Returns:
      this HTMLTag instance for method chaining
    • font

      public HTMLTag font(FontDescriptor fontDescriptor)
      Sets the font of this HTML tag using the specified font descriptor.
      Parameters:
      fontDescriptor - the font descriptor to use
      Returns:
      this HTMLTag instance for method chaining
    • fontColor

      public HTMLTag fontColor(Color color)
      Sets the font color of this HTML tag using the specified AWT color.
      Parameters:
      color - the AWT color to use
      Returns:
      this HTMLTag instance for method chaining
    • fontWeight

      public HTMLTag fontWeight(String weight)
      Sets the font weight of this HTML tag using the specified weight value. Accepts predefined values or numeric values between 100 and 900.
      Parameters:
      weight - the font weight value
      Returns:
      this HTMLTag instance for method chaining
      Throws:
      IllegalArgumentException - if the weight value is invalid
    • lineHeight

      public HTMLTag lineHeight(double lineHeight)
      Sets the line height of this HTML tag as a multiple of the font size.
      Parameters:
      lineHeight - the line height as a double (e.g., 1.5 for 150% of the font size)
      Returns:
      this HTMLTag instance for method chaining
    • fontSizeInPx

      public HTMLTag fontSizeInPx(double size)
      Sets the font size of this HTML tag using the specified size in pixels.
      Parameters:
      size - the font size in pixels (must be positive)
      Returns:
      this HTMLTag instance for method chaining
      Throws:
      IllegalArgumentException - if the size is not positive
    • fontFamily

      public HTMLTag fontFamily(String fontFamily)
      Sets the font family of this HTML tag using the specified font family names.
      Parameters:
      fontFamily - the font family name(s) to use
      Returns:
      this HTMLTag instance for method chaining
    • fontStyle

      public HTMLTag fontStyle(FontPosture posture)
      Sets the font style of this HTML tag using the specified FontPosture.
      Parameters:
      posture - the FontPosture to use
      Returns:
      this HTMLTag instance for method chaining
    • backgroundColor

      public HTMLTag backgroundColor(Color color)
      Sets the background color using AWT Color converted to RGBA format.
      Parameters:
      color - the background color to apply
      Returns:
      this HTMLTag instance for method chaining
    • marginInPx

      public HTMLTag marginInPx(Side side, int marginPx)
      Sets the margin of this HTML tag for the specified side in pixels.
      Parameters:
      side - the side to set the margin for
      marginPx - the margin in pixels
      Returns:
      this HTMLTag instance for method chaining
    • paddingInPx

      public HTMLTag paddingInPx(Side side, int paddingPx)
      Sets the padding of this HTML tag for the specified side in pixels.
      Parameters:
      side - the side to set the padding for
      paddingPx - the padding in pixels
      Returns:
      this HTMLTag instance for method chaining
    • maxWidthInPx

      public HTMLTag maxWidthInPx(int maxWidthPx)
      Sets the maximum width of this HTML tag in pixels.
      Parameters:
      maxWidthPx - the maximum width in pixels
      Returns:
      this HTMLTag instance for method chaining
    • maxHeightInPx

      public HTMLTag maxHeightInPx(int maxHeightPx)
      Sets the maximum height of this HTML tag in pixels.
      Parameters:
      maxHeightPx - the maximum height in pixels
      Returns:
      this HTMLTag instance for method chaining
    • textAlignment

      public HTMLTag textAlignment(TextAlignment textAlignment)
      Sets the text alignment of this HTML tag.
      Parameters:
      textAlignment - the text alignment to use
      Returns:
      this HTMLTag instance for method chaining
    • getChildren

      public Collection<HTMLConvertable> getChildren()
      Returns the children of this HTML tag.
      Returns:
      a collection of children, or an empty collection if no children are present
    • tag

      public String tag()
      Returns the tag name of this HTML tag.
      Returns:
      the tag name
    • toHTML

      public String toHTML(Set<String> outStrings)
      Description copied from interface: HTMLConvertable
      Converts the object to an HTML string.
      Specified by:
      toHTML in interface HTMLConvertable
      Parameters:
      outStrings - set of unique strings of text content
      Returns:
    • copy

      public HTMLTag copy(Map<String,String> stringsSubstitutionMap)
      Description copied from interface: HTMLConvertable
      Creates a deep copy of this HTMLConvertable element, preserving the element's structure while substituting text content using the provided translation mapping.
      Specified by:
      copy in interface HTMLConvertable
      Parameters:
      stringsSubstitutionMap - a map where keys are original text strings (as collected by HTMLConvertable.toHTML(Set)) and values are their translations. During copying, any encountered text content present in this map will be replaced with its translated version.
      Returns:
      a new independent copy of this HTMLConvertable instance with translated content
    • fillCopy

      protected void fillCopy(HTMLTag tag, Map<String,String> stringsSubstitutionMap)
    • colorToRGBA

      public static String colorToRGBA(Color color)
      Converts an AWT color to an RGBA string.
      Parameters:
      color - the AWT color to convert
      Returns:
      the RGBA string representation of the color
    • getAttributes

      public Map<String,String> getAttributes()
      Returns attributes of this HTML tag.
      Returns:
      a collection of attributes
    • toCSS

      public static String toCSS(FontDescriptor fontDescriptor)
      Converts FontDescriptor to a CSS string.
      Parameters:
      fontDescriptor - font descriptor to convert
      Returns:
      the CSS string representation of the font descriptor