Merge "Add support to FancyFlexTable for finding/inserting items"

This commit is contained in:
Shawn Pearce
2013-06-14 15:48:01 +00:00
committed by Gerrit Code Review
2 changed files with 76 additions and 43 deletions

View File

@@ -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) {

View File

@@ -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();