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