Skip to content

Instantly share code, notes, and snippets.

@branflake2267
Last active October 30, 2017 22:10
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save branflake2267/3259a33bd7a7d7b5ba5096819fc52870 to your computer and use it in GitHub Desktop.
Tree Grid With CompositeCell with custom cells.
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell;
import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.StringLabelProvider;
import com.sencha.gxt.test.client.treegrid_compositecell.TreeGridWithCompositeCellExample.BaseDto;
public class CustomComboBoxCellHasCell implements HasCell<BaseDto, String> {
private ComboBoxCell<String> comboBoxCell;
private ListStore<String> store;
public CustomComboBoxCellHasCell() {
store = new ListStore<String>(new ModelKeyProvider<String>() {
@Override
public String getKey(String item) {
return item;
}
});
store.add("a");
store.add("b");
store.add("c");
comboBoxCell = new ComboBoxCell<String>(store, new StringLabelProvider<String>());
comboBoxCell.setTriggerAction(TriggerAction.ALL);
}
@Override
public Cell<String> getCell() {
return comboBoxCell;
}
@Override
public FieldUpdater<BaseDto, String> getFieldUpdater() {
return new FieldUpdater<BaseDto, String>() {
@Override
public void update(int index, BaseDto object, String value) {
// TODO something like object.setValue(value);
}
};
}
@Override
public String getValue(BaseDto object) {
return object.getName();
}
public ListStore<String> getComboStore() {
return store;
}
}
package com.sencha.gxt.test.client.treegrid_compositecell;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.sencha.gxt.test.client.treegrid_compositecell.TreeGridWithCompositeCellExample.BaseDto;
/**
* The Text Cell
*/
public class EmptyHasCell implements HasCell<BaseDto, BaseDto> {
private Cell<BaseDto> cell;
public EmptyHasCell() {
cell = new AbstractCell<BaseDto>() {
@Override
public void render(Context context, BaseDto value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant("");
}
};
}
@Override
public Cell<BaseDto> getCell() {
return cell;
}
@Override
public FieldUpdater<BaseDto, BaseDto> getFieldUpdater() {
return new FieldUpdater<BaseDto, BaseDto>() {
@Override
public void update(int index, BaseDto object, BaseDto value) {
GWT.log("FieldUpdater value=" + value);
}
};
}
@Override
public BaseDto getValue(BaseDto object) {
return object;
}
}
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.sencha.gxt.test.client.treegrid_compositecell.TreeGridWithCompositeCellExample.BaseDto;
/**
* The Text Cell
*/
public class LabelHasCell implements HasCell<BaseDto, BaseDto> {
private Cell<BaseDto> cell;
public LabelHasCell(final String label) {
cell = new AbstractCell<BaseDto>() {
@Override
public void render(Context context, BaseDto value, SafeHtmlBuilder sb) {
sb.appendHtmlConstant(label + ":");
}
};
}
@Override
public Cell<BaseDto> getCell() {
return cell;
}
@Override
public FieldUpdater<BaseDto, BaseDto> getFieldUpdater() {
return new FieldUpdater<BaseDto, BaseDto>() {
@Override
public void update(int index, BaseDto object, BaseDto value) {
GWT.log("FieldUpdater value=" + value);
}
};
}
@Override
public BaseDto getValue(BaseDto object) {
return object;
}
}
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.sencha.gxt.cell.core.client.TextButtonCell;
import com.sencha.gxt.test.client.treegrid_compositecell.TreeGridWithCompositeCellExample.BaseDto;
/**
* The Text Button Cell
*/
public class TextButtonHasCell implements HasCell<BaseDto, String> {
private TextButtonCell textInputCell;
public TextButtonHasCell() {
textInputCell = new TextButtonCell();
}
@Override
public Cell<String> getCell() {
return textInputCell;
}
@Override
public FieldUpdater<BaseDto, String> getFieldUpdater() {
return new FieldUpdater<BaseDto, String>() {
@Override
public void update(int index, BaseDto object, String value) {
// TODO nothing to update
}
};
}
@Override
public String getValue(BaseDto object) {
// Same value for every button
return "Change";
}
}
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.GWT;
import com.sencha.gxt.cell.core.client.form.TextInputCell;
import com.sencha.gxt.test.client.treegrid_compositecell.TreeGridWithCompositeCellExample.BaseDto;
/**
* The Text Cell
*/
public class TextCellHasCell implements HasCell<BaseDto, String> {
private TextInputCell textInputCell;
public TextCellHasCell() {
textInputCell = new TextInputCell();
}
@Override
public Cell<String> getCell() {
return textInputCell;
}
@Override
public FieldUpdater<BaseDto, String> getFieldUpdater() {
return new FieldUpdater<BaseDto, String>() {
@Override
public void update(int index, BaseDto object, String value) {
//TODO something like object.setSymbol(value);
object.setName(value);
GWT.log("FieldUpdater value=" + value);
}
};
}
@Override
public String getValue(BaseDto object) {
return object.getName();
}
}
import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.GWT;
import com.sencha.gxt.cell.core.client.form.TextInputCell;
import com.sencha.gxt.test.client.treegrid_compositecell.TreeGridWithCompositeCellExample.BaseDto;
/**
* The Text Cell
*/
public class TextCellHasCell implements HasCell<BaseDto, String> {
private TextInputCell textInputCell;
public TextCellHasCell() {
textInputCell = new TextInputCell();
}
@Override
public Cell<String> getCell() {
return textInputCell;
}
@Override
public FieldUpdater<BaseDto, String> getFieldUpdater() {
return new FieldUpdater<BaseDto, String>() {
@Override
public void update(int index, BaseDto object, String value) {
//TODO something like object.setSymbol(value);
object.setName(value);
GWT.log("FieldUpdater value=" + value);
}
};
}
@Override
public String getValue(BaseDto object) {
return object.getName();
}
}
import java.io.Serializable;
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.CompositeCell;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.Style.Visibility;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.core.client.IdentityValueProvider;
import com.sencha.gxt.core.client.ToStringValueProvider;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.TreeStore;
import com.sencha.gxt.widget.core.client.container.Viewport;
import com.sencha.gxt.widget.core.client.grid.ColumnConfig;
import com.sencha.gxt.widget.core.client.grid.ColumnModel;
import com.sencha.gxt.widget.core.client.tree.Tree.TreeNode;
import com.sencha.gxt.widget.core.client.treegrid.TreeGrid;
public class TreeGridWithCompositeCellExample implements EntryPoint {
public static class CompositeCellExt<C> extends CompositeCell<C> {
public CompositeCellExt(List<HasCell<C, ?>> hasCells) {
super(hasCells);
}
public native List<HasCell<C, ?>> getCells() /*-{
return this.@com.google.gwt.cell.client.CompositeCell::hasCells;
}-*/;
}
private FolderDto brahms;
@Override
public void onModuleLoad() {
final TreeGrid<BaseDto> treeGrid = createTreeGrid();
Viewport vp = new Viewport();
vp.add(treeGrid);
RootPanel.get().add(vp);
}
class KeyProvider implements ModelKeyProvider<BaseDto> {
@Override
public String getKey(BaseDto item) {
return (item instanceof FolderDto ? "f-" : "m-") + item.getId().toString();
}
}
protected CompositeCell<BaseDto> initCompositeCell(List<HasCell<BaseDto, ?>> cells) {
return new CompositeCell<BaseDto>(cells) {
@Override
protected <X> void render(Context context, BaseDto value, SafeHtmlBuilder sb, HasCell<BaseDto, X> hasCell) {
// draw the cells in a line
Cell<X> cell = hasCell.getCell();
sb.appendHtmlConstant("<sdiv style='display: table;'>");
cell.render(context, hasCell.getValue(value), sb);
sb.appendHtmlConstant("</div>");
}
};
}
public TreeGrid<BaseDto> createTreeGrid() {
TreeStore<BaseDto> store = new TreeStore<BaseDto>(new KeyProvider());
FolderDto root = getMusicRootFolder();
for (BaseDto base : root.getChildren()) {
store.add(base);
if (base instanceof FolderDto) {
processFolder(store, (FolderDto) base);
}
}
// Composite cell construction
TextCellHasCell textCell1 = new TextCellHasCell();
TextButtonHasCell textCell2 = new TextButtonHasCell();
CustomComboBoxCellHasCell comboCell = new CustomComboBoxCellHasCell();
final List<HasCell<BaseDto, ?>> cells = new ArrayList<HasCell<BaseDto, ?>>();
// ~~~ TODO use more cells for labels, and data
// Line 1
cells.add(new LabelHasCell("Text Input Label"));
cells.add(textCell1);
cells.add(new LabelHasCell("Button Label"));
cells.add(textCell2);
cells.add(new EmptyHasCell()); // line break
// Line 2
cells.add(new LabelHasCell("Combo Label"));
cells.add(comboCell);
cells.add(new LabelHasCell("Label 4"));
cells.add(new EmptyHasCell()); // line break
// ~~~ composite cell construction and rendering
CompositeCellExt<BaseDto> compositeCell = new CompositeCellExt<BaseDto>(cells) {
@Override
public void render(Context context, BaseDto value, SafeHtmlBuilder sb) {
// ~~~ Note this is going to be the container element
sb.appendHtmlConstant("<div style='border: 1px solid purple; width: 100%; padding: 4px;'>");
for (HasCell<BaseDto, ?> hasCell : getCells()) {
if (hasCell instanceof EmptyHasCell) {
sb.appendHtmlConstant("<div style='display:block; padding: 4px;'>");
render(context, value, sb, hasCell);
sb.appendHtmlConstant("</div>");
} else {
sb.appendHtmlConstant("<div style='display:table-cell; padding: 4px;'>");
render(context, value, sb, hasCell);
sb.appendHtmlConstant("</div>");
}
}
sb.appendHtmlConstant("</div>");
}
// ~~~ the parent container added a div, so get the child
@Override
protected Element getContainerElement(Element parent) {
// ~~~ div with border
Element ce = super.getContainerElement(parent);
Element child = ce.getChild(0).cast();
return child;
}
@Override
protected <X> void render(Context context, BaseDto value, SafeHtmlBuilder sb, HasCell<BaseDto, X> hasCell) {
Cell<X> cell = hasCell.getCell();
cell.render(context, hasCell.getValue(value), sb);
}
};
ColumnConfig<BaseDto, String> cc1 = new ColumnConfig<BaseDto, String>(new ToStringValueProvider<BaseDto>("name"));
cc1.setHeader("Name");
cc1.setResizable(true);
cc1.setWidth(100);
cc1.setCell(new AbstractCell<String>() {
@Override
public void render(Context context, String value, SafeHtmlBuilder sb) {
// skip rendering nothing
}
});
ColumnConfig<BaseDto, BaseDto> cc2 = new ColumnConfig<BaseDto, BaseDto>(new IdentityValueProvider("info"));
cc2.setHeader("Some Other Col");
cc2.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
// ~~~ add the composite cell
cc2.setCell(compositeCell);
List<ColumnConfig<BaseDto, ?>> columns = new ArrayList<ColumnConfig<BaseDto, ?>>();
columns.add(cc1);
columns.add(cc2);
ColumnModel<BaseDto> cm = new ColumnModel<BaseDto>(columns);
// ~~~~ option to hiding the joint
final TreeGrid<BaseDto> tree = new TreeGrid<BaseDto>(store, cm, cc1) {
@Override
protected void afterRenderView() {
super.afterRenderView();
TreeNode<BaseDto> node = findNode(brahms);
GWT.log("node=" + node);
Element el = getTreeView().getJointElement(node);
el.getStyle().setVisibility(Visibility.HIDDEN);
}
};
tree.getView().setAutoExpandColumn(cc2);
tree.getView().setAutoExpandMax(1000);
return tree;
}
private void processFolder(TreeStore<BaseDto> store, FolderDto folder) {
for (BaseDto child : folder.getChildren()) {
store.add(folder, child);
if (child instanceof FolderDto) {
processFolder(store, (FolderDto) child);
}
}
}
public FolderDto getMusicRootFolder() {
FolderDto root = makeFolder("Root");
FolderDto author = makeFolder("Beethoven");
List<BaseDto> children = new ArrayList<BaseDto>();
children.add(author);
root.setChildren(children);
FolderDto genre = makeFolder("Quartets");
author.addChild(genre);
FolderDto quartets = genre;
genre.addChild(makeMusic("Six String Quartets", author, genre));
genre.addChild(makeMusic("Three String Quartets", author, genre));
genre.addChild(makeMusic("Grosse Fugue for String Quartets", author, genre));
genre = makeFolder("Sonatas");
quartets.addChild(genre);
genre.addChild(makeMusic("Sonata in A Minor", author, genre));
genre.addChild(makeMusic("Sonata in F Major", author, genre));
genre = makeFolder("Concertos");
author.addChild(genre);
genre.addChild(makeMusic("No. 1 - C", author, genre));
genre.addChild(makeMusic("No. 2 - B-Flat Major", author, genre));
genre.addChild(makeMusic("No. 3 - C Minor", author, genre));
genre.addChild(makeMusic("No. 4 - G Major", author, genre));
genre.addChild(makeMusic("No. 5 - E-Flat Major", author, genre));
genre = makeFolder("Symphonies");
author.addChild(genre);
genre.addChild(makeMusic("No. 1 - C Major", author, genre));
genre.addChild(makeMusic("No. 2 - D Major", author, genre));
genre.addChild(makeMusic("No. 3 - E-Flat Major", author, genre));
genre.addChild(makeMusic("No. 4 - B-Flat Major", author, genre));
genre.addChild(makeMusic("No. 5 - C Minor", author, genre));
genre.addChild(makeMusic("No. 6 - F Major", author, genre));
genre.addChild(makeMusic("No. 7 - A Major", author, genre));
genre.addChild(makeMusic("No. 8 - F Major", author, genre));
genre.addChild(makeMusic("No. 9 - D Minor", author, genre));
author = makeFolder("Brahms");
brahms = author;
root.addChild(author);
genre = makeFolder("Concertos");
author.addChild(genre);
genre.addChild(makeMusic("Violin Concerto", author, genre));
genre.addChild(makeMusic("Double Concerto - A Minor", author, genre));
genre.addChild(makeMusic("Piano Concerto No. 1 - D Minor", author, genre));
genre.addChild(makeMusic("Piano Concerto No. 2 - B-Flat Major", author, genre));
genre = makeFolder("Quartets");
author.addChild(genre);
genre.addChild(makeMusic("Piano Quartet No. 1 - G Minor", author, genre));
genre.addChild(makeMusic("Piano Quartet No. 2 - A Major", author, genre));
genre.addChild(makeMusic("Piano Quartet No. 3 - C Minor", author, genre));
genre.addChild(makeMusic("String Quartet No. 3 - B-Flat Minor", author, genre));
genre = makeFolder("Sonatas");
author.addChild(genre);
genre.addChild(makeMusic("Two Sonatas for Clarinet - F Minor", author, genre));
genre.addChild(makeMusic("Two Sonatas for Clarinet - E-Flat Major", author, genre));
genre = makeFolder("Symphonies");
author.addChild(genre);
genre.addChild(makeMusic("No. 1 - C Minor", author, genre));
genre.addChild(makeMusic("No. 2 - D Minor", author, genre));
genre.addChild(makeMusic("No. 3 - F Major", author, genre));
genre.addChild(makeMusic("No. 4 - E Minor", author, genre));
author = makeFolder("Mozart");
root.addChild(author);
genre = makeFolder("Concertos");
author.addChild(genre);
genre.addChild(makeMusic("Piano Concerto No. 12", author, genre));
genre.addChild(makeMusic("Piano Concerto No. 17", author, genre));
genre.addChild(makeMusic("Clarinet Concerto", author, genre));
genre.addChild(makeMusic("Violin Concerto No. 5", author, genre));
genre.addChild(makeMusic("Violin Concerto No. 4", author, genre));
return root;
}
private static long autoId = 0;
private FolderDto makeFolder(String name) {
FolderDto theReturn = new FolderDto(++autoId, name);
theReturn.setChildren(new ArrayList<BaseDto>());
return theReturn;
}
private MusicDto makeMusic(String name, FolderDto author, FolderDto genre) {
return makeMusic(name, author.getName(), genre.getName());
}
private MusicDto makeMusic(String name, String author, String genre) {
return new MusicDto(++autoId, name, genre, author);
}
public class BaseDto implements Serializable {
private Long id;
private String name;
protected BaseDto() {
}
public BaseDto(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public class FolderDto extends BaseDto {
private List<BaseDto> children;
protected FolderDto() {
}
public FolderDto(Long id, String name) {
super(id, name);
}
public List<BaseDto> getChildren() {
return children;
}
public void setChildren(List<BaseDto> children) {
this.children = children;
}
public void addChild(BaseDto child) {
getChildren().add(child);
}
}
public class MusicDto extends BaseDto {
private String genre;
private String author;
protected MusicDto() {
}
public MusicDto(Long id, String name, String genre, String author) {
super(id, name);
this.genre = genre;
this.author = author;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
}
@branflake2267
Copy link
Author

screen shot 2017-10-30 at 2 35 31 pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment