Skip to content

Instantly share code, notes, and snippets.

@canthony
Created July 24, 2012 10:31
Show Gist options
  • Save canthony/3169309 to your computer and use it in GitHub Desktop.
Save canthony/3169309 to your computer and use it in GitHub Desktop.
Shows use of a Vaadin Table Generator
package com.example;
import com.vaadin.Application;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.ui.Embedded;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
public class TestApp extends Application {
public void init() {
Window mainWindow = new TestWindow();
setMainWindow(mainWindow);
}
public class TestWindow extends Window {
public TestWindow() {
IndexedContainer container = createContainer();
Table table = createTable(container);
VerticalLayout mainLayout = new VerticalLayout();
mainLayout.addComponent(table);
mainLayout.setSizeFull();
setContent(mainLayout);
}
/* Build a model for the table with the data : Lots of ways of doing this;
Just a quick-and-dirty example */
private IndexedContainer createContainer() {
IndexedContainer container = new IndexedContainer();
container.addContainerProperty("title", String.class, null);
container.addContainerProperty("url", String.class, null);
container.addContainerProperty("height", Integer.class, null);
container.addContainerProperty("width", Integer.class, null);
addItem(container, "Sami Viitanen", "http://vaadin.com/vaadin-theme/images/company/personnel/alump.png", 121, 134);
addItem(container, "Anna Koskinen", "http://vaadin.com/vaadin-theme/images/company/personnel/anna.png", 121, 134);
addItem(container, "Artur Signell", "http://vaadin.com/vaadin-theme/images/company/personnel/artur.png", 121, 134);
return container;
}
private void addItem(IndexedContainer container, String title, String url, int height, int width) {
Object itemId = container.addItem();
container.getItem(itemId).getItemProperty("title").setValue(title);
container.getItem(itemId).getItemProperty("url").setValue(url);
container.getItem(itemId).getItemProperty("height").setValue(height);
container.getItem(itemId).getItemProperty("width").setValue(width);
}
private Table createTable(IndexedContainer container) {
Table table = new Table("Image Table", container);
table.setSizeFull();
table.addGeneratedColumn("image", new Table.ColumnGenerator() {
@Override
public Object generateCell(Table source, Object itemId, Object columnId) {
Item item = source.getItem(itemId);
String url = (String) item.getItemProperty("url").getValue();
int height = (Integer) item.getItemProperty("height").getValue();
int width = (Integer) item.getItemProperty("width").getValue();
return new Embedded("", new ExternalResource(url));
}
});
table.setVisibleColumns(new Object[]{"image", "title"});
table.setColumnExpandRatio("title", 1f);
return table;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>ExampleApp</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>com.example.TestApp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ExampleApp</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment