简体   繁体   English

如何在CellTable(GWT 2.4)中为ImageResourceCell创建PanelPopup?

[英]How can I create a PanelPopup for an ImageResourceCell in a CellTable (GWT 2.4)?

Our application displays a CellTable which has an ImageResourceCell column: 我们的应用程序显示一个CellTable,其中具有一个ImageResourceCell列:

    Column<Alert, ImageResource> noteColumn = new Column<Alert, ImageResource>(new ImageResourceCell()) {
        @Override
        public ImageResource getValue(Alert alert) {
            if (alert.getNote() != null && alert.getNote().length() > 0) {
                return images.note();
            } else {
                return images.emptyNote();
            }
        }
      };
    dataTable.addColumn(noteColumn, "");

We would like to modify this column so that a PopupPanel is displayed when the user moves his mouse over the image. 我们想修改此列,以便在用户将鼠标移到图像上时显示PopupPanel。 The content of the PopupPanel should be specific for each row in the column. PopupPanel的内容应特定于该列中的每一行。 What is the best way to achieve this? 实现此目标的最佳方法是什么?

My initial attempt (based on an answer to a somewhat-similar question ) looks like this, however the problem here is that I am not able to get the "note" from each Alert for the PopupPanel: 我的最初尝试(基于对一个类似问题的答案)看起来像这样,但是这里的问题是我无法从PopupPanel的每个Alert中获取“注释”:

private class MyImageResourceCell extends ImageResourceCell {
    @Override
    public Set<String> getConsumedEvents() {
        Set<String> consumedEvents = new HashSet<String>();
        consumedEvents.add("mouseover");
        consumedEvents.add("mouseout");
        return consumedEvents;
    }

    @Override
    public void onBrowserEvent(Context context,
                               Element parent,
                               ImageResource value,
                               NativeEvent event,
                               ValueUpdater<ImageResource> imageResourceValueUpdater) {
        switch (DOM.eventGetType((Event) event)) {
            case Event.ONMOUSEOVER:
                // show panel
                break;
            case Event.ONMOUSEOUT:
                // hide panel
                break;
            default:
                break;
        }
    }
}

private void createAndAddNoteColumn() {
    Column<Alert, ImageResource> noteColumn = new Column<Alert, ImageResource>(new MyImageCell()) {
        @Override
        public ImageResource getValue(Alert alert) {
            if (alert.getNote() != null && alert.getNote().length() > 0) {
                return images.note();
            } else {
                return images.emptyNote();
            }
        }
    };
    dataTable.addColumn(noteColumn, "");
}

Instead of creating the PopupPanel as part of the table column, I left the column as a normal ImageResource column, and then added a cellPreviewHandler to capture the mouseover event for the column in question: 我没有将PopupPanel创建为table列的一部分,而是将该列保留为普通的ImageResource列,然后添加了cellPreviewHandler来捕获有关列的mouseover事件:

    final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
    dataTable.addCellPreviewHandler(new CellPreviewEvent.Handler<Alert>() {
        @Override
        public void onCellPreview(CellPreviewEvent<Alert> alertCellPreviewEvent) {
            if ("mouseover".equals(alertCellPreviewEvent.getNativeEvent().getType())
                    && alertCellPreviewEvent.getColumn() == 5
                    && alertCellPreviewEvent.getValue().getNote() != null) {
                // using hard-coded column number isn't the most elegant solution, but it's ok for now.
                int left = alertCellPreviewEvent.getNativeEvent().getClientX();
                int top = alertCellPreviewEvent.getNativeEvent().getClientY();
                simplePopup.setWidget(new HTML(alertCellPreviewEvent.getValue().getNote()));
                simplePopup.setPopupPosition(left, top);
                simplePopup.show();
            } else {
                simplePopup.hide();
            }
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM