Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[nikiroo-utils.git] / src / jexer / demos / Demo5.java
index 8763aa1f5193d47acf3d5e676fa70631e245a33d..e63abc1b848c37d8a9ea7b06346eefe875af7f31 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"),
@@ -31,6 +31,7 @@ package jexer.demos;
 import java.awt.Font;
 import java.awt.event.WindowEvent;
 import java.awt.event.WindowListener;
+import java.util.ResourceBundle;
 import javax.swing.JFrame;
 import javax.swing.JPanel;
 import javax.swing.JSplitPane;
@@ -44,6 +45,15 @@ import jexer.backend.SwingBackend;
  */
 public class Demo5 implements WindowListener {
 
+    /**
+     * Translated strings.
+     */
+    private static final ResourceBundle i18n = ResourceBundle.getBundle(Demo5.class.getName());
+
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * The first demo application instance.
      */
@@ -54,6 +64,10 @@ public class Demo5 implements WindowListener {
      */
     DemoApplication app2 = null;
 
+    // ------------------------------------------------------------------------
+    // WindowListener ---------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Pass window events into the event queue.
      *
@@ -122,34 +136,64 @@ public class Demo5 implements WindowListener {
         // Ignore
     }
 
+    // ------------------------------------------------------------------------
+    // Demo5 ------------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
     /**
      * Run two demo applications in separate panes.
      */
     private void addApplications() {
 
-        // Spin up the frame
-        JFrame frame = new JFrame();
-        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
-        frame.addWindowListener(this);
-
-        // Create two panels with two applications, each with a different
-        // font size.
+        /*
+         * In this demo we will create two swing panels with two
+         * independently running applications, each with a different font
+         * size.
+         */
+
+        /*
+         * First we create a panel to put it on.  We need this to pass to
+         * SwingBackend's constructor, so that it knows not to create a new
+         * frame.
+         */
         JPanel app1Panel = new JPanel();
-        SwingBackend app1Backend = new SwingBackend(app1Panel, new Object(),
+
+        /*
+         * Next, we create the Swing backend.  The "listener" (second
+         * argument, set to null) is what the backend wakes up on every event
+         * received.  Typically this is the TApplication.  TApplication sets
+         * it in its constructor, so we can pass null here and be fine.
+         */
+        SwingBackend app1Backend = new SwingBackend(app1Panel, null,
             80, 25, 16);
+        // Now that we have the backend, construct the TApplication.
         app1 = new DemoApplication(app1Backend);
-        app1Backend.setListener(app1);
 
+        /*
+         * The second panel is the same sequence, except that we also change
+         * the font from the default Terminus to JVM monospaced.
+         */
         JPanel app2Panel = new JPanel();
-        SwingBackend app2Backend = new SwingBackend(app2Panel, new Object(),
+        SwingBackend app2Backend = new SwingBackend(app2Panel, null,
             80, 25, 18);
         app2 = new DemoApplication(app2Backend);
         Font font = new Font(Font.MONOSPACED, Font.PLAIN, 18);
         app2Backend.setFont(font);
-        app2Backend.setListener(app2);
+
+        /*
+         * Now that the applications are ready, spin them off on their
+         * threads.
+         */
         (new Thread(app1)).start();
         (new Thread(app2)).start();
 
+        /*
+         * The rest of this is standard Swing.  Set up a frame, a split pane,
+         * put each of the panels on it, and make it visible.
+         */
+        JFrame frame = new JFrame();
+        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
+        frame.addWindowListener(this);
         JSplitPane mainPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
             app1Panel, app2Panel);
         mainPane.setOneTouchExpandable(true);
@@ -158,7 +202,7 @@ public class Demo5 implements WindowListener {
         mainPane.setBorder(null);
         frame.setContentPane(mainPane);
 
-        frame.setTitle("Two Jexer Apps In One Swing UI");
+        frame.setTitle(i18n.getString("frameTitle"));
         frame.setSize(1000, 640);
         frame.setVisible(true);
     }