Class InlineHTML

java.lang.Object
com.yetnt.utils.builders.InlineHTML

public class InlineHTML extends Object
The most important class that helps me avoid learning the pain that is JFormattedTextField

InlineHTML is a utility class designed to simplify the creation of rich text content for JLabels. It provides methods for applying various HTML-like formatting, such as bold, italic, underline, font colour, font size, and paragraph wrapping, without directly manipulating raw HTML strings.

The class internally manages a list of opening and closing HTML tags, which are applied to the content when toString() or wrapHTML() is called. This allows for method chaining to build complex formatted text efficiently.

Example usage:


     InlineHTML richText = new InlineHTML("Hello, World!")
         .bold()                              // wraps in <b></b> tag
         .font(Color.BLUE)                    // wraps in <font color="#0000FF"></font>
         .paragraph();                        // wraps in <p></p>
     myLabel.setText(richText.wrapHTML());    // finally wraps in <html></html>
     

From J3Engine

Author:
Lehlogonolo Poole
  • Field Details

    • content

      private String content
    • open

      private ArrayList<String> open
    • close

      private ArrayList<String> close
    • HORIZONTAL_LINE

      public static String HORIZONTAL_LINE
      HTML constant for a horizontal rule.
    • LINE_BREAK

      public static String LINE_BREAK
      HTML constant for a line break.
    • GREATER_THAN

      public static String GREATER_THAN
      HTML entity for the greater than symbol.
    • LESS_THAN

      public static String LESS_THAN
      HTML entity for the less than symbol.
    • EQUAL

      public static String EQUAL
      HTML entity for the equals symbol.
  • Constructor Details

    • InlineHTML

      public InlineHTML(String cont)
      Constructs a new InlineHTML instance with the given content.
      Parameters:
      cont - The initial string content.
    • InlineHTML

      public InlineHTML()
      Constructs an empty InlineHTML instance. The content can be added later using add() or addLn() methods.
    • InlineHTML

      public InlineHTML(String cont, boolean esc)
      Constructs a new InlineHTML instance with the given content, optionally escaping HTML special characters.
      Parameters:
      cont - The initial string content.
      esc - If true, HTML special characters (<, >, =) will be escaped.
  • Method Details

    • from

      public static InlineHTML from(String string, InlineHTML style)
      Creates a new InlineHTML instance from a given string and copies the opening and closing tags from an existing InlineHTML style object. This is useful for applying an existing style to new content.
      Parameters:
      string - The initial string content for the new instance.
      style - The InlineHTML instance from which to copy the open and close tags.
      Returns:
      A new InlineHTML instance with the specified content and copied style.
    • add

      public InlineHTML add(String cont)
      Appends additional content to the current rich text.
      Parameters:
      cont - The string content to append.
      Returns:
      The current InlineHTML instance for method chaining.
    • addLn

      public InlineHTML addLn(String cont)
      Appends additional content to the current rich text, preceded by an HTML line break.
      Parameters:
      cont - The string content to append.
      Returns:
      The current InlineHTML instance for method chaining.
    • addAttributes

      public InlineHTML addAttributes(LinkedHashMap<String,String> attributes)
      Adds attributes to the most recently opened HTML tag. This method is useful for dynamically adding properties like id, class, or other custom attributes to a tag that has just been opened using methods like wrapTag().

      The attributes are appended to the last tag added to the open list.

      Parameters:
      attributes - A LinkedHashMap where keys are attribute names and values are attribute values.
      Returns:
      The current InlineHTML instance for method chaining.
    • addStyle

      public InlineHTML addStyle(LinkedHashMap<String,String> style)
      Adds inline CSS styles to the most recently opened HTML tag. This method allows for dynamic application of CSS properties to a tag that has just been opened. It checks if a 'style' attribute already exists on the last opened tag and throws a RuntimeException if it does, to prevent overwriting.

      The styles are formatted into a CSS string and then added as a 'style' attribute.

      Parameters:
      style - A LinkedHashMap where keys are CSS property names and values are their corresponding values.
      Returns:
      The current InlineHTML instance for method chaining.
    • bold

      public InlineHTML bold()
      Applies bold formatting to the content.
      Returns:
      The current InlineHTML instance for method chaining.
    • italic

      public InlineHTML italic()
      Applies italic formatting to the content.
      Returns:
      The current InlineHTML instance for method chaining.
    • underline

      public InlineHTML underline()
      Applies underline formatting to the content.
      Returns:
      The current InlineHTML instance for method chaining.
    • font

      public InlineHTML font(Color col)
      Applies a specific colour to the font of the content. The colour is converted to a hexadecimal RGB string.
      Parameters:
      col - The Color object to apply.
      Returns:
      The current InlineHTML instance for method chaining.
    • font

      public InlineHTML font(Color col, String size)
      Applies a specific colour and size to the font of the content. The colour is converted to a hexadecimal RGB string.
      Parameters:
      col - The Colour object to apply.
      size - The font size as a string (e.g., "1", "+2", "-3").
      Returns:
      The current InlineHTML instance for method chaining.
    • font

      public InlineHTML font(String size)
      Applies a specific font size to the content.
      Parameters:
      size - The font size as a string (e.g., "1", "+2", "-3").
      Returns:
      The current InlineHTML instance for method chaining.
    • colToStr

      public static String colToStr(Color col)
    • font

      public InlineHTML font(Color col, String size, Color backgroundCol)
      Applies a specific colour, size, and background colour to the font of the content. The colours are converted to hexadecimal RGB strings.
      Parameters:
      col - The font Colour object to apply.
      size - The font size as a string (e.g., "1", "+2", "-3").
      backgroundCol - The background Colour object to apply.
      Returns:
      The current InlineHTML instance for method chaining.
    • paragraph

      public InlineHTML paragraph()
      Wraps the content in a paragraph tag.
      Returns:
      The current InlineHTML instance for method chaining.
    • heading

      public InlineHTML heading(InlineHTML.Heading heading)
      Applies a heading tag (H1-H6) to the content.
      Parameters:
      heading - The InlineHTML.Heading enum representing the desired heading level.
      Returns:
      The current InlineHTML instance for method chaining.
    • superscript

      public InlineHTML superscript()
      Applies superscript formatting to the content.
      Returns:
      The current InlineHTML instance for method chaining.
    • subscript

      public InlineHTML subscript()
      Applies subscript formatting to the content.
      Returns:
      The current InlineHTML instance for method chaining.
    • wrapTag

      public InlineHTML wrapTag(String tag, LinkedHashMap<String,String> attributes)
      Wraps the content with a generic HTML tag, applying specified attributes. This method is used internally by other formatting methods like bold(), italic(), and font().
      Parameters:
      tag - The HTML tag name (e.g., "b", "i", "font").
      attributes - A LinkedHashMap of attribute names and their values (e.g., "color" -> "#FF0000").
      Returns:
      The current InlineHTML instance for method chaining.
    • wrapTag

      public InlineHTML wrapTag(String tag, LinkedHashMap<String,String> attributes, LinkedHashMap<String,String> style)
      Wraps the content with a generic HTML tag, applying specified attributes and inline styles. This method allows for more complex styling than wrapTag(String, LinkedHashMap).
      Parameters:
      tag - The HTML tag name (e.g., "div", "span").
      attributes - A LinkedHashMap of attribute names and their values (e.g., "id" -> "myDiv").
      style - A LinkedHashMap of CSS style properties and their values (e.g., "color" -> "red", "font-size" -> "12px").
      Returns:
      The current InlineHTML instance for method chaining.
    • wrapDiv

      public InlineHTML wrapDiv(int width)
      Wraps the content in a div tag with a specified width.
      Parameters:
      width - The width of the div in pixels.
      Returns:
      The current InlineHTML instance for method chaining.
    • wrapUsing

      public InlineHTML wrapUsing(InlineHTML other)
      Applies the opening and closing tags from another InlineHTML instance to the current instance. The tags from other are added before the current instance's own tags, effectively wrapping the current instance's content and styling.
      Parameters:
      other - The InlineHTML instance whose tags are to be applied.
      Returns:
      The current InlineHTML instance for method chaining.
    • wrapHTML

      public String wrapHTML()
      Wraps the generated HTML string with <html> tags.
      Returns:
      The complete HTML string, including the <html> and </html> tags.
    • toString

      public String toString()
      Generates the HTML string representation of the rich text content, applying all accumulated open and close tags.
      Overrides:
      toString in class Object
      Returns:
      The HTML string.
    • htmlOf

      public static String htmlOf(InlineHTML... richTexts)
      StaticRefs factory method to combine multiple InlineHTML instances into a single HTML string, wrapped in an <html> tag.
      Parameters:
      richTexts - An array of InlineHTML objects to combine.
      Returns:
      A single HTML string containing all the rich text content.
    • htmlOf

      public static String htmlOf(String... txt)
      StaticRefs factory method to combine multiple String instances into a single HTML string, wrapped in an <html> tag.
      Parameters:
      txt - An array of String objects to combine.
      Returns:
      A single HTML string containing all the text content.
    • paragraphWrap

      public static InlineHTML paragraphWrap(InlineHTML... richTexts)
      StaticRefs factory method to combine multiple InlineHTML instances and wrap them within a single paragraph <p> tag.
      Parameters:
      richTexts - An array of InlineHTML objects to combine.
      Returns:
      A new InlineHTML instance representing the combined content within a paragraph.
    • getRawContent

      public String getRawContent()
    • add

      public InlineHTML add(InlineHTML jLabelRichText)
    • getClose

      public ArrayList<String> getClose()
      Returns the list of closing HTML tags that will be applied to the content. These tags are applied in reverse order when toString() is called.
      Returns:
      An ArrayList of closing HTML tag strings.
    • getOpen

      public ArrayList<String> getOpen()
      Returns the list of opening HTML tags that will be applied to the content. These tags are applied in order when toString() is called.
      Returns:
      An ArrayList of opening HTML tag strings.
    • setClose

      public InlineHTML setClose(ArrayList<String> close)
      Sets the list of closing HTML tags. This method is primarily used internally for copying styles or for advanced manipulation.
      Parameters:
      close - An ArrayList of closing HTML tag strings to set.
      Returns:
      The current InlineHTML instance for method chaining.
    • setOpen

      public InlineHTML setOpen(ArrayList<String> open)
      Sets the list of opening HTML tags. This method is primarily used internally for copying styles or for advanced manipulation.
      Parameters:
      open - An ArrayList of opening HTML tag strings to set.
      Returns:
      The current InlineHTML instance for method chaining.