Bug 415119 - SWT: Disposong a TabBox is changing the selected group box in scout model

https://bugs.eclipse.org/bugs/show_bug.cgi?id=415119

(cherry picked from commit fa594a0c6b606abad39091ab9d357bff95eab419)

Change-Id: If771df00da2bb825930ca6f6786305577346cf5e
Reviewed-on: https://git.eclipse.org/r/15677
Tested-by: Hudson CI
Reviewed-by: Ken Lee <kle@bsiag.com>
IP-Clean: Ken Lee <kle@bsiag.com>
diff --git a/org.eclipse.scout.rt.ui.swt/src/org/eclipse/scout/rt/ui/swt/form/fields/tabbox/SwtScoutTabBox.java b/org.eclipse.scout.rt.ui.swt/src/org/eclipse/scout/rt/ui/swt/form/fields/tabbox/SwtScoutTabBox.java
index 648e9aa..9f0acca 100644
--- a/org.eclipse.scout.rt.ui.swt/src/org/eclipse/scout/rt/ui/swt/form/fields/tabbox/SwtScoutTabBox.java
+++ b/org.eclipse.scout.rt.ui.swt/src/org/eclipse/scout/rt/ui/swt/form/fields/tabbox/SwtScoutTabBox.java
@@ -13,8 +13,6 @@
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.util.HashMap;
-import java.util.Map;
-import java.util.Map.Entry;
 
 import org.eclipse.scout.commons.OptimisticLock;
 import org.eclipse.scout.commons.RunnableWithData;
@@ -41,6 +39,7 @@
   private static final IScoutLogger LOG = ScoutLogManager.getLogger(SwtScoutTabBox.class);
 
   private HashMap<CTabItem, SwtScoutTabItem> m_tabs;
+  private HashMap<IGroupBox, SwtScoutTabItem> m_scoutTabMapping;
   private Listener m_uiTabFocusListener;
   private P_TabListener m_tabListener = new P_TabListener();
   private OptimisticLock m_selectedTabLock;
@@ -73,11 +72,30 @@
 
       @Override
       public void widgetDisposed(DisposeEvent e) {
-        if (m_tabs != null) {
-          for (Entry<CTabItem, SwtScoutTabItem> tabEntry : m_tabs.entrySet()) {
-            tabEntry.getKey().dispose();
-            tabEntry.getValue().dispose();
+        if (m_scoutTabMapping != null) {
+          IGroupBox selectedGroupBox = getScoutObject().getSelectedTab();
+          IGroupBox[] groupBoxes = getScoutObject().getGroupBoxes();
+          //reverse order to avoid flickering
+          for (int i = groupBoxes.length - 1; i >= 0; i--) {
+            if (groupBoxes[i] != selectedGroupBox) {
+              SwtScoutTabItem swtScoutTabItem = m_scoutTabMapping.remove(groupBoxes[i]);
+              if (swtScoutTabItem != null) {
+                m_tabs.remove(swtScoutTabItem.getTabItem());
+                swtScoutTabItem.getTabItem().dispose();
+                swtScoutTabItem.dispose();
+              }
+            }
           }
+          if (selectedGroupBox != null) {
+            SwtScoutTabItem swtScoutTabItem = m_scoutTabMapping.remove(selectedGroupBox);
+            if (swtScoutTabItem != null) {
+              m_tabs.remove(swtScoutTabItem.getTabItem());
+              swtScoutTabItem.getTabItem().dispose();
+              swtScoutTabItem.dispose();
+            }
+          }
+          m_scoutTabMapping = null;
+          m_tabs = null;
         }
       }
     });
@@ -87,12 +105,14 @@
     try {
       getSwtContainer().setRedraw(false);
       m_tabs = new HashMap<CTabItem, SwtScoutTabItem>();
+      m_scoutTabMapping = new HashMap<IGroupBox, SwtScoutTabItem>();
       for (IGroupBox box : getScoutObject().getGroupBoxes()) {
         if (box.isVisible()) {
           // XXX make tabitem exchangeable... extension point and provide a ITabItemInterface
           SwtScoutTabItem item = new SwtScoutTabItem();
           item.createField(getSwtField(), box, getEnvironment());
           m_tabs.put(item.getTabItem(), item);
+          m_scoutTabMapping.put(box, item);
         }
       }
       SwtScoutTabItem selectedTabItem = getTabItem(getScoutObject().getSelectedTab());
@@ -167,14 +187,11 @@
       m_selectedTabLock.acquire();
       //
       IGroupBox selectedTab = getScoutObject().getSelectedTab();
-      CTabItem foundItem = null;
-      for (Map.Entry<CTabItem, SwtScoutTabItem> e : m_tabs.entrySet()) {
-        IGroupBox test = e.getValue().getScoutObject();
-        if (test == selectedTab) {
-          foundItem = e.getKey();
-          break;
-        }
+      SwtScoutTabItem swtScoutTabItem = m_scoutTabMapping.get(selectedTab);
+      if (swtScoutTabItem == null) {
+        return;
       }
+      CTabItem foundItem = swtScoutTabItem.getTabItem();
       if (foundItem != null) {
         getSwtField().setSelection(foundItem);
       }
@@ -268,6 +285,7 @@
         item = new SwtScoutTabItem();
         item.createField(getSwtField(), groupBox, getEnvironment());
         m_tabs.put(item.getTabItem(), item);
+        m_scoutTabMapping.put(groupBox, item);
 
       }
     }
@@ -281,6 +299,7 @@
       getSwtContainer().setRedraw(false);
       SwtScoutTabItem item = getTabItem(groupBox);
       m_tabs.remove(item.getTabItem());
+      m_scoutTabMapping.remove(groupBox);
       if (item != null && item.isInitialized() && !item.isDisposed()) {
         item.getTabItem().dispose();
         item.dispose();
@@ -295,12 +314,7 @@
     if (groupBox == null) {
       return null;
     }
-    for (SwtScoutTabItem item : m_tabs.values()) {
-      if (item.getScoutObject() == groupBox) {
-        return item;
-      }
-    }
-    return null;
+    return m_scoutTabMapping.get(groupBox);
   }
 
 }