Class DefaultTerminalFactory

  • All Implemented Interfaces:
    TerminalFactory

    public class DefaultTerminalFactory
    extends java.lang.Object
    implements TerminalFactory
    This TerminalFactory implementation uses a simple auto-detection mechanism for figuring out which terminal implementation to create based on characteristics of the system the program is running on.

    Note that for all systems with a graphical environment present, the SwingTerminalFrame will be chosen. You can suppress this by calling setForceTextTerminal(true) on this factory.

    • Field Detail

      • DEFAULT_OUTPUT_STREAM

        private static final java.io.OutputStream DEFAULT_OUTPUT_STREAM
      • DEFAULT_INPUT_STREAM

        private static final java.io.InputStream DEFAULT_INPUT_STREAM
      • DEFAULT_CHARSET

        private static final java.nio.charset.Charset DEFAULT_CHARSET
      • outputStream

        private final java.io.OutputStream outputStream
      • inputStream

        private final java.io.InputStream inputStream
      • charset

        private final java.nio.charset.Charset charset
      • initialTerminalSize

        private TerminalSize initialTerminalSize
      • forceTextTerminal

        private boolean forceTextTerminal
      • preferTerminalEmulator

        private boolean preferTerminalEmulator
      • forceAWTOverSwing

        private boolean forceAWTOverSwing
      • telnetPort

        private int telnetPort
      • inputTimeout

        private int inputTimeout
      • title

        private java.lang.String title
      • autoOpenTerminalFrame

        private boolean autoOpenTerminalFrame
    • Constructor Detail

      • DefaultTerminalFactory

        public DefaultTerminalFactory()
        Creates a new DefaultTerminalFactory with all properties set to their defaults
      • DefaultTerminalFactory

        public DefaultTerminalFactory​(java.io.OutputStream outputStream,
                                      java.io.InputStream inputStream,
                                      java.nio.charset.Charset charset)
        Creates a new DefaultTerminalFactory with I/O and character set options customisable.
        Parameters:
        outputStream - Output stream to use for text-based Terminal implementations
        inputStream - Input stream to use for text-based Terminal implementations
        charset - Character set to assume the client is using
    • Method Detail

      • createTerminal

        public Terminal createTerminal()
                                throws java.io.IOException
        Description copied from interface: TerminalFactory
        Instantiates a Terminal according to the factory implementation.
        Specified by:
        createTerminal in interface TerminalFactory
        Returns:
        Terminal implementation
        Throws:
        java.io.IOException - If there was an I/O error with the underlying input/output system
      • createHeadlessTerminal

        public Terminal createHeadlessTerminal()
                                        throws java.io.IOException
        Instantiates a Terminal according to the factory implementation with the exception that preferTerminalEmulator is always ignored. You may want to use this method when using tools that rely on AOT compilation such as Graal native-image to ensure AWT/Swing code paths are not hit.
        Returns:
        Terminal implementation
        Throws:
        java.io.IOException - If there was an I/O error with the underlying input/output system
      • createTerminalEmulator

        public Terminal createTerminalEmulator()
        Creates a new terminal emulator window which will be either Swing-based or AWT-based depending on what is available on the system
        Returns:
        New terminal emulator exposed as a Terminal interface
      • createTelnetTerminal

        public TelnetTerminal createTelnetTerminal()
        Creates a new TelnetTerminal Note: a telnetPort should have been set with setTelnetPort(), otherwise creation of TelnetTerminal will most likely fail.
        Returns:
        New terminal emulator exposed as a Terminal interface
      • isAwtHeadless

        private boolean isAwtHeadless()
      • hasSwing

        private boolean hasSwing()
      • makeWindowVisible

        private void makeWindowVisible​(Terminal terminal)
      • setInitialTerminalSize

        public DefaultTerminalFactory setInitialTerminalSize​(TerminalSize initialTerminalSize)
        Sets a hint to the TerminalFactory of what size to use when creating the terminal. Most terminals are not created on request but for example the SwingTerminal and SwingTerminalFrame are and this value will be passed down on creation.
        Parameters:
        initialTerminalSize - Size (in rows and columns) of the newly created terminal
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setForceTextTerminal

        public DefaultTerminalFactory setForceTextTerminal​(boolean forceTextTerminal)
        Controls whether a text-based Terminal shall be created even if the system supports a graphical environment
        Parameters:
        forceTextTerminal - If true, will always create a text-based Terminal
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setPreferTerminalEmulator

        public DefaultTerminalFactory setPreferTerminalEmulator​(boolean preferTerminalEmulator)
        Controls whether a Swing or AWT TerminalFrame shall be preferred if the system has both a Console and a graphical environment
        Parameters:
        preferTerminalEmulator - If true, will prefer creating a graphical terminal emulator
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setUnixTerminalCtrlCBehaviour

        public DefaultTerminalFactory setUnixTerminalCtrlCBehaviour​(UnixLikeTerminal.CtrlCBehaviour unixTerminalCtrlCBehaviour)
        Sets the default CTRL-C behavior to use for all UnixTerminal objects created by this factory. You can use this to tell Lanterna to trap CTRL-C instead of exiting the application. Non-UNIX terminals are not affected by this.
        Parameters:
        unixTerminalCtrlCBehaviour - CTRL-C behavior to use for UnixTerminal:s
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setTelnetPort

        public DefaultTerminalFactory setTelnetPort​(int telnetPort)
        Primarily for debugging applications with mouse interactions: If no Console is available (e.g. from within an IDE), then fall back to TelnetTerminal on specified port. If both a non-null mouseCapture mode and a positive telnetPort are specified, then as long as Swing/AWT Terminal emulators do not support MouseCapturing, a TelnetTerminal will be preferred over the graphical Emulators.
        Parameters:
        telnetPort - the TCP/IP port on which to eventually wait for a connection. A value less or equal 0 disables creation of a TelnetTerminal. Note, that ports less than 1024 typically require system privileges to listen on.
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setInputTimeout

        public DefaultTerminalFactory setInputTimeout​(int inputTimeout)
        Only for StreamBasedTerminals: After seeing e.g. an Escape (but nothing else yet), wait up to the specified number of time units for more bytes to make up a complete sequence. This may be necessary on slow channels, or if some client terminal sends each byte of a sequence in its own TCP packet.
        Parameters:
        inputTimeout - how long to wait for possible completions of sequences. units are of a 1/4 second, so e.g. 12 would wait up to 3 seconds.
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setForceAWTOverSwing

        public DefaultTerminalFactory setForceAWTOverSwing​(boolean forceAWTOverSwing)
        Normally when a graphical terminal emulator is created by the factory, it will create a SwingTerminalFrame unless Swing is not present in the system. Setting this property to true will make it create an AWTTerminalFrame even if Swing is present
        Parameters:
        forceAWTOverSwing - If true, will always create an AWTTerminalFrame over a SwingTerminalFrame if asked to create a graphical terminal emulator
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setAutoOpenTerminalEmulatorWindow

        public DefaultTerminalFactory setAutoOpenTerminalEmulatorWindow​(boolean autoOpenTerminalFrame)
        Controls whether a SwingTerminalFrame shall be automatically shown (.setVisible(true)) immediately after creation. If false, you will manually need to call .setVisible(true) on the JFrame to actually see the terminal window. Default for this value is true.
        Parameters:
        autoOpenTerminalFrame - Automatically open SwingTerminalFrame after creation
        Returns:
        Itself
      • setTerminalEmulatorTitle

        public DefaultTerminalFactory setTerminalEmulatorTitle​(java.lang.String title)
        Sets the title to use on created SwingTerminalFrames created by this factory
        Parameters:
        title - Title to use on created SwingTerminalFrames created by this factory
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setTerminalEmulatorFrameAutoCloseTrigger

        public DefaultTerminalFactory setTerminalEmulatorFrameAutoCloseTrigger​(TerminalEmulatorAutoCloseTrigger autoCloseTrigger)
        Sets the auto-close trigger to use on created SwingTerminalFrames created by this factory. This will reset any previous triggers. If called with null, all triggers are cleared.
        Parameters:
        autoCloseTrigger - Auto-close trigger to use on created SwingTerminalFrames created by this factory, or null to clear all existing triggers
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • addTerminalEmulatorFrameAutoCloseTrigger

        public DefaultTerminalFactory addTerminalEmulatorFrameAutoCloseTrigger​(TerminalEmulatorAutoCloseTrigger autoCloseTrigger)
        Adds an auto-close trigger to use on created SwingTerminalFrames created by this factory
        Parameters:
        autoCloseTrigger - Auto-close trigger to add to the created SwingTerminalFrames created by this factory
        Returns:
        Reference to itself, so multiple calls can be chained
      • setTerminalEmulatorColorConfiguration

        public DefaultTerminalFactory setTerminalEmulatorColorConfiguration​(TerminalEmulatorColorConfiguration colorConfiguration)
        Sets the color configuration to use on created SwingTerminalFrames created by this factory
        Parameters:
        colorConfiguration - Color configuration to use on created SwingTerminalFrames created by this factory
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setTerminalEmulatorDeviceConfiguration

        public DefaultTerminalFactory setTerminalEmulatorDeviceConfiguration​(TerminalEmulatorDeviceConfiguration deviceConfiguration)
        Sets the device configuration to use on created SwingTerminalFrames created by this factory
        Parameters:
        deviceConfiguration - Device configuration to use on created SwingTerminalFrames created by this factory
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setTerminalEmulatorFontConfiguration

        public DefaultTerminalFactory setTerminalEmulatorFontConfiguration​(AWTTerminalFontConfiguration fontConfiguration)
        Sets the font configuration to use on created SwingTerminalFrames created by this factory
        Parameters:
        fontConfiguration - Font configuration to use on created SwingTerminalFrames created by this factory
        Returns:
        Reference to itself, so multiple .set-calls can be chained
      • setMouseCaptureMode

        public DefaultTerminalFactory setMouseCaptureMode​(MouseCaptureMode mouseCaptureMode)
        Sets the mouse capture mode the terminal should use. Please note that this is an extension which isn't widely supported! If both a non-null mouseCapture mode and a positive telnetPort are specified, then as long as Swing/AWT Terminal emulators do not support MouseCapturing, a TelnetTerminal will be preferred over the graphical Emulators.
        Parameters:
        mouseCaptureMode - Capture mode for mouse interactions
        Returns:
        Itself
      • createWindowsTerminal

        private Terminal createWindowsTerminal()
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • createCygwinTerminal

        private Terminal createCygwinTerminal​(java.io.OutputStream outputStream,
                                              java.io.InputStream inputStream,
                                              java.nio.charset.Charset charset)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • createUnixTerminal

        private Terminal createUnixTerminal​(java.io.OutputStream outputStream,
                                            java.io.InputStream inputStream,
                                            java.nio.charset.Charset charset)
                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • isOperatingSystemWindows

        private static boolean isOperatingSystemWindows()
        Detects whether the running platform is Windows* by looking at the operating system name system property