Prep for 2019 release
[fanfix.git] / src / jexer / demos / DemoMainWindow.java
index 598ac7f0ce92f889d56a78a6b42d7f41675dad02..1ae35f0b7dab058f42fdef8e7b6946241d00b6fe 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.demos;
 
-import jexer.*;
+import java.io.File;
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.ResourceBundle;
+
+import jexer.TAction;
+import jexer.TApplication;
+import jexer.TEditColorThemeWindow;
+import jexer.TEditorWindow;
+import jexer.TLabel;
+import jexer.TProgressBar;
+import jexer.TTimer;
+import jexer.TWidget;
+import jexer.TWindow;
+import jexer.event.TCommandEvent;
 import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
@@ -38,21 +55,51 @@ import static jexer.TKeypress.*;
  */
 public class DemoMainWindow extends TWindow {
 
-    // Timer that increments a number.
+    /**
+     * Translated strings.
+     */
+    private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName());
+
+    // ------------------------------------------------------------------------
+    // Variables --------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * Timer that increments a number.
+     */
     private TTimer timer;
 
-    // Timer label is updated with timer ticks.
+    /**
+     * Timer label is updated with timer ticks.
+     */
     TLabel timerLabel;
 
     /**
-     * We need to override onClose so that the timer will no longer be called
-     * after we close the window.  TTimers currently are completely unaware
-     * of the rest of the UI classes.
+     * Timer increment used by the timer loop.  Has to be at class scope so
+     * that it can be accessed by the anonymous TAction class.
      */
-    @Override
-    public void onClose() {
-        getApplication().removeTimer(timer);
-    }
+    int timerI = 0;
+
+    /**
+     * Progress bar used by the timer loop.  Has to be at class scope so that
+     * it can be accessed by the anonymous TAction class.
+     */
+    TProgressBar progressBar;
+
+    /**
+     * Day of week label is updated with TSpinner clicks.
+     */
+    TLabel dayOfWeekLabel;
+
+    /**
+     * Day of week to demonstrate TSpinner.  Has to be at class scope so that
+     * it can be accessed by the anonymous TAction class.
+     */
+    GregorianCalendar calendar = new GregorianCalendar();
+
+    // ------------------------------------------------------------------------
+    // Constructors -----------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
      * Construct demo window.  It will be centered on screen.
@@ -63,11 +110,6 @@ public class DemoMainWindow extends TWindow {
         this(parent, CENTERED | RESIZABLE);
     }
 
