Package jexer.backend

Class SwingTerminal

  • All Implemented Interfaces:
    java.awt.event.ComponentListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.MouseWheelListener, java.awt.event.WindowListener, java.util.EventListener, Screen, TerminalReader

    public class SwingTerminal
    extends LogicalScreen
    implements TerminalReader, java.awt.event.ComponentListener, java.awt.event.KeyListener, java.awt.event.MouseListener, java.awt.event.MouseMotionListener, java.awt.event.MouseWheelListener, java.awt.event.WindowListener
    This Screen backend reads keystrokes and mouse events and draws to either a Java Swing JFrame (potentially triple-buffered) or a JComponent. This class is a bit of an inversion of typical GUI classes. It performs all of the drawing logic from SwingTerminal (which is not a Swing class), and uses a SwingComponent wrapper class to call the JFrame or JComponent methods.
    • Field Detail

      • FONTFILE

        public static final java.lang.String FONTFILE
        The terminus font resource filename.
        See Also:
        Constant Field Values
    • Constructor Detail

      • SwingTerminal

        public SwingTerminal​(Backend backend,
                             int windowWidth,
                             int windowHeight,
                             int fontSize,
                             java.lang.Object listener)
        Public constructor creates a new JFrame to render to.
        Parameters:
        backend - the backend that will read from this terminal
        windowWidth - the number of text columns to start with
        windowHeight - the number of text rows to start with
        fontSize - the size in points. Good values to pick are: 16, 20, 22, and 24.
        listener - the object this backend needs to wake up when new input comes in
      • SwingTerminal

        public SwingTerminal​(Backend backend,
                             javax.swing.JComponent component,
                             int windowWidth,
                             int windowHeight,
                             int fontSize,
                             java.lang.Object listener)
        Public constructor renders to an existing JComponent.
        Parameters:
        backend - the backend that will read from this terminal
        component - the Swing component to render to
        windowWidth - the number of text columns to start with
        windowHeight - the number of text rows to start with
        fontSize - the size in points. Good values to pick are: 16, 20, 22, and 24.
        listener - the object this backend needs to wake up when new input comes in
    • Method Detail

      • setTitle

        public void setTitle​(java.lang.String title)
        Set the window title.
        Specified by:
        setTitle in interface Screen
        Overrides:
        setTitle in class LogicalScreen
        Parameters:
        title - the new title
      • hasEvents

        public boolean hasEvents()
        Check if there are events in the queue.
        Specified by:
        hasEvents in interface TerminalReader
        Returns:
        if true, getEvents() has something to return to the backend
      • getEvents

        public void getEvents​(java.util.List<TInputEvent> queue)
        Return any events in the IO queue.
        Specified by:
        getEvents in interface TerminalReader
        Parameters:
        queue - list to append new events to
      • closeTerminal

        public void closeTerminal()
        Restore terminal to normal state.
        Specified by:
        closeTerminal in interface TerminalReader
      • setListener

        public void setListener​(java.lang.Object listener)
        Set listener to a different Object.
        Specified by:
        setListener in interface TerminalReader
        Parameters:
        listener - the new listening object that run() wakes up on new input
      • reloadOptions

        public void reloadOptions()
        Reload options from System properties.
        Specified by:
        reloadOptions in interface TerminalReader
      • isTripleBuffer

        public boolean isTripleBuffer()
        Get triple buffering flag.
        Returns:
        true if triple buffering is enabled
      • setTripleBuffer

        public void setTripleBuffer​(boolean tripleBuffer)
        Set triple buffering.
        Parameters:
        tripleBuffer - if true, enable triple buffering
      • setMouseStyle

        public void setMouseStyle​(java.lang.String mouseStyle)
        Set the mouse pointer (cursor) style.
        Parameters:
        mouseStyle - the pointer style string, one of: "default", "none", "hand", "text", "move", or "crosshair"
      • getMouseStyle

        public java.lang.String getMouseStyle()
        Get the mouse cursor style.
        Returns:
        the cursor style string, one of: "default", "none", "hand", "text", "move", or "crosshair"
      • setCursorStyle

        public void setCursorStyle​(SwingTerminal.CursorStyle cursorStyle)
        Set the cursor style.
        Parameters:
        cursorStyle - the new cursor style
      • setCursorStyle

        public void setCursorStyle​(java.lang.String cursorStyleString)
        Set the cursor style.
        Parameters:
        cursorStyleString - the new cursor style
      • getTextWidth

        public int getTextWidth()
        Get the width of a character cell in pixels.
        Specified by:
        getTextWidth in interface Screen
        Overrides:
        getTextWidth in class LogicalScreen
        Returns:
        the width in pixels of a character cell
      • getTextHeight

        public int getTextHeight()
        Get the height of a character cell in pixels.
        Specified by:
        getTextHeight in interface Screen
        Overrides:
        getTextHeight in class LogicalScreen
        Returns:
        the height in pixels of a character cell
      • getBlinkMillis

        public long getBlinkMillis()
        Get the number of millis to wait before switching the blink from visible to invisible.
        Returns:
        the number of milli to wait before switching the blink from visible to invisible
      • getCursorBlinkVisible

        public boolean getCursorBlinkVisible()
        Get the current status of the blink flag.
        Returns:
        true if the cursor and blinking text should be visible
      • getFontSize

        public int getFontSize()
        Get the font size in points.
        Returns:
        font size in points
      • setFontSize

        public void setFontSize​(int fontSize)
        Set the font size in points.
        Parameters:
        fontSize - font size in points
      • setFont

        public void setFont​(java.awt.Font font)
        Set to a new font, and resize the screen to match its dimensions.
        Parameters:
        font - the new font
      • getFont

        public java.awt.Font getFont()
        Get the font this screen was last set to.
        Returns:
        the font
      • setDefaultFont

        public void setDefaultFont()
        Set the font to Terminus, the best all-around font for both CP437 and ISO8859-1.
      • getTextAdjustX

        public int getTextAdjustX()
        Get the X text adjustment.
        Returns:
        X text adjustment
      • setTextAdjustX

        public void setTextAdjustX​(int textAdjustX)
        Set the X text adjustment.
        Parameters:
        textAdjustX - the X text adjustment
      • getTextAdjustY

        public int getTextAdjustY()
        Get the Y text adjustment.
        Returns:
        Y text adjustment
      • setTextAdjustY

        public void setTextAdjustY​(int textAdjustY)
        Set the Y text adjustment.
        Parameters:
        textAdjustY - the Y text adjustment
      • getTextAdjustHeight

        public int getTextAdjustHeight()
        Get the height text adjustment.
        Returns:
        height text adjustment
      • setTextAdjustHeight

        public void setTextAdjustHeight​(int textAdjustHeight)
        Set the height text adjustment.
        Parameters:
        textAdjustHeight - the height text adjustment
      • getTextAdjustWidth

        public int getTextAdjustWidth()
        Get the width text adjustment.
        Returns:
        width text adjustment
      • setTextAdjustWidth

        public void setTextAdjustWidth​(int textAdjustWidth)
        Set the width text adjustment.
        Parameters:
        textAdjustWidth - the width text adjustment
      • attrToForegroundColor

        public static java.awt.Color attrToForegroundColor​(CellAttributes attr)
        Convert a CellAttributes foreground color to an Swing Color.
        Parameters:
        attr - the text attributes
        Returns:
        the Swing Color
      • attrToBackgroundColor

        public static java.awt.Color attrToBackgroundColor​(CellAttributes attr)
        Convert a CellAttributes background color to an Swing Color.
        Parameters:
        attr - the text attributes
        Returns:
        the Swing Color
      • resizeToScreen

        public void resizeToScreen()
        Resize the physical screen to match the logical screen dimensions.
        Overrides:
        resizeToScreen in class LogicalScreen
      • paint

        public void paint​(java.awt.Graphics gr)
        Paint redraws the whole screen.
        Parameters:
        gr - the Swing Graphics context
      • shutdown

        public void shutdown()
        Restore terminal to normal state.
      • textColumn

        public int textColumn​(int x)
        Convert pixel column position to text cell column position.
        Parameters:
        x - pixel column position
        Returns:
        text cell column position
      • textRow

        public int textRow​(int y)
        Convert pixel row position to text cell row position.
        Parameters:
        y - pixel row position
        Returns:
        text cell row position
      • getSessionInfo

        public SessionInfo getSessionInfo()
        Getter for sessionInfo.
        Returns:
        the SessionInfo
      • getSwingComponent

        public SwingComponent getSwingComponent()
        Getter for the underlying Swing component.
        Returns:
        the SwingComponent
      • isImagesOverText

        public boolean isImagesOverText()
        Check if screen will support incomplete image fragments over text display.
        Returns:
        true if images can partially obscure text
      • isPixelMouse

        public boolean isPixelMouse()
        Check if terminal is reporting pixel-based mouse position.
        Returns:
        true if single-pixel mouse movements are reported
      • setPixelMouse

        public void setPixelMouse​(boolean pixelMouse)
        Set request for terminal to report pixel-based mouse position.
        Parameters:
        pixelMouse - if true, single-pixel mouse movements will be reported
      • keyReleased

        public void keyReleased​(java.awt.event.KeyEvent key)
        Pass Swing keystrokes into the event queue.
        Specified by:
        keyReleased in interface java.awt.event.KeyListener
        Parameters:
        key - keystroke received
      • keyTyped

        public void keyTyped​(java.awt.event.KeyEvent key)
        Pass Swing keystrokes into the event queue.
        Specified by:
        keyTyped in interface java.awt.event.KeyListener
        Parameters:
        key - keystroke received
      • keyPressed

        public void keyPressed​(java.awt.event.KeyEvent key)
        Pass Swing keystrokes into the event queue.
        Specified by:
        keyPressed in interface java.awt.event.KeyListener
        Parameters:
        key - keystroke received
      • windowActivated

        public void windowActivated​(java.awt.event.WindowEvent event)
        Pass window events into the event queue.
        Specified by:
        windowActivated in interface java.awt.event.WindowListener
        Parameters:
        event - window event received
      • windowClosed

        public void windowClosed​(java.awt.event.WindowEvent event)
        Pass window events into the event queue.
        Specified by:
        windowClosed in interface java.awt.event.WindowListener
        Parameters:
        event - window event received
      • windowClosing

        public void windowClosing​(java.awt.event.WindowEvent event)
        Pass window events into the event queue.
        Specified by:
        windowClosing in interface java.awt.event.WindowListener
        Parameters:
        event - window event received
      • windowDeactivated

        public void windowDeactivated​(java.awt.event.WindowEvent event)
        Pass window events into the event queue.
        Specified by:
        windowDeactivated in interface java.awt.event.WindowListener
        Parameters:
        event - window event received
      • windowDeiconified

        public void windowDeiconified​(java.awt.event.WindowEvent event)
        Pass window events into the event queue.
        Specified by:
        windowDeiconified in interface java.awt.event.WindowListener
        Parameters:
        event - window event received
      • windowIconified

        public void windowIconified​(java.awt.event.WindowEvent event)
        Pass window events into the event queue.
        Specified by:
        windowIconified in interface java.awt.event.WindowListener
        Parameters:
        event - window event received
      • windowOpened

        public void windowOpened​(java.awt.event.WindowEvent event)
        Pass window events into the event queue.
        Specified by:
        windowOpened in interface java.awt.event.WindowListener
        Parameters:
        event - window event received
      • componentHidden

        public void componentHidden​(java.awt.event.ComponentEvent event)
        Pass component events into the event queue.
        Specified by:
        componentHidden in interface java.awt.event.ComponentListener
        Parameters:
        event - component event received
      • componentShown

        public void componentShown​(java.awt.event.ComponentEvent event)
        Pass component events into the event queue.
        Specified by:
        componentShown in interface java.awt.event.ComponentListener
        Parameters:
        event - component event received
      • componentMoved

        public void componentMoved​(java.awt.event.ComponentEvent event)
        Pass component events into the event queue.
        Specified by:
        componentMoved in interface java.awt.event.ComponentListener
        Parameters:
        event - component event received
      • componentResized

        public void componentResized​(java.awt.event.ComponentEvent event)
        Pass component events into the event queue.
        Specified by:
        componentResized in interface java.awt.event.ComponentListener
        Parameters:
        event - component event received
      • mouseDragged

        public void mouseDragged​(java.awt.event.MouseEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mouseDragged in interface java.awt.event.MouseMotionListener
        Parameters:
        mouse - mouse event received
      • mouseMoved

        public void mouseMoved​(java.awt.event.MouseEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mouseMoved in interface java.awt.event.MouseMotionListener
        Parameters:
        mouse - mouse event received
      • mouseClicked

        public void mouseClicked​(java.awt.event.MouseEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mouseClicked in interface java.awt.event.MouseListener
        Parameters:
        mouse - mouse event received
      • mouseEntered

        public void mouseEntered​(java.awt.event.MouseEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mouseEntered in interface java.awt.event.MouseListener
        Parameters:
        mouse - mouse event received
      • mouseExited

        public void mouseExited​(java.awt.event.MouseEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mouseExited in interface java.awt.event.MouseListener
        Parameters:
        mouse - mouse event received
      • mousePressed

        public void mousePressed​(java.awt.event.MouseEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mousePressed in interface java.awt.event.MouseListener
        Parameters:
        mouse - mouse event received
      • mouseReleased

        public void mouseReleased​(java.awt.event.MouseEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mouseReleased in interface java.awt.event.MouseListener
        Parameters:
        mouse - mouse event received
      • mouseWheelMoved

        public void mouseWheelMoved​(java.awt.event.MouseWheelEvent mouse)
        Pass mouse events into the event queue.
        Specified by:
        mouseWheelMoved in interface java.awt.event.MouseWheelListener
        Parameters:
        mouse - mouse event received