1 | package org.expeditee.items;
|
---|
2 |
|
---|
3 | import javax.swing.JComboBox;
|
---|
4 |
|
---|
5 | public class SampleWidget1 extends InteractiveWidget {
|
---|
6 |
|
---|
7 | static String testItems[] = new String[] {"dog", "fish", "cat", "pig"};
|
---|
8 |
|
---|
9 | private JComboBox _combo;
|
---|
10 |
|
---|
11 | public SampleWidget1(Text source, String[] args) {
|
---|
12 | super(source, new JComboBox(testItems), 200, 200, 50, 50);
|
---|
13 | _combo = (JComboBox)super._swingComponent;
|
---|
14 |
|
---|
15 | // Set state
|
---|
16 | if (args != null) {
|
---|
17 |
|
---|
18 | int selectedItem = 0;
|
---|
19 |
|
---|
20 | // extract selected index
|
---|
21 | if (args.length >= 1 && args[0] != null) {
|
---|
22 | try {
|
---|
23 | selectedItem = Integer.parseInt(args[0]);
|
---|
24 | } catch (NumberFormatException e) {
|
---|
25 | e.printStackTrace();
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | // Get defined items
|
---|
30 | if (args.length >= 2) {
|
---|
31 | _combo.removeAllItems();
|
---|
32 | for (int i = 1; i < args.length; i++) {
|
---|
33 | if (args[i] != null) _combo.addItem(args[i]);
|
---|
34 | }
|
---|
35 | if (_combo.getItemCount() == 0) {
|
---|
36 | for (String s : testItems) _combo.addItem(s);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | if (selectedItem < 0) selectedItem = 0;
|
---|
41 | else if (selectedItem >= _combo.getItemCount())
|
---|
42 | selectedItem = _combo.getItemCount() - 1;
|
---|
43 | _combo.setSelectedIndex(selectedItem);
|
---|
44 |
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | }
|
---|
49 |
|
---|
50 | @Override
|
---|
51 | protected String[] getArgs() {
|
---|
52 |
|
---|
53 | String[] stateArgs = new String[_combo.getItemCount() + 1];
|
---|
54 | stateArgs[0] = Integer.toString(_combo.getSelectedIndex());
|
---|
55 | for (int i = 0; i < _combo.getItemCount(); i++) {
|
---|
56 | stateArgs[i + 1] = _combo.getItemAt(i).toString();
|
---|
57 | }
|
---|
58 |
|
---|
59 | return stateArgs;
|
---|
60 | }
|
---|
61 |
|
---|
62 | @Override
|
---|
63 | public float getMinimumBorderThickness() {
|
---|
64 | return 0.0F;
|
---|
65 | }
|
---|
66 | }
|
---|