|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 package javax.microedition.lcdui; |
|
18 |
|
19 import java.util.Vector; |
|
20 |
|
21 /** |
|
22 * ChoiceImpl class implements the basic functionality of the Choice interface. |
|
23 * <br> List and ChoiceGroup class use this class as their member to implement |
|
24 * their own functionality. |
|
25 */ |
|
26 final class ChoiceImpl |
|
27 { |
|
28 |
|
29 /** |
|
30 * Internal class for storing Choice data. |
|
31 */ |
|
32 private class ChoiceData |
|
33 { |
|
34 String text; |
|
35 Image img; |
|
36 Font font; |
|
37 boolean sel; |
|
38 |
|
39 ChoiceData(String text, Image img, Font font, boolean sel) |
|
40 { |
|
41 this.text = text; |
|
42 this.img = img; |
|
43 this.font = font; |
|
44 this.sel = sel; |
|
45 } |
|
46 } |
|
47 |
|
48 /** |
|
49 * Array of ChoiceData items. |
|
50 */ |
|
51 private Vector items = new Vector(); |
|
52 |
|
53 /** |
|
54 * Fit policy flag. |
|
55 */ |
|
56 private int fitPolicy; |
|
57 |
|
58 /** |
|
59 * Stores if the choice allows single or multiple selections. |
|
60 */ |
|
61 private boolean multiSel; |
|
62 |
|
63 /** |
|
64 * Constructor. |
|
65 * |
|
66 * @param multiSel multi or single selection |
|
67 */ |
|
68 ChoiceImpl(boolean multiSel) |
|
69 { |
|
70 this.multiSel = multiSel; |
|
71 } |
|
72 |
|
73 /** |
|
74 * Check if the arrays are valid. |
|
75 * |
|
76 * @param textElements string array |
|
77 * @param imgElements image array |
|
78 */ |
|
79 void check(String[] textElements, Image[] imgElements) |
|
80 { |
|
81 // check textElements |
|
82 if(textElements == null) |
|
83 { |
|
84 throw new NullPointerException( |
|
85 MsgRepository.CHOICE_EXCEPTION_TEXT_ARRAY_NULL); |
|
86 } |
|
87 // check every text element |
|
88 for(int i = 0; i < textElements.length; i++) |
|
89 { |
|
90 if(textElements[i] == null) |
|
91 { |
|
92 throw new NullPointerException( |
|
93 MsgRepository.CHOICE_EXCEPTION_ITEM_NULL + i); |
|
94 } |
|
95 } |
|
96 // check imgElements array length |
|
97 if(imgElements != null && imgElements.length != textElements.length) |
|
98 { |
|
99 throw new IllegalArgumentException( |
|
100 MsgRepository.CHOICE_EXCEPTION_INVALID_ARRAY_LENGTHS); |
|
101 } |
|
102 } |
|
103 |
|
104 /** |
|
105 * Append item with specified text and image. |
|
106 */ |
|
107 int append(String text, Image image) |
|
108 { |
|
109 validateString(text); |
|
110 ChoiceData data = new ChoiceData(text, image, Font.getDefaultFont(), |
|
111 (!multiSel && size() == 0)); |
|
112 items.addElement(data); |
|
113 // return index of added element |
|
114 return size() - 1; |
|
115 } |
|
116 |
|
117 /** |
|
118 * Insert item with specified text and image. |
|
119 */ |
|
120 void insert(int elementNum, String text, Image image) |
|
121 { |
|
122 validateString(text); |
|
123 validatePosition(elementNum, size() + 1); |
|
124 ChoiceData data = new ChoiceData(text, image, Font.getDefaultFont(), |
|
125 (!multiSel && size() == 0)); |
|
126 items.insertElementAt(data, elementNum); |
|
127 } |
|
128 |
|
129 /** |
|
130 * Set item with specified text and image. |
|
131 */ |
|
132 void set(int elementNum, String text, Image image) |
|
133 { |
|
134 validateString(text); |
|
135 validatePosition(elementNum, size()); |
|
136 ChoiceData data = (ChoiceData) items.elementAt(elementNum); |
|
137 data.text = text; |
|
138 data.img = image; |
|
139 } |
|
140 |
|
141 /** |
|
142 * Set item's font. |
|
143 */ |
|
144 void setFont(int elementNum, Font font) |
|
145 { |
|
146 validatePosition(elementNum, size()); |
|
147 Font checkedFont = (font == null ? Font.getDefaultFont() : font); |
|
148 ((ChoiceData) items.elementAt(elementNum)).font = checkedFont; |
|
149 } |
|
150 |
|
151 /** |
|
152 * Delete item from specified position. |
|
153 */ |
|
154 void delete(int elementNum) |
|
155 { |
|
156 validatePosition(elementNum, size()); |
|
157 if(!multiSel && isSelected(elementNum) && size() > 1) |
|
158 { |
|
159 // we are removing the selected item and there are more |
|
160 if(elementNum == size() - 1) |
|
161 { |
|
162 // select previous item |
|
163 setSelected(elementNum - 1, true); |
|
164 } |
|
165 else |
|
166 { |
|
167 // select next item |
|
168 setSelected(elementNum + 1, true); |
|
169 } |
|
170 } |
|
171 items.removeElementAt(elementNum); |
|
172 } |
|
173 |
|
174 /** |
|
175 * Delete all items. |
|
176 */ |
|
177 void deleteAll() |
|
178 { |
|
179 items.removeAllElements(); |
|
180 } |
|
181 |
|
182 /** |
|
183 * Get fit policy. |
|
184 */ |
|
185 int getFitPolicy() |
|
186 { |
|
187 return fitPolicy; |
|
188 } |
|
189 |
|
190 /** |
|
191 * Get the Font of a given item. |
|
192 */ |
|
193 Font getFont(int elementNum) |
|
194 { |
|
195 validatePosition(elementNum, size()); |
|
196 return ((ChoiceData) items.elementAt(elementNum)).font; |
|
197 } |
|
198 |
|
199 /** |
|
200 * Get the image of a given item. |
|
201 */ |
|
202 Image getImage(int elementNum) |
|
203 { |
|
204 validatePosition(elementNum, size()); |
|
205 return ((ChoiceData) items.elementAt(elementNum)).img; |
|
206 } |
|
207 |
|
208 /** |
|
209 * Get the text of a given item. |
|
210 */ |
|
211 String getString(int elementNum) |
|
212 { |
|
213 validatePosition(elementNum, size()); |
|
214 return ((ChoiceData) items.elementAt(elementNum)).text; |
|
215 } |
|
216 |
|
217 /** |
|
218 * Set fit policy. |
|
219 */ |
|
220 void setFitPolicy(int newFitPolicy) |
|
221 { |
|
222 switch(newFitPolicy) |
|
223 { |
|
224 case Choice.TEXT_WRAP_DEFAULT: |
|
225 case Choice.TEXT_WRAP_OFF: |
|
226 case Choice.TEXT_WRAP_ON: |
|
227 fitPolicy = newFitPolicy; |
|
228 break; |
|
229 default: |
|
230 throw new IllegalArgumentException( |
|
231 MsgRepository.CHOICE_EXCEPTION_INVALID_FIT_POLICY); |
|
232 } |
|
233 } |
|
234 |
|
235 /** |
|
236 * Get the selection index. Returns the first selection index if multiple |
|
237 * selection is allowed. |
|
238 */ |
|
239 int getSelectedIndex() |
|
240 { |
|
241 final int size = size(); |
|
242 if(!multiSel) |
|
243 { |
|
244 for(int i = 0; i < size; i++) |
|
245 { |
|
246 if(isSelected(i)) |
|
247 { |
|
248 return i; |
|
249 } |
|
250 } |
|
251 } |
|
252 return -1; |
|
253 } |
|
254 |
|
255 /** |
|
256 * Checks if an item is selected. |
|
257 */ |
|
258 boolean isSelected(int elementNum) |
|
259 { |
|
260 validatePosition(elementNum, size()); |
|
261 return ((ChoiceData) items.elementAt(elementNum)).sel; |
|
262 } |
|
263 |
|
264 /** |
|
265 * Sets an item's selection. |
|
266 */ |
|
267 void setSelected(int elementNum, boolean select) |
|
268 { |
|
269 validatePosition(elementNum, size()); |
|
270 if(multiSel) |
|
271 { |
|
272 ((ChoiceData) items.elementAt(elementNum)).sel = select; |
|
273 } |
|
274 else |
|
275 { |
|
276 if(select) |
|
277 { |
|
278 // clear all |
|
279 final int size = size(); |
|
280 for(int i = 0; i < size; i++) |
|
281 { |
|
282 ((ChoiceData) items.elementAt(i)).sel = false; |
|
283 } |
|
284 // set just one |
|
285 ((ChoiceData) items.elementAt(elementNum)).sel = true; |
|
286 } |
|
287 } |
|
288 } |
|
289 |
|
290 /** |
|
291 * Get selection flags. |
|
292 */ |
|
293 int getSelectedFlags(boolean[] selectedArray) |
|
294 { |
|
295 validateSelectedArray(selectedArray); |
|
296 final int size = size(); |
|
297 int numSelected = 0; |
|
298 for(int i = 0; i < selectedArray.length; i++) |
|
299 { |
|
300 if( (i < size) && (((ChoiceData) items.elementAt(i)).sel)) |
|
301 { |
|
302 selectedArray[i] = true; |
|
303 numSelected++; |
|
304 } |
|
305 else |
|
306 { |
|
307 selectedArray[i] = false; |
|
308 } |
|
309 } |
|
310 |
|
311 return numSelected; |
|
312 } |
|
313 |
|
314 /** |
|
315 * Set selection flags. |
|
316 */ |
|
317 void setSelectedFlags(boolean[] selectedArray) |
|
318 { |
|
319 validateSelectedArray(selectedArray); |
|
320 final int size = size(); |
|
321 if(size > 0) |
|
322 { |
|
323 if(multiSel) |
|
324 { |
|
325 for(int i = 0; i < size; i++) |
|
326 { |
|
327 ((ChoiceData) items.elementAt(i)).sel = selectedArray[i]; |
|
328 } |
|
329 } |
|
330 else |
|
331 { |
|
332 int firstSelected = 0; |
|
333 for(int i = 0; i < size; i++) |
|
334 { |
|
335 if(selectedArray[i]) |
|
336 { |
|
337 firstSelected = i; |
|
338 break; |
|
339 } |
|
340 } |
|
341 setSelected(firstSelected, true); |
|
342 } |
|
343 } |
|
344 } |
|
345 |
|
346 /** |
|
347 * Returns the size of the list. |
|
348 * |
|
349 * @return the lists size |
|
350 */ |
|
351 int size() |
|
352 { |
|
353 return items.size(); |
|
354 } |
|
355 |
|
356 /** |
|
357 * Validates position. |
|
358 * |
|
359 * @param position an index of the text array |
|
360 * @param upperBoundary upper boundary for position |
|
361 */ |
|
362 private void validatePosition(int position, int upperBoundary) |
|
363 { |
|
364 if(position < 0 || position >= upperBoundary) |
|
365 { |
|
366 throw new IndexOutOfBoundsException( |
|
367 MsgRepository.CHOICE_EXCEPTION_INVALID_ITEM_INDEX); |
|
368 } |
|
369 } |
|
370 |
|
371 /** |
|
372 * Validates a string. |
|
373 * |
|
374 * @param str string |
|
375 */ |
|
376 private void validateString(String str) |
|
377 { |
|
378 if(str == null) |
|
379 { |
|
380 throw new NullPointerException( |
|
381 MsgRepository.CHOICE_EXCEPTION_STRING_NULL); |
|
382 } |
|
383 } |
|
384 |
|
385 /** |
|
386 * Validates an array containing selections. |
|
387 * |
|
388 * @param selectedArray selected array |
|
389 */ |
|
390 private void validateSelectedArray(boolean[] selectedArray) |
|
391 { |
|
392 if(selectedArray == null) |
|
393 { |
|
394 throw new NullPointerException( |
|
395 MsgRepository.CHOICE_EXCEPTION_ARRAY_NULL); |
|
396 } |
|
397 if(selectedArray.length < size()) |
|
398 { |
|
399 throw new IllegalArgumentException( |
|
400 MsgRepository.CHOICE_EXCEPTION_INVALID_ARRAY_SIZE); |
|
401 } |
|
402 } |
|
403 |
|
404 } |