package com.cn.client;
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.CompositeCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class CellTest implements EntryPoint {
private Cell<Department> contactCell;
private Label selectedLabel = new Label();
public void onModuleLoad() {
List<Department> depts = new ArrayList<Department>();
Department c = new Department("项目部");
c.setId(1);
Department c1 = new Department("财务部");
c1.setId(2);
Department c2 = new Department("开发部");
c2.setId(3);
Department c3 = new Department("测试部");
c3.setId(149);
depts.add(c);
depts.add(c1);
depts.add(c2);
depts.add(c3);
final DefaultSelectionEventManager<Department> selectionManager = DefaultSelectionEventManager.createCheckboxManager();
final MultiSelectionModel<Department> selectionModel = new MultiSelectionModel<Department>(DepartmentCell.KEY_PROVIDER);
selectionModel.addSelectionChangeHandler(
new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
StringBuilder sb = new StringBuilder();
boolean first = true;
List<Department> selected = new ArrayList<Department>(
selectionModel.getSelectedSet());
for (Department value : selected) {
if (first) {
first = false;
} else {
sb.append(",");
}
sb.append(value.getName());
}
selectedLabel.setText("选择:"+sb.toString());
}
});
List<HasCell<Department, ?>> hasCells = new ArrayList<HasCell<Department, ?>>();
hasCells.add(new HasCell<Department, Boolean>() {
private CheckboxCell cell = new CheckboxCell(true, false);
public Cell<Boolean> getCell() {
return cell;
}
public FieldUpdater<Department, Boolean> getFieldUpdater() {
return null;
}
public Boolean getValue(Department object) {
return selectionModel.isSelected(object);
}
});
hasCells.add(new HasCell<Department, Department>() {
private DepartmentCell cell = new DepartmentCell();
public Cell<Department> getCell() {
return cell;
}
public FieldUpdater<Department, Department> getFieldUpdater() {
return null;
}
public Department getValue(Department object) {
return object;
}
});
contactCell = new CompositeCell<Department>(hasCells) {
@Override
public void render(Context context, Department value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<table><tbody><tr>");
super.render(context, value, sb);
sb.appendHtmlConstant("</tr></tbody></table>");
}
@Override
protected Element getContainerElement(Element parent) {
return parent.getFirstChildElement().getFirstChildElement().getFirstChildElement();
}
@Override
protected <X> void render(Context context, Department value,
SafeHtmlBuilder sb, HasCell<Department, X> hasCell) {
Cell<X> cell = hasCell.getCell();
sb.appendHtmlConstant("<td>");
cell.render(context, hasCell.getValue(value), sb);
sb.appendHtmlConstant("</td>");
}
};
final CellList<Department> cellList = new CellList<Department>(contactCell);
cellList.setRowData(depts);
cellList.setSelectionModel(selectionModel,selectionManager);
HorizontalPanel hpanel = new HorizontalPanel();
VerticalPanel vpanel = new VerticalPanel();
vpanel.add(cellList);
vpanel.add(selectedLabel);
Button submitButton = new Button("提交");
submitButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if(selectedLabel.getText() == null && selectedLabel.getText().equals("")){
Window.alert("没有选择!");
}else{
Window.alert(selectedLabel.getText());
}
}
});
hpanel.add(vpanel);
hpanel.add(submitButton);
RootPanel.get("root").add(hpanel);
}
private static class DepartmentCell extends AbstractCell<Department> {
@Override
public void render(Context context,Department value, SafeHtmlBuilder sb) {
if (value == null) {
return;
}
sb.appendHtmlConstant("<table>");
sb.appendHtmlConstant("<td style='font-size:95%;'>");
sb.appendEscaped(value.getName());
sb.appendHtmlConstant("</td></tr></table>");
}
public static final ProvidesKey<Department> KEY_PROVIDER = new ProvidesKey<Department>() {
@Override
public Object getKey(Department item) {
return item == null ? null : item.getId();
}
};
}
private class Department implements Comparable<Department> {
private int id;
private String name;
public Department(String name) {
super();
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Department o) {
return (o == null || o.name == null) ? -1 : -o.name.compareTo(name);
}
@Override
public boolean equals(Object o) {
if (o instanceof Department) {
return id == ((Department) o).id;
}
return false;
}
}
}
要实现单选则:cellList.setSelectionModel(selectionModel);
要实现多选则:cellList.setSelectionModel(selectionModel,selectionManager);
import java.util.ArrayList;
import java.util.List;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.cell.client.CompositeCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.cellview.client.CellList;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.DefaultSelectionEventManager;
import com.google.gwt.view.client.MultiSelectionModel;
import com.google.gwt.view.client.ProvidesKey;
import com.google.gwt.view.client.SelectionChangeEvent;
/**
* Entry point classes define <code>onModuleLoad()</code>.
*/
public class CellTest implements EntryPoint {
private Cell<Department> contactCell;
private Label selectedLabel = new Label();
public void onModuleLoad() {
List<Department> depts = new ArrayList<Department>();
Department c = new Department("项目部");
c.setId(1);
Department c1 = new Department("财务部");
c1.setId(2);
Department c2 = new Department("开发部");
c2.setId(3);
Department c3 = new Department("测试部");
c3.setId(149);
depts.add(c);
depts.add(c1);
depts.add(c2);
depts.add(c3);
final DefaultSelectionEventManager<Department> selectionManager = DefaultSelectionEventManager.createCheckboxManager();
final MultiSelectionModel<Department> selectionModel = new MultiSelectionModel<Department>(DepartmentCell.KEY_PROVIDER);
selectionModel.addSelectionChangeHandler(
new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
StringBuilder sb = new StringBuilder();
boolean first = true;
List<Department> selected = new ArrayList<Department>(
selectionModel.getSelectedSet());
for (Department value : selected) {
if (first) {
first = false;
} else {
sb.append(",");
}
sb.append(value.getName());
}
selectedLabel.setText("选择:"+sb.toString());
}
});
List<HasCell<Department, ?>> hasCells = new ArrayList<HasCell<Department, ?>>();
hasCells.add(new HasCell<Department, Boolean>() {
private CheckboxCell cell = new CheckboxCell(true, false);
public Cell<Boolean> getCell() {
return cell;
}
public FieldUpdater<Department, Boolean> getFieldUpdater() {
return null;
}
public Boolean getValue(Department object) {
return selectionModel.isSelected(object);
}
});
hasCells.add(new HasCell<Department, Department>() {
private DepartmentCell cell = new DepartmentCell();
public Cell<Department> getCell() {
return cell;
}
public FieldUpdater<Department, Department> getFieldUpdater() {
return null;
}
public Department getValue(Department object) {
return object;
}
});
contactCell = new CompositeCell<Department>(hasCells) {
@Override
public void render(Context context, Department value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("<table><tbody><tr>");
super.render(context, value, sb);
sb.appendHtmlConstant("</tr></tbody></table>");
}
@Override
protected Element getContainerElement(Element parent) {
return parent.getFirstChildElement().getFirstChildElement().getFirstChildElement();
}
@Override
protected <X> void render(Context context, Department value,
SafeHtmlBuilder sb, HasCell<Department, X> hasCell) {
Cell<X> cell = hasCell.getCell();
sb.appendHtmlConstant("<td>");
cell.render(context, hasCell.getValue(value), sb);
sb.appendHtmlConstant("</td>");
}
};
final CellList<Department> cellList = new CellList<Department>(contactCell);
cellList.setRowData(depts);
cellList.setSelectionModel(selectionModel,selectionManager);
HorizontalPanel hpanel = new HorizontalPanel();
VerticalPanel vpanel = new VerticalPanel();
vpanel.add(cellList);
vpanel.add(selectedLabel);
Button submitButton = new Button("提交");
submitButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if(selectedLabel.getText() == null && selectedLabel.getText().equals("")){
Window.alert("没有选择!");
}else{
Window.alert(selectedLabel.getText());
}
}
});
hpanel.add(vpanel);
hpanel.add(submitButton);
RootPanel.get("root").add(hpanel);
}
private static class DepartmentCell extends AbstractCell<Department> {
@Override
public void render(Context context,Department value, SafeHtmlBuilder sb) {
if (value == null) {
return;
}
sb.appendHtmlConstant("<table>");
sb.appendHtmlConstant("<td style='font-size:95%;'>");
sb.appendEscaped(value.getName());
sb.appendHtmlConstant("</td></tr></table>");
}
public static final ProvidesKey<Department> KEY_PROVIDER = new ProvidesKey<Department>() {
@Override
public Object getKey(Department item) {
return item == null ? null : item.getId();
}
};
}
private class Department implements Comparable<Department> {
private int id;
private String name;
public Department(String name) {
super();
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int compareTo(Department o) {
return (o == null || o.name == null) ? -1 : -o.name.compareTo(name);
}
@Override
public boolean equals(Object o) {
if (o instanceof Department) {
return id == ((Department) o).id;
}
return false;
}
}
}
要实现单选则:cellList.setSelectionModel(selectionModel);
要实现多选则:cellList.setSelectionModel(selectionModel,selectionManager);