resizing fixes
[fanfix.git] / src / jexer / demos / DemoTextFieldWindow.java
CommitLineData
2ce6dab2
KL
1/*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
a69ed767 6 * Copyright (C) 2019 Kevin Lamonte
2ce6dab2
KL
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 * DEALINGS IN THE SOFTWARE.
25 *
26 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
27 * @version 1
28 */
29package jexer.demos;
30
a69ed767 31import java.text.MessageFormat;
3096642b 32import java.util.Calendar;
a69ed767 33import java.util.Date;
3096642b
KL
34import java.util.GregorianCalendar;
35import java.util.Locale;
a69ed767 36import java.util.ResourceBundle;
051e2913 37
a69ed767
KL
38import jexer.TAction;
39import jexer.TApplication;
40import jexer.TCalendar;
41import jexer.TField;
3096642b 42import jexer.TLabel;
a69ed767
KL
43import jexer.TMessageBox;
44import jexer.TWindow;
d8dc8aea 45import jexer.layout.StretchLayoutManager;
2ce6dab2
KL
46import static jexer.TCommand.*;
47import static jexer.TKeypress.*;
48
49/**
50 * This window demonstates the TField and TPasswordField widgets.
51 */
52public class DemoTextFieldWindow extends TWindow {
53
a69ed767
KL
54 /**
55 * Translated strings.
56 */
57 private static final ResourceBundle i18n = ResourceBundle.getBundle(DemoTextFieldWindow.class.getName());
58
051e2913
KL
59 // ------------------------------------------------------------------------
60 // Variables --------------------------------------------------------------
61 // ------------------------------------------------------------------------
62
63 /**
64 * Calendar. Has to be at class scope so that it can be accessed by the
65 * anonymous TAction class.
66 */
67 TCalendar calendar = null;
68
3096642b
KL
69 /**
70 * Day of week label is updated with TSpinner clicks.
71 */
72 TLabel dayOfWeekLabel;
73
74 /**
75 * Day of week to demonstrate TSpinner. Has to be at class scope so that
76 * it can be accessed by the anonymous TAction class.
77 */
78 GregorianCalendar dayOfWeekCalendar = new GregorianCalendar();
79
43ad7b6c
KL
80 // ------------------------------------------------------------------------
81 // Constructors -----------------------------------------------------------
82 // ------------------------------------------------------------------------
83
2ce6dab2
KL
84 /**
85 * Constructor.
86 *
87 * @param parent the main application
88 */
89 DemoTextFieldWindow(final TApplication parent) {
90 this(parent, TWindow.CENTERED | TWindow.RESIZABLE);
91 }
92
93 /**
94 * Constructor.
95 *
96 * @param parent the main application
97 * @param flags bitmask of MODAL, CENTERED, or RESIZABLE
98 */
99 DemoTextFieldWindow(final TApplication parent, final int flags) {
100 // Construct a demo window. X and Y don't matter because it
101 // will be centered on screen.
a69ed767 102 super(parent, i18n.getString("windowTitle"), 0, 0, 60, 20, flags);
2ce6dab2 103
d8dc8aea
KL
104 setLayoutManager(new StretchLayoutManager(getWidth(), getHeight()));
105
2ce6dab2
KL
106 int row = 1;
107
a69ed767 108 addLabel(i18n.getString("textField1"), 1, row);
2ce6dab2 109 addField(35, row++, 15, false, "Field text");
a69ed767 110 addLabel(i18n.getString("textField2"), 1, row);
2ce6dab2 111 addField(35, row++, 15, true);
a69ed767 112 addLabel(i18n.getString("textField3"), 1, row);
2ce6dab2 113 addPasswordField(35, row++, 15, false);
a69ed767 114 addLabel(i18n.getString("textField4"), 1, row);
2ce6dab2 115 addPasswordField(35, row++, 15, true, "hunter2");
a69ed767 116 addLabel(i18n.getString("textField5"), 1, row);
24489803 117 TField selected = addField(35, row++, 40, false,
a69ed767 118 i18n.getString("textField6"));
2ce6dab2
KL
119 row += 1;
120
051e2913
KL
121 calendar = addCalendar(1, row++,
122 new TAction() {
123 public void DO() {
a69ed767
KL
124 getApplication().messageBox(i18n.getString("calendarTitle"),
125 MessageFormat.format(i18n.getString("calendarMessage"),
126 new Date(calendar.getValue().getTimeInMillis())),
051e2913
KL
127 TMessageBox.Type.OK);
128 }
129 }
130 );
131
3096642b
KL
132 dayOfWeekLabel = addLabel("Wednesday-", 35, row - 1, "tmenu", false);
133 dayOfWeekLabel.setLabel(String.format("%-10s",
134 dayOfWeekCalendar.getDisplayName(Calendar.DAY_OF_WEEK,
135 Calendar.LONG, Locale.getDefault())));
136
137 addSpinner(35 + dayOfWeekLabel.getWidth(), row - 1,
138 new TAction() {
139 public void DO() {
140 dayOfWeekCalendar.add(Calendar.DAY_OF_WEEK, 1);
141 dayOfWeekLabel.setLabel(String.format("%-10s",
142 dayOfWeekCalendar.getDisplayName(
143 Calendar.DAY_OF_WEEK, Calendar.LONG,
144 Locale.getDefault())));
145 }
146 },
147 new TAction() {
148 public void DO() {
149 dayOfWeekCalendar.add(Calendar.DAY_OF_WEEK, -1);
150 dayOfWeekLabel.setLabel(String.format("%-10s",
151 dayOfWeekCalendar.getDisplayName(
152 Calendar.DAY_OF_WEEK, Calendar.LONG,
153 Locale.getDefault())));
154 }
155 }
156 );
157
158
a69ed767
KL
159 addButton(i18n.getString("closeWindow"),
160 (getWidth() - 14) / 2, getHeight() - 4,
2ce6dab2
KL
161 new TAction() {
162 public void DO() {
163 getApplication().closeWindow(DemoTextFieldWindow.this);
164 }
165 }
166 );
167
24489803
KL
168 activate(selected);
169
a69ed767
KL
170 statusBar = newStatusBar(i18n.getString("statusBar"));
171 statusBar.addShortcutKeypress(kbF1, cmHelp,
172 i18n.getString("statusBarHelp"));
173 statusBar.addShortcutKeypress(kbF2, cmShell,
174 i18n.getString("statusBarShell"));
175 statusBar.addShortcutKeypress(kbF3, cmOpen,
176 i18n.getString("statusBarOpen"));
177 statusBar.addShortcutKeypress(kbF10, cmExit,
178 i18n.getString("statusBarExit"));
2ce6dab2 179 }
43ad7b6c 180
2ce6dab2 181}