Class Terminal

All Implemented Interfaces:
KeyListener, MouseListener, MouseMotionListener, ImageObserver, MenuContainer, Serializable, Runnable, EventListener

public final class Terminal extends JComponent implements Runnable, KeyListener, MouseListener, MouseMotionListener
This class implements a simple Terminal window to access interface ports for debugging. It opens input and output streams to the specified device (Internet socket or serial device), interprets the bytes from the input stream as ISO-8859-1 characters (including control characters) to display, and accepts user input from the keyboard and sends it to the output stream (optionally locally echoing the entered characters to the screen). The screen implements a simple emulation of a DEC VT102 screen. It will be 24 lines tall and either 80 or 132 characters wide. The screen maintains positional and escape sequence context state from the input characters. It also supports optional auto-LF for some systems that refuse to send line feeds after carriage returns. TODO: optionally support UTF-8 instead of ISO-8859-1?
Author:
Andrew Pavlin, KA2DDO
See Also:
  • Field Details

    • NULL_INPUT_STREAM

      public static final InputStream NULL_INPUT_STREAM
      Fake InputStream that is always at end-of-file.
  • Constructor Details

    • Terminal

      public Terminal(InputStream is, OutputStream os, String portName, boolean echoed)
      Create a Terminal screen widget for managing the named connection, optionally opening input and output streams from and to that connection.
      Parameters:
      is - InputStream to read for characters to display on the screen widget, or null to not open the stream yet
      os - OutputStream to write keyboard characters to, or null to not provide an OutputStream yet
      portName - String name to title the screen widget
      echoed - boolean true if characters typed into the widget by the user should be displayed, rather than waiting for an echo-back from the connection
  • Method Details

    • setInputStream

      public void setInputStream(InputStream is)
      Set the InputStream to read for characters to display.
      Parameters:
      is - InputStream to read
    • setOutputStream

      public void setOutputStream(OutputStream os) throws IllegalStateException
      Set the OutputStream to write user-typed characters.
      Parameters:
      os - OutputStream to write
      Throws:
      IllegalStateException
    • setDoAutoLF

      public void setDoAutoLF(boolean doAutoLF)
      Specify whether a linefeed should be injected automatically after a carriage return if a linefeed is not explicitly provided by the remote system.
      Parameters:
      doAutoLF - boolean true if automatic linefeed injection should be performed
    • addAutoFlush

      public void addAutoFlush(char ch)
      Add another character to the set of user-typed characters that cause a output stream flush. By default, only Carriage Return causes a forced flush.
      Parameters:
      ch - char to add
    • removeAutoFlush

      public void removeAutoFlush(char ch)
      Remove a character from the set of user-typed characters that cause a output stream flush.
      Parameters:
      ch - char to remove
    • getHeight

      public int getHeight()
      Returns the current height of this component. This method is preferable to writing component.getBounds().height, or component.getSize().height because it doesn't cause any heap allocations.
      Overrides:
      getHeight in class JComponent
      Returns:
      the current height of this component
    • getWidth

      public int getWidth()
      Returns the current width of this component. This method is preferable to writing component.getBounds().width, or component.getSize().width because it doesn't cause any heap allocations.
      Overrides:
      getWidth in class JComponent
      Returns:
      the current width of this component
    • validate

      public void validate()
      Validates this container and all of its subcomponents.

      The validate method is used to cause a container to lay out its subcomponents again. It should be invoked when this container's subcomponents are modified (added to or removed from the container, or layout-related information changed) after the container has been displayed.

      If this Container is not valid, this method invokes the validateTree method and marks this Container as valid. Otherwise, no action is performed.

      Overrides:
      validate in class Container
      See Also:
    • getMinimumSize

      public Dimension getMinimumSize()
      If the minimum size has been set to a non-null value just returns it. If the UI delegate's getMinimumSize method returns a non-null value then return that; otherwise defer to the component's layout manager.
      Overrides:
      getMinimumSize in class JComponent
      Returns:
      the value of the minimumSize property
    • getPreferredSize

      public Dimension getPreferredSize()
      If the preferredSize has been set to a non-null value just returns it. If the UI delegate's getPreferredSize method returns a non null value then return that; otherwise defer to the component's layout manager.
      Overrides:
      getPreferredSize in class JComponent
      Returns:
      the value of the preferredSize property
    • paintComponent

      protected void paintComponent(Graphics g)
      Overrides:
      paintComponent in class JComponent
      Parameters:
      g - the Graphics object to protect
    • run

      public final void run()
      DO NOT CALL. The Terminal instance uses this method on a background thread to read characters from the input stream and render the characters on the screen widget.
      Specified by:
      run in interface Runnable
    • close

      public void close()
      Shut down a Terminal and close the I/O streams it is connected to.
    • displayLocalText

      public void displayLocalText(String text)
      Display the programmatically-generated text on the Terminal screen. Typically used to display state change information when the connection associated with a Terminal changes state.
      Parameters:
      text - String to append (as if it was received at this instant from the input stream); string should be less than 128 characters long
    • keyPressed

      public void keyPressed(KeyEvent e)
      Invoked when a key has been pressed. See the class description for KeyEvent for a definition of a key pressed event.
      Specified by:
      keyPressed in interface KeyListener
    • keyTyped

      public void keyTyped(KeyEvent e)
      Invoked when a key has been typed. See the class description for KeyEvent for a definition of a key typed event.
      Specified by:
      keyTyped in interface KeyListener
    • keyReleased

      public void keyReleased(KeyEvent e)
      Invoked when a key has been released. See the class description for KeyEvent for a definition of a key released event.
      Specified by:
      keyReleased in interface KeyListener
    • mouseClicked

      public void mouseClicked(MouseEvent e)
      Invoked when the mouse button has been clicked (pressed and released) on a component.
      Specified by:
      mouseClicked in interface MouseListener
    • mousePressed

      public void mousePressed(MouseEvent e)
      Invoked when a mouse button has been pressed on a component.
      Specified by:
      mousePressed in interface MouseListener
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      Invoked when a mouse button has been released on a component.
      Specified by:
      mouseReleased in interface MouseListener
    • mouseEntered

      public void mouseEntered(MouseEvent e)
      Invoked when the mouse enters a component.
      Specified by:
      mouseEntered in interface MouseListener
    • mouseExited

      public void mouseExited(MouseEvent e)
      Invoked when the mouse exits a component.
      Specified by:
      mouseExited in interface MouseListener
    • mouseDragged

      public void mouseDragged(MouseEvent e)
      Invoked when a mouse button is pressed on a component and then dragged. MOUSE_DRAGGED events will continue to be delivered to the component where the drag originated until the mouse button is released (regardless of whether the mouse position is within the bounds of the component).

      Due to platform-dependent Drag and Drop implementations, MOUSE_DRAGGED events may not be delivered during a native Drag and Drop operation.

      Specified by:
      mouseDragged in interface MouseMotionListener
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      Invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.
      Specified by:
      mouseMoved in interface MouseMotionListener
    • showTerminal

      public static void showTerminal(InputStream is, OutputStream os, boolean closeStreamsOnClose, JComponent parent, String portType, String portName) throws IOException
      Create a modal Terminal dialog window for the specified pair of streams
      Parameters:
      is - InputStream to read from remote connection
      os - OutputStream to write user keyboard input to remote connection
      closeStreamsOnClose - boolean true if streams should be closed when dialog window is closed
      parent - JComponent widget that is the windowing system parent of this created dialog window
      portType - String of port type that should be displayed in the dialog window title bar
      portName - String of specific port instance name that should be displayed in the dialog window title bar
      Throws:
      IOException - if terminal could not accept streams or streams could not be closed successfully when the dialog was dismissed