I’ve been trying to use a column generator to display an image based on a string value within the underlying data model. The problem is that when i add the column generator the table becomes empty. I’ve alse tried returning a simple Label without success.
The code is very simple:
package webalarmer;
import com.vaadin.Application;
import com.vaadin.terminal.ClassResource;
import com.vaadin.ui.*;
public class Main extends Application {
private static final Integer PID_LEVEL = 0;
private static final Integer PID_TIMESTAMP = 1;
private static final Integer PID_PROCESS = 2;
private static final Integer PID_MESSAGE = 3;
private static final String LVL_INF = "INF";
private static final String LVL_WAR = "WAR";
private static final String LVL_ERR = "ERR";
private int nextItemId = 0;
@Override
public void init() {
Window mainWnd = new Window("WebAlarmer");
Window alarmListWnd = new Window("Listado de Alarmas");
alarmListWnd.setWidth("640px");
alarmListWnd.setHeight("480px");
alarmListWnd.center();
alarmListWnd.setContent(new VerticalLayout());
((VerticalLayout) alarmListWnd.getContent()).setSpacing(true);
Table table = new Table();
table.setWidth("100%");
table.setImmediate(true);
table.setSelectable(true);
table.addContainerProperty(PID_LEVEL, String.class, null, "Nivel", null, null);
table.addContainerProperty(PID_TIMESTAMP, String.class, null, "Timestamp", null, null);
table.addContainerProperty(PID_PROCESS, String.class, null, "Proceso", null, null);
table.addContainerProperty(PID_MESSAGE, String.class, null, "Mensaje", null, null);
table.setColumnExpandRatio(PID_MESSAGE, 1.0f);
table.addGeneratedColumn(PID_LEVEL, new Table.ColumnGenerator() {
public Component generateCell(Table source, Object itemId, Object columnId) {
String level = (String) source.getItem(itemId).getItemProperty(columnId).getValue();
if (level.equals(LVL_INF)) {
return new Embedded(null, new ClassResource("/webalarmer/images/inf.png", source.getApplication()));
}
if (level.equals(LVL_WAR)) {
return new Embedded(null, new ClassResource("/webalarmer/images/war.png", source.getApplication()));
}
if (level.equals(LVL_ERR)) {
return new Embedded(null, new ClassResource("/webalarmer/images/err.png", source.getApplication()));
}
return null;
}
});
table.setVisibleColumns(new Object[]{PID_LEVEL, PID_TIMESTAMP, PID_PROCESS, PID_MESSAGE});
table.addItem(new Object[]{LVL_INF, "20091119101232", "receptor.sh", "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa."}, nextItemId++);
table.addItem(new Object[]{LVL_WAR, "20091120101232", "receptor.sh", "Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet."}, nextItemId++);
table.addItem(new Object[]{LVL_ERR, "20091129101232", "receptor.sh", "Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna."}, nextItemId++);
alarmListWnd.addComponent(table);
mainWnd.addWindow(alarmListWnd);
setMainWindow(mainWnd);
}
}
I would really appreciate if someone could help me with this issue.
Regards!