#18 move to event-driven main loop
[nikiroo-utils.git] / src / jexer / demos / DemoMainWindow.java
index 5d74d7df94ae7198d83760572edb358c32201eb3..209bd1350ccef2f4a2d7464dcd0d7b2c126d86f2 100644 (file)
@@ -3,7 +3,7 @@
  *
  * The MIT License (MIT)
  *
- * Copyright (C) 2016 Kevin Lamonte
+ * Copyright (C) 2017 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.demos;
 
+import java.io.*;
+
 import jexer.*;
+import jexer.event.*;
+import static jexer.TCommand.*;
+import static jexer.TKeypress.*;
 
 /**
  * This is the main "demo" application window.  It makes use of the TTimer,
@@ -75,21 +80,19 @@ public class DemoMainWindow extends TWindow {
     private DemoMainWindow(final TApplication parent, final int flags) {
         // Construct a demo window.  X and Y don't matter because it will be
         // centered on screen.
-        super(parent, "Demo Window", 0, 0, 60, 24, flags);
+        super(parent, "Demo Window", 0, 0, 64, 23, flags);
 
         int row = 1;
 
         // Add some widgets
-        if (!isModal()) {
-            addLabel("Message Boxes", 1, row);
-            addButton("&MessageBoxes", 35, row,
-                new TAction() {
-                    public void DO() {
-                        new DemoMsgBoxWindow(getApplication());
-                    }
+        addLabel("Message Boxes", 1, row);
+        addButton("&MessageBoxes", 35, row,
+            new TAction() {
+                public void DO() {
+                    new DemoMsgBoxWindow(getApplication());
                 }
-            );
-        }
+            }
+        );
         row += 2;
 
         addLabel("Open me as modal", 1, row);
@@ -100,93 +103,87 @@ public class DemoMainWindow extends TWindow {
                 }
             }
         );
+        row += 2;
 
+        addLabel("Text fields", 1, row);
+        addButton("Field&s", 35, row,
+            new TAction() {
+                public void DO() {
+                    new DemoTextFieldWindow(getApplication());
+                }
+            }
+        );
         row += 2;
 
-        addLabel("Variable-width text field:", 1, row);
-        addField(35, row++, 15, false, "Field text");
-        addLabel("Fixed-width text field:", 1, row);
-        addField(35, row++, 15, true);
-        addLabel("Variable-width password:", 1, row);
-        addPasswordField(35, row++, 15, false);
-        addLabel("Fixed-width password:", 1, row);
-        addPasswordField(35, row++, 15, true, "hunter2");
-        row += 1;
-
-        if (!isModal()) {
-            addLabel("Radio buttons and checkboxes", 1, row);
-            addButton("&Checkboxes", 35, row,
-                new TAction() {
-                    public void DO() {
-                        new DemoCheckboxWindow(getApplication());
-                    }
+        addLabel("Radio buttons and checkboxes", 1, row);
+        addButton("&Checkboxes", 35, row,
+            new TAction() {
+                public void DO() {
+                    new DemoCheckboxWindow(getApplication());
                 }
-            );
-        }
+            }
+        );
         row += 2;
 
-        /*
-        if (!isModal()) {
-            addLabel("Editor window", 1, row);
-            addButton("Edito&r", 35, row,
-                {
-                    new TEditor(application, 0, 0, 60, 15);
+        addLabel("Editor window", 1, row);
+        addButton("&1 Widget", 35, row,
+            new TAction() {
+                public void DO() {
+                    new DemoEditorWindow(getApplication());
                 }
-            );
-        }
+            }
+        );
+        addButton("&2 Window", 48, row,
+            new TAction() {
+                public void DO() {
+                    new TEditorWindow(getApplication());
+                }
+            }
+        );
         row += 2;
-         */
-
-        if (!isModal()) {
-            addLabel("Text areas", 1, row);
-            addButton("&Text", 35, row,
-                new TAction() {
-                    public void DO() {
-                        new DemoTextWindow(getApplication());
-                    }
+
+        addLabel("Text areas", 1, row);
+        addButton("&Text", 35, row,
+            new TAction() {
+                public void DO() {
+                    new DemoTextWindow(getApplication());
                 }
-            );
-        }
+            }
+        );
         row += 2;
 
-        if (!isModal()) {
-            addLabel("Tree views", 1, row);
-            addButton("Tree&View", 35, row,
-                new TAction() {
-                    public void DO() {
-                        try {
-                            new DemoTreeViewWindow(getApplication());
-                        } catch (Exception e) {
-                            e.printStackTrace();
-                        }
+        addLabel("Tree views", 1, row);
+        addButton("Tree&View", 35, row,
+            new TAction() {
+                public void DO() {
+                    try {
+                        new DemoTreeViewWindow(getApplication());
+                    } catch (Exception e) {
+                        e.printStackTrace();
                     }
                 }
-            );
-        }
+            }
+        );
         row += 2;
 
-        if (!isModal()) {
-            addLabel("Terminal", 1, row);
-            addButton("Termi&nal", 35, row,
-                new TAction() {
-                    public void DO() {
-                        getApplication().openTerminal(0, 0);
-                    }
+        addLabel("Terminal", 1, row);
+        addButton("Termi&nal", 35, row,
+            new TAction() {
+                public void DO() {
+                    getApplication().openTerminal(0, 0);
                 }
-            );
-        }
+            }
+        );
         row += 2;
 
-        if (!isModal()) {
-            addLabel("Color editor", 1, row);
-            addButton("Co&lors", 35, row,
-                new TAction() {
-                    public void DO() {
-                        new TEditColorThemeWindow(getApplication());
-                    }
+        addLabel("Color editor", 1, row);
+        addButton("Co&lors", 35, row,
+            new TAction() {
+                public void DO() {
+                    new TEditColorThemeWindow(getApplication());
                 }
-            );
-        }
+            }
+        );
         row += 2;
 
         progressBar = addProgressBar(1, row, 22, 0);
@@ -200,10 +197,48 @@ public class DemoMainWindow extends TWindow {
                     timerLabel.setWidth(timerLabel.getLabel().length());
                     if (timerI < 100) {
                         timerI++;
+                    } else {
+                        timer.setRecurring(false);
                     }
                     progressBar.setValue(timerI);
                 }
             }
         );
+
+        statusBar = newStatusBar("Demo Main Window");
+        statusBar.addShortcutKeypress(kbF1, cmHelp, "Help");
+        statusBar.addShortcutKeypress(kbF2, cmShell, "Shell");
+        statusBar.addShortcutKeypress(kbF3, cmOpen, "Open");
+        statusBar.addShortcutKeypress(kbF10, cmExit, "Exit");
+    }
+
+    /**
+     * Method that subclasses can override to handle posted command events.
+     *
+     * @param command command event
+     */
+    @Override
+    public void onCommand(final TCommandEvent command) {
+        if (command.equals(cmOpen)) {
+            try {
+                String filename = fileOpenBox(".");
+                if (filename != null) {
+                    try {
+                        new TEditorWindow(getApplication(), new File(filename));
+                    } catch (IOException e) {
+                        messageBox("Error", "Error reading file: " +
+                            e.getMessage());
+                    }
+                }
+            } catch (IOException e) {
+                messageBox("Error", "Error opening file dialog: " +
+                    e.getMessage());
+            }
+            return;
+        }
+
+        // Didn't handle it, let children get it instead
+        super.onCommand(command);
     }
+
 }