Add support to FancyFlexTable for finding/inserting items
The code to find an existing item in the table and to find the correct insert position for a new item is useful for different callers and should be in the table class. Change-Id: Ia0f9dbd0f9beb6b356989973bb4426aacd0671b3 Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
		@@ -290,28 +290,12 @@ public class AccountGroupMembersScreen extends AccountGroupScreen {
 | 
			
		||||
          return str == null ? "" : str;
 | 
			
		||||
        }
 | 
			
		||||
      };
 | 
			
		||||
      int insertPosition = table.getRowCount();
 | 
			
		||||
      int left = 1;
 | 
			
		||||
      int right = table.getRowCount() - 1;
 | 
			
		||||
      while (left <= right) {
 | 
			
		||||
        int middle = (left + right) >>> 1; // (left+right)/2
 | 
			
		||||
        AccountInfo i = getRowItem(middle);
 | 
			
		||||
        int cmp = c.compare(i, info);
 | 
			
		||||
 | 
			
		||||
        if (cmp < 0) {
 | 
			
		||||
          left = middle + 1;
 | 
			
		||||
        } else if (cmp > 0) {
 | 
			
		||||
          right = middle - 1;
 | 
			
		||||
        } else {
 | 
			
		||||
          // group is already contained in the table
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
      int insertPos = getInsertRow(c, info);
 | 
			
		||||
      if (insertPos >= 0) {
 | 
			
		||||
        table.insertRow(insertPos);
 | 
			
		||||
        applyDataRowStyle(insertPos);
 | 
			
		||||
        populate(insertPos, info);
 | 
			
		||||
      }
 | 
			
		||||
      insertPosition = left;
 | 
			
		||||
 | 
			
		||||
      table.insertRow(insertPosition);
 | 
			
		||||
      applyDataRowStyle(insertPosition);
 | 
			
		||||
      populate(insertPosition, info);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void populate(final int row, final AccountInfo i) {
 | 
			
		||||
@@ -405,29 +389,12 @@ public class AccountGroupMembersScreen extends AccountGroupScreen {
 | 
			
		||||
          return (str == null) ? "" : str;
 | 
			
		||||
        }
 | 
			
		||||
      };
 | 
			
		||||
 | 
			
		||||
      int insertPosition = table.getRowCount();
 | 
			
		||||
      int left = 1;
 | 
			
		||||
      int right = table.getRowCount() - 1;
 | 
			
		||||
      while (left <= right) {
 | 
			
		||||
        int middle = (left + right) >>> 1; // (left+right)/2
 | 
			
		||||
        GroupInfo i = getRowItem(middle);
 | 
			
		||||
        int cmp = c.compare(i, info);
 | 
			
		||||
 | 
			
		||||
        if (cmp < 0) {
 | 
			
		||||
          left = middle + 1;
 | 
			
		||||
        } else if (cmp > 0) {
 | 
			
		||||
          right = middle - 1;
 | 
			
		||||
        } else {
 | 
			
		||||
          // group is already contained in the table
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
      int insertPos = getInsertRow(c, info);
 | 
			
		||||
      if (insertPos >= 0) {
 | 
			
		||||
        table.insertRow(insertPos);
 | 
			
		||||
        applyDataRowStyle(insertPos);
 | 
			
		||||
        populate(insertPos, info);
 | 
			
		||||
      }
 | 
			
		||||
      insertPosition = left;
 | 
			
		||||
 | 
			
		||||
      table.insertRow(insertPosition);
 | 
			
		||||
      applyDataRowStyle(insertPosition);
 | 
			
		||||
      populate(insertPosition, info);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void populate(final int row, final GroupInfo i) {
 | 
			
		||||
 
 | 
			
		||||
@@ -25,6 +25,7 @@ import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
 | 
			
		||||
import com.google.gwt.user.client.ui.Widget;
 | 
			
		||||
import com.google.gwtexpui.safehtml.client.SafeHtml;
 | 
			
		||||
 | 
			
		||||
import java.util.Comparator;
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
 | 
			
		||||
public abstract class FancyFlexTable<RowItem> extends Composite {
 | 
			
		||||
@@ -58,6 +59,71 @@ public abstract class FancyFlexTable<RowItem> extends Composite {
 | 
			
		||||
    setRowItem(table.getCellFormatter().getElement(row, 0), item);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Finds an item in the table.
 | 
			
		||||
   *
 | 
			
		||||
   * @param comparator comparator by which the items in the table are sorted
 | 
			
		||||
   * @param item the item that should be found
 | 
			
		||||
   * @return if the item is found the number of the row that contains the item;
 | 
			
		||||
   *         if the item is not found <code>-1</code>
 | 
			
		||||
   */
 | 
			
		||||
  protected int findRowItem(Comparator<RowItem> comparator, RowItem item) {
 | 
			
		||||
    int row = lookupRowItem(comparator, item);
 | 
			
		||||
    if (row < table.getRowCount()
 | 
			
		||||
        && comparator.compare(item, getRowItem(row)) == 0) {
 | 
			
		||||
      return row;
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Finds the number of the row where a new item should be inserted into the
 | 
			
		||||
   * table.
 | 
			
		||||
   *
 | 
			
		||||
   * @param comparator comparator by which the items in the table are sorted
 | 
			
		||||
   * @param item the new item that should be inserted
 | 
			
		||||
   * @return if the item is not yet contained in the table, the number of the
 | 
			
		||||
   *         row where the new item should be inserted; if the item is already
 | 
			
		||||
   *         contained in the table <code>-1</code>
 | 
			
		||||
   */
 | 
			
		||||
  protected int getInsertRow(Comparator<RowItem> comparator, RowItem item) {
 | 
			
		||||
    int row = lookupRowItem(comparator, item);
 | 
			
		||||
    if (row >= table.getRowCount()
 | 
			
		||||
        || comparator.compare(item, getRowItem(row)) != 0) {
 | 
			
		||||
      return row;
 | 
			
		||||
    }
 | 
			
		||||
    return -1;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Makes a binary search for the given row item over the table.
 | 
			
		||||
   *
 | 
			
		||||
   * @param comparator comparator by which the items in the table are sorted
 | 
			
		||||
   * @param item the item that should be looked up
 | 
			
		||||
   * @return if the item is found the number of the row that contains the item;
 | 
			
		||||
   *         if the item is not found the number of the row where the item
 | 
			
		||||
   *         should be inserted according to the given comparator.
 | 
			
		||||
   */
 | 
			
		||||
  private int lookupRowItem(Comparator<RowItem> comparator, RowItem item) {
 | 
			
		||||
    int left = 1;
 | 
			
		||||
    int right = table.getRowCount() - 1;
 | 
			
		||||
    while (left <= right) {
 | 
			
		||||
      int middle = (left + right) >>> 1; // (left+right)/2
 | 
			
		||||
      RowItem i = getRowItem(middle);
 | 
			
		||||
      int cmp = comparator.compare(i, item);
 | 
			
		||||
 | 
			
		||||
      if (cmp < 0) {
 | 
			
		||||
        left = middle + 1;
 | 
			
		||||
      } else if (cmp > 0) {
 | 
			
		||||
        right = middle - 1;
 | 
			
		||||
      } else {
 | 
			
		||||
        // item is already contained in the table
 | 
			
		||||
        return middle;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return left;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  protected void resetHtml(final SafeHtml body) {
 | 
			
		||||
    for (final Iterator<Widget> i = table.iterator(); i.hasNext();) {
 | 
			
		||||
      i.next();
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user