如何在Tree Grid GXT(GWT)列中设置图标?

时间:2017-08-08 09:59:03

标签: java gwt tree gxt treegrid

我正在开发GWT应用程序,我应该将Tree Grid中的默认图标更改为我自己的图标。在其他情况下,我可以轻松设置图标,但当我有树格时不是这种情况。当我只有默认网格时,我设置图标的代码是这样的:

Protected List<ColumnConfig> getColumns() {
        List<ColumnConfig> columnConfigs = new ArrayList<ColumnConfig>();

        ColumnConfig columnConfig = new ColumnConfig("status", MSGS.gridUserColumnHeaderStatus(), 50);
        GridCellRenderer<GwtUser> setStatusIcon = new GridCellRenderer<GwtUser>() {

            public String render(GwtUser gwtUser, String property, ColumnData config, int rowIndex, int colIndex, ListStore<GwtUser> deviceList, Grid<GwtUser> grid) {

                KapuaIcon icon;
                if (gwtUser.getStatusEnum() != null) {
                    switch (gwtUser.getStatusEnum()) {
                    case DISABLED:
                        icon = new KapuaIcon(IconSet.USER);
                        icon.setColor(Color.RED);
                        break;
                    case ENABLED:
                        icon = new KapuaIcon(IconSet.USER);
                        icon.setColor(Color.GREEN);
                        break;
                    default:
                        icon = new KapuaIcon(IconSet.USER);
                        icon.setColor(Color.GREY);
                        break;
                    }
                } else {
                    icon = new KapuaIcon(IconSet.USER);
                    icon.setColor(Color.GREY);
                }

                return icon.getInlineHTML();
            }
        };
        columnConfig.setRenderer(setStatusIcon);
        columnConfig.setAlignment(HorizontalAlignment.CENTER);
        columnConfig.setSortable(false);
        columnConfigs.add(columnConfig);

但是当我在Tree Grid上应用它时,只有第一个项目有一个图标,我无法展开网格。这是我目前的Tree Grid代码

 List<ColumnConfig> configs = new ArrayList<ColumnConfig>();

        ColumnConfig column = new ColumnConfig("topicName", MSGS.topicInfoTableTopicHeader(), 150);
        column.setRenderer(new TreeGridCellRenderer<GwtTopic>());
        configs.add(column);

        column = new ColumnConfig("timestamp", MSGS.topicInfoTableLastPostedHeader(), 150);
        configs.add(column);

        store = new TreeStore<GwtTopic>();
        AsyncCallback<List<GwtTopic>> topicsCallback = new AsyncCallback<List<GwtTopic>>() {

            @Override
            public void onSuccess(List<GwtTopic> topics) {
                store.add(topics, true);
                topicInfoGrid.unmask();
            }

            @Override
            public void onFailure(Throwable t) {
                FailureHandler.handle(t);
                topicInfoGrid.unmask();
            }
        };
        dataService.findTopicsTree(currentSession.getSelectedAccount().getId(), topicsCallback);
        topicInfoGrid = new TreeGrid<GwtTopic>(store, new ColumnModel(configs));
        topicInfoGrid.setBorders(false);
        topicInfoGrid.setStateful(false);
        topicInfoGrid.setLoadMask(true);
        topicInfoGrid.mask("Loading");
        topicInfoGrid.setStripeRows(true);
        topicInfoGrid.getView().setAutoFill(true);
        topicInfoGrid.getView().setEmptyText(MSGS.topicInfoGridEmptyText());
        topicInfoGrid.disableTextSelection(false);

如何更改此代码以便能够设置我自己的图标?

1 个答案:

答案 0 :(得分:0)

您可以使用setIconProvider方法更改叶子的图标:

topicInfoGrid.setIconProvider(new IconProvider<GwtTopic>() {
            @Override
            public ImageResource getIcon(GwtTopic gwtTopic) {
                GwtTopicType type = gwtTopic.getType();
                if (type == GwtTopicType.Folder) {
                    if (topicInfoGrid.isExpanded(gwtTopic)) return new TopicInfoResources.Instance.iconFolderOpen();
                    else return TopicInfoResources.Instance.iconFolder();
                }
                return TopicInfoResources.Instance.user();
            }
        });

这是扩展ClientBundle的界面:

public interface TopicInfoResources extends ClientBundle
{
    public static final TopicInfoResources Instance = GWT.create(TopicInfoResources.class);

    @Source("folder.png")               ImageResource iconFolder();
    @Source("folder-open.png")          ImageResource iconFolderOpen();
    @Source("user.png")                 ImageResource user();
}

希望这有帮助!