package to.etc.domui.dom.html;

import java.util.*;

import to.etc.domui.component.meta.*;

/**
 * This is a simple marker which groups radiobuttons together. It can be used as a component too.
 *
 * @author <a href="mailto:jal@etc.to">Frits Jalvingh</a>
 * Created on Feb 4, 2011
 */
public class RadioGroup<T> extends Div implements IHasChangeListener {
	static private int m_gidCounter;

	private String m_groupName;

	private List<RadioButton<T>> m_buttonList = new ArrayList<RadioButton<T>>();

	private T m_value;

	private IValueChanged< ? > m_onValueChanged;

	private boolean m_immediate;

	public RadioGroup() {
		m_groupName = "g" + nextID();
	}

	static private synchronized int nextID() {
		return ++m_gidCounter;
	}

	void addButton(RadioButton<T> b) {
		m_buttonList.add(b);
		b.setChecked(MetaManager.areObjectsEqual(m_value, b.getButtonValue()));
	}

	void removeButton(RadioButton<T> b) {
		m_buttonList.remove(b);
	}

	public String getName() {
		return m_groupName;
	}

	public T getValue() {
		return m_value;
	}

	public void setValue(T value) {
		if(MetaManager.areObjectsEqual(value, m_value))
			return;
		m_value = value;
		for(RadioButton<T> rb : getButtonList()) {
			rb.setChecked(MetaManager.areObjectsEqual(value, rb.getButtonValue()));
		}
	}

	void internalSetValue(T newval) {
//		if(m_value != newval)
//			System.out.println("Changed from " + m_value + " to " + newval);
		m_value = newval;
	}

	public List<RadioButton<T>> getButtonList() {
		return Collections.unmodifiableList(m_buttonList);
	}

	@Override
	public IValueChanged< ? > getOnValueChanged() {
		IValueChanged< ? > vc = m_onValueChanged;
		if(null == vc && isImmediate()) {
			return IValueChanged.DUMMY;
		}
		return vc;
	}

	@Override
	public void setOnValueChanged(IValueChanged< ? > onValueChanged) {
		m_onValueChanged = onValueChanged;
	}

	public boolean isImmediate() {
		return m_immediate;
	}

	public void immediate(boolean immediate) {
		m_immediate = immediate;
	}

	public void immediate() {
		m_immediate = true;
	}
}

@@ -36,22 +36,91 @@
 ort 
-java.util.*
+to.etc.domui.component.meta.MetaManager;%0Aimport to.etc.domui.dom.errors.UIMessage
 ;%0A
-%0A
 impo
@@ -139,24 +139,148 @@
 mui.
-component.meta.*
+trouble.ValidationException;%0Aimport to.etc.domui.util.Msgs;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.List
 ;%0A%0A/
@@ -785,16 +785,47 @@
 diate;%0A%0A
+%09private boolean m_mandatory;%0A%0A
 %09public 
@@ -1236,23 +1236,166 @@
 %7B%0A%09%09
-return m_value;
+try %7B%0A%09%09%09validateBindValue();%0A%09%09%09setMessage(null);%0A%09%09%09return m_value;%0A%09%09%7D catch(ValidationException vx) %7B%0A%09%09%09setMessage(UIMessage.error(vx));%0A%09%09%09throw vx;%0A%09%09%7D
 %0A%09%7D%0A
@@ -1787,24 +1787,370 @@
 newval;%0A%09%7D%0A%0A
+%09final public T getBindValue() %7B%0A%09%09validateBindValue();%0A%09%09return m_value;%0A%09%7D%0A%0A%09final public void setBindValue(T value) %7B%0A%09%09if(MetaManager.areObjectsEqual(m_value, value)) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09setValue(value);%0A%09%7D%0A%0A%09private void validateBindValue() %7B%0A%09%09if(isMandatory() && m_value == null) %7B%0A%09%09%09throw new ValidationException(Msgs.MANDATORY);%0A%09%09%7D%0A%09%7D%0A%0A
 %09public List
@@ -2603,32 +2603,168 @@
 _immediate;%0A%09%7D%0A%0A
+%09public boolean isMandatory() %7B%0A%09%09return m_mandatory;%0A%09%7D%0A%0A%09public void setMandatory(boolean mandatory) %7B%0A%09%09m_mandatory = mandatory;%0A%09%7D%0A%0A
 %09public void imm

Allow mandatory on RadioGroup component.