commit 8474a42a9003522ca6d99b8c28c57426e5545f6b
Author: Christos KK Loverdos <loverdos@gmail.com>
Date:   Thu Jun 20 15:33:47 2013 +0300

    WIP Investigating #4035
    
    Very inconsistent behavior of the selection mechanism. I get erroneous behavior
    with 20 deployments in a row and then suddendly the correct behavior
    for any new deployments.
    
    By deployment, I mean rebuild + deploy. The only thing I do in the mean time
    is put some extra logging statements all over the relevant places.
    
    Refs #4035

diff --git a/src/gr/grnet/pithos/web/client/PithosSelectionEventManager.java b/src/gr/grnet/pithos/web/client/PithosSelectionEventManager.java
index 42b7220..ebb74b2 100644
--- a/src/gr/grnet/pithos/web/client/PithosSelectionEventManager.java
+++ b/src/gr/grnet/pithos/web/client/PithosSelectionEventManager.java
@@ -43,6 +43,7 @@ import com.google.gwt.view.client.*;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 
 /**
  * An implementation of {@link com.google.gwt.view.client.CellPreviewEvent.Handler} that adds selection
@@ -103,6 +104,7 @@ public class PithosSelectionEventManager<T> implements
       if ("click".equals(nativeEvent.getType())) {
         // Ignore if the event didn't occur in the correct column.
         if (column > -1 && column != event.getColumn()) {
+            Pithos.LOG("PithosSelectionEventManager::translateSelectionEvent(), IGNORE because not same column: ", column, " != ", event.getColumn());
           return SelectAction.IGNORE;
         }
 
@@ -114,13 +116,16 @@ public class PithosSelectionEventManager<T> implements
             // Synchronize the checkbox with the current selection state.
             input.setChecked(event.getDisplay().getSelectionModel().isSelected(
                 event.getValue()));
+              Pithos.LOG("PithosSelectionEventManager::translateSelectionEvent(), TOGGLE: synchronizing checkbox with current selection");
             return SelectAction.TOGGLE;
           }
         }
+          Pithos.LOG("PithosSelectionEventManager::translateSelectionEvent(), IGNORE");
         return SelectAction.IGNORE;
       }
 
       // For keyboard events, do the default action.
+        Pithos.LOG("PithosSelectionEventManager::translateSelectionEvent(), DEFAULT, since keyboard event");
       return SelectAction.DEFAULT;
     }
   }
@@ -261,6 +266,7 @@ public class PithosSelectionEventManager<T> implements
   public void doMultiSelection(MultiSelectionModel<? super T> selectionModel,
       HasData<T> display, int row, T rowValue, SelectAction action,
       boolean selectRange, boolean clearOthers) {
+      Pithos.LOG("PithosSelectionEventManager::doMultiSelection(row=", row, ", rowValue=", rowValue, ", action=", action, ", selectRange=", selectRange, ", clearOthers=", clearOthers, ")");
     // Determine if we will add or remove selection.
     boolean addToSelection = true;
     if (action != null) {
@@ -375,13 +381,13 @@ public void onCellPreview(CellPreviewEvent<T> event) {
    */
   protected void handleMultiSelectionEvent(CellPreviewEvent<T> event,
       SelectAction action, MultiSelectionModel<? super T> selectionModel) {
-    Pithos.LOG("handleMultiSelectionEvent(action=", action, ")");
+    Pithos.LOG("PithosSelectionEventManager::handleMultiSelectionEvent(action=", action, ")");
     NativeEvent nativeEvent = event.getNativeEvent();
     String type = nativeEvent.getType();
     boolean rightclick = "mousedown".equals(type) && nativeEvent.getButton()==NativeEvent.BUTTON_RIGHT;
     SelectAction action1 = action;
     if(rightclick){
-        Pithos.LOG("handleMultiSelectionEvent(action=", action, "), rightclick");
+        Pithos.LOG("PithosSelectionEventManager::handleMultiSelectionEvent(action=", action, "), rightclick");
     	boolean shift = nativeEvent.getShiftKey();
         boolean ctrlOrMeta = nativeEvent.getCtrlKey() || nativeEvent.getMetaKey();
         boolean clearOthers = (translator == null) ? !ctrlOrMeta
@@ -391,6 +397,7 @@ public void onCellPreview(CellPreviewEvent<T> event) {
         }
         //if the row is selected then do nothing
         if(selectionModel.isSelected(event.getValue())){
+            Pithos.LOG("PithosSelectionEventManager::handleMultiSelectionEvent(action=", action, "), row is selected, event.getValue()=", event.getValue(), " RETURN");
         	return;
         }
         doMultiSelection(selectionModel, event.getDisplay(), event.getIndex(),
@@ -409,24 +416,23 @@ public void onCellPreview(CellPreviewEvent<T> event) {
       if (action == null || action == SelectAction.DEFAULT) {
         action1 = ctrlOrMeta ? SelectAction.TOGGLE : SelectAction.SELECT;
       }
-        Pithos.LOG("handleMultiSelectionEvent(action=", action, "), click, shift=", shift, ", ctrlOrMeta=", ctrlOrMeta, ", clearOthers=", clearOthers, ", action1=", action1);
+        Pithos.LOG("PithosSelectionEventManager::handleMultiSelectionEvent(action=", action, "), click, shift=", shift, ", ctrlOrMeta=", ctrlOrMeta, ", clearOthers=", clearOthers, ", action1=", action1);
       doMultiSelection(selectionModel, event.getDisplay(), event.getIndex(),
           event.getValue(), action1, shift, clearOthers);
       if(ctrlOrMeta){
-          Pithos.LOG("handleMultiSelectionEvent(action=", action, "), click, ctrlOrMeta=true => cancelling event=", event);
+          Pithos.LOG("PithosSelectionEventManager::handleMultiSelectionEvent(action=", action, "), click, ctrlOrMeta=true => cancelling event=", event);
     	  event.setCanceled(true);
       }
     } else if ("keyup".equals(type)) {
       int keyCode = nativeEvent.getKeyCode();
-        Pithos.LOG("handleMultiSelectionEvent(action=", action, "), keyup, keyCode=", keyCode, ", isSpaceBar=", keyCode == 32);
+        Pithos.LOG("PithosSelectionEventManager::handleMultiSelectionEvent(action=", action, "), keyup, keyCode=", keyCode, ", isSpaceBar=", keyCode == 32);
       if (keyCode == 32) {
         /*
          * Update selection when the space bar is pressed. The spacebar always
          * toggles selection, regardless of whether the control key is pressed.
          */
         boolean shift = nativeEvent.getShiftKey();
-        boolean clearOthers = (translator == null) ? false
-            : translator.clearCurrentSelection(event);
+        boolean clearOthers = (translator != null) && translator.clearCurrentSelection(event);
         if (action == null || action == SelectAction.DEFAULT) {
           action1 = SelectAction.TOGGLE;
         }
@@ -495,12 +501,35 @@ public void onCellPreview(CellPreviewEvent<T> event) {
    * @param selected true to select, false to deselect
    * @param clearOthers true to clear all other selected items
    */
-  protected void selectOne(MultiSelectionModel<? super T> selectionModel,
-      T target, boolean selected, boolean clearOthers) {
-    if (clearOthers) {
-      clearSelection(selectionModel);
-    }
-    selectionModel.setSelected(target, selected);
+  protected void selectOne(
+      MultiSelectionModel<? super T> selectionModel,
+      T target,
+      boolean selected,
+      boolean clearOthers
+  ) {
+      Pithos.LOG("PithosSelectionEventManager::selectOne(target=", target, ", selected=", selected, ", clearOthers=", clearOthers, ")");
+      if(clearOthers) {
+          Pithos.LOG("PithosSelectionEventManager::selectOne(...), before clearSelection");
+          Set<? super T> selectedSetBefore = selectionModel.getSelectedSet();
+          for(Object selectedObj : selectedSetBefore) {
+              Pithos.LOG("  ==> selected: ", selectedObj);
+          }
+
+          clearSelection(selectionModel);
+
+          Pithos.LOG("PithosSelectionEventManager::selectOne(...), after clearSelection");
+          Set<? super T> selectedSetAfter = selectionModel.getSelectedSet();
+          for(Object selectedObj : selectedSetAfter) {
+              Pithos.LOG("  ==> selected: ", selectedObj);
+          }
+      }
+
+      selectionModel.setSelected(target, selected);
+      Pithos.LOG("PithosSelectionEventManager::selectOne(...), after selectionModel.setSelected(target, selected)");
+      Set<? super T> selectedSetAfter2 = selectionModel.getSelectedSet();
+      for(Object selectedObj : selectedSetAfter2) {
+          Pithos.LOG("  ==> selected: ", selectedObj);
+      }
   }
 
   /**
