1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package javax.microedition.lcdui;
25
26 public class Gauge extends Item
27 {
28 static int HEIGHT = 15;
29
30 int value;
31 int maxValue;
32 boolean interactive;
33 public static final int INDEFINITE = -1;
34 public static final int CONTINUOUS_IDLE = 0;
35 public static final int INCREMENTAL_IDLE = 1;
36 public static final int CONTINUOUS_RUNNING = 2;
37 public static final int INCREMENTAL_UPDATING = 3;
38
39
40
41 private int indefiniteFrame;
42 private static final int IDEFINITE_FRAMES = 4;
43
44 static int PAINT_TIMEOUT = 500;
45
46
47 int prefWidth, prefHeight;
48
49 public Gauge(String label, boolean interactive, int maxValue, int initialValue)
50 {
51 super(label);
52
53 this.interactive = interactive;
54
55 setMaxValue(maxValue);
56 setValue(initialValue);
57 }
58
59
60 public void setValue(int value)
61 {
62 if (hasIndefiniteRange()) {
63 if (value != Gauge.CONTINUOUS_IDLE &&
64 value != Gauge.CONTINUOUS_RUNNING &&
65 value != Gauge.INCREMENTAL_IDLE &&
66 value != Gauge.INCREMENTAL_UPDATING) {
67 throw new IllegalArgumentException();
68 } else {
69
70
71 if (value == Gauge.INCREMENTAL_UPDATING &&
72 this.value == Gauge.INCREMENTAL_UPDATING) {
73 updateIndefiniteFrame();
74 } else {
75 this.value = value;
76 repaint();
77 }
78 }
79 } else {
80 if (value < 0) {
81 value = 0;
82 }
83 if (value > maxValue) {
84 value = maxValue;
85 }
86
87 this.value = value;
88 repaint();
89 }
90 }
91
92
93 public int getValue()
94 {
95 return value;
96 }
97
98
99 public void setMaxValue(int maxValue)
100 {
101 if (maxValue > 0) {
102 this.maxValue = maxValue;
103 setValue(getValue());
104 } else {
105 if (isInteractive()) {
106 throw new IllegalArgumentException();
107 } else if (maxValue != Gauge.INDEFINITE) {
108 throw new IllegalArgumentException();
109 } else {
110
111
112
113 if (this.maxValue == Gauge.INDEFINITE) {
114 return;
115 } else {
116 this.maxValue = Gauge.INDEFINITE;
117 this.value = Gauge.INCREMENTAL_IDLE;
118 repaint();
119 }
120 }
121 }
122 }
123
124
125 public int getMaxValue()
126 {
127 return maxValue;
128 }
129
130
131 public boolean isInteractive()
132 {
133 return interactive;
134 }
135
136
137
138 boolean hasIndefiniteRange() {
139 return ((!isInteractive()) &&
140 (getMaxValue() == Gauge.INDEFINITE));
141 }
142
143
144 void updateIndefiniteFrame() {
145 if (hasIndefiniteRange() &&
146 ((getValue() == Gauge.CONTINUOUS_RUNNING) ||
147 (getValue() == Gauge.INCREMENTAL_UPDATING))) {
148 if (indefiniteFrame+1 < Gauge.IDEFINITE_FRAMES)
149 indefiniteFrame++;
150 else
151 indefiniteFrame = 0;
152 repaint();
153 }
154 }
155
156
157 int getHeight()
158 {
159 return super.getHeight() + HEIGHT;
160 }
161
162
163 boolean isFocusable()
164 {
165 return interactive;
166 }
167
168
169 void keyPressed(int keyCode)
170 {
171 if (Display.getGameAction(keyCode) == Canvas.LEFT && value > 0) {
172 value--;
173 repaint();
174 } else if (Display.getGameAction(keyCode) == Canvas.RIGHT && value < maxValue) {
175 value++;
176 repaint();
177 }
178 }
179
180
181 int paint(Graphics g)
182 {
183 super.paintContent(g);
184
185 g.translate(0, super.getHeight());
186
187 if (hasFocus()) {
188 g.drawRect(2, 2, owner.getWidth() - 5, HEIGHT - 5);
189 }
190 if (hasIndefiniteRange()) {
191 if (getValue() == Gauge.CONTINUOUS_IDLE ||
192 getValue() == Gauge.INCREMENTAL_IDLE) {
193 int width = owner.getWidth() - 9;
194 g.drawRect(4, 4, width, HEIGHT - 9);
195 } else {
196 int width = ((owner.getWidth() - 8) << 1) / Gauge.IDEFINITE_FRAMES;
197 int offset = (width >>> 1) * indefiniteFrame;
198 int width2 = 0;
199 if (offset + width > (owner.getWidth() - 8)) {
200 width2 = offset + width - (owner.getWidth() - 8);
201 width -= width2;
202
203 }
204 g.fillRect(4 + offset, 4, width, HEIGHT - 8);
205 if (width2 != 0) {
206 g.fillRect(4, 4, width2, HEIGHT - 8);
207 }
208 }
209 } else {
210 int width = (owner.getWidth() - 8) * value / maxValue;
211 g.fillRect(4, 4, width, HEIGHT - 8);
212 }
213
214 g.translate(0, -super.getHeight());
215 return getHeight();
216 }
217
218
219 int traverse(int gameKeyCode, int top, int bottom, boolean action)
220 {
221 if (gameKeyCode == Canvas.UP) {
222 if (top > 0) {
223 return -top;
224 } else {
225 return Item.OUTOFITEM;
226 }
227 }
228 if (gameKeyCode == Canvas.DOWN) {
229 if (getHeight() > bottom) {
230 return getHeight() - bottom;
231 } else {
232 return Item.OUTOFITEM;
233 }
234 }
235
236 return 0;
237 }
238
239
240
241 public void setPreferredSize(int w, int h) {
242 Screen owner = this.getOwner();
243
244 if (owner != null && owner instanceof Alert)
245 return;
246 else
247 super.setPreferredSize(w, h);
248 }
249
250 public void setLayout(int layout) {
251 if (owner != null && owner instanceof Alert)
252 return;
253 else
254 super.setLayout(layout);
255 }
256
257 public void setLabel(String label) {
258 if (owner != null && owner instanceof Alert)
259 return;
260 else
261 super.setLabel(label);
262 }
263
264 public void addCommand(Command cmd) {
265 if (owner != null && owner instanceof Alert)
266 return;
267 else
268 super.addCommand(cmd);
269 }
270
271 public void setDefaultCommand(Command cmd) {
272 if (owner != null && owner instanceof Alert)
273 return;
274 else
275 super.setDefaultCommand(cmd);
276 }
277
278 public void setItemCommandListener(ItemCommandListener l) {
279 if (owner != null && owner instanceof Alert)
280 return;
281 else
282 super.setItemCommandListener(l);
283 }
284 }