use of org.netxms.ui.eclipse.datacollection.widgets.internal.SummaryTableItemComparator in project netxms by netxms.
the class SummaryTableWidget method update.
/**
* Update viewer with fresh table data
*
* @param table table
*/
public void update(final Table table) {
if (!viewer.isInitialized()) {
final String[] names = table.getColumnDisplayNames();
final int[] widths = new int[names.length];
Arrays.fill(widths, 100);
viewer.createColumns(names, widths, 0, SWT.UP);
final IDialogSettings settings = Activator.getDefault().getDialogSettings();
// $NON-NLS-1$
final String key = viewPart.getViewSite().getId() + ".SummaryTable." + Integer.toString(tableId);
WidgetHelper.restoreTreeViewerSettings(viewer, settings, key);
// $NON-NLS-1$
String value = settings.get(key + ".useMultipliers");
if (value != null)
useMultipliers = Boolean.parseBoolean(value);
labelProvider.setUseMultipliers(useMultipliers);
viewer.getControl().addDisposeListener(new DisposeListener() {
@Override
public void widgetDisposed(DisposeEvent e) {
WidgetHelper.saveTreeViewerSettings(viewer, settings, key);
// $NON-NLS-1$
settings.put(key + ".useMultipliers", useMultipliers);
}
});
viewer.setComparator(new SummaryTableItemComparator(table));
}
labelProvider.setColumnDataTypes(table.getColumnDataTypes());
if (sortingColumnList != null && sortingColumnList.size() > 0) {
List<SortItem> sortItem = new ArrayList<SortItem>();
for (int i = 0; i < sortingColumnList.size(); i++) {
boolean isDesc = sortingColumnList.get(i).charAt(0) == '>' ? true : false;
int index = table.getColumnIndex(sortingColumnList.get(i).substring(1));
if (index >= 0) {
sortItem.add(new SortItem(index, isDesc));
}
}
// find index of columns to compare and desc or asc
final List<SortItem> sortItemFin = sortItem;
table.sort(new Comparator<TableRow>() {
public int compare(TableRow row1, TableRow row2) {
// compare lines
int result = 0;
int i = 0;
while (result == 0 && i < sortItemFin.size()) {
result = compareItem(row1, row2, sortItemFin.get(i).colIndex, sortItemFin.get(i).isDesc);
i++;
}
return result;
}
private int compareItem(TableRow row1, TableRow row2, int index, boolean sortDesc) {
TableCell c1 = row1.get(index);
TableCell c2 = row2.get(index);
String s1 = (c1 != null) ? c1.getValue() : "";
String s2 = (c2 != null) ? c2.getValue() : "";
int result = 0;
try {
double value1 = Double.parseDouble(s1);
double value2 = Double.parseDouble(s2);
result = Double.compare(value1, value2);
} catch (NumberFormatException e) {
result = s1.compareToIgnoreCase(s2);
}
return sortDesc ? -result : result;
}
});
viewer.setComparator(null);
}
if (showLineCount > 0)
viewer.setInput(table.getFirstRows(showLineCount));
else
viewer.setInput(table);
viewer.expandAll();
}
Aggregations