001 // Copyright 2009 The Apache Software Foundation
002 //
003 // Licensed under the Apache License, Version 2.0 (the "License");
004 // you may not use this file except in compliance with the License.
005 // You may obtain a copy of the License at
006 //
007 // http://www.apache.org/licenses/LICENSE-2.0
008 //
009 // Unless required by applicable law or agreed to in writing, software
010 // distributed under the License is distributed on an "AS IS" BASIS,
011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012 // See the License for the specific language governing permissions and
013 // limitations under the License.
014
015 package org.apache.tapestry5.internal.translator;
016
017 import org.apache.tapestry5.Field;
018
019 import java.text.ParseException;
020
021 /**
022 * Used to generate the client-side JSON specification for how a number-based validator operates. Uses {@link
023 * org.apache.tapestry5.ioc.services.ThreadLocale} to determine the locale for any locale-specific operations.
024 *
025 * @since 5.1.0.1
026 */
027 public interface NumericTranslatorSupport
028 {
029 /**
030 * Parses a client-submitted value in a localized manner.
031 *
032 * @param type desired type of value
033 * @param clientValue value from client; this will be trimmed of leading/trailing whitespace
034 * @param <T>
035 * @return the parsed value
036 * @throws ParseException
037 * @see org.apache.tapestry5.Translator#parseClient(org.apache.tapestry5.Field, String, String)
038 */
039 <T extends Number> T parseClient(Class<T> type, String clientValue) throws ParseException;
040
041 /**
042 * Converts a server-side value to a client-side string. Integer types are formatted simply; decimal types may be
043 * formatted using thousands-seperator commas.
044 *
045 * @param type type of value to convert
046 * @param value current (non-null) value
047 * @param <T>
048 * @return value formatted
049 */
050 <T extends Number> String toClient(Class<T> type, T value);
051
052 /**
053 * Returns the default message key for parse failures for the indicated type.
054 *
055 * @param type
056 * @param <T>
057 * @return a message key: either "integer-format-exception" or "number-format-exception"
058 */
059 <T extends Number> String getMessageKey(Class<T> type);
060
061 /**
062 * Adds client-side format validation for the field, appropriate to the indicated type.
063 *
064 * @param type value type
065 * @param field field to which validation should be added
066 * @param message message if the client-side value can't be parsed as a number
067 * @param <T>
068 */
069 <T extends Number> void addValidation(Class<T> type, Field field, String message);
070 }