Merge commit '7a455971fed716123933d0f685a0d6eebcf3282b'
[nikiroo-utils.git] / src / jexer / demos / Demo8.java
index a74b7a9717f0c9dae0a0ac8c1818affea8b4a9b4..19fe5ff52649fffcc7a1ad64f3fccc0dc686db6f 100644 (file)
  */
 package jexer.demos;
 
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.text.MessageFormat;
 import java.util.ResourceBundle;
 
 import jexer.TApplication;
-import jexer.TPanel;
-import jexer.TSplitPane;
-import jexer.TTerminalWidget;
-import jexer.TText;
-import jexer.TWindow;
-import jexer.layout.BoxLayoutManager;
-import jexer.menu.TMenu;
+import jexer.backend.*;
+import jexer.demos.DemoApplication;
+import jexer.net.TelnetServerSocket;
+
 
 /**
- * This class shows off TSplitPane.
+ * This class shows off the use of MultiBackend and MultiScreen.
  */
 public class Demo8 {
 
@@ -49,25 +49,6 @@ public class Demo8 {
      */
     private static final ResourceBundle i18n = ResourceBundle.getBundle(Demo8.class.getName());
 
-    // ------------------------------------------------------------------------
-    // Constants --------------------------------------------------------------
-    // ------------------------------------------------------------------------
-
-    private static String TEXT =
-"This is an example of a reflowable text field.  Some example text follows.\n" +
-"\n" +
-"Notice that some menu items should be disabled when this window has focus.\n" +
-"\n" +
-"This library implements a text-based windowing system loosely " +
-"reminiscent of Borland's [Turbo " +
-"Vision](http://en.wikipedia.org/wiki/Turbo_Vision) library.  For those " +
-"wishing to use the actual C++ Turbo Vision library, see [Sergio " +
-"Sigala's updated version](http://tvision.sourceforge.net/) that runs " +
-"on many more platforms.\n" +
-"\n" +
-"This library is licensed MIT.  See the file LICENSE for the full license " +
-"for the details.\n";
-
     // ------------------------------------------------------------------------
     // Demo8 ------------------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -77,51 +58,97 @@ public class Demo8 {
      *
      * @param args Command line arguments
      */
-    public static void main(final String [] args) throws Exception {
-        // This demo will build everything "from the outside".
-
-        // Swing is the default backend on Windows unless explicitly
-        // overridden by jexer.Swing.
-        TApplication.BackendType backendType = TApplication.BackendType.XTERM;
-        if (System.getProperty("os.name").startsWith("Windows")) {
-            backendType = TApplication.BackendType.SWING;
-        }
-        if (System.getProperty("os.name").startsWith("Mac")) {
-            backendType = TApplication.BackendType.SWING;
-        }
-        if (System.getProperty("jexer.Swing") != null) {
-            if (System.getProperty("jexer.Swing", "false").equals("true")) {
-                backendType = TApplication.BackendType.SWING;
-            } else {
-                backendType = TApplication.BackendType.XTERM;
+    public static void main(final String [] args) {
+        ServerSocket server = null;
+        try {
+
+            /*
+             * In this demo we will create a headless application that anyone
+             * can telnet to.
+             */
+
+            /*
+             * Check the arguments for the port to listen on.
+             */
+            if (args.length == 0) {
+                System.err.println(i18n.getString("usageString"));
+                return;
+            }
+            int port = Integer.parseInt(args[0]);
+
+            /*
+             * We create a headless screen and use it to establish a
+             * MultiBackend.
+             */
+            HeadlessBackend headlessBackend = new HeadlessBackend();
+            MultiBackend multiBackend = new MultiBackend(headlessBackend);
+
+            /*
+             * Now we create the shared application (a standard demo) and
+             * spin it up.
+             */
+            DemoApplication demoApp = new DemoApplication(multiBackend);
+            (new Thread(demoApp)).start();
+            multiBackend.setListener(demoApp);
+
+            /*
+             * Fire up the telnet server.
+             */
+            server = new TelnetServerSocket(port);
+            while (demoApp.isRunning()) {
+                Socket socket = server.accept();
+                System.out.println(MessageFormat.
+                    format(i18n.getString("newConnection"), socket));
+
+                ECMA48Backend ecmaBackend = new ECMA48Backend(demoApp,
+                    socket.getInputStream(),
+                    socket.getOutputStream());
+
+                /*
+                 * Add this screen to the MultiBackend, and at this point we
+                 * have the telnet client able to use the shared demo
+                 * application.
+                 */
+                multiBackend.addBackend(ecmaBackend);
+
+                /*
+                 * Emit the connection information from telnet.
+                 */
+                Thread.sleep(500);
+                System.out.println(MessageFormat.
+                    format(i18n.getString("terminal"),
+                    ((jexer.net.TelnetInputStream) socket.getInputStream()).
+                        getTerminalType()));
+                System.out.println(MessageFormat.
+                    format(i18n.getString("username"),
+                    ((jexer.net.TelnetInputStream) socket.getInputStream()).
+                        getUsername()));
+                System.out.println(MessageFormat.
+                    format(i18n.getString("language"),
+                    ((jexer.net.TelnetInputStream) socket.getInputStream()).
+                        getLanguage()));
+
+            } // while (demoApp.isRunning())
+
+            /*
+             * When the application exits, kill all of the connections too.
+             */
+            multiBackend.shutdown();
+            server.close();
+
+            System.out.println(i18n.getString("exitMain"));
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (server != null) {
+                try {
+                    server.close();
+                } catch (Exception e) {
+                    // SQUASH
+                }
             }
         }
-
-        // For this demo, let's disable the status bar.
-        System.setProperty("jexer.hideStatusBar", "true");
-
-        TApplication app = new TApplication(backendType);
-        app.addToolMenu();
-        app.addFileMenu();
-        TWindow window = new TWindow(app, i18n.getString("windowTitle"),
-            60, 22);
-
-        TMenu paneMenu = app.addMenu(i18n.getString("paneMenu"));
-        paneMenu.addDefaultItem(TMenu.MID_SPLIT_VERTICAL, true);
-        paneMenu.addDefaultItem(TMenu.MID_SPLIT_HORIZONTAL, true);
-
-        TSplitPane pane = window.addSplitPane(0, 0, window.getWidth() - 2,
-            window.getHeight() - 2, true);
-
-        // pane.setLeft(new TText(null, TEXT, 0, 0, 10, 10));
-        // pane.setRight(new TText(null, TEXT, 0, 0, 10, 10));
-
-        // For this demo, let's require ptypipe
-        System.setProperty("jexer.TTerminal.ptypipe", "true");
-        pane.setLeft(new TTerminalWidget(null, 0, 0));
-        pane.setRight(new TTerminalWidget(null, 0, 0));
-
-        app.run();
     }
 
 }