[jboss-cvs] jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter ...
Peter Muir
peter at bleepbleep.org.uk
Fri Jun 15 13:06:59 EDT 2007
User: pmuir
Date: 07/06/15 13:06:59
Added: ui/src/main/java/org/jboss/seam/ui/converter
NoSelectionConverter.java EntityConverter.java
EnumConverter.java EntityConverterStore.java
ConverterChain.java DateTimeConverter.java
PrioritizableConverter.java
Log:
Refactor cdk port
Revision Changes Path
1.1 date: 2007/06/15 17:06:58; author: pmuir; state: Exp;jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter/NoSelectionConverter.java
Index: NoSelectionConverter.java
===================================================================
package org.jboss.seam.ui.converter;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
public class NoSelectionConverter implements Converter
{
public static final String NO_SELECTION_VALUE = "org.jboss.seam.ui.NoSelectionConverter.noSelectionValue";
public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException
{
if (value == null) {
return null;
} else if (value.equals(NO_SELECTION_VALUE)) {
return null;
} else {
return ConverterChain.CONTINUE;
}
}
public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException
{
if (value == null) {
return NO_SELECTION_VALUE;
} else {
return ConverterChain.CONTINUE;
}
}
}
1.1 date: 2007/06/15 17:06:58; author: pmuir; state: Exp;jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter/EntityConverter.java
Index: EntityConverter.java
===================================================================
package org.jboss.seam.ui.converter;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.ScopeType.CONVERSATION;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import java.io.Serializable;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import javax.persistence.EntityManager;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.annotations.jsf.Converter;
import org.jboss.seam.core.Expressions.ValueExpression;
/**
* Allows conversion of an entity to/from a key which can be written to a page.
*
* Any annotated Entity will work, or any entity if a PersistenceProvider for your ORM exists
*/
@Name("org.jboss.seam.ui.entityConverter")
@Scope(CONVERSATION)
@Install(precedence = BUILT_IN)
@Converter
@Intercept(NEVER)
public class EntityConverter implements
javax.faces.convert.Converter, Serializable
{
private ValueExpression<EntityManager> entityManager;
private EntityConverterStore entityIdentifierStore;
@Create
public void create()
{
entityIdentifierStore = EntityConverterStore.instance();
}
private void init()
{
if (getEntityManager() != null)
{
entityIdentifierStore.setEntityManager(getEntityManager());
}
}
@SuppressWarnings("unchecked")
@Transactional
public String getAsString(FacesContext facesContext, UIComponent cmp, Object value) throws ConverterException
{
init();
if (value == null)
{
return null;
}
if (value instanceof String)
{
return (String) value;
}
return entityIdentifierStore.put(value).toString();
}
@Transactional
public Object getAsObject(FacesContext facesContext, UIComponent cmp, String value) throws ConverterException
{
init();
if (value == null)
{
return null;
}
return entityIdentifierStore.get(new Integer(value));
}
public void setEntityManager(ValueExpression<EntityManager> entityManager)
{
this.entityManager = entityManager;
}
private EntityManager getEntityManager() {
return entityManager == null ? null : entityManager.getValue();
}
}
1.1 date: 2007/06/15 17:06:58; author: pmuir; state: Exp;jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter/EnumConverter.java
Index: EnumConverter.java
===================================================================
package org.jboss.seam.ui.converter;
import static org.jboss.seam.InterceptionType.NEVER;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import java.util.Collection;
import javax.el.ValueExpression;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.ConverterException;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.jsf.Converter;
@Name("org.jboss.seam.ui.EnumConverter")
@Scope(ScopeType.EVENT)
@Install(precedence = BUILT_IN)
@Converter
@Intercept(NEVER)
public class EnumConverter implements javax.faces.convert.Converter
{
public Object getAsObject(FacesContext context, UIComponent comp, String value)
throws ConverterException
{
ValueExpression expr = comp.getValueExpression("value");
Class enumType = expr.getType(context.getELContext());
if (enumType.isEnum())
{
return Enum.valueOf(enumType, value);
}
else
{
for (Object child : comp.getChildren())
{
if (child instanceof UIComponent)
{
UIComponent c = (UIComponent) child;
expr = c.getValueExpression("value");
Object val = expr.getValue(context.getELContext());
if (val == null)
{
throw new ConverterException("Cannot get items");
}
Class t = val.getClass();
if (t.isArray() && t.getComponentType().isEnum())
{
return Enum.valueOf(t.getComponentType(), value);
}
else if (val instanceof Collection)
{
t = ((Collection) val).iterator().next().getClass();
return Enum.valueOf(t, value);
}
}
}
}
throw new ConverterException("Unable to find selectItems with enum values.");
}
public String getAsString(FacesContext context, UIComponent component, Object object)
throws ConverterException
{
return object == null ? null : ((Enum) object).name();
}
}
1.1 date: 2007/06/15 17:06:59; author: pmuir; state: Exp;jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter/EntityConverterStore.java
Index: EntityConverterStore.java
===================================================================
package org.jboss.seam.ui.converter;
import static org.jboss.seam.annotations.Install.BUILT_IN;
import static org.jboss.seam.ScopeType.PAGE;
import static org.jboss.seam.InterceptionType.NEVER;
import java.util.ArrayList;
import java.util.List;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Transactional;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.framework.EntityIdentifier;
import org.jboss.seam.framework.MutableEntityController;
/**
* Stores entity identifiers under a key, which can be used on a page
*
*/
@Name("org.jboss.seam.ui.entityConverterStore")
@Install(precedence=BUILT_IN)
@Scope(PAGE)
@Intercept(NEVER)
public class EntityConverterStore extends MutableEntityController
{
private List<EntityIdentifier> store = new ArrayList<EntityIdentifier>();
/**
* Load and return the entity stored
* @param key
* @return The entity or null if no entity is available at that key
*/
@Transactional
public Object get(Integer key)
{
try
{
return store.get(key).find(getEntityManager());
}
catch (IndexOutOfBoundsException e)
{
return null;
}
}
/**
* Store an entity id/clazz
* @param entity The entity to store
* @return The key under which the clazz/id are stored
*/
@Transactional
public Integer put(Object entity)
{
EntityIdentifier key = new EntityIdentifier(entity, getEntityManager());
if (!store.contains(key))
{
store.add(key);
setDirty();
}
return store.indexOf(key);
}
public static EntityConverterStore instance()
{
if (!Contexts.isPageContextActive())
{
throw new IllegalArgumentException("Page scope not active");
}
return (EntityConverterStore) Component.getInstance(EntityConverterStore.class);
}
}
1.1 date: 2007/06/15 17:06:59; author: pmuir; state: Exp;jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter/ConverterChain.java
Index: ConverterChain.java
===================================================================
package org.jboss.seam.ui.converter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.el.ValueExpression;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.component.ValueHolder;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
/**
* This class provides a chainable converter for JSF.
*
* Any JSF converter can be placed at the end of the chain. A converter that is
* placed higher up the chain should return ConverterChain.CONTINUE if
* conversion has failed. If the all converters run return
* ConverterChain.CONTINUE an unconverted value will be returned.
*
* A converter can be placed in the chain with a priority, the order in which
* converters with the same priority is run is not specified.
*
*/
public class ConverterChain implements Converter, StateHolder
{
public static final String CONTINUE = "org.jboss.seam.ui.ConverterChain.continue";
/**
* This places the converter at the end of the chain. No garuntee is made
* about the order converters which are placed on the queue with this
* priority will be run
*/
public static final int CHAIN_END = Integer.MAX_VALUE;
/**
* This places the converter at the head of the chain. No garuntee is made
* about the order converters which are placed on the queue with this
* priority will be run
*/
public static final int CHAIN_START = 0;
private List<PrioritizableConverter> converters;
private boolean dirty;
public ConverterChain()
{
// A Priority Queue would be nice but JSF has issues serializing that
converters = new ArrayList<PrioritizableConverter>();
}
/**
* Set up a ConverterChain for this component.
*
* This replaces any existing converter with a ConverterChain with the
* existing Converter at the end of the chain
*
* @param component
*/
public ConverterChain(UIComponent component)
{
this();
if (component instanceof ValueHolder)
{
ValueHolder valueHolder = (ValueHolder) component;
if (!(valueHolder.getConverter() instanceof ConverterChain))
{
ValueExpression converterValueExpression = component.getValueExpression("converter");
if (converterValueExpression != null)
{
addConverterToChain(converterValueExpression);
}
else if (valueHolder.getConverter() != null)
{
addConverterToChain(valueHolder.getConverter());
}
else
{
ValueExpression valueExpression = component.getValueExpression("value");
FacesContext facesContext = FacesContext.getCurrentInstance();
if (valueExpression != null)
{
Converter converter = facesContext.getApplication().createConverter(
valueExpression.getType(facesContext.getELContext()));
if (converter != null)
{
addConverterToChain(converter);
}
}
}
valueHolder.setConverter(this);
}
}
}
public Object getAsObject(FacesContext context, UIComponent component, String value)
throws ConverterException
{
Object output = value;
for (Converter converter : getConverters())
{
Object result = converter.getAsObject(context, component, value);
if (!CONTINUE.equals(result))
{
output = result;
break;
}
}
return output;
}
public String getAsString(FacesContext context, UIComponent component, Object value)
throws ConverterException
{
String output = value == null ? null : value.toString();
for (Converter converter : getConverters())
{
String result = converter.getAsString(context, component, value);
if (!CONTINUE.equals(result))
{
output = result;
break;
}
}
return output;
}
/**
* Add a converter to the end of the chain
*/
public boolean addConverterToChain(Converter c)
{
return addConverterToChain(c, CHAIN_END);
}
/**
* Add a converter to the end of the chain
*/
public boolean addConverterToChain(ValueExpression c)
{
return addConverterToChain(c, CHAIN_END);
}
/**
* Add a converter to the chain with a defined priority
*/
public boolean addConverterToChain(Converter c, int priority)
{
if (c != null)
{
dirty = true;
return converters.add(new PrioritizableConverter(c, priority));
}
else
{
return false;
}
}
/**
* Add a converter to the chain with a defined priority
*/
public boolean addConverterToChain(ValueExpression c, int priority)
{
if (c != null)
{
dirty = true;
return converters.add(new PrioritizableConverter(c, priority));
}
else
{
return false;
}
}
private boolean _transient;
public boolean isTransient()
{
return _transient;
}
public void restoreState(FacesContext context, Object state)
{
Object[] values = (Object[]) state;
converters = (List<PrioritizableConverter>) UIComponentBase.restoreAttachedState(context,
values[0]);
dirty = true;
}
public Object saveState(FacesContext context)
{
Object[] values = new Object[1];
values[0] = UIComponentBase.saveAttachedState(context, converters);
return values;
}
public void setTransient(boolean newTransientValue)
{
this._transient = newTransientValue;
}
private List<PrioritizableConverter> getConverters()
{
if (dirty)
{
Collections.sort(converters);
}
return converters;
}
public boolean containsConverterType(Converter converter) {
// TODO Improve this
for (Converter c : converters) {
if (c.getClass().equals(converter)) {
return true;
}
}
return false;
}
}
1.1 date: 2007/06/15 17:06:59; author: pmuir; state: Exp;jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter/DateTimeConverter.java
Index: DateTimeConverter.java
===================================================================
/**
* License Agreement.
*
* Ajax4jsf 1.1 - Natural Ajax for Java Server Faces (JSF)
*
* Copyright (C) 2007 Exadel, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* 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
*/
package org.jboss.seam.ui.converter;
import java.util.TimeZone;
import org.jboss.seam.InterceptionType;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Intercept;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.jsf.Converter;
import org.jboss.seam.contexts.Contexts;
@Name("org.jboss.seam.ui.DateTimeConverter")
@Scope(ScopeType.EVENT)
@Intercept(InterceptionType.NEVER)
@Converter
@Install(precedence=Install.BUILT_IN)
public class DateTimeConverter extends javax.faces.convert.DateTimeConverter{
private static final String CONVERTER_ID = "org.jboss.seam.ui.DateTimeConverter";
public DateTimeConverter()
{
setTimeZone( getTimeZone() );
}
@Override
public TimeZone getTimeZone()
{
if ( Contexts.isApplicationContextActive() )
{
return org.jboss.seam.core.TimeZone.instance();
}
else
{
return TimeZone.getDefault();
}
}
}
1.1 date: 2007/06/15 17:06:59; author: pmuir; state: Exp;jboss-seam/ui/src/main/java/org/jboss/seam/ui/converter/PrioritizableConverter.java
Index: PrioritizableConverter.java
===================================================================
package org.jboss.seam.ui.converter;
import javax.el.ValueExpression;
import javax.faces.component.StateHolder;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
/**
* Helper class for ConverterChain
*
*/
public class PrioritizableConverter implements Converter, Comparable<PrioritizableConverter>,
StateHolder
{
private ValueExpression valueExpression;
private Converter delegate;
private int priority;
public PrioritizableConverter()
{
}
public PrioritizableConverter(ValueExpression vb, int priority)
{
this.valueExpression = vb;
this.priority = priority;
}
public PrioritizableConverter(Converter delegate, int priority)
{
this.delegate = delegate;
this.priority = priority;
}
public Converter getDelegate()
{
if (valueExpression != null)
{
return (Converter) valueExpression.getValue(FacesContext.getCurrentInstance().getELContext());
}
else
{
return delegate;
}
}
public int getPriority()
{
return priority;
}
public Object getAsObject(FacesContext context, UIComponent component, String value)
throws ConverterException
{
return getDelegate().getAsObject(context, component, value);
}
public String getAsString(FacesContext context, UIComponent component, Object value)
throws ConverterException
{
return getDelegate().getAsString(context, component, value);
}
public int compareTo(PrioritizableConverter o)
{
return this.getPriority() - o.getPriority();
}
private boolean _transient;
public boolean isTransient()
{
return _transient;
}
public void restoreState(FacesContext context, Object state)
{
Object[] values = (Object[]) state;
delegate = (Converter) UIComponentBase.restoreAttachedState(context, values[0]);
priority = (Integer) values[1];
valueExpression = (ValueExpression) values[2];
}
public Object saveState(FacesContext context)
{
Object[] values = new Object[3];
values[0] = UIComponentBase.saveAttachedState(context, delegate);
values[1] = priority;
values[2] = valueExpression;
return values;
}
public void setTransient(boolean newTransientValue)
{
this._transient = newTransientValue;
}
}
More information about the jboss-cvs-commits
mailing list