quarta-feira, 10 de setembro de 2008

DynamicTableModel - Evoluindo a Idéia de Algo Flexível

Melhorando um pouco o Table Model do swing, deixando ele um pouco mais compatível com o JPA e mais simples de utilizar sem precisar carregar um punhado de bibliotecas. Sei que o código poderia ser mais bem otimizado, porém, o objetivo foi a facilidade de utilização. Comecei seguindo idéias que vi na web, porém resolvi seguir um caminho próprio.


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.AbstractTableModel;

public class DynamicTableModel extends AbstractTableModel {

private String[] columnNames = null;
private List data = null;

public List getData() {
return data;
}

public void setData(List dataset) throws IntrospectionException {
initModel(dataset);
}
private Class c = null;

public Class getC() {
return c;
}

public String[] getColumnNames() {
return columnNames;
}

public DynamicTableModel(List dataset) throws IntrospectionException {
super();
initModel(dataset);
}

private void initModel(List dataset) throws IntrospectionException {
System.out.println("dataset.size() = " + dataset.size());
if (dataset.size() > 0) {
c = dataset.get(0).getClass();
System.out.println(c.getName());

PropertyDescriptor[] p = java.beans.Introspector.getBeanInfo(c, Object.class).getPropertyDescriptors();
columnNames = new String[p.length];
System.out.println("p.length = " + p.length);
for (int i = 0; i < p.length; i++) {
// coloca o nome na coluna
String s = p[i].getName();
s = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
System.out.println("s = " + s);
columnNames[i] = s;
}
this.data = dataset;
}
}

@Override
public Class getColumnClass(int c) {
System.out.println("getColumnClass(int c) = " + c);
return getValueAt(0, c).getClass();
}

public int getColumnCount() {
if (columnNames == null) {
return 0;
} else {
System.out.println("getColumnCount = " + columnNames.length);
return columnNames.length;
}
}

public int getRowCount() {
if (data == null) {
return 0;
} else {
return data.size();
}
}

@Override
public String getColumnName(int col) {
return columnNames[col];
}

public Object getObjectAtRow(int row) {
Object o = null;
if (data.size() > 0 && row < data.size()) {
o = data.get(row);
}
return o;
}

public List Object> getObjectAtRows(int[] rows) {
ArrayList Object> os = new ArrayList Object>();
if (data.size() > 0) {
for (int i : rows) {
if (data.size() > i) {
os.add(data.get(i));
}
}
}
return os;
}

public Object getValueAt(int row, int col) {
Object o = null;
if (data.size() > 0 && row < data.size()) {
try {
Object oRow = data.get(row);
Method method = c.getMethod("get" + columnNames[col], new Class[]{});
Class t = method.getReturnType();
if (t.isPrimitive()) {
System.out.println("t.getName() = " + t.getName());
if (t.equals(java.lang.Integer.TYPE) || t.getName().equals("int")) {
o = new Integer(method.invoke(oRow, new Object[]{}).toString());
} else if (t.equals(java.lang.Boolean.TYPE) || t.getName().equals("boolean")) {
o = new Boolean(method.invoke(oRow, new Object[]{}).toString());
} else if (t.equals(java.lang.Float.TYPE) || t.getName().equals("float")) {
o = new Integer(method.invoke(oRow, new Object[]{}).toString());
} else if (t.equals(java.lang.Long.TYPE) || t.getName().equals("long")) {
o = new Integer(method.invoke(oRow, new Object[]{}).toString());
} else {
o = oRow;
}
} else {
System.out.println("Nao primitivo t.getName() = " + t.getName());
if (t.getName().equals("java.lang.String")) {
o = new String(method.invoke(oRow, new Object[]{}).toString());
} else {
o = oRow;
}
}
} catch (Exception ex) {
System.out.println("ex = " + ex);
Logger.getLogger(DynamicTableModel.class.getName()).log(Level.SEVERE, null, ex);
}

}
return o;
}
}

Nenhum comentário: