From fc4d459c27ff80e8b6da3acf8d8ea7fbacce9b3b Mon Sep 17 00:00:00 2001 From: Edwin Kempin Date: Tue, 11 Jun 2013 11:17:45 +0200 Subject: [PATCH] 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 --- .../admin/AccountGroupMembersScreen.java | 53 +++------------ .../gerrit/client/ui/FancyFlexTable.java | 66 +++++++++++++++++++ 2 files changed, 76 insertions(+), 43 deletions(-) diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccountGroupMembersScreen.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccountGroupMembersScreen.java index 51ff978fa4..cc87d50dbf 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccountGroupMembersScreen.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/admin/AccountGroupMembersScreen.java @@ -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) { diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/FancyFlexTable.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/FancyFlexTable.java index 1edb8fde10..079352720e 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/FancyFlexTable.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/ui/FancyFlexTable.java @@ -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 extends Composite { @@ -58,6 +59,71 @@ public abstract class FancyFlexTable 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 -1 + */ + protected int findRowItem(Comparator 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 -1 + */ + protected int getInsertRow(Comparator 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 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 i = table.iterator(); i.hasNext();) { i.next();