|
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 org.eclipse.swt.graphics.Point; |
|
20 |
|
21 /** |
|
22 * Item representing a Choice group. |
|
23 */ |
|
24 public class ChoiceGroup extends Item implements Choice { |
|
25 |
|
26 private ChoiceImpl choiceImpl; |
|
27 private int type; |
|
28 |
|
29 /** |
|
30 * Constructor. |
|
31 * |
|
32 * @param label the label |
|
33 * @param type the type |
|
34 */ |
|
35 public ChoiceGroup(String label, int type) { |
|
36 this(label, type, new String[] {}, null); |
|
37 } |
|
38 |
|
39 /** |
|
40 * Constructor. |
|
41 * |
|
42 * @param label the label |
|
43 * @param type the type |
|
44 * @param textElements text elements |
|
45 * @param imgElements image elements |
|
46 * |
|
47 * @throws IllegalArgumentException if type is invalid |
|
48 * @throws IllegalArgumentException if image elements array is not null and |
|
49 * the length of the arrays don't match |
|
50 * @throws NullPointerException if the text elements array is null |
|
51 * @throws NullPointerException if any of the text elements is null |
|
52 */ |
|
53 public ChoiceGroup(String label, int type, |
|
54 String[] textElements, |
|
55 Image[] imgElements) { |
|
56 switch (type) { |
|
57 case Choice.EXCLUSIVE: |
|
58 case Choice.POPUP: |
|
59 choiceImpl = new ChoiceImpl(false); |
|
60 break; |
|
61 case Choice.MULTIPLE: |
|
62 choiceImpl = new ChoiceImpl(true); |
|
63 break; |
|
64 default: |
|
65 throw new IllegalArgumentException( |
|
66 MsgRepository.CHOICEGROUP_EXCEPTION_INVALID_TYPE); |
|
67 } |
|
68 choiceImpl.check(textElements, imgElements); |
|
69 setLabel(label != null ? label : ""); |
|
70 this.type = type; |
|
71 // append elements |
|
72 for (int i = 0; i < textElements.length; i++) { |
|
73 append(textElements[i], imgElements != null |
|
74 ? imgElements[i] : null); |
|
75 } |
|
76 } |
|
77 |
|
78 /** |
|
79 * Append item with specified text and image. |
|
80 * |
|
81 * @param text the text |
|
82 * @param img the image |
|
83 * @return index of added item |
|
84 */ |
|
85 public int append(String text, Image img) { |
|
86 int ret = choiceImpl.append(text, img); |
|
87 updateParent(UPDATE_SIZE_CHANGED); |
|
88 return ret; |
|
89 } |
|
90 |
|
91 /** |
|
92 * Insert item with specified text and image. |
|
93 * |
|
94 * @param position the item index |
|
95 * @param text the text |
|
96 * @param img the image |
|
97 */ |
|
98 public void insert(int position, String text, Image img) { |
|
99 choiceImpl.insert(position, text, img); |
|
100 updateParent(UPDATE_SIZE_CHANGED); |
|
101 } |
|
102 |
|
103 /** |
|
104 * Set item with specified text and image. |
|
105 * |
|
106 * @param position the item index |
|
107 * @param text the text |
|
108 * @param img the image |
|
109 */ |
|
110 public void set(int position, String text, Image img) { |
|
111 choiceImpl.set(position, text, img); |
|
112 updateParent(UPDATE_CONTENT); |
|
113 } |
|
114 |
|
115 /** |
|
116 * Remove item at specified position. |
|
117 * |
|
118 * @param position the item index |
|
119 */ |
|
120 public void delete(int position) { |
|
121 choiceImpl.delete(position); |
|
122 updateParent(UPDATE_SIZE_CHANGED); |
|
123 } |
|
124 |
|
125 /** |
|
126 * Remove all items. |
|
127 */ |
|
128 public void deleteAll() { |
|
129 choiceImpl.deleteAll(); |
|
130 updateParent(UPDATE_SIZE_CHANGED); |
|
131 } |
|
132 |
|
133 /** |
|
134 * Get the fit policy of this ChoiceGroup. |
|
135 * |
|
136 * @return the ChoiceGroup's fit policy |
|
137 */ |
|
138 public int getFitPolicy() { |
|
139 return choiceImpl.getFitPolicy(); |
|
140 } |
|
141 |
|
142 /** |
|
143 * Get the font used in a ChoiceGroup item. |
|
144 * |
|
145 * @param position the index of the item |
|
146 * @return the items font |
|
147 */ |
|
148 public Font getFont(int position) { |
|
149 return choiceImpl.getFont(position); |
|
150 } |
|
151 |
|
152 /** |
|
153 * Get the image part of a ChoiceGroup item. |
|
154 * |
|
155 * @param position the index of the item |
|
156 * @return the items image part |
|
157 */ |
|
158 public Image getImage(int position) { |
|
159 return choiceImpl.getImage(position); |
|
160 } |
|
161 |
|
162 /** |
|
163 * Get the string part of a ChoiceGroup item. |
|
164 * |
|
165 * @param position the index of the item |
|
166 * @return the items string part |
|
167 */ |
|
168 public String getString(int position) { |
|
169 return choiceImpl.getString(position); |
|
170 } |
|
171 |
|
172 /** |
|
173 * Get selected flags. |
|
174 * |
|
175 * @param selectedArray an array with selected items |
|
176 * @return selected flags |
|
177 */ |
|
178 public int getSelectedFlags(boolean[] selectedArray) { |
|
179 return choiceImpl.getSelectedFlags(selectedArray); |
|
180 } |
|
181 |
|
182 /** |
|
183 * Returns the selected item's index. |
|
184 * |
|
185 * @return the selected index |
|
186 */ |
|
187 public int getSelectedIndex() { |
|
188 return choiceImpl.getSelectedIndex(); |
|
189 } |
|
190 |
|
191 /** |
|
192 * Returns if the specified element is selected. |
|
193 * |
|
194 * @param position specified element index |
|
195 * @return true if its selected, false otherwise |
|
196 */ |
|
197 public boolean isSelected(int position) { |
|
198 return choiceImpl.isSelected(position); |
|
199 } |
|
200 |
|
201 /** |
|
202 * Set the fit policy of this ChoiceGroup. |
|
203 * |
|
204 * @param newFitPolicy the new fit policy |
|
205 */ |
|
206 public void setFitPolicy(int newFitPolicy) { |
|
207 choiceImpl.setFitPolicy(newFitPolicy); |
|
208 updateParent(UPDATE_SIZE_CHANGED); |
|
209 } |
|
210 |
|
211 /** |
|
212 * Set the font of a ChoiceGroup item. |
|
213 * |
|
214 * @param position the index of the item |
|
215 * @param font the desired font |
|
216 */ |
|
217 public void setFont(int position, Font font) { |
|
218 choiceImpl.setFont(position, font); |
|
219 updateParent(UPDATE_SIZE_CHANGED); |
|
220 } |
|
221 |
|
222 /** |
|
223 * Set selected flags. |
|
224 * |
|
225 * @param selectedArray an array with selected items |
|
226 */ |
|
227 public void setSelectedFlags(boolean[] selectedArray) { |
|
228 choiceImpl.setSelectedFlags(selectedArray); |
|
229 updateParent(UPDATE_CONTENT); |
|
230 } |
|
231 |
|
232 /** |
|
233 * Set selected index. |
|
234 * |
|
235 * @param position the index of the item |
|
236 * @param select selected or not |
|
237 */ |
|
238 public void setSelectedIndex(int position, boolean select) { |
|
239 choiceImpl.setSelected(position, select); |
|
240 updateParent(UPDATE_CONTENT); |
|
241 } |
|
242 |
|
243 /** |
|
244 * Returns the size of the ChoiceGroup. |
|
245 * |
|
246 * @return the lists size |
|
247 */ |
|
248 public int size() { |
|
249 return choiceImpl.size(); |
|
250 } |
|
251 |
|
252 /** |
|
253 * Calculates minimum size of this item. |
|
254 * |
|
255 * @return Minimum size. |
|
256 */ |
|
257 Point calculateMinimumSize() { |
|
258 return ChoiceGroupLayouter.calculateMinimumBounds(this); |
|
259 } |
|
260 |
|
261 /** |
|
262 * Calculates preferred size of this item. |
|
263 * |
|
264 * @return Preferred size. |
|
265 */ |
|
266 Point calculatePreferredSize() { |
|
267 return ChoiceGroupLayouter.calculatePreferredBounds(this); |
|
268 } |
|
269 |
|
270 /** |
|
271 * Called by widget listeners to update Item value. |
|
272 */ |
|
273 void internalSetSelectedIndex(int position, boolean select) { |
|
274 choiceImpl.setSelected(position, select); |
|
275 // notify item state listener |
|
276 notifyStateChanged(); |
|
277 } |
|
278 |
|
279 /** |
|
280 * Return layout with optional custom flags. |
|
281 * |
|
282 * @return layout directive |
|
283 */ |
|
284 int internalGetLayout() { |
|
285 return super.internalGetLayout() | Item.LAYOUT_NEWLINE_BEFORE; |
|
286 } |
|
287 |
|
288 /** |
|
289 * Return the ChoiceGroup type. |
|
290 */ |
|
291 final int getType() { |
|
292 return type; |
|
293 } |
|
294 |
|
295 /* (non-Javadoc) |
|
296 * @see javax.microedition.lcdui.Item#isFocusable() |
|
297 */ |
|
298 boolean isFocusable() { |
|
299 return true; |
|
300 } |
|
301 |
|
302 } |