Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[nikiroo-utils.git] / src / jexer / backend / SwingBackend.java
index 876015e4d125874591dcf5da5bc32cfd694dd207..8a342b604f8d438988df943c741b91f0d54b2f06 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2017 Kevin Lamonte
+ * Copyright (C) 2019 Kevin Lamonte
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  */
 package jexer.backend;
 
-import java.util.List;
+import java.awt.Font;
 import javax.swing.JComponent;
 
-import jexer.event.TInputEvent;
-
 /**
  * This class uses standard Swing calls to handle screen, keyboard, and mouse
  * I/O.
  */
-public final class SwingBackend extends GenericBackend {
+public class SwingBackend extends GenericBackend {
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
-     * Input events are processed by this Terminal.
+     * Public constructor.  The window will be 80x25 with font size 20 pts.
      */
-    private SwingTerminal terminal;
+    public SwingBackend() {
+        this(null, 80, 25, 20);
+    }
 
     /**
      * Public constructor.  The window will be 80x25 with font size 20 pts.
@@ -54,6 +58,30 @@ public final class SwingBackend extends GenericBackend {
         this(listener, 80, 25, 20);
     }
 
+    /**
+     * Public constructor will spawn a new JFrame with font size 20 pts.
+     *
+     * @param windowWidth the number of text columns to start with
+     * @param windowHeight the number of text rows to start with
+     */
+    public SwingBackend(final int windowWidth, final int windowHeight) {
+        this(null, windowWidth, windowHeight, 20);
+    }
+
+    /**
+     * Public constructor will spawn a new JFrame.
+     *
+     * @param windowWidth the number of text columns to start with
+     * @param windowHeight the number of text rows to start with
+     * @param fontSize the size in points.  Good values to pick are: 16, 20,
+     * 22, and 24.
+     */
+    public SwingBackend(final int windowWidth, final int windowHeight,
+        final int fontSize) {
+
+        this(null, windowWidth, windowHeight, fontSize);
+    }
+
     /**
      * Public constructor will spawn a new JFrame.
      *
@@ -72,10 +100,10 @@ public final class SwingBackend extends GenericBackend {
             listener);
 
         // Hang onto the session info
-        this.sessionInfo = terminal.getSessionInfo();
+        this.sessionInfo = ((SwingTerminal) terminal).getSessionInfo();
 
         // SwingTerminal is the screen too
-        screen = terminal;
+        screen = (SwingTerminal) terminal;
     }
 
     /**
@@ -97,58 +125,43 @@ public final class SwingBackend extends GenericBackend {
             fontSize, listener);
 
         // Hang onto the session info
-        this.sessionInfo = terminal.getSessionInfo();
+        this.sessionInfo = ((SwingTerminal) terminal).getSessionInfo();
 
         // SwingTerminal is the screen too
-        screen = terminal;
+        screen = (SwingTerminal) terminal;
     }
 
-    /**
-     * Sync the logical screen to the physical device.
-     */
-    @Override
-    public void flushScreen() {
-        screen.flushPhysical();
-    }
+    // ------------------------------------------------------------------------
+    // SwingBackend -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
-     * Get keyboard, mouse, and screen resize events.
+     * Set to a new font, and resize the screen to match its dimensions.
      *
-     * @param queue list to append new events to
-     */
-    @Override
-    public void getEvents(final List<TInputEvent> queue) {
-        if (terminal.hasEvents()) {
-            terminal.getEvents(queue);
-        }
-    }
-
-    /**
-     * Close the I/O, restore the console, etc.
+     * @param font the new font
      */
-    @Override
-    public void shutdown() {
-        terminal.closeTerminal();
+    public void setFont(final Font font) {
+        ((SwingTerminal) terminal).setFont(font);
     }
 
     /**
-     * Set the window title.
+     * Get the number of millis to wait before switching the blink from
+     * visible to invisible.
      *
-     * @param title the new title
+     * @return the number of milli to wait before switching the blink from
+     * visible to invisible
      */
-    @Override
-    public void setTitle(final String title) {
-        screen.setTitle(title);
+    public long getBlinkMillis() {
+        return ((SwingTerminal) terminal).getBlinkMillis();
     }
 
     /**
-     * Set listener to a different Object.
+     * Getter for the underlying Swing component.
      *
-     * @param listener the new listening object that run() wakes up on new
-     * input
+     * @return the SwingComponent
      */
-    public void setListener(final Object listener) {
-        terminal.setListener(listener);
+    public SwingComponent getSwingComponent() {
+        return ((SwingTerminal) terminal).getSwingComponent();
     }
 
 }