Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Show treegrid folder icon but not leaf icon

    Browser: Firefox 60.1
    Smartgwt : Version 6.1p, Built 2017-09-05
    GWT: 2.7

    I can't seem to figure out how to show the folder icon as a custom icon but hide the leaf icon. Below is latest attempt. I successfully show the folder icon but I can't figure out how to hide the leaf one.

    public class MyTreeGrid extends TreeGrid {

    protected TreeGridField nodeId = new TreeGridField("id", "Id");
    protected TreeGridField rootNode = new TreeGridField("rootValue", "Root");
    protected TreeGridField isFolder = new TreeGridField("isFolder", "isFolder");
    protected String context;

    public MyTreeGrid(String context) {
    this.context = context;
    buildComponents();
    }

    protected void buildComponents() {
    setHeight100();
    setWidth100();
    setDataSource(buildDS());
    setAutoFetchData(false);
    setFields(nodeId, rootNode, isFolder);
    nodeId.setHidden(true);
    isFolder.setHidden(true);
    setSelectionAppearance(SelectionAppearance.CHECKBOX);
    setSelectionType(SelectionStyle.SIMPLE);
    setOverflow(Overflow.VISIBLE);
    setShowRecordComponents(true);
    setShowRecordComponentsByCell(true);
    setCustomIconProperty("rootIcon");
    }

    @Override
    protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {
    String fieldName = this.getFieldName(colNum);
    if (fieldName.equals("rootValue")) {
    Boolean isFolder = record.getAttribute("isFolder");
    if (!isFolder) {
    setIconSize(1);
    setNodeIcon(null);
    } else {
    return null;
    }
    } else {
    return null;
    }
    }

    protected MyDataSource buildDS() {
    return new MyDataSource(context);
    }
    }

    public class MyDataSource extends RestDataSource() {

    private String context;

    public MyDataSource(String context) {
    this.context = context;
    setClientOnly(false);
    setDataFormat(DSDataFormat.JSON);
    setJsonPrefix(""):
    setJsonSuffix("");
    init();
    }

    protected void init() {
    DataSourceField id = new DataSourceTextField("id", "ID");
    id.setPrimaryKey(true);
    id.setRequired(true);
    id.setDisplayField("value");

    DataSourceField parentId = new DataSourceTextField("id", "ID");
    parentId.setRootValue("root");

    DataSourceField value = DatasourceTextField("value", "Value");
    DataSourceBooleanField isFolder = new DataSourceBooleanField("isFolder", "isFolder");
    isFolder.setRequired('true");

    setFields(id, value, isFolder);

    setDataURL("rest/service/findItems");
    }
    }

    my Rest service creates dummy data as follows:

    protected List<MyItem> createDummyItems() {
    List<MyItem> attr = new ArrayList<MyItem>();

    String[] fruits = new String[] {"apple", "orange", "kiwi"}
    String[] veggies = new String[] {"salad", "carrot", "onion"}

    String parentId = "Fruits";
    MyItem root = new MyItem("root", parentId, parentId, true);
    attr.add(root);

    for (int i=0; i<3; i++) {
    String val = fruits[i];
    MyItem child = new MyItem(parentId, parentId+val, val, false);
    attr.add(child);
    }

    parentId = "Veggies";
    root = new MyItem("root", parentId, parentId, true);
    attr.add(root);

    for (int i=0; i<3; i++) {
    String val = veggies[i];
    MyItem child = new MyItem(parentId, parentId+val, val, false);
    attr.add(child);
    }

    return attr;
    }

    public class MyItem() {
    private String rootIcon;
    private String value;
    private String id;
    private String parentId;
    private Boolean isFolder;
    private String rootValue;

    public MyItem(String parentId, String id, String value, boolean isFolder) {
    this.parentId = parentId;
    this.id = id;
    this.value = value;
    this.isFolder = isFolder;
    if (isFolder) {
    rootIcon = "myicon.gif";
    } else {
    rootIcon = null;
    }

    }

    }

    public class MyProject implements EntryPoint {

    MyTreeGrid treeGrid = null;
    String context;

    public void onModuleLoad() {
    context = GWT.getHostPageBaseURL();
    HLayout mainContainer = new HLayout();
    mainContainer.setWidth(800);
    mainContainer.setHeight(500);
    treeGrid = new MyTreeGrid(context);
    mainContainer.addMember(treeGrid);
    treeGrid.fetchData();

    }

    }

    #2
    I think I figured it out. I just needed to override the following method to the TreeGrid

    @Override
    protected String getIcon(Record record, boolean defaultState) {
    Boolean isFolder = record.getAttributeAsBoolean("isFolder");
    if (isFolder) {
    return "myicon.gif";
    } else {
    return null;
    }
    }

    Comment

    Working...
    X