66 * the length of the arrays don't match |
68 * the length of the arrays don't match |
67 * @throws NullPointerException if the text elements array is null |
69 * @throws NullPointerException if the text elements array is null |
68 * @throws NullPointerException if any of the text elements is null |
70 * @throws NullPointerException if any of the text elements is null |
69 */ |
71 */ |
70 public List(String title, int type, String[] textElements, |
72 public List(String title, int type, String[] textElements, |
71 Image[] imgElements) { |
73 Image[] imgElements) |
|
74 { |
72 super(title); |
75 super(title); |
73 switch (type) { |
76 switch(type) |
74 case Choice.IMPLICIT: |
77 { |
75 case Choice.EXCLUSIVE: |
78 case Choice.IMPLICIT: |
76 choiceImpl = new ChoiceImpl(false); |
79 case Choice.EXCLUSIVE: |
77 break; |
80 choiceImpl = new ChoiceImpl(false); |
78 case Choice.MULTIPLE: |
81 break; |
79 choiceImpl = new ChoiceImpl(true); |
82 case Choice.MULTIPLE: |
80 break; |
83 choiceImpl = new ChoiceImpl(true); |
81 default: |
84 break; |
82 throw new IllegalArgumentException( |
85 default: |
83 MsgRepository.LIST_EXCEPTION_INVALID_TYPE); |
86 throw new IllegalArgumentException( |
|
87 MsgRepository.LIST_EXCEPTION_INVALID_TYPE); |
84 } |
88 } |
85 choiceImpl.check(textElements, imgElements); |
89 choiceImpl.check(textElements, imgElements); |
86 this.selectCommand = SELECT_COMMAND; |
90 this.selectCommand = SELECT_COMMAND; |
87 this.type = type; |
91 this.type = type; |
88 construct(); |
92 construct(); |
89 // append elements |
93 // append elements |
90 for (int i = 0; i < textElements.length; i++) { |
94 for(int i = 0; i < textElements.length; i++) |
|
95 { |
91 append(textElements[i], imgElements != null |
96 append(textElements[i], imgElements != null |
92 ? imgElements[i] : null); |
97 ? imgElements[i] : null); |
93 } |
98 } |
94 } |
99 } |
95 |
100 |
96 /* (non-Javadoc) |
101 /* (non-Javadoc) |
97 * @see Displayable#eswtConstructContent(int) |
102 * @see Displayable#eswtConstructContent(int) |
98 */ |
103 */ |
99 Composite eswtConstructContent(int style) { |
104 Composite eswtConstructContent(int style) |
|
105 { |
100 Composite comp = super.eswtConstructContent(style); |
106 Composite comp = super.eswtConstructContent(style); |
101 eswtTable = new TableExtension(comp, getStyle(type)); |
107 eswtTable = new TableExtension(comp, getStyle(type)); |
102 eswtTable.getHorizontalBar().setVisible(false); |
108 eswtTable.getHorizontalBar().setVisible(false); |
103 return comp; |
109 return comp; |
104 } |
110 } |
105 |
111 |
106 /** |
112 /** |
107 * Called by Display when Displayable should become visible. |
113 * Called by Display when Displayable should become visible. |
108 */ |
114 */ |
109 void eswtHandleShowCurrentEvent() { |
115 void eswtHandleShowCurrentEvent() |
|
116 { |
110 super.eswtHandleShowCurrentEvent(); |
117 super.eswtHandleShowCurrentEvent(); |
111 eswtTable.addSelectionListener(eswtTableListener); |
118 eswtTable.addSelectionListener(eswtTableListener); |
112 } |
119 } |
113 |
120 |
114 /** |
121 /** |
115 * Called by Display when Displayable should become hidden. |
122 * Called by Display when Displayable should become hidden. |
116 */ |
123 */ |
117 void eswtHandleHideCurrentEvent() { |
124 void eswtHandleHideCurrentEvent() |
|
125 { |
118 super.eswtHandleHideCurrentEvent(); |
126 super.eswtHandleHideCurrentEvent(); |
119 eswtTable.removeSelectionListener(eswtTableListener); |
127 eswtTable.removeSelectionListener(eswtTableListener); |
120 } |
128 } |
121 |
129 |
122 /* (non-Javadoc) |
130 /* (non-Javadoc) |
123 * @see Displayable#eswtHandleResizeEvent(int, int) |
131 * @see Displayable#eswtHandleResizeEvent(int, int) |
124 */ |
132 */ |
125 void eswtHandleResizeEvent(int width, int height) { |
133 void eswtHandleResizeEvent(int width, int height) |
|
134 { |
126 super.eswtHandleResizeEvent(width, height); |
135 super.eswtHandleResizeEvent(width, height); |
127 eswtTable.setBounds(getContentComp().getClientArea()); |
136 eswtTable.setBounds(getContentComp().getClientArea()); |
128 } |
137 } |
129 |
138 |
130 /** |
139 /** |
131 * Get Table style based on list type. |
140 * Get Table style based on list type. |
132 */ |
141 */ |
133 private int getStyle(int listType) { |
142 private int getStyle(int listType) |
|
143 { |
134 int tableStyle = SWT.NONE; |
144 int tableStyle = SWT.NONE; |
135 switch (listType) { |
145 switch(listType) |
136 case Choice.IMPLICIT: |
146 { |
137 tableStyle |= SWT.SINGLE; |
147 case Choice.IMPLICIT: |
138 break; |
148 tableStyle |= SWT.SINGLE; |
139 case Choice.EXCLUSIVE: |
149 break; |
140 tableStyle |= SWT.SINGLE | SWT.RADIO; |
150 case Choice.EXCLUSIVE: |
141 break; |
151 tableStyle |= SWT.SINGLE | SWT.RADIO; |
142 case Choice.MULTIPLE: |
152 break; |
143 tableStyle |= SWT.MULTI | SWT.CHECK; |
153 case Choice.MULTIPLE: |
144 break; |
154 tableStyle |= SWT.MULTI | SWT.CHECK; |
145 default: |
155 break; |
146 break; |
156 default: |
|
157 break; |
147 } |
158 } |
148 return tableStyle; |
159 return tableStyle; |
149 } |
160 } |
150 |
161 |
151 private void updateSelection() { |
162 private void updateSelection() |
152 ESWTUIThreadRunner.syncExec(new Runnable() { |
163 { |
153 public void run() { |
164 ESWTUIThreadRunner.syncExec(new Runnable() |
|
165 { |
|
166 public void run() |
|
167 { |
154 eswtUpdateSelection(); |
168 eswtUpdateSelection(); |
155 } |
169 } |
156 }); |
170 }); |
157 } |
171 } |
158 |
172 |
159 /** |
173 /** |
160 * Update eSWT Table selection. |
174 * Update eSWT Table selection. |
161 */ |
175 */ |
162 private void eswtUpdateSelection() { |
176 private void eswtUpdateSelection() |
163 if (type == IMPLICIT || type == EXCLUSIVE) { |
177 { |
|
178 if(type == IMPLICIT || type == EXCLUSIVE) |
|
179 { |
164 int sel = choiceImpl.getSelectedIndex(); |
180 int sel = choiceImpl.getSelectedIndex(); |
165 if ((sel == 0) || (eswtTable.getSelectionIndex() != sel)) { |
181 if((sel == 0) || (eswtTable.getSelectionIndex() != sel)) |
|
182 { |
166 eswtTable.setSelection(sel); |
183 eswtTable.setSelection(sel); |
167 } |
184 } |
168 } |
185 } |
169 else { |
186 else |
|
187 { |
170 int size = choiceImpl.size(); |
188 int size = choiceImpl.size(); |
171 for (int i = 0; i < size; i++) { |
189 for(int i = 0; i < size; i++) |
172 if (choiceImpl.isSelected(i)) { |
190 { |
|
191 if(choiceImpl.isSelected(i)) |
|
192 { |
173 eswtTable.select(i); |
193 eswtTable.select(i); |
174 } |
194 } |
175 else { |
195 else |
|
196 { |
176 eswtTable.deselect(i); |
197 eswtTable.deselect(i); |
177 } |
198 } |
178 } |
199 } |
179 } |
200 } |
180 } |
201 } |
181 |
202 |
182 private void eswtInsertItem(int index) { |
203 private void eswtInsertItem(int index) |
|
204 { |
183 TableItem item = new TableItem(eswtTable, SWT.NONE, index); |
205 TableItem item = new TableItem(eswtTable, SWT.NONE, index); |
184 Image img = choiceImpl.getImage(index); |
206 Image img = choiceImpl.getImage(index); |
185 item.setImage(0, Image.getESWTImage(img)); |
207 item.setImage(0, Image.getESWTImage(img)); |
186 item.setText(0, choiceImpl.getString(index)); |
208 item.setText(0, choiceImpl.getString(index)); |
187 } |
209 } |
188 |
210 |
189 private void eswtSetItem(int index) { |
211 private void eswtSetItem(int index) |
|
212 { |
190 TableItem item = eswtTable.getItem(index); |
213 TableItem item = eswtTable.getItem(index); |
191 Image img = choiceImpl.getImage(index); |
214 Image img = choiceImpl.getImage(index); |
192 item.setImage(0, Image.getESWTImage(img)); |
215 item.setImage(0, Image.getESWTImage(img)); |
193 item.setText(0, choiceImpl.getString(index)); |
216 item.setText(0, choiceImpl.getString(index)); |
194 } |
217 } |
195 |
218 |
196 private void eswtDeleteItem(int index) { |
219 private void eswtDeleteItem(int index) |
|
220 { |
197 eswtTable.getItem(index).dispose(); |
221 eswtTable.getItem(index).dispose(); |
198 choiceImpl.delete(index); |
222 choiceImpl.delete(index); |
199 } |
223 } |
200 |
224 |
201 private void eswtDeleteAllItems() { |
225 private void eswtDeleteAllItems() |
202 for (int i = eswtTable.getItemCount() - 1; i >= 0; i--) { |
226 { |
203 if (type == Choice.IMPLICIT) |
227 for(int i = eswtTable.getItemCount() - 1; i >= 0; i--) |
204 { |
228 { |
205 choiceImpl.delete(i); |
229 if(type == Choice.IMPLICIT) |
|
230 { |
|
231 choiceImpl.delete(i); |
206 } |
232 } |
207 eswtTable.getItem(i).dispose(); |
233 eswtTable.getItem(i).dispose(); |
208 } |
234 } |
209 } |
235 } |
210 |
236 |
211 private void eswtSetItemFont(int index) { |
237 private void eswtSetItemFont(int index) |
|
238 { |
212 org.eclipse.swt.graphics.Font font = Font.getESWTFont(choiceImpl |
239 org.eclipse.swt.graphics.Font font = Font.getESWTFont(choiceImpl |
213 .getFont(index)); |
240 .getFont(index)); |
214 eswtTable.getItem(index).setFont(0, font); |
241 eswtTable.getItem(index).setFont(0, font); |
215 } |
242 } |
216 |
243 |
217 /** |
244 /** |
218 * Append item with specified text and image. |
245 * Append item with specified text and image. |
219 * |
246 * |
220 * @param text the text |
247 * @param text the text |
221 * @param img the image |
248 * @param img the image |
222 * @return index of added item |
249 * @return index of added item |
223 */ |
250 */ |
224 public int append(String text, Image img) { |
251 public int append(String text, Image img) |
|
252 { |
225 final int index = choiceImpl.append(text, img); |
253 final int index = choiceImpl.append(text, img); |
226 ESWTUIThreadRunner.syncExec(new Runnable() { |
254 ESWTUIThreadRunner.syncExec(new Runnable() |
227 public void run() { |
255 { |
|
256 public void run() |
|
257 { |
228 eswtInsertItem(index); |
258 eswtInsertItem(index); |
229 eswtUpdateSelection(); |
259 eswtUpdateSelection(); |
230 } |
260 } |
231 }); |
261 }); |
232 return index; |
262 return index; |
271 /** |
307 /** |
272 * Remove item with at specified position. |
308 * Remove item with at specified position. |
273 * |
309 * |
274 * @param position the item index |
310 * @param position the item index |
275 */ |
311 */ |
276 public void delete(int position) { |
312 public void delete(int position) |
|
313 { |
277 // choiceImpl.delete(position); |
314 // choiceImpl.delete(position); |
278 final int index = position; // index of changed element |
315 final int index = position; // index of changed element |
279 ESWTUIThreadRunner.syncExec(new Runnable() { |
316 ESWTUIThreadRunner.syncExec(new Runnable() |
280 public void run() { |
317 { |
|
318 public void run() |
|
319 { |
281 eswtDeleteItem(index); |
320 eswtDeleteItem(index); |
282 eswtUpdateSelection(); |
321 eswtUpdateSelection(); |
283 } |
322 } |
284 }); |
323 }); |
285 } |
324 } |
286 |
325 |
287 /** |
326 /** |
288 * Remove all items. |
327 * Remove all items. |
289 */ |
328 */ |
290 public void deleteAll() { |
329 public void deleteAll() |
291 if (type != Choice.IMPLICIT) |
330 { |
292 { |
331 if(type != Choice.IMPLICIT) |
293 choiceImpl.deleteAll(); |
332 { |
294 } |
333 choiceImpl.deleteAll(); |
295 ESWTUIThreadRunner.syncExec(new Runnable() { |
334 } |
296 public void run() { |
335 ESWTUIThreadRunner.syncExec(new Runnable() |
|
336 { |
|
337 public void run() |
|
338 { |
297 eswtDeleteAllItems(); |
339 eswtDeleteAllItems(); |
298 } |
340 } |
299 }); |
341 }); |
300 } |
342 } |
301 |
343 |
302 /** |
344 /** |
303 * Get the fit policy of this list. |
345 * Get the fit policy of this list. |
304 * |
346 * |
305 * @return the lists fir policy |
347 * @return the lists fir policy |
306 */ |
348 */ |
307 public int getFitPolicy() { |
349 public int getFitPolicy() |
|
350 { |
308 return choiceImpl.getFitPolicy(); |
351 return choiceImpl.getFitPolicy(); |
309 } |
352 } |
310 |
353 |
311 /** |
354 /** |
312 * Get the font used in a list item. |
355 * Get the font used in a list item. |
313 * |
356 * |
314 * @param position the index of the item |
357 * @param position the index of the item |
315 * @return the items font |
358 * @return the items font |
316 */ |
359 */ |
317 public Font getFont(int position) { |
360 public Font getFont(int position) |
|
361 { |
318 return choiceImpl.getFont(position); |
362 return choiceImpl.getFont(position); |
319 } |
363 } |
320 |
364 |
321 /** |
365 /** |
322 * Get the image part of a list item. |
366 * Get the image part of a list item. |
323 * |
367 * |
324 * @param position the index of the item |
368 * @param position the index of the item |
325 * @return the items image part |
369 * @return the items image part |
326 */ |
370 */ |
327 public Image getImage(int position) { |
371 public Image getImage(int position) |
|
372 { |
328 return choiceImpl.getImage(position); |
373 return choiceImpl.getImage(position); |
329 } |
374 } |
330 |
375 |
331 /** |
376 /** |
332 * Get the string part of a list item. |
377 * Get the string part of a list item. |
333 * |
378 * |
334 * @param position the index of the item |
379 * @param position the index of the item |
335 * @return the items string part |
380 * @return the items string part |
336 */ |
381 */ |
337 public String getString(int position) { |
382 public String getString(int position) |
|
383 { |
338 return choiceImpl.getString(position); |
384 return choiceImpl.getString(position); |
339 } |
385 } |
340 |
386 |
341 /** |
387 /** |
342 * Get selected flags. |
388 * Get selected flags. |
343 * |
389 * |
344 * @param selectedArray an array with selected items |
390 * @param selectedArray an array with selected items |
345 * @return selected flags |
391 * @return selected flags |
346 */ |
392 */ |
347 public int getSelectedFlags(boolean[] selectedArray) { |
393 public int getSelectedFlags(boolean[] selectedArray) |
|
394 { |
348 return choiceImpl.getSelectedFlags(selectedArray); |
395 return choiceImpl.getSelectedFlags(selectedArray); |
349 } |
396 } |
350 |
397 |
351 /** |
398 /** |
352 * Returns the selected index. |
399 * Returns the selected index. |
353 * |
400 * |
354 * @return the selected index |
401 * @return the selected index |
355 */ |
402 */ |
356 public int getSelectedIndex() { |
403 public int getSelectedIndex() |
|
404 { |
357 return choiceImpl.getSelectedIndex(); |
405 return choiceImpl.getSelectedIndex(); |
358 } |
406 } |
359 |
407 |
360 /** |
408 /** |
361 * Returns if the specified element is selected. |
409 * Returns if the specified element is selected. |
362 * |
410 * |
363 * @param position specified element index |
411 * @param position specified element index |
364 * @return true if its selected, false otherwise |
412 * @return true if its selected, false otherwise |
365 */ |
413 */ |
366 public boolean isSelected(int position) { |
414 public boolean isSelected(int position) |
|
415 { |
367 return choiceImpl.isSelected(position); |
416 return choiceImpl.isSelected(position); |
368 } |
417 } |
369 |
418 |
370 /** |
419 /** |
371 * Set the fit policy of this list. |
420 * Set the fit policy of this list. |
372 * |
421 * |
373 * @param newFitPolicy the new fit policy |
422 * @param newFitPolicy the new fit policy |
374 */ |
423 */ |
375 public void setFitPolicy(int newFitPolicy) { |
424 public void setFitPolicy(int newFitPolicy) |
|
425 { |
376 choiceImpl.setFitPolicy(newFitPolicy); |
426 choiceImpl.setFitPolicy(newFitPolicy); |
377 ESWTUIThreadRunner.syncExec(new Runnable() { |
427 ESWTUIThreadRunner.syncExec(new Runnable() |
378 public void run() { |
428 { |
|
429 public void run() |
|
430 { |
379 eswtTable.setWordWrap(choiceImpl.getFitPolicy() |
431 eswtTable.setWordWrap(choiceImpl.getFitPolicy() |
380 == Choice.TEXT_WRAP_ON); |
432 == Choice.TEXT_WRAP_ON); |
381 } |
433 } |
382 }); |
434 }); |
383 } |
435 } |
384 |
436 |
385 /** |
437 /** |
386 * Set the font of a list item. |
438 * Set the font of a list item. |
387 * |
439 * |
388 * @param position the index of the item |
440 * @param position the index of the item |
389 * @param font the desired font |
441 * @param font the desired font |
390 */ |
442 */ |
391 public void setFont(int position, Font font) { |
443 public void setFont(int position, Font font) |
|
444 { |
392 choiceImpl.setFont(position, font); |
445 choiceImpl.setFont(position, font); |
393 final int index = position; // index of added element |
446 final int index = position; // index of added element |
394 ESWTUIThreadRunner.syncExec(new Runnable() { |
447 ESWTUIThreadRunner.syncExec(new Runnable() |
395 public void run() { |
448 { |
|
449 public void run() |
|
450 { |
396 eswtSetItemFont(index); |
451 eswtSetItemFont(index); |
397 } |
452 } |
398 }); |
453 }); |
399 } |
454 } |
400 |
455 |
401 /** |
456 /** |
402 * Set the select command of an IMPLICIT list. |
457 * Set the select command of an IMPLICIT list. |
403 * |
458 * |
404 * @param cmd a command. |
459 * @param cmd a command. |
405 */ |
460 */ |
406 public void setSelectCommand(Command cmd) { |
461 public void setSelectCommand(Command cmd) |
407 if (type == Choice.IMPLICIT) { |
462 { |
408 if (cmd != SELECT_COMMAND) { |
463 if(type == Choice.IMPLICIT) |
|
464 { |
|
465 if(cmd != SELECT_COMMAND) |
|
466 { |
409 addCommand(cmd); |
467 addCommand(cmd); |
410 } |
468 } |
411 selectCommand = cmd; |
469 selectCommand = cmd; |
412 } |
470 } |
413 } |
471 } |
415 /** |
473 /** |
416 * Removes command from the List. |
474 * Removes command from the List. |
417 * |
475 * |
418 * @param cmd a command. |
476 * @param cmd a command. |
419 */ |
477 */ |
420 public void removeCommand(Command cmd) { |
478 public void removeCommand(Command cmd) |
421 if (cmd == selectCommand) { |
479 { |
|
480 if(cmd == selectCommand) |
|
481 { |
422 selectCommand = null; |
482 selectCommand = null; |
423 } |
483 } |
424 super.removeCommand(cmd); |
484 super.removeCommand(cmd); |
425 } |
485 } |
426 |
486 |
427 /** |
487 /** |
428 * Set selected flags. |
488 * Set selected flags. |
429 * |
489 * |
430 * @param selectedArray an array with selected items |
490 * @param selectedArray an array with selected items |
431 */ |
491 */ |
432 public void setSelectedFlags(boolean[] selectedArray) { |
492 public void setSelectedFlags(boolean[] selectedArray) |
|
493 { |
433 choiceImpl.setSelectedFlags(selectedArray); |
494 choiceImpl.setSelectedFlags(selectedArray); |
434 updateSelection(); |
495 updateSelection(); |
435 } |
496 } |
436 |
497 |
437 /** |
498 /** |
438 * Set selected index. |
499 * Set selected index. |
439 * |
500 * |
440 * @param position the index of the item |
501 * @param position the index of the item |
441 * @param select selected or not |
502 * @param select selected or not |
442 */ |
503 */ |
443 public void setSelectedIndex(int position, boolean select) { |
504 public void setSelectedIndex(int position, boolean select) |
|
505 { |
444 choiceImpl.setSelected(position, select); |
506 choiceImpl.setSelected(position, select); |
445 updateSelection(); |
507 updateSelection(); |
446 } |
508 } |
447 |
509 |
448 /** |
510 /** |
449 * Returns the size of the list. |
511 * Returns the size of the list. |
450 * |
512 * |
451 * @return the lists size |
513 * @return the lists size |
452 */ |
514 */ |
453 public int size() { |
515 public int size() |
|
516 { |
454 return choiceImpl.size(); |
517 return choiceImpl.size(); |
455 } |
518 } |
456 |
519 |
457 /** |
520 /** |
458 * Table Default Selection listener. |
521 * Table Default Selection listener. |
459 */ |
522 */ |
460 class EswtTableSelectionListener implements SelectionListener { |
523 class EswtTableSelectionListener implements SelectionListener |
461 |
524 { |
462 private void update(SelectionEvent se) { |
525 |
463 if (se.widget != null && se.item != null) { |
526 private void update(SelectionEvent se) |
|
527 { |
|
528 if(se.widget != null && se.item != null) |
|
529 { |
464 int index = ((Table) se.widget).indexOf((TableItem) se.item); |
530 int index = ((Table) se.widget).indexOf((TableItem) se.item); |
465 Logger.method(List.this, "updateSel", String.valueOf(index), |
531 Logger.method(List.this, "updateSel", String.valueOf(index), |
466 se); |
532 se); |
467 if (index >= 0) { |
533 if(index >= 0) |
|
534 { |
468 choiceImpl.setSelected(index, !isSelected(index)); |
535 choiceImpl.setSelected(index, !isSelected(index)); |
469 } |
536 } |
470 } |
537 } |
471 } |
538 } |
472 |
539 |
473 public void widgetDefaultSelected(SelectionEvent se) { |
540 public void widgetDefaultSelected(SelectionEvent se) |
474 if (type == Choice.IMPLICIT) { |
541 { |
475 if (size() > 0) { |
542 if(type == Choice.IMPLICIT) |
|
543 { |
|
544 if(size() > 0) |
|
545 { |
476 callCommandAction(selectCommand); |
546 callCommandAction(selectCommand); |
477 } |
547 } |
478 } |
548 } |
479 } |
549 } |
480 |
550 |
481 public void widgetSelected(SelectionEvent se) { |
551 public void widgetSelected(SelectionEvent se) |
482 if (type == Choice.IMPLICIT || type == Choice.EXCLUSIVE) { |
552 { |
|
553 if(type == Choice.IMPLICIT || type == Choice.EXCLUSIVE) |
|
554 { |
483 update(se); |
555 update(se); |
484 } |
556 } |
485 else if (type == Choice.MULTIPLE) { |
557 else if(type == Choice.MULTIPLE) |
486 if (se.detail == SWT.CHECK) { |
558 { |
|
559 if(se.detail == SWT.CHECK) |
|
560 { |
487 update(se); |
561 update(se); |
488 } |
562 } |
489 } |
563 } |
490 } |
564 } |
491 |
565 |