/**
*
*/
package net.cloudcodex.client.widgets;
import java.util.HashMap;
import java.util.Map;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ToggleButton;
/**
*
* @author Daniel
*
*/
public class ToggleButtonGroup extends HorizontalPanel implements HasClickHandlers {
private final Map<String, ToggleButton> toggleButtons = new HashMap<String, ToggleButton>();
private ToggleButton selectedButton;
public ToggleButtonGroup() {
this.setStyleName("toggleButtonPanel");
}
public void addButton(ToggleButton button) {
button.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
ToggleButton source = (ToggleButton) event.getSource();
setSelectedButton(source);
}
});
this.add(button);
toggleButtons.put(button.getTitle(), button);
}
public void setSelectedButton(ToggleButton source) {
selectedButton = source;
refreshSelectionGroup();
}
private void refreshSelectionGroup() {
for (ToggleButton toggleButton : toggleButtons.values()) {
toggleButton.setDown(false);
}
toggleButtons.get(selectedButton.getTitle()).setDown(true);
}
public ToggleButton getSelectedButton(){
return selectedButton ;
}
@Override
public HandlerRegistration addClickHandler(ClickHandler handler) {
return addDomHandler(handler, ClickEvent.getType());
}
public void setSelectedButton(String selectedToggleButton) {
setSelectedButton(toggleButtons.get(selectedToggleButton));
}
}