Commit | Line | Data |
---|---|---|
daa4106c | 1 | /* |
d502a0e9 KL |
2 | * Jexer - Java Text User Interface |
3 | * | |
e16dda65 | 4 | * The MIT License (MIT) |
d502a0e9 | 5 | * |
a69ed767 | 6 | * Copyright (C) 2019 Kevin Lamonte |
d502a0e9 | 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: | |
d502a0e9 | 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. | |
d502a0e9 | 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. | |
d502a0e9 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 | * TProgressBar implements a simple progress bar. | |
36 | */ | |
051e2913 | 37 | public class TProgressBar extends TWidget { |
d502a0e9 | 38 | |
d36057df KL |
39 | // ------------------------------------------------------------------------ |
40 | // Variables -------------------------------------------------------------- | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
d502a0e9 KL |
43 | /** |
44 | * Value that corresponds to 0% progress. | |
45 | */ | |
46 | private int minValue = 0; | |
47 | ||
d502a0e9 KL |
48 | /** |
49 | * Value that corresponds to 100% progress. | |
50 | */ | |
51 | private int maxValue = 100; | |
52 | ||
d502a0e9 KL |
53 | /** |
54 | * Current value of the progress. | |
55 | */ | |
56 | private int value = 0; | |
57 | ||
d36057df KL |
58 | // ------------------------------------------------------------------------ |
59 | // Constructors ----------------------------------------------------------- | |
60 | // ------------------------------------------------------------------------ | |
d502a0e9 KL |
61 | |
62 | /** | |
63 | * Public constructor. | |
64 | * | |
65 | * @param parent parent widget | |
66 | * @param x column relative to parent | |
67 | * @param y row relative to parent | |
68 | * @param width width of progress bar | |
69 | * @param value initial value of percent complete | |
70 | */ | |
71 | public TProgressBar(final TWidget parent, final int x, final int y, | |
72 | final int width, final int value) { | |
73 | ||
74 | // Set parent and window | |
a83fea2b | 75 | super(parent, false, x, y, width, 1); |
d502a0e9 | 76 | |
d502a0e9 KL |
77 | this.value = value; |
78 | } | |
79 | ||
d36057df KL |
80 | // ------------------------------------------------------------------------ |
81 | // Event handlers --------------------------------------------------------- | |
82 | // ------------------------------------------------------------------------ | |
83 | ||
84 | ||
85 | // ------------------------------------------------------------------------ | |
86 | // TWidget ---------------------------------------------------------------- | |
87 | // ------------------------------------------------------------------------ | |
88 | ||
d502a0e9 KL |
89 | /** |
90 | * Draw a static progress bar. | |
91 | */ | |
92 | @Override | |
93 | public void draw() { | |
94 | CellAttributes completeColor = getTheme().getColor("tprogressbar.complete"); | |
95 | CellAttributes incompleteColor = getTheme().getColor("tprogressbar.incomplete"); | |
96 | ||
97 | float progress = ((float)value - minValue) / ((float)maxValue - minValue); | |
98 | int progressInt = (int)(progress * 100); | |
99 | int progressUnit = 100 / (getWidth() - 2); | |
100 | ||
a69ed767 | 101 | putCharXY(0, 0, GraphicsChars.CP437[0xC3], incompleteColor); |
d502a0e9 KL |
102 | for (int i = 0; i < getWidth() - 2; i++) { |
103 | float iProgress = (float)i / (getWidth() - 2); | |
104 | int iProgressInt = (int)(iProgress * 100); | |
105 | if (iProgressInt <= progressInt - progressUnit) { | |
a69ed767 | 106 | putCharXY(i + 1, 0, GraphicsChars.BOX, completeColor); |
d502a0e9 | 107 | } else { |
a69ed767 | 108 | putCharXY(i + 1, 0, GraphicsChars.SINGLE_BAR, incompleteColor); |
d502a0e9 KL |
109 | } |
110 | } | |
111 | if (value >= maxValue) { | |
a69ed767 | 112 | putCharXY(getWidth() - 2, 0, GraphicsChars.BOX, completeColor); |
d502a0e9 | 113 | } else { |
a69ed767 | 114 | putCharXY(getWidth() - 2, 0, GraphicsChars.SINGLE_BAR, |
d502a0e9 KL |
115 | incompleteColor); |
116 | } | |
a69ed767 | 117 | putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0xB4], |
d502a0e9 KL |
118 | incompleteColor); |
119 | } | |
120 | ||
d36057df KL |
121 | // ------------------------------------------------------------------------ |
122 | // TProgressBar ----------------------------------------------------------- | |
123 | // ------------------------------------------------------------------------ | |
124 | ||
125 | /** | |
126 | * Get the value that corresponds to 0% progress. | |
127 | * | |
128 | * @return the value that corresponds to 0% progress | |
129 | */ | |
130 | public int getMinValue() { | |
131 | return minValue; | |
132 | } | |
133 | ||
134 | /** | |
135 | * Set the value that corresponds to 0% progress. | |
136 | * | |
137 | * @param minValue the value that corresponds to 0% progress | |
138 | */ | |
139 | public void setMinValue(final int minValue) { | |
140 | this.minValue = minValue; | |
141 | } | |
142 | ||
143 | /** | |
144 | * Get the value that corresponds to 100% progress. | |
145 | * | |
146 | * @return the value that corresponds to 100% progress | |
147 | */ | |
148 | public int getMaxValue() { | |
149 | return maxValue; | |
150 | } | |
151 | ||
152 | /** | |
153 | * Set the value that corresponds to 100% progress. | |
154 | * | |
155 | * @param maxValue the value that corresponds to 100% progress | |
156 | */ | |
157 | public void setMaxValue(final int maxValue) { | |
158 | this.maxValue = maxValue; | |
159 | } | |
160 | ||
161 | /** | |
162 | * Get the current value of the progress. | |
163 | * | |
164 | * @return the current value of the progress | |
165 | */ | |
166 | public int getValue() { | |
167 | return value; | |
168 | } | |
169 | ||
170 | /** | |
171 | * Set the current value of the progress. | |
172 | * | |
173 | * @param value the current value of the progress | |
174 | */ | |
175 | public void setValue(final int value) { | |
176 | this.value = value; | |
177 | } | |
178 | ||
d502a0e9 | 179 | } |