#46 StretchLayoutManager working
[fanfix.git] / src / jexer / TProgressBar.java
1 /*
2 * Jexer - Java Text User Interface
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (C) 2019 Kevin Lamonte
7 *
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:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
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.
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 */
37 public class TProgressBar extends TWidget {
38
39 // ------------------------------------------------------------------------
40 // Variables --------------------------------------------------------------
41 // ------------------------------------------------------------------------
42
43 /**
44 * Value that corresponds to 0% progress.
45 */
46 private int minValue = 0;
47
48 /**
49 * Value that corresponds to 100% progress.
50 */
51 private int maxValue = 100;
52
53 /**
54 * Current value of the progress.
55 */
56 private int value = 0;
57
58 // ------------------------------------------------------------------------
59 // Constructors -----------------------------------------------------------
60 // ------------------------------------------------------------------------
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
75 super(parent, false, x, y, width, 1);
76
77 this.value = value;
78 }
79
80 // ------------------------------------------------------------------------
81 // Event handlers ---------------------------------------------------------
82 // ------------------------------------------------------------------------
83
84
85 // ------------------------------------------------------------------------
86 // TWidget ----------------------------------------------------------------
87 // ------------------------------------------------------------------------
88
89 /**
90 * Override TWidget's height: we can only set height at construction
91 * time.
92 *
93 * @param height new widget height (ignored)
94 */
95 @Override
96 public void setHeight(final int height) {
97 // Do nothing
98 }
99
100 /**
101 * Draw a static progress bar.
102 */
103 @Override
104 public void draw() {
105 CellAttributes completeColor = getTheme().getColor("tprogressbar.complete");
106 CellAttributes incompleteColor = getTheme().getColor("tprogressbar.incomplete");
107
108 float progress = ((float)value - minValue) / ((float)maxValue - minValue);
109 int progressInt = (int)(progress * 100);
110 int progressUnit = 100 / (getWidth() - 2);
111
112 putCharXY(0, 0, GraphicsChars.CP437[0xC3], incompleteColor);
113 for (int i = 0; i < getWidth() - 2; i++) {
114 float iProgress = (float)i / (getWidth() - 2);
115 int iProgressInt = (int)(iProgress * 100);
116 if (iProgressInt <= progressInt - progressUnit) {
117 putCharXY(i + 1, 0, GraphicsChars.BOX, completeColor);
118 } else {
119 putCharXY(i + 1, 0, GraphicsChars.SINGLE_BAR, incompleteColor);
120 }
121 }
122 if (value >= maxValue) {
123 putCharXY(getWidth() - 2, 0, GraphicsChars.BOX, completeColor);
124 } else {
125 putCharXY(getWidth() - 2, 0, GraphicsChars.SINGLE_BAR,
126 incompleteColor);
127 }
128 putCharXY(getWidth() - 1, 0, GraphicsChars.CP437[0xB4],
129 incompleteColor);
130 }
131
132 // ------------------------------------------------------------------------
133 // TProgressBar -----------------------------------------------------------
134 // ------------------------------------------------------------------------
135
136 /**
137 * Get the value that corresponds to 0% progress.
138 *
139 * @return the value that corresponds to 0% progress
140 */
141 public int getMinValue() {
142 return minValue;
143 }
144
145 /**
146 * Set the value that corresponds to 0% progress.
147 *
148 * @param minValue the value that corresponds to 0% progress
149 */
150 public void setMinValue(final int minValue) {
151 this.minValue = minValue;
152 }
153
154 /**
155 * Get the value that corresponds to 100% progress.
156 *
157 * @return the value that corresponds to 100% progress
158 */
159 public int getMaxValue() {
160 return maxValue;
161 }
162
163 /**
164 * Set the value that corresponds to 100% progress.
165 *
166 * @param maxValue the value that corresponds to 100% progress
167 */
168 public void setMaxValue(final int maxValue) {
169 this.maxValue = maxValue;
170 }
171
172 /**
173 * Get the current value of the progress.
174 *
175 * @return the current value of the progress
176 */
177 public int getValue() {
178 return value;
179 }
180
181 /**
182 * Set the current value of the progress.
183 *
184 * @param value the current value of the progress
185 */
186 public void setValue(final int value) {
187 this.value = value;
188 }
189
190 }