Java tutorial
/* * Copyright 2008. Mount Sinai Hospital, Toronto, Canada. * * Licensed under the Apache License, Version 2.0. You * can find a copy of the license at: * * http://www.apache.org/licenses/LICENSE-2.0 * * IN NO EVENT SHALL MOUNT SINAI HOSPITAL BE LIABLE TO ANY PARTY FOR DIRECT, * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST * PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, * EVEN IF MOUNT SINAI HOSPITAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH * DAMAGE. * * MOUNT SINAI HOSPITAL SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE AND * ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". * MOUNT SINAI HOSPITAL HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS. */ package org.gwtaf.widgets.generic; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import com.google.gwt.event.dom.client.ChangeEvent; import com.google.gwt.event.dom.client.ChangeHandler; import com.google.gwt.event.dom.client.ClickEvent; import com.google.gwt.event.dom.client.ClickHandler; import com.google.gwt.event.dom.client.HasChangeHandlers; import com.google.gwt.event.dom.client.HasClickHandlers; import com.google.gwt.event.logical.shared.ValueChangeEvent; import com.google.gwt.event.logical.shared.ValueChangeHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.HasValue; import com.google.gwt.user.client.ui.Panel; import com.google.gwt.user.client.ui.RadioButton; /** * A collection of radio buttons given some choices in a String[]. This class * also provides an easy way to get to individual radios based on their name. * * @author Arthur Kalmenson * @author Sergey Vesselov */ public class RadioButtonGroup extends Composite implements HasValue<String>, HasClickHandlers, HasChangeHandlers { /** * A map of question to radio button. */ protected Map<String, RadioButton> choiceToRadio = new HashMap<String, RadioButton>(); /** * The main panel. */ protected Panel mainPanel; /** * The group name of the radio buttons. */ private String groupName; /** * Default empty constructor. */ public RadioButtonGroup() { // providing an automatic unique group name setUpWidget(new Date().getTime() + "" + (long) (Math.random() * 100000000000l)); } /** * Creates a new {@link RadioButtonGroup} * * @param groupName * the name of the radio buttons in this group. */ public RadioButtonGroup(String groupName) { // sanity check assert groupName != null : getClass().getName() + ": groupName can't be null."; setUpWidget(groupName); } private void setUpWidget(String groupName) { this.groupName = groupName; this.mainPanel = new FlowPanel(); mainPanel.setStylePrimaryName("gwtaf-RadioButtonGroup"); initWidget(mainPanel); } /** * Sets the choices for this {@link RadioButtonGroup}. The previous choices * are cleared before the new ones are added. * * @param choices * a list of String choices for the {@link RadioButtonGroup}. */ public void setChoices(String[] choices) { mainPanel.clear(); choiceToRadio.clear(); // make a new radio button for each choice we are given for (String choice : choices) { // create the radio button, add some style to it and add our custom // click listener to catch clicks. RadioButton radio = new RadioButton(groupName, choice); choiceToRadio.put(choice, radio); mainPanel.add(radio); } } /** * Returns a reference of the radio button with the provided choice * * @param choice * the choice of the button to be returned * @return the radio button with specified choice. */ public RadioButton getRadio(String choice) { return choiceToRadio.get(choice); } /** * Returns true if at least one radio in this group is checked. * * @return true if at least one radio in this group is checked. */ public boolean isChecked() { boolean set = false; // if any are checked, that means the group is checked. for (RadioButton radio : choiceToRadio.values()) { if (radio.getValue()) { set = true; break; } } return set; } public String getValue() { String result = null; // find the radio button that's checked, and get it's text. There // shouldn't be any more checked since it's radio buttons. for (Entry<String, RadioButton> element : choiceToRadio.entrySet()) { if (element.getValue().getValue()) { result = element.getKey(); break; } } return result; } public void setValue(String value) { RadioButton buttonToCheck = choiceToRadio.get(value); // check buttonToCheck and uncheck the previous one. if (buttonToCheck != null) { RadioButton checked = getRadio(getValue()); if (checked != null) { checked.setValue(false); } buttonToCheck.setValue(true); } // if the value is null, unselect everything. if (value == null) { for (RadioButton button : choiceToRadio.values()) { button.setValue(false); } } } public void setValue(String arg0, boolean arg1) { setValue(arg0); ValueChangeEvent.fire(RadioButtonGroup.this, getValue()); } /** * Returns the number of {@link RadioButton} this collection has. * * @return the number of {@link RadioButton} this collection has. */ public int size() { return choiceToRadio.size(); } public HandlerRegistration addClickHandler(ClickHandler arg0) { return addDomHandler(arg0, ClickEvent.getType()); } public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { addChangeHandler(new ChangeHandler() { public void onChange(ChangeEvent event) { ValueChangeEvent.fire(RadioButtonGroup.this, getValue()); } }); return addHandler(handler, ValueChangeEvent.getType()); } public HandlerRegistration addChangeHandler(ChangeHandler arg0) { return addDomHandler(arg0, ChangeEvent.getType()); } public String getGroupName() { return groupName; } public void setGroupName(String groupName) { this.groupName = groupName; } }