Class SwingTerminal

  • All Implemented Interfaces:
    java.io.Closeable, java.io.Flushable, java.lang.AutoCloseable, org.jline.terminal.Sized, org.jline.terminal.spi.TerminalExt, org.jline.terminal.Terminal

    public class SwingTerminal
    extends org.jline.terminal.impl.LineDisciplineTerminal
    A Swing-based terminal implementation that extends LineDisciplineTerminal.

    This class provides a proper JLine Terminal implementation that can be embedded in Swing applications to display a terminal interface. It renders terminal content using Java 2D graphics and handles keyboard and mouse input with proper terminal capabilities.

    Features:

    • Full JLine Terminal interface implementation
    • Custom painting for terminal characters and attributes
    • ANSI color support with configurable color palette
    • Font configuration with monospace font support
    • Keyboard input handling with proper terminal capabilities
    • Mouse support for cursor positioning
    • Scrollback buffer support
    • Cursor blinking
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  SwingTerminal.TerminalComponent
      JComponent that renders the terminal display.
      • Nested classes/interfaces inherited from interface org.jline.terminal.Terminal

        org.jline.terminal.Terminal.KittyKeyboardMode, org.jline.terminal.Terminal.Mode, org.jline.terminal.Terminal.MouseTracking, org.jline.terminal.Terminal.Signal, org.jline.terminal.Terminal.SignalHandler
    • Field Summary

      • Fields inherited from class org.jline.terminal.impl.LineDisciplineTerminal

        attributes, masterOutput, size, skipNextLf, slaveInput, slaveInputPipe, slaveOutput, slaveReader, slaveWriter
      • Fields inherited from class org.jline.terminal.impl.AbstractTerminal

        bools, closed, currentMouseTracking, encoding, envProvider, handlers, inputEncoding, ints, name, onClose, outputEncoding, palette, status, strings, type
      • Fields inherited from interface org.jline.terminal.Terminal

        TYPE_DUMB, TYPE_DUMB_COLOR
    • Constructor Summary

      Constructors 
      Constructor Description
      SwingTerminal()
      Creates a new SwingTerminal with default dimensions (80x24).
      SwingTerminal​(int columns, int rows)
      Creates a new SwingTerminal with the specified dimensions.
      SwingTerminal​(java.lang.String name, int columns, int rows)
      Creates a new SwingTerminal with the specified name and dimensions.
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      javax.swing.JFrame createFrame​(java.lang.String title)
      Creates a JFrame containing the terminal component.
      void dispose()
      Deprecated.
      Use AbstractTerminal.close() instead, which properly handles all cleanup including disposing of resources.
      protected void doClose()  
      void dump​(long[] screen, int x, int y, int height, int width, int[] cursor)
      Dumps the terminal screen data.
      java.lang.String dump​(long timeout, boolean forceUpdate)
      Dumps the terminal screen data with scrollback.
      SwingTerminal.TerminalComponent getComponent()
      Gets the Swing component that renders the terminal.
      boolean isDirty()
      Checks if the terminal is dirty (needs repainting).
      java.lang.String pollInput()
      Gets input from the terminal component (non-blocking).
      void processInputBytes​(byte[] input)
      Processes input bytes through the terminal's line discipline.
      void setSize​(org.jline.terminal.Sized sz)  
      java.lang.String takeInput()
      Gets input from the terminal component (blocking).
      void write​(java.lang.String text)
      Writes text to the terminal component.
      • Methods inherited from class org.jline.terminal.impl.LineDisciplineTerminal

        doProcessInputByte, getAttributes, getProvider, getSize, getSystemStream, input, output, processInputByte, processInputBytes, processIOException, processOutputByte, raise, reader, setAttributes, writer
      • Methods inherited from class org.jline.terminal.impl.AbstractTerminal

        canPauseResume, checkClosed, checkInterrupted, close, detectTrueColorSupport, echo, echo, echoSignal, encoding, enterRawMode, flush, getBooleanCapability, getCurrentMouseTracking, getCursorPosition, getDefaultBackgroundColor, getDefaultForegroundColor, getenv, getGraphemeClusterMode, getKind, getName, getNumericCapability, getPalette, getStatus, getStatus, getStringCapability, getType, handle, hasFocusSupport, hasInBandResizeSupport, hasKittyKeyboardSupport, hasMouseSupport, hasPollSupport, inputEncoding, isClusterGrouped, isModeSupported, outputEncoding, parseInfoCmp, pause, pause, paused, puts, readMouseEvent, readMouseEvent, readMouseEvent, readMouseEvent, resetKittyKeyboardMode, resume, setEnv, setGraphemeClusterMode, setKittyKeyboardMode, setOnClose, supportsGraphemeClusterMode, toString, trackFocus, trackInBandResize, trackMouse
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.jline.terminal.Terminal

        beginSynchronizedUpdate, endSynchronizedUpdate, getBufferSize, getColumns, getHeight, getRows, getWidth, setSize, synchronizedUpdate
    • Constructor Detail

      • SwingTerminal

        public SwingTerminal​(int columns,
                             int rows)
                      throws java.io.IOException
        Creates a new SwingTerminal with the specified dimensions.
        Parameters:
        columns - the number of columns
        rows - the number of rows
        Throws:
        java.io.IOException - if an I/O error occurs during initialization
      • SwingTerminal

        public SwingTerminal()
                      throws java.io.IOException
        Creates a new SwingTerminal with default dimensions (80x24).
        Throws:
        java.io.IOException - if an I/O error occurs during initialization
      • SwingTerminal

        public SwingTerminal​(java.lang.String name,
                             int columns,
                             int rows)
                      throws java.io.IOException
        Creates a new SwingTerminal with the specified name and dimensions.
        Parameters:
        name - the terminal name
        columns - the number of columns
        rows - the number of rows
        Throws:
        java.io.IOException - if an I/O error occurs during initialization
    • Method Detail

      • createFrame

        public javax.swing.JFrame createFrame​(java.lang.String title)
        Creates a JFrame containing the terminal component.
        Parameters:
        title - the frame title
        Returns:
        the created frame
      • pollInput

        public java.lang.String pollInput()
        Gets input from the terminal component (non-blocking).
        Returns:
        the next input string, or null if none available
      • takeInput

        public java.lang.String takeInput()
                                   throws java.lang.InterruptedException
        Gets input from the terminal component (blocking).
        Returns:
        the next input string
        Throws:
        java.lang.InterruptedException - if interrupted while waiting
      • processInputBytes

        public void processInputBytes​(byte[] input)
                               throws java.io.IOException
        Processes input bytes through the terminal's line discipline.
        Overrides:
        processInputBytes in class org.jline.terminal.impl.LineDisciplineTerminal
        Parameters:
        input - the input bytes to process
        Throws:
        java.io.IOException - if an I/O error occurs
      • write

        public void write​(java.lang.String text)
        Writes text to the terminal component.
        Parameters:
        text - the text to write
      • dump

        public void dump​(long[] screen,
                         int x,
                         int y,
                         int height,
                         int width,
                         int[] cursor)
        Dumps the terminal screen data.
        Parameters:
        screen - the screen data array to fill
        x - the starting x coordinate
        y - the starting y coordinate
        height - the height to dump
        width - the width to dump
        cursor - the cursor position array to fill
      • dump

        public java.lang.String dump​(long timeout,
                                     boolean forceUpdate)
                              throws java.lang.InterruptedException
        Dumps the terminal screen data with scrollback.
        Parameters:
        timeout - the number of scrollback lines
        forceUpdate - whether to include scrollback
        Returns:
        the screen data
        Throws:
        java.lang.InterruptedException - if interrupted while waiting
      • isDirty

        public boolean isDirty()
        Checks if the terminal is dirty (needs repainting).
        Returns:
        true if dirty
      • doClose

        protected void doClose()
                        throws java.io.IOException
        Overrides:
        doClose in class org.jline.terminal.impl.LineDisciplineTerminal
        Throws:
        java.io.IOException
      • dispose

        @Deprecated
        public void dispose()
        Deprecated.
        Use AbstractTerminal.close() instead, which properly handles all cleanup including disposing of resources. This method will be made package-private or removed in a future major release.
        Disposes of the terminal resources.
      • setSize

        public void setSize​(org.jline.terminal.Sized sz)
        Specified by:
        setSize in interface org.jline.terminal.Terminal
        Overrides:
        setSize in class org.jline.terminal.impl.LineDisciplineTerminal