Thursday, April 11, 2013

Vaadin < 7.0.3 + TreeTable(or Table) + Context Menu ( Action Handler ) refreshing problem

Today, I had a tedious problem with Vaadin TreeTable's context menu. In addition, I do think that Table has similar problem as well.
Here is what I do :
1. Do addActionHandler(new Action.Handler() { ...}) to TreeTable.
2. After that, there is a button click event that triggers : removeAllActionHandlers();from TreeTable.
3. Lastly, I add new ActionHandler to TreeTable : addActionHandler(new Action.Handler() { ...})
I expected that context menus from Action.Handler created in step 3 will replace those context menus from step 1. However, that's not the case, context menu did not get updated/refreshed.
Although one ticket was submitted and marked as fixed in Vaadin's bug tracker : . It seems that I still need to force a refresh with refreshRowCache() to refresh context menu. However, refreshRowCache() has problems with generated columns of TreeTable that I can't use it. Moreover, refreshRowCache() is not such a good function to use according to Vaadin's document: "Note that calling this method is not cheap so avoid calling it unnecessarily" So I had a deeper look at the code :
  /**  
    * Removes all action handlers  
    */  
   public void removeAllActionHandlers() {  
     actionHandlers = null;  
     actionMapper = null;  
     // Assures the visual refresh. No need to reset the page buffer  
     // before as the content has not changed, only the action  
     // handlers.  
     refreshRenderedCells();  
   }  
The key problem lies in refreshRenderedCells function
  protected void refreshRenderedCells() {  
     ...  
     if (!isContentRefreshesEnabled) {  
       return;  
     }  
           ...  
  }  
Then I noticed isContentRefreshesEnabled is false. As a result, context menu was not updated properly:
  protected void enableContentRefreshing(boolean refreshContent) {  
     isContentRefreshesEnabled = true;  
     if (refreshContent) {  
       refreshRenderedCells();  
       // Ensure that client gets a response  
       markAsDirty();  
     }  
   }  
Voila, instead of calling refreshRowCache(), enableContentRefreshing(true) should be called to update the context menu.
1. Do addActionHandler(new Action.Handler() { ...}) to TreeTable.
2. After that, there is a button click event that trigger : removeAllActionHandlers();from TreeTable.
3. Add new ActionHandler to TreeTable : addActionHandler(new Action.Handler() { ...})
4. Call enableContentRefreshing(true)


I am not entirely sure if this is a bug, but at least it is my work-around for this problem.

No comments:

Post a Comment