[corejsf~subversion:166] ...
- From: cayhorstmann@kenai.com
- To: commits@corejsf.kenai.com
- Subject: [corejsf~subversion:166] ...
- Date: Tue, 2 Feb 2010 04:53:33 +0000
Project: corejsf
Repository: subversion
Revision: 166
Author: cayhorstmann
Date: 2010-02-02 04:53:31 UTC
Link:
Log Message:
------------
...
Revisions:
----------
166
Added Paths:
------------
ch09/composite-date/web/WEB-INF
ch09/composite-date/web/resources/util
ch09/composite-date/web/WEB-INF/web.xml
ch09/composite-date/web/WEB-INF/sun-web.xml
ch09/composite-date/src/java/util/messages.properties
ch09/composite-date/src/java/com/corejsf/UserBean.java
ch09/composite-date/web/resources/util/date.xhtml
ch09/composite-date/src/java/com/corejsf/util/Messages.java
ch09/composite-date/src
ch09/composite-date/web/index.xhtml
ch09/composite-date/web/WEB-INF/beans.xml
ch09/composite-date/web/WEB-INF/faces-config.xml
ch09/composite-date/src/java/util
ch09/composite-date/web/resources/css
ch09/composite-date/src/java/com/corejsf
ch09/composite-date/web/resources/css/styles.css
ch09/composite-date/src/java/util/date.java
ch09/composite-date/src/java
ch09/composite-date/src/java/com/corejsf/util
ch09/composite-date/src/java/com
ch09/composite-date
ch09/composite-date/web
ch09/composite-date/web/welcome.xhtml
ch09/composite-date/web/resources
ch09/composite-date/src/java/com/corejsf/Dates.java
Diffs:
------
Index: ch09/composite-date/src/java/util/messages.properties
===================================================================
--- ch09/composite-date/src/java/util/messages.properties (revision 0)
+++ ch09/composite-date/src/java/util/messages.properties (revision 166)
@@ -0,0 +1,2 @@
+invalidDate=Invalid date.
+invalidDate_detail=The entered date is not valid.
\ No newline at end of file
Index: ch09/composite-date/src/java/util/date.java
===================================================================
--- ch09/composite-date/src/java/util/date.java (revision 0)
+++ ch09/composite-date/src/java/util/date.java (revision 166)
@@ -0,0 +1,77 @@
+package util;
+
+import com.corejsf.util.Messages;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.Locale;
+import java.util.ResourceBundle;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.NamingContainer;
+import javax.faces.component.UIInput;
+import javax.faces.context.FacesContext;
+import javax.faces.convert.ConverterException;
+
+public class date extends UIInput implements NamingContainer {
+ public String getFamily() {
+ return "javax.faces.NamingContainer";
+ }
+
+ public void encodeBegin(FacesContext context) throws IOException {
+ Date date = (Date) getValue();
+ Calendar cal = new GregorianCalendar();
+ cal.setTime(date);
+ UIInput dayComponent = (UIInput) findComponent("day");
+ UIInput monthComponent = (UIInput) findComponent("month");
+ UIInput yearComponent = (UIInput) findComponent("year");
+ dayComponent.setValue(cal.get(Calendar.DATE));
+ monthComponent.setValue(cal.get(Calendar.MONTH) + 1);
+ yearComponent.setValue(cal.get(Calendar.YEAR));
+ super.encodeBegin(context);
+ }
+
+ public Object getSubmittedValue() {
+ return this;
+ }
+
+ protected Object getConvertedValue(FacesContext context, Object
newSubmittedValue)
+ throws ConverterException {
+ UIInput dayComponent = (UIInput) findComponent("day");
+ UIInput monthComponent = (UIInput) findComponent("month");
+ UIInput yearComponent = (UIInput) findComponent("year");
+ int day = (Integer) dayComponent.getValue();
+ int month = (Integer) monthComponent.getValue();
+ int year = (Integer) yearComponent.getValue();
+ if (isValidDate(day, month, year))
+ return new GregorianCalendar(year, month - 1, day).getTime();
+ else {
+ FacesMessage message
+ = Messages.getMessage("util.messages", "invalidDate", null);
+ message.setSeverity(FacesMessage.SEVERITY_ERROR);
+ throw new ConverterException(message);
+ }
+ }
+
+ private static boolean isValidDate(int d, int m, int y) {
+ if (d < 1 || m < 1 || m > 12) {
+ return false;
+ }
+ if (m == 2) {
+ if (isLeapYear(y)) {
+ return d <= 29;
+ } else {
+ return d <= 28;
+ }
+ } else if (m == 4 || m == 6 || m == 9 || m == 11) {
+ return d <= 30;
+ } else {
+ return d <= 31;
+ }
+ }
+
+ private static boolean isLeapYear(int y) {
+ return y % 4 == 0 && (y % 400 == 0 || y % 100 != 0);
+ }
+}
\ No newline at end of file
Index: ch09/composite-date/src/java/com/corejsf/Dates.java
===================================================================
--- ch09/composite-date/src/java/com/corejsf/Dates.java (revision 0)
+++ ch09/composite-date/src/java/com/corejsf/Dates.java (revision 166)
@@ -0,0 +1,35 @@
+package com.corejsf;
+import java.io.Serializable;
+import java.text.DateFormatSymbols;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import javax.inject.Named;
+ // or import javax.faces.bean.ManagedBean;
+import javax.enterprise.context.ApplicationScoped;
+ // or import javax.faces.bean.ApplicationScoped;
+
+@Named // or @ManagedBean
+@ApplicationScoped
+public class Dates implements Serializable {
+ private int[] days;
+ private int[] years;
+ private Map<String, Integer> months;
+
+ private static int[] intArray(int from, int to) {
+ int[] result = new int[to - from + 1];
+ for (int i = from; i <= to; i++) result[i - from] = i;
+ return result;
+ }
+
+ public Dates() {
+ days = intArray(1, 31);
+ years = intArray(1900, 2100);
+ months = new LinkedHashMap<String, Integer>();
+ String[] names = new DateFormatSymbols().getMonths();
+ for (int i = 0; i < 12; i++) months.put(names[i], i + 1);
+ }
+
+ public int[] getDays() { return days; }
+ public int[] getYears() { return years; }
+ public Map<String, Integer> getMonths() { return months; }
+}
Index: ch09/composite-date/src/java/com/corejsf/UserBean.java
===================================================================
--- ch09/composite-date/src/java/com/corejsf/UserBean.java (revision 0)
+++ ch09/composite-date/src/java/com/corejsf/UserBean.java (revision 166)
@@ -0,0 +1,27 @@
+package com.corejsf;
+
+import java.io.Serializable;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import javax.inject.Named;
+ // or import javax.faces.bean.ManagedBean;
+import javax.enterprise.context.SessionScoped;
+ // or import javax.faces.bean.SessionScoped;
+import javax.validation.constraints.Past;
+
+@Named("user") // or @ManagedBean(name="user")
+@SessionScoped
+public class UserBean implements Serializable {
+ private String name;
+ private String password;
+ @Past private Date birthday = new GregorianCalendar(2000, 0, 1).getTime();
+
+ public String getName() { return name; }
+ public void setName(String newValue) { name = newValue; }
+
+ public String getPassword() { return password; }
+ public void setPassword(String newValue) { password = newValue; }
+
+ public Date getBirthday() { return birthday; }
+ public void setBirthday(Date newValue) { birthday = newValue; }
+}
Index: ch09/composite-date/src/java/com/corejsf/util/Messages.java
===================================================================
--- ch09/composite-date/src/java/com/corejsf/util/Messages.java (revision 0)
+++ ch09/composite-date/src/java/com/corejsf/util/Messages.java (revision 166)
@@ -0,0 +1,82 @@
+package com.corejsf.util;
+
+import java.text.MessageFormat;
+import java.util.Locale;
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+import javax.faces.application.Application;
+import javax.faces.application.FacesMessage;
+import javax.faces.component.UIViewRoot;
+import javax.faces.context.FacesContext;
+
+public class Messages {
+ public static FacesMessage getMessage(String bundleName, String
resourceId,
+ Object[] params) {
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application app = context.getApplication();
+ String appBundle = app.getMessageBundle();
+ Locale locale = getLocale(context);
+ ClassLoader loader = getClassLoader();
+ String summary = getString(appBundle, bundleName, resourceId,
+ locale, loader, params);
+ if (summary == null) summary = "???" + resourceId + "???";
+ String detail = getString(appBundle, bundleName, resourceId +
"_detail",
+ locale, loader, params);
+ return new FacesMessage(summary, detail);
+ }
+
+ public static String getString(String bundle, String resourceId,
+ Object[] params) {
+ FacesContext context = FacesContext.getCurrentInstance();
+ Application app = context.getApplication();
+ String appBundle = app.getMessageBundle();
+ Locale locale = getLocale(context);
+ ClassLoader loader = getClassLoader();
+ return getString(appBundle, bundle, resourceId, locale, loader,
params);
+ }
+
+ public static String getString(String bundle1, String bundle2,
+ String resourceId, Locale locale, ClassLoader loader,
+ Object[] params) {
+ String resource = null;
+ ResourceBundle bundle;
+
+ if (bundle1 != null) {
+ bundle = ResourceBundle.getBundle(bundle1, locale, loader);
+ if (bundle != null)
+ try {
+ resource = bundle.getString(resourceId);
+ } catch (MissingResourceException ex) {
+ }
+ }
+
+ if (resource == null) {
+ bundle = ResourceBundle.getBundle(bundle2, locale, loader);
+ if (bundle != null)
+ try {
+ resource = bundle.getString(resourceId);
+ } catch (MissingResourceException ex) {
+ }
+ }
+
+ if (resource == null) return null; // no match
+ if (params == null) return resource;
+
+ MessageFormat formatter = new MessageFormat(resource, locale);
+ return formatter.format(params);
+ }
+
+ public static Locale getLocale(FacesContext context) {
+ Locale locale = null;
+ UIViewRoot viewRoot = context.getViewRoot();
+ if (viewRoot != null) locale = viewRoot.getLocale();
+ if (locale == null) locale = Locale.getDefault();
+ return locale;
+ }
+
+ public static ClassLoader getClassLoader() {
+ ClassLoader loader = Thread.currentThread().getContextClassLoader();
+ if (loader == null) loader = ClassLoader.getSystemClassLoader();
+ return loader;
+ }
+}
\ No newline at end of file
Index: ch09/composite-date/web/index.xhtml
===================================================================
--- ch09/composite-date/web/index.xhtml (revision 0)
+++ ch09/composite-date/web/index.xhtml (revision 166)
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:h="http://java.sun.com/jsf/html"
+ xmlns:ui="http://java.sun.com/jsf/facelets"
+ xmlns:util="http://java.sun.com/jsf/composite/util">
+ <h:head>
+ <title>Welcome</title>
+ <h:outputStylesheet library="css" name="styles.css"/>
+ </h:head>
+ <h:body>
+ <h:form>
+ <h3>Please enter your birthday.</h3>
+ <util:date id="date" value="#{user.birthday}"/>
+ <br/><h:message for="date" styleClass="error"/>
+ <p><h:commandButton value="Next" action="welcome"/></p>
+ </h:form>
+
+ </h:body>
+</html>
\ No newline at end of file
Index: ch09/composite-date/web/welcome.xhtml
===================================================================
--- ch09/composite-date/web/welcome.xhtml (revision 0)
+++ ch09/composite-date/web/welcome.xhtml (revision 166)
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:h="http://java.sun.com/jsf/html">
+ <h:head>
+ <title>Welcome</title>
+ </h:head>
+ <h:body>
+ <h:form>
+ Your birthday is #{user.birthday}!
+ <br/>
+ <h:commandButton value="Back" action="index"/>
+ </h:form>
+ </h:body>
+</html>
\ No newline at end of file
Index: ch09/composite-date/web/WEB-INF/sun-web.xml
===================================================================
--- ch09/composite-date/web/WEB-INF/sun-web.xml (revision 0)
+++ ch09/composite-date/web/WEB-INF/sun-web.xml (revision 166)
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application
Server 9.0 Servlet 2.5//EN"
"http://www.sun.com/software/appserver/dtds/sun-web-app_2_5-0.dtd">
+<sun-web-app error-url="">
+ <context-root>/composite-date</context-root>
+ <class-loader delegate="true"/>
+ <jsp-config>
+ <property name="keepgenerated" value="true">
+ <description>Keep a copy of the generated servlet class' java
code.</description>
+ </property>
+ </jsp-config>
+</sun-web-app>
Index: ch09/composite-date/web/WEB-INF/faces-config.xml
===================================================================
--- ch09/composite-date/web/WEB-INF/faces-config.xml (revision 0)
+++ ch09/composite-date/web/WEB-INF/faces-config.xml (revision 166)
@@ -0,0 +1,13 @@
+<?xml version="1.0"?>
+<faces-config xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+ http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
+ version="2.0">
+ <application>
+ <resource-bundle>
+ <base-name>com.corejsf.messages</base-name>
+ <var>msgs</var>
+ </resource-bundle>
+ </application>
+</faces-config>
\ No newline at end of file
Index: ch09/composite-date/web/WEB-INF/beans.xml
===================================================================
Index: ch09/composite-date/web/WEB-INF/web.xml
===================================================================
--- ch09/composite-date/web/WEB-INF/web.xml (revision 0)
+++ ch09/composite-date/web/WEB-INF/web.xml (revision 166)
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="http://java.sun.com/xml/ns/javaee"
+ xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
+ http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ version="2.5">
+ <servlet>
+ <servlet-name>Faces Servlet</servlet-name>
+ <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
+ </servlet>
+ <servlet-mapping>
+ <servlet-name>Faces Servlet</servlet-name>
+ <url-pattern>/faces/*</url-pattern>
+ </servlet-mapping>
+ <welcome-file-list>
+ <welcome-file>faces/index.xhtml</welcome-file>
+ </welcome-file-list>
+ <context-param>
+ <param-name>javax.faces.PROJECT_STAGE</param-name>
+ <param-value>Development</param-value>
+ </context-param>
+</web-app>
\ No newline at end of file
Index: ch09/composite-date/web/resources/css/styles.css
===================================================================
--- ch09/composite-date/web/resources/css/styles.css (revision 0)
+++ ch09/composite-date/web/resources/css/styles.css (revision 166)
@@ -0,0 +1,3 @@
+.error {
+ color: red;
+}
\ No newline at end of file
Index: ch09/composite-date/web/resources/util/date.xhtml
===================================================================
--- ch09/composite-date/web/resources/util/date.xhtml (revision 0)
+++ ch09/composite-date/web/resources/util/date.xhtml (revision 166)
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml"
+ xmlns:h="http://java.sun.com/jsf/html"
+ xmlns:composite="http://java.sun.com/jsf/composite"
+ xmlns:f="http://java.sun.com/jsf/core">
+
+ <composite:interface>
+ <composite:attribute name="value" type="java.util.Date"/>
+ </composite:interface>
+
+ <composite:implementation>
+ <h:selectOneMenu id="day" converter="javax.faces.Integer">
+ <f:selectItems value="#{dates.days}"/>
+ </h:selectOneMenu>
+ <h:selectOneMenu id="month" converter="javax.faces.Integer">
+ <f:selectItems value="#{dates.months}"/>
+ </h:selectOneMenu>
+ <h:selectOneMenu id="year" converter="javax.faces.Integer">
+ <f:selectItems value="#{dates.years}"/>
+ </h:selectOneMenu>
+ </composite:implementation>
+</html>
|
[corejsf~subversion:166] ... |
cayhorstmann | 02/02/2010 |





