Package jexer.backend
Class SwingTerminal
- java.lang.Object
-
- jexer.backend.LogicalScreen
-
- jexer.backend.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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
SwingTerminal.CursorStyle
Cursor style to draw.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
FONTFILE
The terminus font resource filename.-
Fields inherited from class jexer.backend.LogicalScreen
cursorVisible, cursorX, cursorY, height, logical, physical, reallyCleared, width
-
-
Constructor Summary
Constructors Constructor Description SwingTerminal(Backend backend, int windowWidth, int windowHeight, int fontSize, java.lang.Object listener)
Public constructor creates a new JFrame to render to.SwingTerminal(Backend backend, javax.swing.JComponent component, int windowWidth, int windowHeight, int fontSize, java.lang.Object listener)
Public constructor renders to an existing JComponent.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static java.awt.Color
attrToBackgroundColor(CellAttributes attr)
Convert a CellAttributes background color to an Swing Color.static java.awt.Color
attrToForegroundColor(CellAttributes attr)
Convert a CellAttributes foreground color to an Swing Color.void
closeTerminal()
Restore terminal to normal state.void
componentHidden(java.awt.event.ComponentEvent event)
Pass component events into the event queue.void
componentMoved(java.awt.event.ComponentEvent event)
Pass component events into the event queue.void
componentResized(java.awt.event.ComponentEvent event)
Pass component events into the event queue.void
componentShown(java.awt.event.ComponentEvent event)
Pass component events into the event queue.void
flushPhysical()
Push the logical screen to the physical device.long
getBlinkMillis()
Get the number of millis to wait before switching the blink from visible to invisible.boolean
getCursorBlinkVisible()
Get the current status of the blink flag.SwingTerminal.CursorStyle
getCursorStyle()
Get the cursor style.void
getEvents(java.util.List<TInputEvent> queue)
Return any events in the IO queue.java.awt.Font
getFont()
Get the font this screen was last set to.int
getFontSize()
Get the font size in points.java.lang.String
getMouseStyle()
Get the mouse cursor style.SessionInfo
getSessionInfo()
Getter for sessionInfo.SwingComponent
getSwingComponent()
Getter for the underlying Swing component.int
getTextAdjustHeight()
Get the height text adjustment.int
getTextAdjustWidth()
Get the width text adjustment.int
getTextAdjustX()
Get the X text adjustment.int
getTextAdjustY()
Get the Y text adjustment.int
getTextHeight()
Get the height of a character cell in pixels.int
getTextWidth()
Get the width of a character cell in pixels.boolean
hasEvents()
Check if there are events in the queue.boolean
isImagesOverText()
Check if screen will support incomplete image fragments over text display.boolean
isPixelMouse()
Check if terminal is reporting pixel-based mouse position.boolean
isTripleBuffer()
Get triple buffering flag.void
keyPressed(java.awt.event.KeyEvent key)
Pass Swing keystrokes into the event queue.void
keyReleased(java.awt.event.KeyEvent key)
Pass Swing keystrokes into the event queue.void
keyTyped(java.awt.event.KeyEvent key)
Pass Swing keystrokes into the event queue.void
mouseClicked(java.awt.event.MouseEvent mouse)
Pass mouse events into the event queue.void
mouseDragged(java.awt.event.MouseEvent mouse)
Pass mouse events into the event queue.void
mouseEntered(java.awt.event.MouseEvent mouse)
Pass mouse events into the event queue.void
mouseExited(java.awt.event.MouseEvent mouse)
Pass mouse events into the event queue.void
mouseMoved(java.awt.event.MouseEvent mouse)
Pass mouse events into the event queue.void
mousePressed(java.awt.event.MouseEvent mouse)
Pass mouse events into the event queue.void
mouseReleased(java.awt.event.MouseEvent mouse)
Pass mouse events into the event queue.void
mouseWheelMoved(java.awt.event.MouseWheelEvent mouse)
Pass mouse events into the event queue.void
paint(java.awt.Graphics gr)
Paint redraws the whole screen.void
reloadOptions()
Reload options from System properties.void
resizeToScreen()
Resize the physical screen to match the logical screen dimensions.void
setCursorStyle(java.lang.String cursorStyleString)
Set the cursor style.void
setCursorStyle(SwingTerminal.CursorStyle cursorStyle)
Set the cursor style.void
setDefaultFont()
Set the font to Terminus, the best all-around font for both CP437 and ISO8859-1.void
setFont(java.awt.Font font)
Set to a new font, and resize the screen to match its dimensions.void
setFontSize(int fontSize)
Set the font size in points.void
setListener(java.lang.Object listener)
Set listener to a different Object.void
setMouseStyle(java.lang.String mouseStyle)
Set the mouse pointer (cursor) style.void
setPixelMouse(boolean pixelMouse)
Set request for terminal to report pixel-based mouse position.void
setTextAdjustHeight(int textAdjustHeight)
Set the height text adjustment.void
setTextAdjustWidth(int textAdjustWidth)
Set the width text adjustment.void
setTextAdjustX(int textAdjustX)
Set the X text adjustment.void
setTextAdjustY(int textAdjustY)
Set the Y text adjustment.void
setTitle(java.lang.String title)
Set the window title.void
setTripleBuffer(boolean tripleBuffer)
Set triple buffering.void
shutdown()
Restore terminal to normal state.int
textColumn(int x)
Convert pixel column position to text cell column position.int
textRow(int y)
Convert pixel row position to text cell row position.void
windowActivated(java.awt.event.WindowEvent event)
Pass window events into the event queue.void
windowClosed(java.awt.event.WindowEvent event)
Pass window events into the event queue.void
windowClosing(java.awt.event.WindowEvent event)
Pass window events into the event queue.void
windowDeactivated(java.awt.event.WindowEvent event)
Pass window events into the event queue.void
windowDeiconified(java.awt.event.WindowEvent event)
Pass window events into the event queue.void
windowIconified(java.awt.event.WindowEvent event)
Pass window events into the event queue.void
windowOpened(java.awt.event.WindowEvent event)
Pass window events into the event queue.-
Methods inherited from class jexer.backend.LogicalScreen
clear, clearPhysical, copySelection, drawBox, drawBox, drawBoxShadow, getAttrXY, getCharXY, getCharXY, getClipBottom, getClipLeft, getClipRight, getClipTop, getCursorX, getCursorY, getHeight, getOffsetX, getOffsetY, getWidth, hideCursor, hLineXY, invertCell, invertCell, isCursorVisible, isDirty, putAll, putAttrXY, putAttrXY, putCharXY, putCharXY, putCharXY, putCursor, putFullwidthCharXY, putFullwidthCharXY, putFullwidthCharXY, putStringXY, putStringXY, reset, resetClipping, setClipBottom, setClipLeft, setClipRight, setClipTop, setDimensions, setHeight, setOffsetX, setOffsetY, setSelection, setWidth, snapshot, unsetImageRow, vLineXY
-
-
-
-
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 terminalwindowWidth
- the number of text columns to start withwindowHeight
- the number of text rows to start withfontSize
- 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 terminalcomponent
- the Swing component to render towindowWidth
- the number of text columns to start withwindowHeight
- the number of text rows to start withfontSize
- 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 interfaceScreen
- Overrides:
setTitle
in classLogicalScreen
- Parameters:
title
- the new title
-
flushPhysical
public void flushPhysical()
Push the logical screen to the physical device.- Specified by:
flushPhysical
in interfaceScreen
- Overrides:
flushPhysical
in classLogicalScreen
-
hasEvents
public boolean hasEvents()
Check if there are events in the queue.- Specified by:
hasEvents
in interfaceTerminalReader
- 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 interfaceTerminalReader
- Parameters:
queue
- list to append new events to
-
closeTerminal
public void closeTerminal()
Restore terminal to normal state.- Specified by:
closeTerminal
in interfaceTerminalReader
-
setListener
public void setListener(java.lang.Object listener)
Set listener to a different Object.- Specified by:
setListener
in interfaceTerminalReader
- 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 interfaceTerminalReader
-
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"
-
getCursorStyle
public SwingTerminal.CursorStyle getCursorStyle()
Get the cursor style.- Returns:
- the cursor style
-
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 interfaceScreen
- Overrides:
getTextWidth
in classLogicalScreen
- 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 interfaceScreen
- Overrides:
getTextHeight
in classLogicalScreen
- 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 classLogicalScreen
-
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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.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 interfacejava.awt.event.MouseWheelListener
- Parameters:
mouse
- mouse event received
-
-