Class HTMLTag

java.lang.Object
com.amalgamasimulation.desktop.html.HTMLTag
All Implemented Interfaces:
HTMLConvertable
Direct Known Subclasses:
HTMLChart, HTMLHeader, 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

    • copy

      public HTMLTag copy()
      Creates a deep copy of this HTMLTag including its children, styles, and attributes.
      Returns:
      new independent copy of this HTMLTag instance
    • 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(org.eclipse.swt.graphics.Font font)
      Sets the font of this HTML tag using the specified SWT font.
      Parameters:
      font - the SWT font 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(javafx.scene.text.FontPosture posture)
      Sets the font style of this HTML tag using the specified JavaFX FontPosture.
      Parameters:
      posture - the JavaFX 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(javafx.geometry.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(javafx.geometry.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(javafx.scene.text.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
    • toHTML

      public String toHTML()
      Converts this HTML tag and its contents to an HTML string.
      Specified by:
      toHTML in interface HTMLConvertable
      Returns:
      the HTML representation of this tag and its contents
    • 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
    • toCSS

      public static String toCSS(org.eclipse.swt.graphics.Font font, boolean includeName)
      Converts an SWT font to a CSS string.
      Parameters:
      font - the SWT font to convert
      includeName - whether to include the font name in the CSS string
      Returns:
      the CSS string representation of the font