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