How to remove "no data" labels from empty nodes in GWT?

426 Views Asked by At

I have a widget that inherits from CellTree. If the node not have the child elements, this node can be opened and shows "no data" label.

I'd like to see nodes without child's displayed as empty.

That's how I fill the tree. My DictionaryTreeDataProvider class (relevant part):

public class DictionaryTreeDataProvider extends ListDataProvider<MValue> {
    private final DictionariesServiceAsync service = GWT.create(DictionariesService.class);

    ...    

    @Override
    public void onRangeChanged(HasData<MValue> result) {
        service.queryDictionaryValues(range, query, new AsyncCallback<SubsetResult<MValue>>() {
            @Override
            public void onFailure(Throwable t) {
            }

            @Override
            public void onSuccess(SubsetResult<MValue> result) {
                getList().clear();
                for (MValue value : result.items) {
                    getList().add(value);
                }
            }
        });
    }
}

On the server side I make EJB call which fills SubsetResult. I found that this problem fixed in version of GWT-2.5.0-rc2 (see https://groups.google.com/forum/#!topic/google-web-toolkit/d-rFUmyHTT4).


Now everything is OK, thanks to @moutellou. I did as he suggested:

...
@Override
public void onSuccess(SubsetResult<MValue> result) {
    if (result.length == 0) {
        updateRowCount(-1, true);
        return;
    } else {
        for (MValue value : result.items) {
            // some checks here
            getList().add(value);
        }
    }
}
...
3

There are 3 best solutions below

0
On BEST ANSWER

This is how I removed the no data label in my DataProvider

 //Fetch children
  int size = children.size();
  if (size == 0) {
     updateRowCount(-1, true); //Method called on AsyncDataProvider 
     return;
  }  
0
On

In the TreeViewModel, make sure that the isLeaf method returns true if the argument value has no children. Example:

@Override
public boolean isLeaf(Object value) {
    if (value instanceof DepartmentDto) {
        DepartmentDto department = (DepartmentDto) value;
        return department.getEmployees().isEmpty();
    } else if (value instanceof EmployeeDto) {
        return true;
    } else {
        return false;
    }
}

In this case, a department should declare itself as a leaf only if it has no employees, an employee will declare itself as a leaf, and default to false.

Note that value many also be an internal GWT node. In this example, it might not necessarily be just DepartmentDto and EmployeeDto.

0
On

Some alternative solution. Can be defined interface that extends the interface CellTree.Resources. In this interface must specify the path to the CSS, which override the desired style.

Interface CellTree.Resources:

public class CellTree extends AbstractCellTree implements HasAnimation,
    Focusable {
   ...  
  /**
   * A ClientBundle that provides images for this widget.
   */
  public interface Resources extends ClientBundle {

    /**
     * An image indicating a closed branch.
     */
    @ImageOptions(flipRtl = true)
    @Source("cellTreeClosedArrow.png")
    ImageResource cellTreeClosedItem();

    /**
     * An image indicating that a node is loading.
     */
    @ImageOptions(flipRtl = true)
    ImageResource cellTreeLoading();

    /**
     * An image indicating an open branch.
     */
    @ImageOptions(flipRtl = true)
    @Source("cellTreeOpenArrow.png")
    ImageResource cellTreeOpenItem();

    /**
     * The background used for selected items.
     */
    @ImageOptions(repeatStyle = RepeatStyle.Horizontal, flipRtl = true)
    ImageResource cellTreeSelectedBackground();

    /**
     * The styles used in this widget.
     */
    @Source(Style.DEFAULT_CSS)
    Style cellTreeStyle();
  } 
...
}

Interface CustomCellTreeResources, based on CellTree.Resources:

import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.user.cellview.client.CellTree;

public interface CustomCellTreeResources extends CellTree.Resources {
    static final String STYLE_PATH = "components/common/client/static/custom-cell-tree.css";

    @Override
    @ClientBundle.Source({CellTree.Style.DEFAULT_CSS, STYLE_PATH})
    CellTree.Style cellTreeStyle();
}

Overriding rule:

.cellTreeEmptyMessage {
    display: none;
}

Create an instance:

private final static CellTree.Resources customCellTreeResources = 
    GWT.create(CustomCellTreeResources.class);

And next need to explicitly pass customCellTreeResources to the CellTree class constructor.

Message is not displayed more.

Mandatory: before filing the list, ie, before clicking on a node, the list should be cleaned( getList().clear();):

@Override
public void onRangeChanged(HasData<MValue> result) {
    service.queryDictionaryValues(range, query, 
          new AsyncCallback<SubsetResult<MValue>>() {
        @Override
        public void onFailure(Throwable t) {}
        @Override
        public void onSuccess(SubsetResult<MValue> result) {
            getList().clear();
            for (MValue value : result.items) {
                getList().add(value);
            }
        }
    });
}