0be2b98ce3ff73e0a9668b621f2639df791f0c1d
[nikiroo-utils.git] / src / jexer / TPasswordField.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
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 */
29 package jexer;
30
31 import jexer.bits.CellAttributes;
32 import jexer.bits.StringUtils;
33
34 /**
35 * TPasswordField implements an editable text field that displays
36 * stars/asterisks when it is not active.
37 */
38 public class TPasswordField extends TField {
39
40 // ------------------------------------------------------------------------
41 // Constructors -----------------------------------------------------------
42 // ------------------------------------------------------------------------
43
44 /**
45 * Public constructor.
46 *
47 * @param parent parent widget
48 * @param x column relative to parent
49 * @param y row relative to parent
50 * @param width visible text width
51 * @param fixed if true, the text cannot exceed the display width
52 */
53 public TPasswordField(final TWidget parent, final int x, final int y,
54 final int width, final boolean fixed) {
55
56 this(parent, x, y, width, fixed, "", null, null);
57 }
58
59 /**
60 * Public constructor.
61 *
62 * @param parent parent widget
63 * @param x column relative to parent
64 * @param y row relative to parent
65 * @param width visible text width
66 * @param fixed if true, the text cannot exceed the display width
67 * @param text initial text, default is empty string
68 */
69 public TPasswordField(final TWidget parent, final int x, final int y,
70 final int width, final boolean fixed, final String text) {
71
72 this(parent, x, y, width, fixed, text, null, null);
73 }
74
75 /**
76 * Public constructor.
77 *
78 * @param parent parent widget
79 * @param x column relative to parent
80 * @param y row relative to parent
81 * @param width visible text width
82 * @param fixed if true, the text cannot exceed the display width
83 * @param text initial text, default is empty string
84 * @param enterAction function to call when enter key is pressed
85 * @param updateAction function to call when the text is updated
86 */
87 public TPasswordField(final TWidget parent, final int x, final int y,
88 final int width, final boolean fixed, final String text,
89 final TAction enterAction, final TAction updateAction) {
90
91 // Set parent and window
92 super(parent, x, y, width, fixed, text, enterAction, updateAction);
93 }
94
95 // ------------------------------------------------------------------------
96 // TField -----------------------------------------------------------------
97 // ------------------------------------------------------------------------
98
99 /**
100 * Draw the text field.
101 */
102 @Override
103 public void draw() {
104 CellAttributes fieldColor;
105
106 boolean showStars = false;
107 if (isAbsoluteActive()) {
108 fieldColor = getTheme().getColor("tfield.active");
109 } else {
110 fieldColor = getTheme().getColor("tfield.inactive");
111 showStars = true;
112 }
113
114 int end = windowStart + getWidth();
115 if (end > StringUtils.width(text)) {
116 end = StringUtils.width(text);
117 }
118
119 hLineXY(0, 0, getWidth(), backgroundChar, fieldColor);
120 if (showStars) {
121 hLineXY(0, 0, getWidth() - 2, '*', fieldColor);
122 } else {
123 putStringXY(0, 0, text.substring(screenToTextPosition(windowStart),
124 screenToTextPosition(end)), fieldColor);
125 }
126
127 // Fix the cursor, it will be rendered by TApplication.drawAll().
128 updateCursor();
129 }
130
131 }