In SmartGWT, what event tells you if something is being drawn?
-
06-12-2019 - |
Question
For example, I have a TreeGrid
and I have added a ContextMenu
to the TreeGrid
. Now I want to draw the ContextMenu
only when the user right-click a Leaf Node. My idea is to detect when the ContextMenu
is about to be drawn and check if the right-clicked node is a Leaf node:
- If the node is a leaf, draw the
ContextMenu
- If the node is NOT a leaf, cancel the draw
But what event handles the drawing of a component?
UPDATE:
The showContextMenuHandler
would not work. Actually this is a SmartGWT
bug, because if I say contextMenu.show(), I actually get 2 context menus: one from SmartGWT
(the one I made) and the second one of the browser which contains elements such as (next page, previous page, select All, page source, ..). the same goes for LeafContextClickHandler
, ...
This is why I want to add a draw handler for the ContextMenu itself to prevent it from drawing in special cases.
Here is an image explaining the bug:
Solution
Your issue with the ShowContextMenuHandler is not a bug, but rather a "feature" of SmartGWT. In any case if you don't want to show the browser's menu, all you have to do is to stop the propagation of the event to the browser. This can be easily achieved with the following code snippet:
addShowContextMenuHandler(new ShowContextMenuHandler() {
@Override
public void onShowContextMenu(ShowContextMenuEvent event) {
//Do not propagate to the browser's menu!
event.cancel();
//Continue with your code ....
}
}
OTHER TIPS
There are a couple of ways of doing this. You must have a reference to the component being drawn. Then you can add a DrawHandler , VisibilityChangedHandler or in the case of the example , a ShowContextMenuHandler .
In your example though , I think the better way to achieve said mechanism is by using LeafClickHandlers and FolderClickHandlers on the TreeGrid.