简单地说,我有一个FlexTable(或者类似于继承自FlexTable的东西),并且想要将一个ClickHandler(或一个扩展的ClickHandler)添加到FlexTable中,而不是ClickListener,它应该可以处理“left-单击“ - 和”右键单击“-events。
我知道,GWT-core不支持右键单击/ ContextMenus,因为GWT-dev-teams认为,网页应用程序应该通过右键单击显示浏览器默认上下文菜单的“常规行为”;我完全同意,无论如何我必须为客户实施它。 在http://whatwouldnickdo.com/wordpress/370/gwt-right-click-context-menu/有一个例子,但这是使用GWTs“旧事件处理模型”。我正在寻找一种不使用弃用的解决方案(在GWT 2.0.4中)。
提前感谢任何好的提示或“解决方案”!
答案 0 :(得分:3)
我找到了一个有效的解决方案。
现在看起来很简单。我多次尝试类似的事情却没有成功;但现在它有效。 显然,解决方案并没有真正使用我在开始发布时所考虑的“新事件模型”...... ...但是,如果没有覆盖com.google.gwt.user.client.ui.Widget #onBrowserEvent(Event theEvent),我看不到任何解决方案。
public class RceFlexTable extends FlexTable {
public RceFlexTable() {
sinkEvents(Event.ONCONTEXTMENU);
}
List<List<ContextClickHandler> contextClicktClickHandlers= new ArrayList<ContextClickHandler>();
public void addContextClickHandler(ContextClickHandler theContextClickHandler ){
contextClicktClickHandlers.add(theContextClickHandler);
}
void removeContextClickHandler(ContextClickHandler theContextClickHandler ){
contextClicktClickHandlers.remove(theContextClickHandler);
}
@Override
public void onBrowserEvent(Event theEvent) {
int anEventType = DOM.eventGetType(theEvent);
if(anEventType == Event.ONCONTEXTMENU) {
//Window.alert("context=Right Click detected");
for(int i=0; i<contextClicktClickHandlers.size();i++){
contextClicktClickHandlers.get(i).handleContextClick(theEvent);
}
// System.out.println("context=Right Click detected");
theEvent.stopPropagation();// This will stop the event from being propagated
theEvent.preventDefault();
} else {
// other browser events
super.onBrowserEvent(theEvent);
}
}
}
public interface ContextClickHandler {
/**
* hanler for right clicks
* @param theEvent
*/
void handleContextClick(Event theEvent);
}
答案 1 :(得分:0)
您需要扩展FlexTable来处理鼠标事件;鼠标右键不会触发ClickEvent
。为简洁起见,我只是实现了mouseDown:
public class FlexTableWithMouseEvents
extends FlexTable implements HasMouseDownHandlers {
public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
return addDomHandler(handler, MouseDownEvent.getType());
}
// This useful method is taken from HTMLTable. Only the signature needs
// to be changed, unfortunately that means copying the whole method.
public Cell getCellForEvent(MouseEvent event) {
Element td = getEventTargetCell(Event.as(event.getNativeEvent()));
if (td == null) {
return null;
}
Element tr = DOM.getParent(td);
Element body = DOM.getParent(tr);
int row = DOM.getChildIndex(body, tr);
int column = DOM.getChildIndex(tr, td);
return new Cell(row, column);
}
}
然后:
flexTable.addMouseDownHandler(new MouseDownHandler() {
public void onClick(MouseDownEvent event) {
Cell cell = flexTable.getCellForEvent(event);
int button = event.getNativeEvent().getButton();
if (button == NativeEvent.BUTTON_LEFT) {
doLeftClick(cell);
}
if (button == NativeEvent.BUTTON_RIGHT) {
event.preventDefault();
doRightClick(cell);
}
}
}