X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fjexer%2Fdemos%2FDemoMainWindow.java;h=8f77448b29f45c65cd67e52c38268e0564ad1e0d;hb=12b90437b5f22c2ae6e9b9b14c3b62b60f6143e5;hp=9a834645c5aa6c6176453fc6838dc452ec2b7d6f;hpb=0d47c5460c8e9d1198928308767a63ad35f46eb8;p=fanfix.git diff --git a/src/jexer/demos/DemoMainWindow.java b/src/jexer/demos/DemoMainWindow.java index 9a83464..8f77448 100644 --- a/src/jexer/demos/DemoMainWindow.java +++ b/src/jexer/demos/DemoMainWindow.java @@ -1,36 +1,52 @@ /* * Jexer - Java Text User Interface * - * License: LGPLv3 or later + * The MIT License (MIT) * - * This module is licensed under the GNU Lesser General Public License - * Version 3. Please see the file "COPYING" in this directory for more - * information about the GNU Lesser General Public License Version 3. + * Copyright (C) 2019 Kevin Lamonte * - * Copyright (C) 2015 Kevin Lamonte + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public License - * as published by the Free Software Foundation; either version 3 of - * the License, or (at your option) any later version. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see - * http://www.gnu.org/licenses/, or write to the Free Software - * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA - * 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * @author Kevin Lamonte [kevin.lamonte@gmail.com] * @version 1 */ package jexer.demos; -import jexer.*; +import java.io.File; +import java.io.IOException; +import java.text.MessageFormat; +import java.util.ResourceBundle; + +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.*; /** * This is the main "demo" application window. It makes use of the TTimer, @@ -38,21 +54,57 @@ import jexer.*; */ public class DemoMainWindow extends TWindow { - // Timer that increments a number. - private TTimer timer; + /** + * Translated strings. + */ + private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoMainWindow.class.getName()); + + // ------------------------------------------------------------------------ + // Variables -------------------------------------------------------------- + // ------------------------------------------------------------------------ + + /** + * Timer that increments a number. + */ + private TTimer timer1; + + /** + * Timer that increments a number. + */ + private TTimer timer2; - // 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 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 progressBar2; + + // ------------------------------------------------------------------------ + // Constructors ----------------------------------------------------------- + // ------------------------------------------------------------------------ /** * Construct demo window. It will be centered on screen. @@ -63,11 +115,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,123 +124,247 @@ 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); + + setLayoutManager(new StretchLayoutManager(getWidth() - 2, + getHeight() - 2)); 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(i18n.getString("messageBoxLabel"), 1, row); + TWidget first = addButton(i18n.getString("messageBoxButton"), 35, row, + new TAction() { + public void DO() { + new DemoMsgBoxWindow(getApplication()); } - ); - } + } + ); 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); } } ); + row += 2; + addLabel(i18n.getString("textFieldLabel"), 1, row); + addButton(i18n.getString("textFieldButton"), 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"); + addLabel(i18n.getString("radioButtonLabel"), 1, row); + addButton(i18n.getString("radioButtonButton"), 35, row, + new TAction() { + public void DO() { + new DemoCheckBoxWindow(getApplication()); + } + } + ); row += 2; - if (!isModal()) { - addLabel("Radio buttons and checkboxes", 1, row); - addButton("&Checkboxes", 35, row, - new TAction() { - public void DO() { - new DemoCheckboxWindow(getApplication()); - } + 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; - /* - if (!isModal()) { - addLabel("Editor window", 1, row); - addButton("Edito&r", 35, row, - { - new TEditor(application, 0, 0, 60, 15); + addLabel(i18n.getString("textAreaLabel"), 1, row); + addButton(i18n.getString("textAreaButton"), 35, row, + new TAction() { + public void DO() { + new DemoTextWindow(getApplication()); } - ); - } + } + ); row += 2; - */ - if (!isModal()) { - addLabel("Text areas", 1, row); - addButton("&Text", 35, row, - new TAction() { - public void DO() { - new DemoTextWindow(getApplication()); - } + 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; - 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(i18n.getString("treeViewLabel"), 1, row); + addButton(i18n.getString("treeViewButton"), 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(i18n.getString("terminalLabel"), 1, row); + addButton(i18n.getString("terminalButton"), 35, row, + new TAction() { + public void DO() { + getApplication().openTerminal(0, 0); } - ); - } + } + ); row += 2; - progressBar = addProgressBar(1, row, 22, 0); + addLabel(i18n.getString("colorEditorLabel"), 1, row); + addButton(i18n.getString("colorEditorButton"), 35, row, + new TAction() { + public void DO() { + new TEditColorThemeWindow(getApplication()); + } + } + ); + + 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 { + timer2.setRecurring(false); + } + progressBar2.setValue(timer2I); + } + } + ); + + /* + addButton("Exception", 35, row + 3, + new TAction() { + public void DO() { + try { + throw new RuntimeException("FUBAR'd!"); + } catch (Exception e) { + new jexer.TExceptionDialog(getApplication(), e); } - progressBar.setValue(timerI); } } ); + */ + + 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(timer1); + getApplication().removeTimer(timer2); } + + /** + * 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); + } + }