fixing up StringDict(TableRow) and Table.trim() now removes extra rows and columns

This commit is contained in:
Ben Fry
2017-01-31 04:55:03 -05:00
parent ea65d7f034
commit c90176cf8a
3 changed files with 62 additions and 8 deletions

View File

@@ -110,9 +110,12 @@ public class StringDict {
/**
* Create a dictionary that maps between column titles and cell entries
* in a TableRow.
* in a TableRow. If two columns have the same name, the later column's
* values will override the earlier values.
*/
public StringDict(TableRow row) {
this(row.getColumnCount());
String[] titles = row.getColumnTitles();
if (titles == null) {
titles = new StringList(IntList.fromRange(row.getColumnCount())).array();
@@ -120,6 +123,8 @@ public class StringDict {
for (int col = 0; col < row.getColumnCount(); col++) {
set(titles[col], row.getString(col));
}
// remove unused and overwritten entries
crop();
}

View File

@@ -4059,8 +4059,58 @@ public class Table {
for (int col = 0; col < getColumnCount(); col++) {
trim(col);
}
// remove empty columns
int lastColumn = getColumnCount() - 1;
//while (isEmptyColumn(lastColumn) && lastColumn >= 0) {
while (isEmptyArray(getStringColumn(lastColumn)) && lastColumn >= 0) {
lastColumn--;
}
setColumnCount(lastColumn + 1);
// remove empty rows (starting from the end)
int lastRow = lastRowIndex();
//while (isEmptyRow(lastRow) && lastRow >= 0) {
while (isEmptyArray(getStringRow(lastRow)) && lastRow >= 0) {
lastRow--;
}
setRowCount(lastRow + 1);
}
protected boolean isEmptyArray(String[] contents) {
for (String entry : contents) {
if (entry != null && entry.length() > 0) {
return false;
}
}
return true;
}
/*
protected boolean isEmptyColumn(int column) {
String[] contents = getStringColumn(column);
for (String entry : contents) {
if (entry != null && entry.length() > 0) {
return false;
}
}
return true;
}
protected boolean isEmptyRow(int row) {
String[] contents = getStringRow(row);
for (String entry : contents) {
if (entry != null && entry.length() > 0) {
return false;
}
}
return true;
}
*/
/**
* @param column ID number of the column to trim
*/