Conforme eu disse, modifiquei para usar o FieldResolver, agora o código está mais limpo e ainda deixa o programador escolher se quer o FieldHandler ou MethodHandler.
package com.towel.bean;
/*
* Copyright (c) 2011 Felipe Priuli
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*The above copyright notice and this permission notice shall be included at least in this file.
*
* The Software shall be used for Good, not Evil.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import java.util.ArrayList;
import java.util.List;
import com.towel.bean.Formatter;
import com.towel.el.FieldResolver;
/**
* <code>DynamicFormatter</code> classe que implementa o
* <code>com.towel.bean.Formatter</code> esta classe é um Formatter dinâmico,
* podendo ser utilizado para vario formatar varios tipos de entidades, classes
* aquelas que serão utilizadas em um combobox do
* <code>ObjectComboBoxModel</code> do projeto Towel, antigo MarkUtils, do Marky
* Vasconcelos ( http://code.google.com/p/towel/ )
*
* @author Felipe Priuli
* @author Marcos Vasconcelos
* @version 0.2 beta 20/01/2011
* @param <T>
* - Tipo da classe que representa este Formatter
*/
public class DynamicFormatter<T> implements Formatter {
private Class<T> clazz;
protected List<FieldResolver> fieldList;
/**
* Um texto aquele que ira separar o valor caso tenha mais de um campo sendo
* invocado.
*/
protected String separetor;
/**
* Contrutor
*
* @param t
* - Class name que representa este Formatter
*/
public DynamicFormatter(Class<T> t) {
this.clazz = t;
this.fieldList = new ArrayList<FieldResolver>(4);
}
/**
* Contrutor
*
* @param t
* - Class name que representa este Formatter
* @param fieldName
* - o nome do campo que sera invocado para obter o texto do
* combobox
* @author Felipe Priuli 20/01/2011
*/
public DynamicFormatter(Class<T> t, String fieldName) {
this(t);
this.clazz = t;
this.fieldList.add(new FieldResolver(t, fieldName));
}
/**
* Contrutor
*
* @param t
* - Class name que representa este Formatter
* @param fiedlNameList
* - Os campos que serão invocado para obter o texto do combobox
* @param separetor
* - o texto que ira separar os texto do campos
*/
public DynamicFormatter(Class<T> t, List<FieldResolver> fiedlNameList,
String separetor) {
this(t);
this.clazz = t;
this.fieldList.addAll(fiedlNameList);
this.separetor = separetor;
}
@Override
@SuppressWarnings("unchecked")
public Object format(final Object arg0) {
if (this.separetor == null) {
this.separetor = "";
}
if (arg0 == null) {
return "";
}
T obj = ((T) arg0);
try {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < this.fieldList.size(); i++) {
sb.append(fieldList.get(i).getValue(obj));
if (!((i + 1) == this.fieldList.size())) {
sb.append(this.separetor);
}
}
return sb.toString();
} catch (SecurityException e) {
throw new IllegalArgumentException(e);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Field is no pattern JavaBeans to acess method", e);
}
}
@Override
public String getName() {
return clazz.getClass().getSimpleName().toLowerCase();
}
@Override
public Object parse(Object arg0) {
return null;// Never get invoked, JComboBox cannot be editable
}
/**
* Seta o texto que ira separar os valores caso tenha mais de um campo sendo
* invocado.
*
* @return the separetor
*/
public String getSeparetor() {
return separetor;
}
/**
* Obtem o texto que ira separar os valores caso tenham mais de um campo
* sendo invocado.
*
* @param separetor
* the separetor to set
*/
public void setSeparetor(String separetor) {
this.separetor = separetor;
}
/**
* @return the fieldList
*/
public List<FieldResolver> getFieldList() {
return fieldList;
}
}