OLD | NEW |
1 package autotest.common.ui; | 1 package autotest.common.ui; |
2 | 2 |
3 import com.google.gwt.user.client.ui.Composite; | 3 import autotest.afe.IRadioButton; |
4 import com.google.gwt.user.client.ui.HorizontalPanel; | |
5 import com.google.gwt.user.client.ui.Panel; | |
6 import com.google.gwt.user.client.ui.RadioButton; | |
7 | 4 |
8 import java.util.ArrayList; | 5 import java.util.ArrayList; |
9 import java.util.List; | 6 import java.util.List; |
10 | 7 |
11 public class RadioChooser extends Composite { | 8 public class RadioChooser { |
| 9 public static interface Display { |
| 10 public IRadioButton generateRadioButton(String groupName, String choice)
; |
| 11 } |
| 12 |
12 private static int groupNameCounter = 0; | 13 private static int groupNameCounter = 0; |
13 | |
14 private List<RadioButton> radioButtons = new ArrayList<RadioButton>(); | |
15 private RadioButton defaultButton; | |
16 private Panel container = new HorizontalPanel(); | |
17 private String groupName = getFreshGroupName(); | 14 private String groupName = getFreshGroupName(); |
18 | 15 private List<IRadioButton> radioButtons = new ArrayList<IRadioButton>(); |
19 public RadioChooser() { | 16 private IRadioButton defaultButton; |
20 initWidget(container); | 17 |
21 setStyleName("radio-chooser"); | 18 private Display display; |
| 19 |
| 20 public void bindDisplay(Display display) { |
| 21 this.display = display; |
22 } | 22 } |
23 | 23 |
24 private static String getFreshGroupName() { | 24 private static String getFreshGroupName() { |
25 groupNameCounter++; | 25 groupNameCounter++; |
26 return "group" + Integer.toString(groupNameCounter); | 26 return "group" + Integer.toString(groupNameCounter); |
27 } | 27 } |
28 | 28 |
29 public void addChoice(String choice) { | 29 public void addChoice(String choice) { |
30 RadioButton button = new RadioButton(groupName, choice); | 30 IRadioButton button = display.generateRadioButton(groupName, choice); |
31 if (radioButtons.isEmpty()) { | 31 if (radioButtons.isEmpty()) { |
32 // first button in this group | 32 // first button in this group |
33 defaultButton = button; | 33 defaultButton = button; |
34 button.setValue(true); | 34 button.setValue(true); |
35 } | 35 } |
36 radioButtons.add(button); | 36 radioButtons.add(button); |
37 container.add(button); | |
38 } | 37 } |
39 | 38 |
40 public String getSelectedChoice() { | 39 public String getSelectedChoice() { |
41 for (RadioButton button : radioButtons) { | 40 for (IRadioButton button : radioButtons) { |
42 if (button.getValue()) { | 41 if (button.getValue()) { |
43 return button.getText(); | 42 return button.getText(); |
44 } | 43 } |
45 } | 44 } |
46 throw new RuntimeException("No radio button selected"); | 45 throw new RuntimeException("No radio button selected"); |
47 } | 46 } |
48 | 47 |
49 public void reset() { | 48 public void reset() { |
50 if (defaultButton != null) { | 49 if (defaultButton != null) { |
51 defaultButton.setValue(true); | 50 defaultButton.setValue(true); |
52 } | 51 } |
53 } | 52 } |
54 | 53 |
55 public void setDefaultChoice(String defaultChoice) { | 54 public void setDefaultChoice(String defaultChoice) { |
56 defaultButton = findButtonForChoice(defaultChoice); | 55 defaultButton = findButtonForChoice(defaultChoice); |
57 } | 56 } |
58 | 57 |
59 public void setSelectedChoice(String choice) { | 58 public void setSelectedChoice(String choice) { |
60 findButtonForChoice(choice).setValue(true); | 59 findButtonForChoice(choice).setValue(true); |
61 } | 60 } |
62 | 61 |
63 private RadioButton findButtonForChoice(String choice) { | 62 private IRadioButton findButtonForChoice(String choice) { |
64 for (RadioButton button : radioButtons) { | 63 for (IRadioButton button : radioButtons) { |
65 if (button.getText().equals(choice)) { | 64 if (button.getText().equals(choice)) { |
66 return button; | 65 return button; |
67 } | 66 } |
68 } | 67 } |
69 throw new RuntimeException("No such choice found: " + choice); | 68 throw new RuntimeException("No such choice found: " + choice); |
70 } | 69 } |
71 } | 70 } |
OLD | NEW |