/*
 * DomUI Java User Interface library
 * Copyright (c) 2010 by Frits Jalvingh, Itris B.V.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * See the "sponsors" file for a list of supporters.
 *
 * The latest version of DomUI and related code, support and documentation
 * can be found at http://www.domui.org/
 * The contact for the project is Frits Jalvingh <jal@etc.to>.
 */
package to.etc.domui.converter;

import to.etc.domui.trouble.*;
import to.etc.domui.util.*;

public class MaxMinValidator implements IValueValidator<Number> {
	private Number m_max, m_min;

	/**
	 * Create a validator comparing to these INCLUSIVE bounds.
	 * @param max
	 * @param min
	 */
	public MaxMinValidator(Number min, Number max) {
		m_max = max;
		m_min = min;
	}

	/**
	 * Sigh. Of course Number does not implement Comparable, because that would
	 * be useful.
	 * @see to.etc.domui.converter.IValueValidator#validate(java.lang.Object)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void validate(Number input) throws Exception {
		Class< ? > ac = input.getClass();
		if(m_max.getClass() == ac && m_min.getClass() == ac && input instanceof Comparable< ? >) {
			int r = ((Comparable<Number>) input).compareTo(m_min);
			if(r < 0) {
				throw new ValidationException(Msgs.V_TOOSMALL, m_min.toString());
			}
			r = ((Comparable<Number>) input).compareTo(m_max);
			if(r > 0) {
				throw new ValidationException(Msgs.V_TOOLARGE, m_max.toString());
			}
		} else {
			if(input.doubleValue() > m_max.doubleValue())
				throw new ValidationException(Msgs.V_TOOLARGE, m_max.toString());
			if(input.doubleValue() < m_min.doubleValue())
				throw new ValidationException(Msgs.V_TOOSMALL, m_min.toString());
		}
	}
}

@@ -1104,16 +1104,78 @@
 erter;%0A%0A
+import javax.annotation.*;%0A%0Aimport to.etc.domui.dom.errors.*;%0A
 import t
@@ -1323,16 +1323,60 @@
 m_min;%0A%0A
+%09@Nullable%0A%09private final UIMessage m_msg;%0A%0A
 %09/**%0A%09 *
@@ -1462,16 +1462,120 @@
 ram min%0A
+%09 * @param msg If specified this error message will be shown, otherwise default error message is shown.%0A
 %09 */%0A%09pu
@@ -1617,16 +1617,41 @@
 mber max
+, @Nullable UIMessage msg
 ) %7B%0A%09%09m_
@@ -1676,16 +1676,209 @@
  = min;%0A
+%09%09m_msg = msg;%0A%09%7D%0A%0A%09/**%0A%09 * Create a validator comparing to these INCLUSIVE bounds.%0A%09 * @param max%0A%09 * @param min%0A%09 */%0A%09public MaxMinValidator(Number min, Number max) %7B%0A%09%09this(min, max, null);%0A
 %09%7D%0A%0A%09/**
@@ -2363,32 +2363,13 @@
 hrow
- new ValidationException
+Error
 (Msg
@@ -2383,35 +2383,24 @@
 SMALL, m_min
-.toString()
 );%0A%09%09%09%7D%0A%09%09%09r
@@ -2477,32 +2477,13 @@
 hrow
- new ValidationException
+Error
 (Msg
@@ -2497,35 +2497,24 @@
 LARGE, m_max
-.toString()
 );%0A%09%09%09%7D%0A%09%09%7D 
@@ -2582,32 +2582,13 @@
 hrow
- new ValidationException
+Error
 (Msg
@@ -2606,27 +2606,16 @@
 E, m_max
-.toString()
 );%0A%09%09%09if
@@ -2671,55 +2671,277 @@
 hrow
- new ValidationException(Msgs.V_TOOSMALL, m_min
+Error(Msgs.V_TOOSMALL, m_min);%0A%09%09%7D%0A%09%7D%0A%0A%09private void throwError(String vToosmall, Number val) %7B%0A%09%09if(m_msg != null) %7B%0A%09%09%09throw new ValidationException(m_msg.getBundle(), m_msg.getCode(), m_msg.getParameters());%0A%09%09%7D else %7B%0A%09%09%09throw new ValidationException(vToosmall, val
 .toS

Update MaxMinValidator to accept a custom error message