Commit | Line | Data |
---|---|---|
d502a0e9 KL |
1 | /** |
2 | * Jexer - Java Text User Interface | |
3 | * | |
4 | * License: LGPLv3 or later | |
5 | * | |
6 | * This module is licensed under the GNU Lesser General Public License | |
7 | * Version 3. Please see the file "COPYING" in this directory for more | |
8 | * information about the GNU Lesser General Public License Version 3. | |
9 | * | |
10 | * Copyright (C) 2015 Kevin Lamonte | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU Lesser General Public License | |
14 | * as published by the Free Software Foundation; either version 3 of | |
15 | * the License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU Lesser General Public | |
23 | * License along with this program; if not, see | |
24 | * http://www.gnu.org/licenses/, or write to the Free Software | |
25 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
26 | * 02110-1301 USA | |
27 | * | |
28 | * @author Kevin Lamonte [kevin.lamonte@gmail.com] | |
29 | * @version 1 | |
30 | */ | |
31 | package jexer; | |
32 | ||
33 | import jexer.bits.CellAttributes; | |
34 | import jexer.bits.GraphicsChars; | |
35 | ||
36 | /** | |
37 | * TProgressBar implements a simple progress bar. | |
38 | */ | |
39 | public final class TProgressBar extends TWidget { | |
40 | ||
41 | /** | |
42 | * Value that corresponds to 0% progress. | |
43 | */ | |
44 | private int minValue = 0; | |
45 | ||
46 | /** | |
47 | * Get the value that corresponds to 0% progress. | |
48 | * | |
49 | * @return the value that corresponds to 0% progress | |
50 | */ | |
51 | public int getMinValue() { | |
52 | return minValue; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Set the value that corresponds to 0% progress. | |
57 | * | |
58 | * @param minValue the value that corresponds to 0% progress | |
59 | */ | |
60 | public void setMinValue(final int minValue) { | |
61 | this.minValue = minValue; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Value that corresponds to 100% progress. | |
66 | */ | |
67 | private int maxValue = 100; | |
68 | ||
69 | /** | |
70 | * Get the value that corresponds to 100% progress. | |
71 | * | |
72 | * @return the value that corresponds to 100% progress | |
73 | */ | |
74 | public int getMaxValue() { | |
75 | return maxValue; | |
76 | } | |
77 | ||
78 | /** | |
79 | * Set the value that corresponds to 100% progress. | |
80 | * | |
81 | * @param maxValue the value that corresponds to 100% progress | |
82 | */ | |
83 | public void setMaxValue(final int maxValue) { | |
84 | this.maxValue = maxValue; | |
85 | } | |
86 | ||
87 | /** | |
88 | * Current value of the progress. | |
89 | */ | |
90 | private int value = 0; | |
91 | ||
92 | /** | |
93 | * Get the current value of the progress. | |
94 | * | |
95 | * @return the current value of the progress | |
96 | */ | |
97 | public int getValue() { | |
98 | return value; | |
99 | } | |
100 | ||
101 | /** | |
102 | * Set the current value of the progress. | |
103 | * | |
104 | * @param value the current value of the progress | |
105 | */ | |
106 | public void setValue(final int value) { | |
107 | this.value = value; | |
108 | } | |
109 | ||
110 | /** | |
111 | * Public constructor. | |
112 | * | |
113 | * @param parent parent widget | |
114 | * @param x column relative to parent | |
115 | * @param y row relative to parent | |
116 | * @param width width of progress bar | |
117 | * @param value initial value of percent complete | |
118 | */ | |
119 | public TProgressBar(final TWidget parent, final int x, final int y, | |
120 | final int width, final int value) { | |
121 | ||
122 | // Set parent and window | |
123 | super(parent, false); | |
124 | ||
125 | setX(x); | |
126 | setY(y); | |
127 | setHeight(1); | |
128 | setWidth(width); | |
129 | this.value = value; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Draw a static progress bar. | |
134 | */ | |
135 | @Override | |
136 | public void draw() { | |
137 | CellAttributes completeColor = getTheme().getColor("tprogressbar.complete"); | |
138 | CellAttributes incompleteColor = getTheme().getColor("tprogressbar.incomplete"); | |
139 | ||
140 | float progress = ((float)value - minValue) / ((float)maxValue - minValue); | |
141 | int progressInt = (int)(progress * 100); | |
142 | int progressUnit = 100 / (getWidth() - 2); | |
143 | ||
144 | getScreen().putCharXY(0, 0, GraphicsChars.CP437[0xC3], incompleteColor); | |
145 | for (int i = 0; i < getWidth() - 2; i++) { | |
146 | float iProgress = (float)i / (getWidth() - 2); | |
147 | int iProgressInt = (int)(iProgress * 100); | |
148 | if (iProgressInt <= progressInt - progressUnit) { | |
149 | getScreen().putCharXY(i + 1, 0, GraphicsChars.BOX, | |
150 | completeColor); | |
151 | } else { | |
152 | getScreen().putCharXY(i + 1, 0, GraphicsChars.SINGLE_BAR, | |
153 | incompleteColor); | |
154 | } | |
155 | } | |
156 | if (value >= maxValue) { | |
157 | getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.BOX, | |
158 | completeColor); | |
159 | } else { | |
160 | getScreen().putCharXY(getWidth() - 2, 0, GraphicsChars.SINGLE_BAR, | |
161 | incompleteColor); | |
162 | } | |
163 | getScreen().putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0xB4], | |
164 | incompleteColor); | |
165 | } | |
166 | ||
167 | } |