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