Data Bean
InternatBean is a Java bean that maps three of its properties (language, date and number) to an HTML form. The bean also has several properties that don't participate to the mapping process (locale, parsedDate, dateExample, dateFormat, parsedNumber, numberExample and numberFormat).
The language property acts as an intermediary between the locale property and the language element of the HTML form. The locale property cannot be mapped directly to a form element because it is neither a standard property nor a data bean. Also, locale is a read only property.
The date property acts as an intermediary between the parsedDate property and the date element of the HTML form. The parsedDate property cannot be mapped directly to a form element because it is neither a standard property nor a data bean.
The number property acts as an intermediary between the parsedNumber property and the number element of the HTML form. The parsedNumber property is a standard property, but it cannot be mapped directly to a form element because its value is converted from a String using java.text.NumberFormat, while that framework uses java.lang.Float for such a conversion.
The getDateFormat() and getNumberFormat() methods return instances of java.text.DateFormat and java.text.NumberFormat that are used to convert the values of date / number to the types of parsedDate / parsedNumber and vice-versa. The setter methods make sure that the values of these properties are kept in synch. The setters also do application-specific validation. The setDate() and setNumber() methods throw an IllegalArgumentException to signal any user error.
The getDateExample() and getNumberExample() return String examples that are inserted within the HTML form page in order to help the users.
InternatBean.java:
package com.devsphere.examples.mapping.internat;
import java.text.*;
import java.util.*;
/**
* InternatBean class
*/
public class InternatBean implements java.io.Serializable {
public static final Date DATE_EXAMPLE = new Date();
public static final float NUMBER_EXAMPLE = 123.45f;
public static final Locale LOCALES[] = {
new Locale("en", "", ""), // English
new Locale("de", "", ""), // German
new Locale("fr", "", ""), // French
new Locale("it", "", ""), // Italian
new Locale("es", "", "") // Spanish
};
private int language;
private String date;
private Date parsedDate;
private DateFormat cachedDateFormat;
private String number;
private float parsedNumber;
private NumberFormat cachedNumberFormat;
/**
* No-arg constructor
*/
public InternatBean() {
}
/**
* Gets the language property
*/
public int getLanguage() {
return this.language;
}
/**
* Sets the language property
*/
public void setLanguage(int value) {
if (value <>= LOCALES.length)
throw new IllegalArgumentException();
language = value;
cachedDateFormat = null;
cachedNumberFormat = null;
}
/**
* Gets the locale object
*/
public Locale getLocale() {
return LOCALES[getLanguage()];
}
/**
* Gets the date property
*/
public String getDate() {
return date;
}
/**
* Sets the date property
*/
public void setDate(String value) {
try {
setParsedDate(getDateFormat().parse(value));
} catch (ParseException e) {
throw new IllegalArgumentException();
}
}
/**
* Gets the parsed date
*/
public Date getParsedDate() {
return parsedDate;
}
/**
* Sets the date property
*/
public void setParsedDate(Date value) {
parsedDate = value;
date = getDateFormat().format(parsedDate);
}
/**
* Gets an example for the date property
*/
public String getDateExample() {
return getDateFormat().format(DATE_EXAMPLE);
}
/**
* Gets the date format
*/
public DateFormat getDateFormat() {
if (cachedDateFormat == null)
cachedDateFormat = DateFormat.getDateInstance(
DateFormat.SHORT, getLocale());
return cachedDateFormat;
}
/**
* Gets the number property
*/
public String getNumber() {
return number;
}
/**
* Sets the number property
*/
public void setNumber(String value) {
try {
setParsedNumber(getNumberFormat().parse(value).floatValue());
number = getNumberFormat().format(parsedNumber);
} catch (ParseException e) {
throw new IllegalArgumentException();
}
}
/**
* Gets the parsed number
*/
public float getParsedNumber() {
return parsedNumber;
}
/**
* Sets the number property
*/
public void setParsedNumber(float value) {
parsedNumber = value;
number = getNumberFormat().format(parsedNumber);
}
/**
* Gets an example for the number property
*/
public String getNumberExample() {
return getNumberFormat().format(NUMBER_EXAMPLE);
}
/**
* Gets the number format
*/
public NumberFormat getNumberFormat() {
if (cachedNumberFormat == null)
cachedNumberFormat = NumberFormat.getNumberInstance(getLocale());
return cachedNumberFormat;
}
}
SOURCE:http://www.devsphere.com/mapping/docs/guide/internat.html
No comments:
Post a Comment