-    // These are used by the timer loop.  They have to be at class scope so
-    // that they can be accessed by the anonymous TAction class.
-    int timerI = 0;
-    TProgressBar progressBar;
-
     /**
      * Constructor.
      *
@@ -77,13 +119,13 @@ 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, 23, flags);
+        super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags);
 
         int row = 1;
 
         // Add some widgets
-        addLabel("Message Boxes", 1, row);
-        addButton("&MessageBoxes", 35, row,
+        addLabel(i18n.getString("messageBoxLabel"), 1, row);
+        TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row,
             new TAction() {
                 public void DO() {
                     new DemoMsgBoxWindow(getApplication());
@@ -92,8 +134,8 @@ public class DemoMainWindow extends TWindow {
         );
         row += 2;
 
-        addLabel("Open me as modal", 1, row);
-        addButton("W&indow", 35, row,
+        addLabel(i18n.getString("openModalLabel"), 1, row);
+        addButton(i18n.getString("openModalButton"), 35, row,
             new TAction() {
                 public void DO() {
                     new DemoMainWindow(getApplication(), MODAL);
@@ -102,8 +144,8 @@ public class DemoMainWindow extends TWindow {
         );
         row += 2;
 
-        addLabel("Text fields", 1, row);
-        addButton("Field&s", 35, row,
+        addLabel(i18n.getString("textFieldLabel"), 1, row);
+        addButton(i18n.getString("textFieldButton"), 35, row,
             new TAction() {
                 public void DO() {
                     new DemoTextFieldWindow(getApplication());
@@ -112,28 +154,35 @@ public class DemoMainWindow extends TWindow {
         );
         row += 2;
 
-        addLabel("Radio buttons and checkboxes", 1, row);
-        addButton("&Checkboxes", 35, row,
+        addLabel(i18n.getString("radioButtonLabel"), 1, row);
+        addButton(i18n.getString("radioButtonButton"), 35, row,
             new TAction() {
                 public void DO() {
-                    new DemoCheckboxWindow(getApplication());
+                    new DemoCheckBoxWindow(getApplication());
                 }
             }
         );
         row += 2;
 
-        addLabel("Editor window", 1, row);
-        addButton("Edito&r", 35, row,
+        addLabel(i18n.getString("editorLabel"), 1, row);
+        addButton(i18n.getString("editorButton1"), 35, row,
             new TAction() {
                 public void DO() {
                     new DemoEditorWindow(getApplication());
                 }
             }
         );
+        addButton(i18n.getString("editorButton2"), 48, row,
+            new TAction() {
+                public void DO() {
+                    new TEditorWindow(getApplication());
+                }
+            }
+        );
         row += 2;
 
-        addLabel("Text areas", 1, row);
-        addButton("&Text", 35, row,
+        addLabel(i18n.getString("textAreaLabel"), 1, row);
+        addButton(i18n.getString("textAreaButton"), 35, row,
             new TAction() {
                 public void DO() {
                     new DemoTextWindow(getApplication());
@@ -142,8 +191,8 @@ public class DemoMainWindow extends TWindow {
         );
         row += 2;
 
-        addLabel("Tree views", 1, row);
-        addButton("Tree&View", 35, row,
+        addLabel(i18n.getString("treeViewLabel"), 1, row);
+        addButton(i18n.getString("treeViewButton"), 35, row,
             new TAction() {
                 public void DO() {
                     try {
@@ -156,8 +205,8 @@ public class DemoMainWindow extends TWindow {
         );
         row += 2;
 
-        addLabel("Terminal", 1, row);
-        addButton("Termi&nal", 35, row,
+        addLabel(i18n.getString("terminalLabel"), 1, row);
+        addButton(i18n.getString("terminalButton"), 35, row,
             new TAction() {
                 public void DO() {
                     getApplication().openTerminal(0, 0);
@@ -166,8 +215,8 @@ public class DemoMainWindow extends TWindow {
         );
         row += 2;
 
-        addLabel("Color editor", 1, row);
-        addButton("Co&lors", 35, row,
+        addLabel(i18n.getString("colorEditorLabel"), 1, row);
+        addButton(i18n.getString("colorEditorButton"), 35, row,
             new TAction() {
                 public void DO() {
                     new TEditColorThemeWindow(getApplication());
@@ -178,25 +227,108 @@ public class DemoMainWindow extends TWindow {
 
         progressBar = addProgressBar(1, row, 22, 0);
         row++;
-        timerLabel = addLabel("Timer", 1, row);
+        timerLabel = addLabel(i18n.getString("timerLabel"), 1, row);
         timer = getApplication().addTimer(250, true,
             new TAction() {
 
                 public void DO() {
-                    timerLabel.setLabel(String.format("Timer: %d", timerI));
+                    timerLabel.setLabel(String.format(i18n.
+                            getString("timerText"), timerI));
                     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");
+        dayOfWeekLabel = addLabel("Wednesday-", 35, row - 1, "tmenu", false);
+        dayOfWeekLabel.setLabel(String.format("%-10s",
+                calendar.getDisplayName(Calendar.DAY_OF_WEEK,
+                    Calendar.LONG, Locale.getDefault())));
+
+        addSpinner(35 + dayOfWeekLabel.getWidth(), row - 1,
+            new TAction() {
+                public void DO() {
+                    calendar.add(Calendar.DAY_OF_WEEK, 1);
+                    dayOfWeekLabel.setLabel(String.format("%-10s",
+                            calendar.getDisplayName(
+                            Calendar.DAY_OF_WEEK, Calendar.LONG,
+                            Locale.getDefault())));
+                }
+            },
+            new TAction() {
+                public void DO() {
+                    calendar.add(Calendar.DAY_OF_WEEK, -1);
+                    dayOfWeekLabel.setLabel(String.format("%-10s",
+                            calendar.getDisplayName(
+                            Calendar.DAY_OF_WEEK, Calendar.LONG,
+                            Locale.getDefault())));
+                }
+            }
+        );
+
+
+        activate(first);
+
+        statusBar = newStatusBar(i18n.getString("statusBar"));
+        statusBar.addShortcutKeypress(kbF1, cmHelp,
+            i18n.getString("statusBarHelp"));
+        statusBar.addShortcutKeypress(kbF2, cmShell,
+            i18n.getString("statusBarShell"));
+        statusBar.addShortcutKeypress(kbF3, cmOpen,
+            i18n.getString("statusBarOpen"));
+        statusBar.addShortcutKeypress(kbF10, cmExit,
+            i18n.getString("statusBarExit"));
     }
+
+    // ------------------------------------------------------------------------
+    // TWindow ----------------------------------------------------------------
+    // ------------------------------------------------------------------------
+
+    /**
+     * We need to override onClose so that the timer will no longer be called
+     * after we close the window.  TTimers currently are completely unaware
+     * of the rest of the UI classes.
+     */
+    @Override
+    public void onClose() {
+        getApplication().removeTimer(timer);
+    }
+
+    /**
+     * 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(i18n.getString("errorTitle"),
+                            MessageFormat.format(i18n.
+                                getString("errorReadingFile"), e.getMessage()));
+                    }
+                }
+            } catch (IOException e) {
+                        messageBox(i18n.getString("errorTitle"),
+                            MessageFormat.format(i18n.
+                                getString("errorOpeningFile"), e.getMessage()));
+            }
+            return;
+        }
+
+        // Didn't handle it, let children get it instead
+        super.onCommand(command);
+    }
+
 }