afe4e2787162c840fba00af5114d87b9b765736b
[nikiroo-utils.git] / src / jexer / TPasswordField.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * License: LGPLv3 or later
5 *
6 * This module is licensed under the GNU Lesser General Public License
7 * Version 3. Please see the file "COPYING" in this directory for more
8 * information about the GNU Lesser General Public License Version 3.
9 *
10 * Copyright (C) 2015 Kevin Lamonte
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this program; if not, see
24 * http://www.gnu.org/licenses/, or write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
26 * 02110-1301 USA
27 *
28 * @author Kevin Lamonte [kevin.lamonte@gmail.com]
29 * @version 1
30 */
31 package jexer;
32
33 import jexer.bits.CellAttributes;
34 import jexer.bits.GraphicsChars;
35
36 /**
37 * TField implements an editable text field.
38 */
39 public final class TPasswordField extends TField {
40
41 /**
42 * Public constructor.
43 *
44 * @param parent parent widget
45 * @param x column relative to parent
46 * @param y row relative to parent
47 * @param width visible text width
48 * @param fixed if true, the text cannot exceed the display width
49 */
50 public TPasswordField(final TWidget parent, final int x, final int y,
51 final int width, final boolean fixed) {
52
53 this(parent, x, y, width, fixed, "", null, null);
54 }
55
56 /**
57 * Public constructor.
58 *
59 * @param parent parent widget
60 * @param x column relative to parent
61 * @param y row relative to parent
62 * @param width visible text width
63 * @param fixed if true, the text cannot exceed the display width
64 * @param text initial text, default is empty string
65 */
66 public TPasswordField(final TWidget parent, final int x, final int y,
67 final int width, final boolean fixed, final String text) {
68
69 this(parent, x, y, width, fixed, text, null, null);
70 }
71
72 /**
73 * Public constructor.
74 *
75 * @param parent parent widget
76 * @param x column relative to parent
77 * @param y row relative to parent
78 * @param width visible text width
79 * @param fixed if true, the text cannot exceed the display width
80 * @param text initial text, default is empty string
81 * @param enterAction function to call when enter key is pressed
82 * @param updateAction function to call when the text is updated
83 */
84 public TPasswordField(final TWidget parent, final int x, final int y,
85 final int width, final boolean fixed, final String text,
86 final TAction enterAction, final TAction updateAction) {
87
88 // Set parent and window
89 super(parent, x, y, width, fixed, text, enterAction, updateAction);
90 }
91
92 /**
93 * Draw the text field.
94 */
95 @Override
96 public void draw() {
97 CellAttributes fieldColor;
98
99 boolean showStars = false;
100 if (isAbsoluteActive()) {
101 fieldColor = getTheme().getColor("tfield.active");
102 } else {
103 fieldColor = getTheme().getColor("tfield.inactive");
104 showStars = true;
105 }
106
107 int end = windowStart + getWidth();
108 if (end > text.length()) {
109 end = text.length();
110 }
111
112 getScreen().hLineXY(0, 0, getWidth(), GraphicsChars.HATCH, fieldColor);
113 if (showStars) {
114 getScreen().hLineXY(0, 0, getWidth() - 2, '*',
115 fieldColor);
116 } else {
117 getScreen().putStringXY(0, 0, text.substring(windowStart, end),
118 fieldColor);
119 }
120
121 // Fix the cursor, it will be rendered by TApplication.drawAll().
122 updateCursor();
123 }
124
125 }