Class ColoredTextPane

  • All Implemented Interfaces:
    ImageObserver, MenuContainer, Printable, Serializable, Accessible, Scrollable

    public class ColoredTextPane
    extends JTextPane
    implements Printable

    A JTextPane that supports ANSI colors. This pane adds an append method that will append a given string to the pane; however, it will also understand certain ANSI color codes and manipulate the pane to these colors. See the constants class to see what codes have been interpreted by this pane. This also makes the pane ignore word wrap.

    The method for which ANSI color codes can be derived was mainly received by this stack overflow question. However, we have since improved on it. When you display the pane, it will look like this:

    Since:
    JTalker 0.1.5
    Version:
    0.0.1
    Author:
    C. William Oswald
    See Also:
    ANSIColorConstants, Serialized Form
    • Field Detail

      • currentColor

        protected Color currentColor
        The current color we are using when we are scanning. This is an instance variable because the append method needs a variable to store where it is at while scanning the string. This can be a background color or a foreground color, as determined by isBackground.
      • isBackground

        protected boolean isBackground
        Whether the current color is a background color. This is an instance variable because the append method needs a variable to store whether the current color is a background color while scanning the string.
    • Constructor Detail

      • ColoredTextPane

        public ColoredTextPane()
        Constructs a ColoredTextPane. This pane is uneditable, and has the default background and foreground as defined in the constants class. Moreover, it also uses the font as defined in the gui constants class.
    • Method Detail

      • getScrollableTracksViewportWidth

        public boolean getScrollableTracksViewportWidth()
        Returns true if a viewport should always force the width of this Scrollable to match the width of the viewport.
        Specified by:
        getScrollableTracksViewportWidth in interface Scrollable
        Overrides:
        getScrollableTracksViewportWidth in class JEditorPane
        Returns:
        false if the ComponentUI's preferred width is less than or equal to the parents width, true otherwise.
      • print

        public int print​(Graphics g,
                         PageFormat format,
                         int pageIndex)
        Prints the given page. This will use the current graphics on the pane and draw them on a buffered image, which will be sent to the printer service. This will not print everything that has been appended to the colored text pane; it will only print what is currently displayed.
        Specified by:
        print in interface Printable
        Parameters:
        g - The graphics to draw on (what will be printed).
        format - The format to use when transforming the graphics.
        pageIndex - The page number (determines which page to print).
      • setText

        public void setText​(String text)
        Sets the text of this ColoredTextPane, which is expected to be in the format of the content type of this editor. To see the exact exact documentation, visit this link. We needed to override this method due to our coloring of the pane. With this in mind, we do call super.setText(text) at the end of our setText method. However, we have to ensure that the color state is only preserved to our current state of color (not what our selection contains).
        Overrides:
        setText in class JEditorPane
        Parameters:
        text - The new text to be set; if null the old text will be deleted.
      • append

        public void append​(String s)
        Appends the given text to the pane. This string may contain ANSI color codes; we interpret them and change the pane accordingly.
        Parameters:
        s - The string that may contain ANSI color codes.
      • getColorMode

        public boolean getColorMode()
        Returns whether colors have been enabled with the text pane.
        Returns:
        Whether colors have been enabled with the text pane.
      • getHistory

        public String getHistory()
        Returns all of the text that has been appended to this pane. This is stored within a StringBuffer, and text is added to this buffer whenever the append method is called. This will save any ANSI escape codes sent to it.
        Returns:
        All of the text that has been appended to this pane.
        See Also:
        StringBuffer
      • setColorMode

        public void setColorMode​(boolean colorMode)
        Sets the new color mode. If this is false, this will disable any colors that have been drawn. It will also redraw the text as it came in the pane (if the mode has been changed).
        Parameters:
        colorMode - The new color mode (true for colors enabled).
      • append

        protected void append​(String s,
                              Color c,
                              boolean background)
        Appends the text to the pane using the given color. This is the main backbone to the append method. This will use the given color (and whether it is a background color) and the current AttributeSet to draw the given text.
        Parameters:
        s - The text to draw.
        c - The color of the text.
        background - Whether this is a background or foreground color.