51 /** |
51 /** |
52 * If Gauge is changed, reasons for Re-layouting. |
52 * If Gauge is changed, reasons for Re-layouting. |
53 */ |
53 */ |
54 static final int UPDATE_MAXVALUE = UPDATE_ITEM_MAX << 1; |
54 static final int UPDATE_MAXVALUE = UPDATE_ITEM_MAX << 1; |
55 static final int UPDATE_VALUE = UPDATE_ITEM_MAX << 2; |
55 static final int UPDATE_VALUE = UPDATE_ITEM_MAX << 2; |
56 |
|
57 |
56 |
58 private int maxValue; |
57 private int maxValue; |
59 private int value; |
58 private int value; |
60 private boolean interactive; |
59 private boolean interactive; |
61 private boolean isGaugeCreation; |
|
62 |
60 |
63 /** |
61 /** |
64 * Constructor. |
62 * Constructor. |
65 * |
63 * |
66 * @param name the label. |
64 * @param name the label. |
67 * @param interactive if its interactive. |
65 * @param interactive if its interactive. |
68 * @param maxVal the maximum value. |
66 * @param maxVal the maximum value. |
69 * @param initVal the initial value. |
67 * @param initVal the initial value. |
70 */ |
68 */ |
71 public Gauge(String name, boolean interactive, int maxVal, int initVal) |
69 public Gauge(String aName, boolean aInteractive, int aMaxVal, int aInitVal) |
72 { |
70 { |
73 setLabel(name); |
71 maxValue = validateMaxValue(aMaxVal, aInteractive); |
74 isGaugeCreation = true; |
72 value = validateValue(aInitVal, maxValue); |
75 this.interactive = interactive; |
73 interactive = aInteractive; |
76 setMaxValue(maxVal); |
74 setLabel(aName); |
77 setValue(initVal); |
|
78 isGaugeCreation = false; |
|
79 } |
|
80 |
|
81 /** |
|
82 * Check value validity. |
|
83 * |
|
84 * @param value the value. |
|
85 * @param maxVal the maximum value. |
|
86 * @return validated value. |
|
87 */ |
|
88 private int checkValue(int value, int maxVal) |
|
89 { |
|
90 if(maxVal == INDEFINITE) |
|
91 { |
|
92 if(isGaugeCreation) |
|
93 { |
|
94 switch(value) |
|
95 { |
|
96 case CONTINUOUS_IDLE: |
|
97 case INCREMENTAL_IDLE: |
|
98 case CONTINUOUS_RUNNING: |
|
99 case INCREMENTAL_UPDATING: |
|
100 break; |
|
101 default: |
|
102 throw new IllegalArgumentException( |
|
103 MsgRepository.GAUGE_EXCEPTION_INVALID_VALUE); |
|
104 } |
|
105 |
|
106 return value; |
|
107 } |
|
108 else |
|
109 { |
|
110 return CONTINUOUS_IDLE; |
|
111 } |
|
112 } |
|
113 else |
|
114 { |
|
115 // make sure the value is in [0, maxValue] range |
|
116 value = (value < 0 ? 0 : value); |
|
117 value = (value > maxVal ? maxVal : value); |
|
118 return value; |
|
119 } |
|
120 } |
|
121 |
|
122 /** |
|
123 * Check maximum value validity. |
|
124 * |
|
125 * @param maxVal the maximum value. |
|
126 * @param interactive is interactive. |
|
127 * @return validated maximum value. |
|
128 */ |
|
129 private static int checkMaxValue(int maxVal, boolean interactive) |
|
130 { |
|
131 if(interactive) |
|
132 { |
|
133 if(maxVal <= 0) |
|
134 { |
|
135 throw new IllegalArgumentException( |
|
136 MsgRepository.GAUGE_EXCEPTION_INVALID_MAXVALUE); |
|
137 } |
|
138 } |
|
139 return maxVal; |
|
140 } |
75 } |
141 |
76 |
142 /** |
77 /** |
143 * Sets value of this gauge. |
78 * Sets value of this gauge. |
144 * |
79 * |
145 * @param value New value. Must be between zero and maxvalue inclusive. |
80 * @param value New value. Must be between zero and maxvalue inclusive. |
146 */ |
81 */ |
147 public void setValue(int value) |
82 public void setValue(int aValue) |
148 { |
83 { |
149 // TODO: eSWT support required |
84 // TODO: eSWT support required |
150 /* |
85 /* |
151 * if Gauge state is incremental-updating and it is non-interactive and |
86 * if Gauge state is incremental-updating and it is non-interactive and |
152 * indefinite, we should update Gauge when this method is called, but |
87 * indefinite, we should update Gauge when this method is called, but |
153 * that requires eSWT extension. |
88 * that requires eSWT extension. |
154 */ |
89 */ |
155 this.value = checkValue(value, this.maxValue); |
90 value = validateValue(aValue, maxValue); |
156 updateParent(UPDATE_VALUE); |
91 updateParent(UPDATE_VALUE); |
157 } |
92 } |
158 |
93 |
159 /** |
94 /** |
160 * Get value. |
95 * Get value. |
169 /** |
104 /** |
170 * Set the maximum value. |
105 * Set the maximum value. |
171 * |
106 * |
172 * @param maxValue the maximum value. |
107 * @param maxValue the maximum value. |
173 */ |
108 */ |
174 public void setMaxValue(int maxValue) |
109 public void setMaxValue(int aMaxValue) |
175 { |
110 { |
176 this.maxValue = checkMaxValue(maxValue, interactive); |
111 int oldMaxValue = maxValue; |
177 this.value = checkValue(getValue(), this.maxValue); |
112 maxValue = validateMaxValue(aMaxValue, interactive); |
|
113 value = validateValue(getValue(), maxValue, oldMaxValue); |
178 updateParent(UPDATE_MAXVALUE); |
114 updateParent(UPDATE_MAXVALUE); |
179 } |
115 } |
180 |
116 |
181 /** |
117 /** |
182 * Get maximum value. |
118 * Get maximum value. |
194 * @return true if the Gauge is interactive. |
130 * @return true if the Gauge is interactive. |
195 */ |
131 */ |
196 public boolean isInteractive() |
132 public boolean isInteractive() |
197 { |
133 { |
198 return interactive; |
134 return interactive; |
|
135 } |
|
136 |
|
137 /** |
|
138 * Check maximum value validity. |
|
139 * |
|
140 * @param aMaxVal the maximum value. |
|
141 * @param aInteractive is interactive. |
|
142 * @return validated maximum value. |
|
143 */ |
|
144 private int validateMaxValue(int aMaxVal, boolean aInteractive) |
|
145 { |
|
146 if((!aInteractive) && (aMaxVal == INDEFINITE)) |
|
147 { |
|
148 return aMaxVal; |
|
149 } |
|
150 |
|
151 if(aMaxVal <= 0) |
|
152 { |
|
153 throw new IllegalArgumentException( |
|
154 MsgRepository.GAUGE_EXCEPTION_INVALID_MAXVALUE); |
|
155 } |
|
156 |
|
157 return aMaxVal; |
|
158 } |
|
159 |
|
160 /** |
|
161 * Check value validity. |
|
162 * |
|
163 * @param aValue the value. |
|
164 * @param aMaxVal the maximum value. |
|
165 * @return validated value. |
|
166 */ |
|
167 private int validateValue(int aValue, int aMaxVal) |
|
168 { |
|
169 if (aMaxVal == INDEFINITE) |
|
170 { |
|
171 switch (aValue) |
|
172 { |
|
173 case CONTINUOUS_IDLE: |
|
174 case INCREMENTAL_IDLE: |
|
175 case CONTINUOUS_RUNNING: |
|
176 case INCREMENTAL_UPDATING: |
|
177 break; |
|
178 default: |
|
179 throw new IllegalArgumentException(); |
|
180 } |
|
181 return aValue; |
|
182 } |
|
183 else |
|
184 { |
|
185 return clampValue(aValue, aMaxVal); |
|
186 } |
|
187 } |
|
188 |
|
189 /** |
|
190 * Check value validity. |
|
191 * |
|
192 * @param aValue the value. |
|
193 * @param aNewMaxVal the new maximum value. |
|
194 * @param aOlddMaxVal the old maximum value. |
|
195 * @return validated value. |
|
196 */ |
|
197 private int validateValue(int aValue, int aNewMaxVal, int aOlddMaxVal) |
|
198 { |
|
199 if (aNewMaxVal == INDEFINITE) |
|
200 { |
|
201 return CONTINUOUS_IDLE; |
|
202 } |
|
203 else if (aOlddMaxVal == INDEFINITE) |
|
204 { |
|
205 return 0; |
|
206 } |
|
207 else |
|
208 { |
|
209 return clampValue(aValue, aNewMaxVal); |
|
210 } |
|
211 } |
|
212 |
|
213 /** |
|
214 * Validates the value against the range. |
|
215 * |
|
216 * @param aValue the value. |
|
217 * @param aMaxVal the maximum value. |
|
218 * @return validated value. |
|
219 */ |
|
220 private static int clampValue(int aValue, int aMaxVal) |
|
221 { |
|
222 aValue = Math.min(aValue, aMaxVal); |
|
223 aValue = Math.max(aValue, 0); |
|
224 return aValue; |
|
225 } |
|
226 |
|
227 /** |
|
228 * @return if the Gauge is indefinite. |
|
229 */ |
|
230 boolean isIndefinite() |
|
231 { |
|
232 return (maxValue == INDEFINITE); |
|
233 } |
|
234 |
|
235 /* (non-Javadoc) |
|
236 * @see javax.microedition.lcdui.Item#isFocusable() |
|
237 */ |
|
238 boolean isFocusable() |
|
239 { |
|
240 return (isInteractive() || (getNumCommands() > 0)); |
|
241 } |
|
242 |
|
243 /** |
|
244 * Returns if this indicator meets the restrictions for its use in an Alert. |
|
245 */ |
|
246 boolean isSuitableForAlert() |
|
247 { |
|
248 return (!isInteractive() |
|
249 && getParent() == null |
|
250 && getLabel() == null |
|
251 && getLayout() == Item.LAYOUT_DEFAULT |
|
252 && !isSizeLocked() |
|
253 && getNumCommands() == 0 |
|
254 && getItemCommandListener() == null); |
|
255 } |
|
256 |
|
257 /** |
|
258 * Called by widget listeners to update Item value. |
|
259 */ |
|
260 void internalSetValue(int newValue) |
|
261 { |
|
262 value = validateValue(newValue, maxValue); |
|
263 updateParent(UPDATE_VALUE); |
|
264 // notify item state listener |
|
265 notifyStateChanged(); |
|
266 } |
|
267 |
|
268 /** |
|
269 * Return layout with optional custom flags. |
|
270 * |
|
271 * @return layout directive |
|
272 */ |
|
273 int internalGetLayout() |
|
274 { |
|
275 return super.internalGetLayout() | Item.LAYOUT_NEWLINE_BEFORE; |
199 } |
276 } |
200 |
277 |
201 /** |
278 /** |
202 * Updates Form or Alert. |
279 * Updates Form or Alert. |
203 * |
280 * |
233 Point calculatePreferredSize() |
310 Point calculatePreferredSize() |
234 { |
311 { |
235 return GaugeLayouter.calculatePreferredBounds(this); |
312 return GaugeLayouter.calculatePreferredBounds(this); |
236 } |
313 } |
237 |
314 |
238 /** |
|
239 * Called by widget listeners to update Item value. |
|
240 */ |
|
241 void internalSetValue(int newValue) |
|
242 { |
|
243 this.value = checkValue(newValue, this.maxValue); |
|
244 // notify item state listener |
|
245 notifyStateChanged(); |
|
246 } |
|
247 |
|
248 /** |
|
249 * Return layout with optional custom flags. |
|
250 * |
|
251 * @return layout directive |
|
252 */ |
|
253 int internalGetLayout() |
|
254 { |
|
255 return super.internalGetLayout() | Item.LAYOUT_NEWLINE_BEFORE; |
|
256 } |
|
257 |
|
258 /** |
|
259 * @return if the Gauge is indefinite. |
|
260 */ |
|
261 boolean isIndefinite() |
|
262 { |
|
263 return (maxValue == INDEFINITE); |
|
264 } |
|
265 |
|
266 /** |
|
267 * Returns if this indicator meets the restrictions for its use in an Alert. |
|
268 */ |
|
269 boolean isSuitableForAlert() |
|
270 { |
|
271 return (!isInteractive() |
|
272 && getParent() == null |
|
273 && getLabel() == null |
|
274 && getLayout() == Item.LAYOUT_DEFAULT |
|
275 && !isSizeLocked() |
|
276 && getNumCommands() == 0 |
|
277 && getItemCommandListener() == null); |
|
278 } |
|
279 |
|
280 /* (non-Javadoc) |
|
281 * @see javax.microedition.lcdui.Item#isFocusable() |
|
282 */ |
|
283 boolean isFocusable() |
|
284 { |
|
285 return (isInteractive() || (getNumCommands() > 0)); |
|
286 } |
|
287 |
|
288 } |
315 } |
|
316 |