Bug 458537 - fixed endless loop on searching in an empty grid

Change-Id: I59d6892c7d970516ebc8c79d11bfb3059df23341
Signed-off-by: Dirk Fauth <dirk.fauth@googlemail.com>
diff --git a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/search/strategy/GridSearchStrategy.java b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/search/strategy/GridSearchStrategy.java
index a7eb32f..dc356ab 100644
--- a/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/search/strategy/GridSearchStrategy.java
+++ b/org.eclipse.nebula.widgets.nattable.core/src/org/eclipse/nebula/widgets/nattable/search/strategy/GridSearchStrategy.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012 Original authors and others.
+ * Copyright (c) 2012, 2015 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
@@ -7,6 +7,7 @@
  *
  * Contributors:
  *     Original authors and others - initial API and implementation
+ *     Dirk Fauth <dirk.fauth@googlemail.com> - Bug 458537
  ******************************************************************************/
 package org.eclipse.nebula.widgets.nattable.search.strategy;
 
@@ -28,8 +29,7 @@
 
     public GridSearchStrategy(IConfigRegistry configRegistry,
             boolean wrapSearch, boolean columnFirst) {
-        this(configRegistry, wrapSearch, ISearchDirection.SEARCH_FORWARD,
-                columnFirst);
+        this(configRegistry, wrapSearch, ISearchDirection.SEARCH_FORWARD, columnFirst);
     }
 
     public GridSearchStrategy(IConfigRegistry configRegistry,
@@ -46,18 +46,15 @@
     }
 
     @Override
-    public PositionCoordinate executeSearch(Object valueToMatch)
-            throws PatternSyntaxException {
+    public PositionCoordinate executeSearch(Object valueToMatch) throws PatternSyntaxException {
 
         ILayer contextLayer = getContextLayer();
         if (!(contextLayer instanceof SelectionLayer)) {
             throw new RuntimeException("For the GridSearchStrategy to work it needs the selectionLayer to be passed as the contextLayer."); //$NON-NLS-1$
         }
         SelectionLayer selectionLayer = (SelectionLayer) contextLayer;
-        PositionCoordinate selectionAnchor = selectionLayer
-                .getSelectionAnchor();
-        boolean hadSelectionAnchor = selectionAnchor.columnPosition >= 0
-                && selectionAnchor.rowPosition >= 0;
+        PositionCoordinate selectionAnchor = selectionLayer.getSelectionAnchor();
+        boolean hadSelectionAnchor = selectionAnchor.columnPosition >= 0 && selectionAnchor.rowPosition >= 0;
         if (!hadSelectionAnchor) {
             selectionAnchor.columnPosition = 0;
             selectionAnchor.rowPosition = 0;
@@ -82,8 +79,7 @@
         }
 
         // Pick start and end values depending on the direction of the search.
-        int direction = this.searchDirection.equals(ISearchDirection.SEARCH_FORWARD) ? 1
-                : -1;
+        int direction = this.searchDirection.equals(ISearchDirection.SEARCH_FORWARD) ? 1 : -1;
         int firstDimStart;
         int firstDimEnd;
         int secondDimStart;
@@ -102,8 +98,7 @@
 
         // Move to the next cell if a selection was active and it's not
         // an incremental search.
-        final boolean startWithNextCell = hadSelectionAnchor
-                && !isIncremental();
+        final boolean startWithNextCell = hadSelectionAnchor && !isIncremental();
         if (startWithNextCell) {
             if (secondDimPosition + direction != secondDimEnd) {
                 // Increment the second dimension
@@ -125,8 +120,9 @@
         }
 
         // Get a sequence of ranges for searching.
-        List<GridRectangle> gridRanges = getRanges(firstDimPosition,
-                secondDimPosition, direction, firstDimStart, firstDimEnd,
+        List<GridRectangle> gridRanges = getRanges(
+                firstDimPosition, secondDimPosition, direction,
+                firstDimStart, firstDimEnd,
                 secondDimStart, secondDimEnd);
 
         // Perform the search.
@@ -134,8 +130,7 @@
         Comparator<String> comparator2 = (Comparator<String>) getComparator();
         return CellDisplayValueSearchUtil.findCell(getContextLayer(),
                 this.configRegistry, gridRanges, valueToMatch, comparator2,
-                isCaseSensitive(), isWholeWord(), isRegex(), isColumnFirst(),
-                isIncludeCollapsed());
+                isCaseSensitive(), isWholeWord(), isRegex(), isColumnFirst(), isIncludeCollapsed());
     }
 
     /**
@@ -151,9 +146,9 @@
      * @param secondDimEnd
      * @return
      */
-    private List<GridRectangle> getRanges(int firstDimPosition,
-            int secondDimPosition, int direction, int firstDimStart,
-            int firstDimEnd, int secondDimStart, int secondDimEnd) {
+    private List<GridRectangle> getRanges(
+            int firstDimPosition, int secondDimPosition, int direction,
+            int firstDimStart, int firstDimEnd, int secondDimStart, int secondDimEnd) {
 
         List<GridRectangle> gridRanges = new ArrayList<GridRectangle>();
         GridRectangle gridRange;
@@ -161,22 +156,25 @@
         // One first-dimension slice starting at the second
         // dimension selection.
         gridRange = new GridRectangle();
-        gridRange.firstDim = new Range(firstDimPosition, firstDimPosition
-                + direction);
+        gridRange.firstDim = new Range(firstDimPosition, firstDimPosition + direction);
         gridRange.secondDim = new Range(secondDimPosition, secondDimEnd);
         gridRanges.add(gridRange);
 
+        if (firstDimStart == 0 && firstDimEnd == 0) {
+            // Bug 458537 - this might happen for a grid without content rows
+            return gridRanges;
+        }
+
         // One or more first-dimension slices to the wrapping boundary.
         gridRange = new GridRectangle();
-        gridRange.firstDim = new Range(firstDimPosition + direction,
-                firstDimEnd);
+        gridRange.firstDim = new Range(firstDimPosition + direction, firstDimEnd);
         gridRange.secondDim = new Range(secondDimStart, secondDimEnd);
         gridRanges.add(gridRange);
 
         // We're done if wrapping is not enabled or if we've already covered the
         // whole table.
-        if (!this.wrapSearch || firstDimPosition == firstDimStart
-                && secondDimPosition == secondDimStart) {
+        if (!this.wrapSearch
+                || firstDimPosition == firstDimStart && secondDimPosition == secondDimStart) {
             return gridRanges;
         }
 
@@ -189,8 +187,7 @@
 
         // One first-dimension slice ending at the second-dimension selection.
         gridRange = new GridRectangle();
-        gridRange.firstDim = new Range(firstDimPosition, firstDimPosition
-                + direction);
+        gridRange.firstDim = new Range(firstDimPosition, firstDimPosition + direction);
         gridRange.secondDim = new Range(secondDimStart, secondDimPosition);
         gridRanges.add(gridRange);