Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / jexer / demos / DemoMainWindow.java
index eef25e29aee47926195631f4ae7eea2bf517d22a..8f77448b29f45c65cd67e52c38268e0564ad1e0d 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 java.io.*;
+import java.io.File;
+import java.io.IOException;
+import java.text.MessageFormat;
+import java.util.ResourceBundle;
 
-import jexer.*;
-import jexer.event.*;
+import jexer.TAction;
+import jexer.TApplication;
+import jexer.TEditColorThemeWindow;
+import jexer.TEditorWindow;
+import jexer.TLabel;
+import jexer.TProgressBar;
+import jexer.TTableWindow;
+import jexer.TTimer;
+import jexer.TWidget;
+import jexer.TWindow;
+import jexer.event.TCommandEvent;
+import jexer.layout.StretchLayoutManager;
 import static jexer.TCommand.*;
 import static jexer.TKeypress.*;
 
@@ -41,6 +54,11 @@ import static jexer.TKeypress.*;
  */
 public class DemoMainWindow extends TWindow {
 
+    /**
+     * Translated strings.
+     */
+    private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName());
+
     // ------------------------------------------------------------------------
     // Variables --------------------------------------------------------------
     // ------------------------------------------------------------------------
@@ -48,7 +66,12 @@ public class DemoMainWindow extends TWindow {
     /**
      * Timer that increments a number.
      */
-    private TTimer timer;
+    private TTimer timer1;
+
+    /**
+     * Timer that increments a number.
+     */
+    private TTimer timer2;
 
     /**
      * Timer label is updated with timer ticks.
@@ -59,13 +82,25 @@ public class DemoMainWindow extends TWindow {
      * Timer increment used by the timer loop.  Has to be at class scope so
      * that it can be accessed by the anonymous TAction class.
      */
-    int timerI = 0;
+    int timer1I = 0;
+
+    /**
+     * Timer increment used by the timer loop.  Has to be at class scope so
+     * that it can be accessed by the anonymous TAction class.
+     */
+    int timer2I = 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 progressBar1;
 
     /**
      * 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;
+    TProgressBar progressBar2;
 
     // ------------------------------------------------------------------------
     // Constructors -----------------------------------------------------------
@@ -89,13 +124,16 @@ 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, 64, 23, flags);
+        super(parent, i18n.getString("windowTitle"), 0, 0, 64, 23, flags);
+
+        setLayoutManager(new StretchLayoutManager(getWidth() - 2,
+                getHeight() - 2));
 
         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());
@@ -104,8 +142,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);
@@ -114,8 +152,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());
@@ -124,25 +162,25 @@ 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("&1 Widget", 35, row,
+        addLabel(i18n.getString("editorLabel"), 1, row);
+        addButton(i18n.getString("editorButton1"), 35, row,
             new TAction() {
                 public void DO() {
                     new DemoEditorWindow(getApplication());
                 }
             }
         );
-        addButton("&2 Window", 48, row,
+        addButton(i18n.getString("editorButton2"), 48, row,
             new TAction() {
                 public void DO() {
                     new TEditorWindow(getApplication());
@@ -151,8 +189,8 @@ public class DemoMainWindow extends TWindow {
         );
         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());
@@ -161,8 +199,27 @@ public class DemoMainWindow extends TWindow {
         );
         row += 2;
 
-        addLabel("Tree views", 1, row);
-        addButton("Tree&View", 35, row,
+        addLabel(i18n.getString("ttableLabel"), 1, row);
+        addButton(i18n.getString("ttableButton1"), 35, row,
+            new TAction() {
+                public void DO() {
+                    new DemoTableWindow(getApplication(),
+                        i18n.getString("tableWidgetDemo"));
+                }
+            }
+        );
+        addButton(i18n.getString("ttableButton2"), 48, row,
+            new TAction() {
+                public void DO() {
+                    new TTableWindow(getApplication(),
+                        i18n.getString("tableDemo"));
+                }
+            }
+        );
+        row += 2;
+
+        addLabel(i18n.getString("treeViewLabel"), 1, row);
+        addButton(i18n.getString("treeViewButton"), 35, row,
             new TAction() {
                 public void DO() {
                     try {
@@ -175,8 +232,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);
@@ -185,40 +242,82 @@ 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());
                 }
             }
         );
-        row += 2;
 
-        progressBar = addProgressBar(1, row, 22, 0);
+        row = 15;
+        progressBar1 = addProgressBar(48, row, 12, 0);
         row++;
-        timerLabel = addLabel("Timer", 1, row);
-        timer = getApplication().addTimer(250, true,
+        timerLabel = addLabel(i18n.getString("timerLabel"), 48, row);
+        timer1 = getApplication().addTimer(250, true,
             new TAction() {
 
                 public void DO() {
-                    timerLabel.setLabel(String.format("Timer: %d", timerI));
+                    timerLabel.setLabel(String.format(i18n.
+                            getString("timerText"), timer1I));
                     timerLabel.setWidth(timerLabel.getLabel().length());
-                    if (timerI < 100) {
-                        timerI++;
+                    if (timer1I < 100) {
+                        timer1I++;
+                    } else {
+                        timer1.setRecurring(false);
+                    }
+                    progressBar1.setValue(timer1I);
+                }
+            }
+        );
+
+        row += 2;
+        progressBar2 = addProgressBar(48, row, 12, 0);
+        progressBar2.setLeftBorderChar('\u255e');
+        progressBar2.setRightBorderChar('\u2561');
+        progressBar2.setCompletedChar('\u2592');
+        progressBar2.setRemainingChar('\u2550');
+        row++;
+        timer2 = getApplication().addTimer(125, true,
+            new TAction() {
+
+                public void DO() {
+                    if (timer2I < 100) {
+                        timer2I++;
                     } else {
-                        timer.setRecurring(false);
+                        timer2.setRecurring(false);
                     }
-                    progressBar.setValue(timerI);
+                    progressBar2.setValue(timer2I);
                 }
             }
         );
 
-        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");
+        /*
+        addButton("Exception", 35, row + 3,
+            new TAction() {
+                public void DO() {
+                    try {
+                        throw new RuntimeException("FUBAR'd!");
+                    } catch (Exception e) {
+                        new jexer.TExceptionDialog(getApplication(), e);
+                    }
+                }
+            }
+        );
+         */
+
+        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"));
     }
 
     // ------------------------------------------------------------------------
@@ -232,7 +331,8 @@ public class DemoMainWindow extends TWindow {
      */
     @Override
     public void onClose() {
-        getApplication().removeTimer(timer);
+        getApplication().removeTimer(timer1);
+        getApplication().removeTimer(timer2);
     }
 
     /**
@@ -247,15 +347,18 @@ public class DemoMainWindow extends TWindow {
                 String filename = fileOpenBox(".");
                 if (filename != null) {
                     try {
-                        new TEditorWindow(getApplication(), new File(filename));
+                        new TEditorWindow(getApplication(),
+                            new File(filename));
                     } catch (IOException e) {
-                        messageBox("Error", "Error reading file: " +
-                            e.getMessage());
+                        messageBox(i18n.getString("errorTitle"),
+                            MessageFormat.format(i18n.
+                                getString("errorReadingFile"), e.getMessage()));
                     }
                 }
             } catch (IOException e) {
-                messageBox("Error", "Error opening file dialog: " +
-                    e.getMessage());
+                        messageBox(i18n.getString("errorTitle"),
+                            MessageFormat.format(i18n.
+                                getString("errorOpeningFile"), e.getMessage()));
             }
             return;
         }