Created
June 25, 2013 09:42
-
-
Save letmaik/5857263 to your computer and use it in GitHub Desktop.
Adds CellTable's Model support to a FlexTable (Google Web Toolkit)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.neothemachine.flextable.client; | |
import com.github.neothemachine.flextable.client.ModelFlexTable.FlexColumn; | |
import com.google.gwt.core.client.GWT; | |
import com.google.gwt.event.dom.client.ClickEvent; | |
import com.google.gwt.event.dom.client.ClickHandler; | |
import com.google.gwt.uibinder.client.UiBinder; | |
import com.google.gwt.uibinder.client.UiField; | |
import com.google.gwt.user.client.ui.Button; | |
import com.google.gwt.user.client.ui.Composite; | |
import com.google.gwt.user.client.ui.InlineHTML; | |
import com.google.gwt.user.client.ui.Label; | |
import com.google.gwt.user.client.ui.Widget; | |
public class Example extends Composite { | |
public class Model { | |
private final String name; | |
private final String color; | |
public Model(String name, String color) { | |
this.name = name; | |
this.color = color; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getColor() { | |
return color; | |
} | |
} | |
interface ExampleUiBinder extends UiBinder<Widget, Example> { | |
} | |
private static ExampleUiBinder uiBinder = GWT.create(ExampleUiBinder.class); | |
@UiField | |
ModelFlexTable<Model> table; | |
public Example() { | |
initWidget(uiBinder.createAndBindUi(this)); | |
table.addColumn(new FlexColumn<Example.Model>() { | |
@Override | |
public Widget createWidget(Model row) { | |
return new Label(row.getName()); | |
} | |
}, "Name"); | |
table.addColumn(new FlexColumn<Example.Model>() { | |
@Override | |
public Widget createWidget(Model row) { | |
return new InlineHTML("<div style='background-color:" + row.getColor() + "; width:50px;height:50px'></div>"); | |
} | |
}, "Color"); | |
table.addColumn(new FlexColumn<Example.Model>() { | |
@Override | |
public Widget createWidget(final Model row) { | |
Button remove = new Button("Remove"); | |
remove.addClickHandler(new ClickHandler() { | |
@Override | |
public void onClick(ClickEvent event) { | |
table.removeRow(row); | |
} | |
}); | |
return remove; | |
} | |
}); | |
table.addRow(new Model("red", "#ff0000")); | |
table.addRow(new Model("green", "#00ff00")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent"> | |
<ui:UiBinder xmlns:ui="urn:ui:com.google.gwt.uibinder" | |
xmlns:g="urn:import:com.google.gwt.user.client.ui" | |
xmlns:flex="urn:import:com.github.neothemachine.flextable.client"> | |
<g:HTMLPanel> | |
<flex:ModelFlexTable ui:field="table" /> | |
</g:HTMLPanel> | |
</ui:UiBinder> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.neothemachine.flextable.client; | |
import java.util.LinkedList; | |
import java.util.List; | |
import com.google.gwt.user.client.ui.Composite; | |
import com.google.gwt.user.client.ui.FlexTable; | |
import com.google.gwt.user.client.ui.Widget; | |
public class ModelFlexTable<T> extends Composite { | |
public interface FlexColumn<T> { | |
Widget createWidget(T row); | |
} | |
private final FlexTable table; | |
private final List<FlexColumn<T>> columns = new LinkedList<FlexColumn<T>>(); | |
private final List<T> rows = new LinkedList<T>(); | |
public ModelFlexTable() { | |
table = new FlexTable(); | |
initWidget(table); | |
} | |
public void addColumn(FlexColumn<T> column) { | |
addColumn(column, ""); | |
} | |
public void addColumn(FlexColumn<T> column, String name) { | |
columns.add(column); | |
if (!"".equals(name)) { | |
table.setText(0, columns.size()-1, name); | |
} | |
recreateRows(); | |
} | |
public void addRow(T row) { | |
rows.add(row); | |
appendRowToTable(row); | |
} | |
public void removeRow(T row) { | |
int idx = rows.indexOf(row); | |
if (idx == -1) { | |
return; | |
} | |
table.removeRow(idx+1); | |
rows.remove(row); | |
} | |
public void setRows(List<T> rows) { | |
this.rows.clear(); | |
this.rows.addAll(rows); | |
recreateRows(); | |
} | |
private void recreateRows() { | |
int rowCount = table.getRowCount(); | |
for (int row = rowCount-1; row > 0; row--) { | |
table.removeRow(row); | |
} | |
for (T row : rows) { | |
appendRowToTable(row); | |
} | |
} | |
private void appendRowToTable(T row) { | |
int rowIdx = table.getRowCount(); | |
int columnIdx = 0; | |
for (FlexColumn<T> column : columns) { | |
Widget widget = column.createWidget(row); | |
table.setWidget(rowIdx, columnIdx, widget); | |
columnIdx++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment