adding Double click event in CellTable cell - GWT

心不动则不痛 提交于 2019-12-28 13:25:23

问题


I am unable to figure out how could I add a double click event to the cell of the CellTable.

Is it possible with GWT CellTable or not?

Is there any workaround

thank you..

al

BTW, i saw this post but there is no reply... http://www.devcomments.com/Adding-DoubleClicks-and-OnContextMenu-to-CellTable-at1066168.htm


回答1:


I crafted something different that just fit my needs:

cellTable.addCellPreviewHandler(new Handler<TitoloProxy>() {

           long lastClick=-1000;

           @Override
           public void onCellPreview(CellPreviewEvent<TitoloProxy> event) {
               long clictAt = System.currentTimeMillis();
               GWT.log("clickAt: "+(clictAt));
               GWT.log("lastClick: "+(lastClick));
               if(event.getNativeEvent().getType().contains("click")){
                   GWT.log(""+(clictAt-lastClick));
                   if(clictAt-lastClick < 300) { // dblclick on 2 clicks detected within 300 ms
                       Window.alert("I am a double click crafted event!");

                   }
                   lastClick = System.currentTimeMillis();
               }
           }
       });



回答2:


    cellTable.addDomHandler(new DoubleClickHandler() {

        @Override
        public void onDoubleClick(DoubleClickEvent event) {
            Window.alert("That's it!");
        }
    }, DoubleClickEvent.getType());



回答3:


Integer row=0;// to hold row index
Integer column=0;// to hold column index 
_Grid.addCellPreviewHandler(new CellPreviewEvent.Handler<Model>() {
// this is to handle row id
        @Override
        public void onCellPreview(final CellPreviewEvent<Model> event) {

            if (BrowserEvents.CLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
                row = event.getIndex();
                column=event.getColumn();
            }

        }
    });

// because Doubleclick handler doesn't give row index or column index we will use addCellPreviewHandler to return row index or column index.
_Grid.addDomHandler(new DoubleClickHandler() {

        @Override
        public void onDoubleClick(final DoubleClickEvent event) {
            System.out.println(" You clicked row = " + row);
            System.out.println(" You clicked column = " + column);

        }
    }, DoubleClickEvent.getType());



回答4:


For cell lists, this code works ok:

  cellList.addDomHandler(new DoubleClickHandler() {
    @Override
    public void onDoubleClick(DoubleClickEvent event) {
      // do the stuff
    }
  }, DoubleClickEvent.getType());

I'm not sure about table cells




回答5:


Because the CellPreview interface does not natively capture double click events you will need add event logic into the Overriden onCellPreview method. First you would think the best way would be to check the click time differences. However it is much more efficient and elegant to use a state machine and count clicks. This is more robust and allows you to deal with multiple event cases - Such as mouse hover, single, and double clicks. The code is pretty straightforward. So enjoy!

public class CellHoverHandler implements Handler<T> {

    Timer singleClickTimer;
    int clickCount = 0;
    int clickDelay = 300;

    @Override
    public void onCellPreview(final CellPreviewEvent<T> event) {
        if (Event.getTypeInt(event.getNativeEvent().getType()) == Event.ONMOUSEOVER) {
            handleOnMouseOver(event);
        } else if (Event.getTypeInt(event.getNativeEvent().getType()) == Event.ONCLICK) {
            clickCount++;
            if (clickCount == 1) {
                singleClickTimer = new Timer() {

                    @Override
                    public void run() {
                        clickCount = 0;
                        handleOnClick(event);
                    }
                };
                singleClickTimer.schedule(clickDelay);
            } else if (clickCount == 2) {
                singleClickTimer.cancel();
                clickCount = 0;
                handleOnDblClick(event);
            }
        }
    }

    private void handleOnMouseOver(CellPreviewEvent<T> event) {
        Element cell = event.getNativeEvent().getEventTarget().cast();
        GWT.log("mouse over event");
    }

    private void handleOnClick(CellPreviewEvent<T> event) {
        Element cell = event.getNativeEvent().getEventTarget().cast();
        GWT.log("click event");
    }

    private void handleOnDblClick(CellPreviewEvent<T> event) {
        Element cell = event.getNativeEvent().getEventTarget().cast();            
        GWT.log("double click event");
    }

OPTIMIZATION: feel free to stick the count, timer, and delay as static class members or global members to reuse. Also check to see if the timer is null before making a new instance. I had omitted this for simplicity. Unlike a lot of other techniques this way still provides you with easy and direct access to the cell event. The technique with overloading the AbstractCell works well too, however sometimes you really don't have custom cells or want to make a custom cell to just handle events on the cell.




回答6:


Leaving this here for future reference

    private Set<GenericEventHandler<T>> dblClickHandlers = new HashSet<>(4);

dblClickHandlers simply maps interface implementations of my choice

    table.addCellPreviewHandler(event -> {
        if (BrowserEvents.DBLCLICK.equalsIgnoreCase(event.getNativeEvent().getType())) {
            LOGGER.info("dblclick (native) " + event.getIndex() + "  " + event.getColumn() + "; " + event.getValue());
            dblClickHandlers.forEach(handler -> {
                handler.onEvent(event.getValue());
            });
        }
    });
    table.sinkBitlessEvent(BrowserEvents.DBLCLICK);

The trick is to sink the 'dblclick' event.




回答7:


If you wanted a text cell that allows you to support your own chosen list of events, you can use this:

public class EventfulTextCell extends AbstractSafeHtmlCell`<String`> {

    private static final String[] NO_CONSUMED_EVENTS = null; 

    public EventfulTextCell() {
        this(NO_CONSUMED_EVENTS);
    }

    public EventfulTextCell(String... consumedEvents) {
        super(SimpleSafeHtmlRenderer.getInstance(), consumedEvents);
    }

    @Override
    public void render(Context context, SafeHtml value, SafeHtmlBuilder sb) {
        if (value != null) {
            sb.append(value);
        }
    }
}

Then you instantiate it:

new EventfulTextCell("click", "dblclick")

Then override the onBrowserEvent() method to process your events.



来源:https://stackoverflow.com/questions/4815596/adding-double-click-event-in-celltable-cell-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!