1 package autotest.common.ui; 2 3 import com.google.gwt.event.dom.client.ContextMenuEvent; 4 import com.google.gwt.event.dom.client.ContextMenuHandler; 5 import com.google.gwt.event.dom.client.DomEvent; 6 import com.google.gwt.event.dom.client.HasContextMenuHandlers; 7 import com.google.gwt.event.shared.HandlerRegistration; 8 import com.google.gwt.user.client.DOM; 9 import com.google.gwt.user.client.Element; 10 import com.google.gwt.user.client.Event; 11 import com.google.gwt.user.client.ui.FlexTable; 12 import com.google.gwt.user.client.ui.HTMLTable; 13 14 public class RightClickTable extends FlexTable 15 implements ContextMenuHandler, HasContextMenuHandlers { 16 17 protected static class RowColumn { 18 int row; 19 int column; 20 RowColumn(int row, int column)21 public RowColumn(int row, int column) { 22 this.row = row; 23 this.column = column; 24 } 25 } 26 27 private boolean hasHandler; 28 29 @Override addContextMenuHandler(ContextMenuHandler handler)30 public HandlerRegistration addContextMenuHandler(ContextMenuHandler handler) { 31 if (!hasHandler) { 32 addDomHandler(this, ContextMenuEvent.getType()); 33 hasHandler = true; 34 } 35 return addDomHandler(handler, ContextMenuEvent.getType()); 36 } 37 38 @Override onContextMenu(ContextMenuEvent event)39 public void onContextMenu(ContextMenuEvent event) { 40 event.preventDefault(); 41 } 42 getCellForDomEvent(DomEvent<?> event)43 public HTMLTable.Cell getCellForDomEvent(DomEvent<?> event) { 44 // This is copied from HTMLTable.getCellForEvent(). 45 final Element td = getEventTargetCell(Event.as(event.getNativeEvent())); 46 if (td == null) { 47 return null; 48 } 49 50 RowColumn position = getCellPosition(td); 51 52 return new HTMLTable.Cell(position.row, position.column) { 53 @Override 54 public Element getElement() { 55 return td; 56 } 57 }; 58 } 59 60 protected RowColumn getCellPosition(Element td) { 61 Element tr = DOM.getParent(td); 62 Element body = DOM.getParent(tr); 63 int row = DOM.getChildIndex(body, tr); 64 int column = DOM.getChildIndex(tr, td); 65 return new RowColumn(row, column); 66 } 67 } 68