Author: chris.laprun(a)jboss.com
Date: 2010-01-12 10:09:42 -0500 (Tue, 12 Jan 2010)
New Revision: 1238
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/FormattableValueRenderer.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRenderer.java
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRendererRegistry.java
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/core/
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/core/renderers/
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/core/renderers/ValueRendererRegistryTestCase.java
Modified:
portal/trunk/web/portal/src/main/webapp/groovy/webui/core/UIGrid.gtmpl
portal/trunk/webui/core/pom.xml
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/UIComponent.java
Log:
- Added capability to specify value renderers based on type on UIComponent.
ValueRendererRegistry records which renderer
is used to render which value type. Default renderers are defined and components can
override them or specify private
renderers as well.
- Removed hardcoded rendering of types in UIGrid.gtmpl which should be now a lot more
reusable by leveraging the new
renderer framework.
- Added test case and activated it.
Modified: portal/trunk/web/portal/src/main/webapp/groovy/webui/core/UIGrid.gtmpl
===================================================================
--- portal/trunk/web/portal/src/main/webapp/groovy/webui/core/UIGrid.gtmpl 2010-01-12
12:22:13 UTC (rev 1237)
+++ portal/trunk/web/portal/src/main/webapp/groovy/webui/core/UIGrid.gtmpl 2010-01-12
15:09:42 UTC (rev 1238)
@@ -1,108 +1,108 @@
<%
- import org.exoplatform.webui.core.UIComponent;
- import org.exoplatform.webui.form.UIForm;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
+ import org.exoplatform.webui.core.UIComponent;
+ import org.exoplatform.webui.form.UIForm;
- String[] beanFields = uicomponent.getBeanFields();
- String[] beanActions = uicomponent.getBeanActions();
- UIComponent uiParent = uicomponent.getParent();
- String name = uicomponent.getLabel();
- UIForm uiForm = uicomponent.getAncestorOfType(UIForm.class);
-
- DateFormat dateFormat = null;
+ String[] beanFields = uicomponent.getBeanFields();
+ String[] beanActions = uicomponent.getBeanActions();
+ UIComponent uiParent = uicomponent.getParent();
+ String name = uicomponent.getLabel();
+ UIForm uiForm = uicomponent.getAncestorOfType(UIForm.class);
%>
<div id="$uicomponent.id">
- <table class="UIGrid" cellspacing="0">
- <thead>
- <tr>
- <%if(name != null) {%>
- <%for(field in beanFields) { %>
- <th><%=_ctx.appRes(name+".header."+field)%></th>
- <%}%>
- <%if(beanActions != null) { %>
- <th><%=_ctx.appRes(name+".header.action")%></th>
- <%}%>
- <%}%>
- <%if(name == null) {%>
- <%for(field in beanFields) { %>
- <th><%=_ctx.appRes(uiParent.getName()+".header."+field)%></th>
- <%}%>
- <%if(beanActions != null && beanActions.length > 0) { %>
- <th><%=_ctx.appRes(uiParent.getName()+".header.action")%></th>
- <%}%>
- <%}%>
- </tr>
- </thead>
- <tbody>
- <%if(uicomponent.getUIPageIterator().getAvailable() < 1) {%>
- <tr>
- <td style="font-style:italic; color: #FF5604; text-align: center;"
colspan="<%=beanFields.length+1%>">
- <%=_ctx.appRes("UIGrid.msg.empty")%>
- </td>
- </tr>
- <%} else {
- def rowClass = null;
- boolean even = true;
- for (bean in uicomponent.getBeans()) {
- if(even) rowClass = "EvenRow";
- else rowClass = "OddRow";
- even = !even;
- %>
- <tr class="$rowClass">
- <%
- for(field in beanFields) {
- def fieldValue = uicomponent.getFieldValue(bean, field);
- def cssClass = "";
- if(fieldValue != null) {
- def fieldClass = fieldValue.getClass();
- if(fieldClass == Integer.class) cssClass = "number";
- else if(java.util.Date.class.isAssignableFrom(fieldClass)) {
- if(dateFormat == null) dateFormat = new SimpleDateFormat("HH:mm:ss
yyyy-MM-dd");
- cssClass = "Datetime";
- fieldValue = dateFormat.format(fieldValue);
- }
- else cssClass = "Text";
- } else {
- fieldValue = "";
- }
- String value = fieldValue.toString();
- println "<td><div class=\""+cssClass+"\"
title='$fieldValue'>"+fieldValue+"</div></td>";
- }
- if(beanActions != null && beanActions.length > 0) {
- %>
- <td>
- <div class="ActionContainer">
- <%
- def beanIdField = uicomponent.getBeanIdField();
- for(action in beanActions) {
- def beanId = uicomponent.getFieldValue(bean, beanIdField) ;
- if(action == null) continue;
- String title = _ctx.appRes(uicomponent.getParent().getName() +
".action.title." + action);
- String actionLink = "";
- if(uiForm != null){
- actionLink = uiForm.event(action, uicomponent.getParent().getId(), beanId);
- } else {
- actionLink = uiParent.event(action, beanId);
- }
- %>
- <img onclick="$actionLink" alt=""
title="$title" src="/eXoResources/skin/sharedImages/Blank.gif"
class="${action}Icon" />
- <%}%>
- </div>
- </td>
- <%
- }
- %>
- </tr>
- <%
- }
- }
- %>
- </tbody>
- </table> <!--End UIGrid-->
- <%
- if(uicomponent.getUIPageIterator().getAvailablePage() > 1) {
- _ctx.renderUIComponent(uicomponent.getUIPageIterator());
- }
- %>
+ <table class="UIGrid" cellspacing="0">
+ <thead>
+ <tr>
+ <% if (name != null)
+ { %>
+ <% for (field in beanFields)
+ { %>
+ <th><%=_ctx.appRes(name + ".header." +
field)%></th>
+ <% } %>
+ <% if (beanActions != null)
+ { %>
+ <th><%=_ctx.appRes(name + ".header.action")%></th>
+ <% } %>
+ <% } %>
+ <% if (name == null)
+ { %>
+ <% for (field in beanFields)
+ { %>
+ <th><%=_ctx.appRes(uiParent.getName() + ".header." +
field)%></th>
+ <% } %>
+ <% if (beanActions != null && beanActions.length > 0)
+ { %>
+ <th><%=_ctx.appRes(uiParent.getName() +
".header.action")%></th>
+ <% } %>
+ <% } %>
+ </tr>
+ </thead>
+ <tbody>
+ <% if (uicomponent.getUIPageIterator().getAvailable() < 1)
+ { %>
+ <tr>
+ <td style="font-style:italic; color: #FF5604; text-align: center;"
colspan="<%=beanFields.length + 1%>">
+ <%=_ctx.appRes("UIGrid.msg.empty")%>
+ </td>
+ </tr>
+ <% }
+ else
+ {
+ def rowClass = null;
+ boolean even = true;
+ for (bean in uicomponent.getBeans())
+ {
+ if (even) rowClass = "EvenRow";
+ else rowClass = "OddRow";
+ even = !even;
+ %>
+ <tr class="$rowClass">
+ <%
+ for (field in beanFields)
+ {
+ def fieldValue = uicomponent.getFieldValue(bean, field);
+ def renderer = uicomponent.getRendererFor(fieldValue);
+ println "<td><div class=\"" +
renderer.getCSSClassFor(fieldValue) + "\" title='$fieldValue'>"
+ renderer.render(fieldValue) + "</div></td>";
+ }
+ if (beanActions != null && beanActions.length > 0)
+ {
+ %>
+ <td>
+ <div class="ActionContainer">
+ <%
+ def beanIdField = uicomponent.getBeanIdField();
+ for (action in beanActions)
+ {
+ def beanId = uicomponent.getFieldValue(bean, beanIdField);
+ if (action == null) continue;
+ String title = _ctx.appRes(uicomponent.getParent().getName() +
".action.title." + action);
+ String actionLink = "";
+ if (uiForm != null)
+ {
+ actionLink = uiForm.event(action,
uicomponent.getParent().getId(), beanId);
+ }
+ else
+ {
+ actionLink = uiParent.event(action, beanId);
+ }
+ %>
+ <img onclick="$actionLink" alt=""
title="$title" src="/eXoResources/skin/sharedImages/Blank.gif"
class="${action}Icon"/>
+ <% } %>
+ </div>
+ </td>
+ <%
+ }
+ %>
+ </tr>
+ <%
+ }
+ }
+ %>
+ </tbody>
+ </table> <!--End UIGrid-->
+<%
+ if (uicomponent.getUIPageIterator().getAvailablePage() > 1)
+ {
+ _ctx.renderUIComponent(uicomponent.getUIPageIterator());
+ }
+%>
</div>
\ No newline at end of file
Modified: portal/trunk/webui/core/pom.xml
===================================================================
--- portal/trunk/webui/core/pom.xml 2010-01-12 12:22:13 UTC (rev 1237)
+++ portal/trunk/webui/core/pom.xml 2010-01-12 15:09:42 UTC (rev 1238)
@@ -19,63 +19,71 @@
-->
-<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
- <parent>
- <groupId>org.exoplatform.portal</groupId>
- <artifactId>exo.portal.webui</artifactId>
- <version>3.0.0-Beta05-SNAPSHOT</version>
- </parent>
-
- <modelVersion>4.0.0</modelVersion>
- <artifactId>exo.portal.webui.core</artifactId>
- <packaging>jar</packaging>
- <name>GateIn Portal WebUI Core</name>
-
- <properties>
- <maven.test.skip>true</maven.test.skip>
- </properties>
-
- <dependencies>
- <dependency>
+<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
<groupId>org.exoplatform.portal</groupId>
- <artifactId>exo.portal.component.web</artifactId>
+ <artifactId>exo.portal.webui</artifactId>
<version>3.0.0-Beta05-SNAPSHOT</version>
- </dependency>
- <dependency>
- <groupId>org.gatein.common</groupId>
- <artifactId>common-common</artifactId>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.jibx</groupId>
- <artifactId>maven-jibx-plugin</artifactId>
- <version>${org.jibx.version}</version>
- <configuration>
- <directory>src/main/resources</directory>
- <includes>
- <includes>binding.xml</includes>
- </includes>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>bind</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
+ </parent>
- <!-- -->
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <configuration>
- <compilerArgument>-proc:none</compilerArgument>
- </configuration>
- </plugin>
- </plugins>
- </build>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>exo.portal.webui.core</artifactId>
+ <packaging>jar</packaging>
+ <name>GateIn Portal WebUI Core</name>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.exoplatform.portal</groupId>
+ <artifactId>exo.portal.component.web</artifactId>
+ <version>3.0.0-Beta05-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.gatein.common</groupId>
+ <artifactId>common-common</artifactId>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.jibx</groupId>
+ <artifactId>maven-jibx-plugin</artifactId>
+ <version>${org.jibx.version}</version>
+ <configuration>
+ <directory>src/main/resources</directory>
+ <includes>
+ <includes>binding.xml</includes>
+ </includes>
+ </configuration>
+ <executions>
+ <execution>
+ <goals>
+ <goal>bind</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+
+ <!-- -->
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <configuration>
+ <compilerArgument>-proc:none</compilerArgument>
+ </configuration>
+ </plugin>
+
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <exclude>org/exoplatform/webui/test/**</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+
+ </plugins>
+ </build>
</project>
Modified:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/UIComponent.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/UIComponent.java 2010-01-12
12:22:13 UTC (rev 1237)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/UIComponent.java 2010-01-12
15:09:42 UTC (rev 1238)
@@ -26,6 +26,8 @@
import org.exoplatform.webui.application.WebuiApplication;
import org.exoplatform.webui.application.WebuiRequestContext;
import org.exoplatform.webui.config.Component;
+import org.exoplatform.webui.core.renderers.ValueRenderer;
+import org.exoplatform.webui.core.renderers.ValueRendererRegistry;
import org.exoplatform.webui.event.Event;
import org.exoplatform.webui.event.Event.Phase;
import org.exoplatform.webui.event.EventListener;
@@ -52,6 +54,8 @@
transient protected Component config;
+ transient private ValueRendererRegistry rendererRegistry = new
ValueRendererRegistry();
+
public String getId()
{
return this.id;
@@ -458,4 +462,14 @@
}
return null;
}
+
+ public ValueRenderer getRendererFor(Object value)
+ {
+ return rendererRegistry.getRendererFor(value == null ? null : value.getClass());
+ }
+
+ public <ValueType> void registerRendererFor(ValueRenderer<ValueType>
renderer, Class<? extends ValueType> valueType)
+ {
+ rendererRegistry.registerRendererFor(renderer, valueType);
+ }
}
\ No newline at end of file
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/FormattableValueRenderer.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/FormattableValueRenderer.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/FormattableValueRenderer.java 2010-01-12
15:09:42 UTC (rev 1238)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.core.renderers;
+
+import java.text.Format;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class FormattableValueRenderer extends ValueRenderer<Object>
+{
+ private Format format;
+ private String cssClass;
+
+ public FormattableValueRenderer(Format format, String cssClass)
+ {
+ this.format = format;
+ this.cssClass = cssClass;
+ }
+
+ @Override
+ public String render(Object value)
+ {
+ if (format != null)
+ {
+ return format.format(value);
+ }
+ else
+ {
+ return super.render(value);
+ }
+ }
+
+ @Override
+ public String getCSSClassFor(Object value)
+ {
+ if (cssClass != null)
+ {
+ return cssClass;
+ }
+ else
+ {
+ return super.getCSSClassFor(value);
+ }
+ }
+}
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRenderer.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRenderer.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRenderer.java 2010-01-12
15:09:42 UTC (rev 1238)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.core.renderers;
+
+import org.gatein.common.util.ParameterValidation;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class ValueRenderer<ValueType>
+{
+ public static final String EMPTY = "";
+ public static final String DEFAULT_CSS_CLASS = "Text";
+ public static final ValueRenderer<Object> DEFAULT_RENDERER = new
ValueRenderer<Object>();
+ public static final ValueRenderer NULL_RENDERER = new ValueRenderer()
+ {
+ @Override
+ public String render(Object value)
+ {
+ return EMPTY;
+ }
+
+ @Override
+ public String getCSSClassFor(Object value)
+ {
+ return EMPTY;
+ }
+ };
+
+
+ public String render(ValueType value)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(value, "Value");
+ return value.toString();
+ }
+
+ public String getCSSClassFor(ValueType value)
+ {
+ return DEFAULT_CSS_CLASS;
+ }
+}
Added:
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRendererRegistry.java
===================================================================
---
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRendererRegistry.java
(rev 0)
+++
portal/trunk/webui/core/src/main/java/org/exoplatform/webui/core/renderers/ValueRendererRegistry.java 2010-01-12
15:09:42 UTC (rev 1238)
@@ -0,0 +1,142 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.core.renderers;
+
+import org.gatein.common.util.ParameterValidation;
+
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class ValueRendererRegistry
+{
+ private static Map<Class, ValueRenderer> DEFAULT_RENDERERS;
+ private Map<Class, ValueRenderer> renderers;
+
+ static
+ {
+ FormattableValueRenderer numberRenderer = new FormattableValueRenderer(null,
"number");
+ registerDefaultRendererFor(numberRenderer, Number.class);
+ registerDefaultRendererFor(numberRenderer, Byte.class);
+ registerDefaultRendererFor(numberRenderer, Double.class);
+ registerDefaultRendererFor(numberRenderer, Float.class);
+ registerDefaultRendererFor(numberRenderer, Integer.class);
+ registerDefaultRendererFor(numberRenderer, Long.class);
+ registerDefaultRendererFor(numberRenderer, Short.class);
+
+ FormattableValueRenderer dateRenderer = new FormattableValueRenderer(new
SimpleDateFormat("HH:mm:ss yyyy-MM-dd"), "Datetime");
+ registerDefaultRendererFor(dateRenderer, Date.class);
+ registerDefaultRendererFor(dateRenderer, java.sql.Date.class);
+ registerDefaultRendererFor(dateRenderer, Time.class);
+ registerDefaultRendererFor(dateRenderer, Timestamp.class);
+ }
+
+
+ public <ValueType> ValueRenderer<ValueType> getRendererFor(Class<?
extends ValueType> valueType)
+ {
+ if (valueType == null)
+ {
+ return ValueRenderer.NULL_RENDERER;
+ }
+
+ // first check local renderers
+ ValueRenderer renderer = getRendererIn(valueType, renderers);
+ if (renderer == null)
+ {
+ // then globally registered ones
+ renderer = getRendererIn(valueType, DEFAULT_RENDERERS);
+
+ // if we haven't found a match, check inheritance and return first match
+ if (renderer == null)
+ {
+ for (Map.Entry<Class, ValueRenderer> entry :
DEFAULT_RENDERERS.entrySet())
+ {
+ Class type = entry.getKey();
+ if (type.isAssignableFrom(valueType))
+ {
+ renderer = entry.getValue();
+
+ // add the found renderers to the default ones so that further look-ups
will be faster
+ registerDefaultRendererFor(renderer, type);
+
+ break;
+ }
+ }
+
+ // if we still haven't found one, use the default
+ if (renderer == null)
+ {
+ renderer = ValueRenderer.DEFAULT_RENDERER;
+ }
+ }
+
+ }
+
+ return renderer;
+ }
+
+ public static <ValueType> void
registerDefaultRendererFor(ValueRenderer<ValueType> renderer, Class<? extends
ValueType> type)
+ {
+ DEFAULT_RENDERERS = registerIn(renderer, type, DEFAULT_RENDERERS);
+ }
+
+ public <ValueType> void registerRendererFor(ValueRenderer<ValueType>
renderer, Class<? extends ValueType> type)
+ {
+ renderers = registerIn(renderer, type, renderers);
+ }
+
+ private static <ValueType> Map<Class, ValueRenderer>
registerIn(ValueRenderer<ValueType> renderer, Class<? extends ValueType> type,
Map<Class, ValueRenderer> renderers)
+ {
+ ParameterValidation.throwIllegalArgExceptionIfNull(type, "Value class");
+ ParameterValidation.throwIllegalArgExceptionIfNull(renderer,
"Renderer");
+
+ if (renderers == null)
+ {
+ renderers = new HashMap<Class, ValueRenderer>(7);
+ }
+
+ renderers.put(type, renderer);
+
+ return renderers;
+ }
+
+ private static ValueRenderer getRendererIn(Class valueType, Map<Class,
ValueRenderer> renderers)
+ {
+ if (renderers == null)
+ {
+ return null;
+ }
+ else
+ {
+ return renderers.get(valueType);
+ }
+ }
+}
Added:
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/core/renderers/ValueRendererRegistryTestCase.java
===================================================================
---
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/core/renderers/ValueRendererRegistryTestCase.java
(rev 0)
+++
portal/trunk/webui/core/src/test/java/org/exoplatform/webui/core/renderers/ValueRendererRegistryTestCase.java 2010-01-12
15:09:42 UTC (rev 1238)
@@ -0,0 +1,97 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2010, Red Hat Middleware, LLC, and individual
+ * contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of
+ * individual contributors.
+ *
+ * This 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 software 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 software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+
+package org.exoplatform.webui.core.renderers;
+
+import junit.framework.TestCase;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @author <a href="mailto:chris.laprun@jboss.com">Chris
Laprun</a>
+ * @version $Revision$
+ */
+public class ValueRendererRegistryTestCase extends TestCase
+{
+ private ValueRendererRegistry registry;
+ private ValueRenderer renderer;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ registry = new ValueRendererRegistry();
+ renderer = null;
+ }
+
+ public void testGetDefaultRenderer()
+ {
+ renderer = registry.getRendererFor(Object.class);
+ assertEquals(ValueRenderer.DEFAULT_RENDERER, renderer);
+ assertEquals(ValueRenderer.DEFAULT_CSS_CLASS, renderer.getCSSClassFor(new
Object()));
+ }
+
+ public void testRegisterLocalRenderer()
+ {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
+ String css = "testDate";
+ registry.registerRendererFor(new FormattableValueRenderer(dateFormat, css),
TestDate.class);
+
+ TestDate date = new TestDate(2010, 1, 12, 13, 37, 0);
+ renderer = registry.getRendererFor(date.getClass());
+ assertNotNull(renderer);
+ assertEquals(dateFormat.format(date), renderer.render(date));
+ assertEquals(css, renderer.getCSSClassFor(date));
+ }
+
+ public void testRenderNullValue()
+ {
+ renderer = registry.getRendererFor(null);
+ assertNotNull(renderer);
+ assertEquals(ValueRenderer.EMPTY, renderer.render(null));
+ assertEquals(ValueRenderer.EMPTY, renderer.getCSSClassFor(null));
+ }
+
+ public void testSupportsPreviousUIGridScenario()
+ {
+ renderer = registry.getRendererFor(Integer.class);
+ assertNotNull(renderer);
+ assertEquals("100", renderer.render(100));
+ assertEquals("number", renderer.getCSSClassFor(100));
+
+ SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss
yyyy-MM-dd");
+ TestDate date = new TestDate(2010, 1, 12, 13, 37, 0);
+ renderer = registry.getRendererFor(date.getClass());
+ assertNotNull(renderer);
+ assertEquals(dateFormat.format(date), renderer.render(date));
+ assertEquals("Datetime", renderer.getCSSClassFor(date));
+ }
+
+ private static class TestDate extends Date
+ {
+ private TestDate(int year, int month, int date, int hrs, int min, int sec)
+ {
+ super(year, month, date, hrs, min, sec);
+ }
+ }
+}