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