Bug 501373 - ColumnHideShowLayer never show all columns again after
persistence

Change-Id: If18bfaac0c2c786d905c7b4873f4f789f734f4ab
Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
diff --git a/org.eclipse.nebula.widgets.nattable.core.test/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayerPersistenceTest.java b/org.eclipse.nebula.widgets.nattable.core.test/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayerPersistenceTest.java
index a39efc2..bf7fa18 100644
--- a/org.eclipse.nebula.widgets.nattable.core.test/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayerPersistenceTest.java
+++ b/org.eclipse.nebula.widgets.nattable.core.test/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayerPersistenceTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2013 Original authors and others.
+ * Copyright (c) 2012, 2016 Original authors and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -10,12 +10,13 @@
  ******************************************************************************/
 package org.eclipse.nebula.widgets.nattable.hideshow;
 
+import static org.junit.Assert.assertEquals;
+
 import java.util.Arrays;
 import java.util.Properties;
 
 import org.eclipse.nebula.widgets.nattable.grid.data.DummyBodyDataProvider;
 import org.eclipse.nebula.widgets.nattable.layer.DataLayer;
-import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -48,28 +49,50 @@
         Properties properties = new Properties();
         this.layer.saveState("prefix", properties);
 
-        Assert.assertEquals(1, properties.size());
-        Assert.assertEquals("3,5,6,", properties.getProperty("prefix"
-                + ColumnHideShowLayer.PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES));
+        assertEquals(1, properties.size());
+        assertEquals("3,5,6,", properties.getProperty("prefix" + ColumnHideShowLayer.PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES));
     }
 
     @Test
     public void testLoadState() {
         Properties properties = new Properties();
-        properties.setProperty("prefix"
-                + ColumnHideShowLayer.PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES,
-                "1,3,5,");
+        properties.setProperty("prefix" + ColumnHideShowLayer.PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES, "1,3,5,");
 
         this.layer.loadState("prefix", properties);
 
-        Assert.assertEquals(7, this.layer.getColumnCount());
+        assertEquals(7, this.layer.getColumnCount());
 
-        Assert.assertEquals(0, this.layer.getColumnIndexByPosition(0));
-        Assert.assertEquals(2, this.layer.getColumnIndexByPosition(1));
-        Assert.assertEquals(4, this.layer.getColumnIndexByPosition(2));
-        Assert.assertEquals(6, this.layer.getColumnIndexByPosition(3));
-        Assert.assertEquals(7, this.layer.getColumnIndexByPosition(4));
-        Assert.assertEquals(8, this.layer.getColumnIndexByPosition(5));
-        Assert.assertEquals(9, this.layer.getColumnIndexByPosition(6));
+        assertEquals(0, this.layer.getColumnIndexByPosition(0));
+        assertEquals(2, this.layer.getColumnIndexByPosition(1));
+        assertEquals(4, this.layer.getColumnIndexByPosition(2));
+        assertEquals(6, this.layer.getColumnIndexByPosition(3));
+        assertEquals(7, this.layer.getColumnIndexByPosition(4));
+        assertEquals(8, this.layer.getColumnIndexByPosition(5));
+        assertEquals(9, this.layer.getColumnIndexByPosition(6));
+    }
+
+    @Test
+    public void testSaveResetSaveLoad() {
+        // first hide a column and save
+        this.layer.hideColumnPositions(Arrays.asList(new Integer[] { 1 }));
+
+        assertEquals(1, this.layer.getHiddenColumnIndexes().size());
+
+        Properties properties = new Properties();
+        this.layer.saveState("prefix", properties);
+
+        assertEquals(1, properties.size());
+        assertEquals("1,", properties.getProperty("prefix" + ColumnHideShowLayer.PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES));
+
+        this.layer.showColumnIndexes(Arrays.asList(new Integer[] { 1 }));
+
+        assertEquals(0, this.layer.getHiddenColumnIndexes().size());
+        this.layer.saveState("prefix", properties);
+
+        assertEquals(0, properties.size());
+
+        this.layer.loadState("prefix", properties);
+
+        assertEquals(0, this.layer.getHiddenColumnIndexes().size());
     }
 }
diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayer.java b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayer.java
index 5d8c065..a42a603 100644
--- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayer.java
+++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/hideshow/ColumnHideShowLayer.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2013 Original authors and others.
+ * Copyright (c) 2012, 2016 Original authors and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -52,8 +52,7 @@
         if (event instanceof IStructuralChangeEvent) {
             IStructuralChangeEvent structuralChangeEvent = (IStructuralChangeEvent) event;
             if (structuralChangeEvent.isHorizontalStructureChanged()) {
-                Collection<StructuralDiff> columnDiffs = structuralChangeEvent
-                        .getColumnDiffs();
+                Collection<StructuralDiff> columnDiffs = structuralChangeEvent.getColumnDiffs();
 
                 if (columnDiffs != null && !columnDiffs.isEmpty()
                         && !StructuralChangeEventHelper.isReorder(columnDiffs)) {
@@ -77,9 +76,11 @@
                 strBuilder.append(index);
                 strBuilder.append(IPersistable.VALUE_SEPARATOR);
             }
-            properties.setProperty(prefix
-                    + PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES,
+            properties.setProperty(
+                    prefix + PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES,
                     strBuilder.toString());
+        } else {
+            properties.remove(prefix + PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES);
         }
 
         super.saveState(prefix, properties);
@@ -90,11 +91,9 @@
         // Bug 396925: always clear the state of the hidden columns, whether
         // there is a state saved or not
         this.hiddenColumnIndexes.clear();
-        String property = properties.getProperty(prefix
-                + PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES);
+        String property = properties.getProperty(prefix + PERSISTENCE_KEY_HIDDEN_COLUMN_INDEXES);
         if (property != null) {
-            StringTokenizer tok = new StringTokenizer(property,
-                    IPersistable.VALUE_SEPARATOR);
+            StringTokenizer tok = new StringTokenizer(property, IPersistable.VALUE_SEPARATOR);
             while (tok.hasMoreTokens()) {
                 String index = tok.nextToken();
                 this.hiddenColumnIndexes.add(Integer.valueOf(index));
@@ -119,10 +118,7 @@
     public void hideColumnPositions(Collection<Integer> columnPositions) {
         Set<Integer> columnIndexes = new HashSet<Integer>();
         for (Integer columnPosition : columnPositions) {
-            columnIndexes
-                    .add(Integer
-                            .valueOf(getColumnIndexByPosition(columnPosition
-                                    .intValue())));
+            columnIndexes.add(getColumnIndexByPosition(columnPosition));
         }
         this.hiddenColumnIndexes.addAll(columnIndexes);
         invalidateCache();
@@ -132,13 +128,11 @@
     public void showColumnIndexes(Collection<Integer> columnIndexes) {
         this.hiddenColumnIndexes.removeAll(columnIndexes);
         invalidateCache();
-        fireLayerEvent(new ShowColumnPositionsEvent(this,
-                getColumnPositionsByIndexes(columnIndexes)));
+        fireLayerEvent(new ShowColumnPositionsEvent(this, getColumnPositionsByIndexes(columnIndexes)));
     }
 
     public void showAllColumns() {
-        Collection<Integer> hiddenColumns = new ArrayList<Integer>(
-                this.hiddenColumnIndexes);
+        Collection<Integer> hiddenColumns = new ArrayList<Integer>(this.hiddenColumnIndexes);
         this.hiddenColumnIndexes.clear();
         invalidateCache();
         fireLayerEvent(new ShowColumnPositionsEvent(this, hiddenColumns));