GUI -- Implemented table sorting in our simple table/row/cell model
- Introduced CellComparator (with default implementation)
Change-Id: I125f52c2c1ca219746b0e506e8837e24fb149038
diff --git a/core/api/src/main/java/org/onosproject/ui/table/CellComparator.java b/core/api/src/main/java/org/onosproject/ui/table/CellComparator.java
new file mode 100644
index 0000000..84d1134
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/CellComparator.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.table;
+
+/**
+ * Defines a comparator for cell values.
+ */
+public interface CellComparator {
+
+ /**
+ * Compares its two arguments for order. Returns a negative integer,
+ * zero, or a positive integer as the first argument is less than, equal
+ * to, or greater than the second.<p>
+ *
+ * Note that nulls are permitted, and should be sorted to the beginning
+ * of an ascending sort; i.e. null is considered to be "smaller" than
+ * non-null values.
+ *
+ * @see java.util.Comparator#compare(Object, Object)
+ *
+ * @param o1 the first object to be compared.
+ * @param o2 the second object to be compared.
+ * @return a negative integer, zero, or a positive integer as the
+ * first argument is less than, equal to, or greater than the
+ * second.
+ * @throws ClassCastException if the arguments' types prevent them from
+ * being compared by this comparator.
+ */
+ int compare(Object o1, Object o2);
+
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/DefaultCellComparator.java b/core/api/src/main/java/org/onosproject/ui/table/DefaultCellComparator.java
new file mode 100644
index 0000000..c27be61
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/DefaultCellComparator.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.table;
+
+/**
+ * A default cell comparator. Implements a lexicographical compare function
+ * (i.e. string sorting). Uses the objects' toString() method and then
+ * compares the resulting strings. Note that null values are acceptable and
+ * are considered "smaller" than any non-null value.
+ */
+public class DefaultCellComparator implements CellComparator {
+ @Override
+ public int compare(Object o1, Object o2) {
+ if (o1 == null && o2 == null) {
+ return 0; // o1 == o2
+ }
+ if (o1 == null) {
+ return -1; // o1 < o2
+ }
+ if (o2 == null) {
+ return 1; // o1 > o2
+ }
+ return o1.toString().compareTo(o2.toString());
+ }
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/TableModel.java b/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
index 3ff78d9..7fb336e 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
@@ -20,6 +20,8 @@
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -29,14 +31,25 @@
import static com.google.common.base.Preconditions.checkNotNull;
/**
- * A model of table data.
+ * A simple model of table data.
+ * <p>
+ * Note that this is not a full MVC type model; the expected usage pattern
+ * is to create an empty table, add rows (by consulting the business model),
+ * sort rows (based on client request parameters), and finally produce the
+ * sorted list of rows.
+ * <p>
+ * The table also provides a mechanism for defining how cell values for a
+ * particular column should be formatted into strings, to help facilitate
+ * the encoding of the table data into a JSON structure.
*/
public class TableModel {
+ private static final CellComparator DEF_CMP = new DefaultCellComparator();
private static final CellFormatter DEF_FMT = new DefaultCellFormatter();
private final String[] columnIds;
private final Set<String> idSet;
+ private final Map<String, CellComparator> comparators = new HashMap<>();
private final Map<String, CellFormatter> formatters = new HashMap<>();
private final List<Row> rows = new ArrayList<>();
@@ -88,6 +101,7 @@
*
* @return formatted table rows
*/
+ // TODO: still need to decide if we need this
public TableRow[] getTableRows() {
return new TableRow[0];
}
@@ -97,11 +111,36 @@
*
* @return raw table rows
*/
+ // TODO: still need to decide if we should expose this
public Row[] getRows() {
return rows.toArray(new Row[rows.size()]);
}
/**
+ * Sets a cell comparator for the specified column.
+ *
+ * @param columnId column identifier
+ * @param comparator comparator to use
+ */
+ public void setComparator(String columnId, CellComparator comparator) {
+ checkNotNull(comparator, "must provide a comparator");
+ checkId(columnId);
+ comparators.put(columnId, comparator);
+ }
+
+ /**
+ * Returns the cell comparator to use on values in the specified column.
+ *
+ * @param columnId column identifier
+ * @return an appropriate cell comparator
+ */
+ private CellComparator getComparator(String columnId) {
+ checkId(columnId);
+ CellComparator cmp = comparators.get(columnId);
+ return cmp == null ? DEF_CMP : cmp;
+ }
+
+ /**
* Sets a cell formatter for the specified column.
*
* @param columnId column identifier
@@ -137,6 +176,56 @@
}
/**
+ * Sorts the table rows based on the specified column, in the
+ * specified direction.
+ *
+ * @param columnId column identifier
+ * @param dir sort direction
+ */
+ public void sort(String columnId, SortDir dir) {
+ Collections.sort(rows, new RowComparator(columnId, dir));
+ }
+
+
+ /** Designates sorting direction. */
+ public enum SortDir {
+ /** Designates an ascending sort. */
+ ASC,
+ /** Designates a descending sort. */
+ DESC
+ }
+
+ /**
+ * Row comparator.
+ */
+ private class RowComparator implements Comparator<Row> {
+ private final String columnId;
+ private final SortDir dir;
+ private final CellComparator cellComparator;
+
+ /**
+ * Constructs a row comparator based on the specified
+ * column identifier and sort direction.
+ *
+ * @param columnId column identifier
+ * @param dir sort direction
+ */
+ public RowComparator(String columnId, SortDir dir) {
+ this.columnId = columnId;
+ this.dir = dir;
+ cellComparator = getComparator(columnId);
+ }
+
+ @Override
+ public int compare(Row a, Row b) {
+ Object cellA = a.get(columnId);
+ Object cellB = b.get(columnId);
+ int result = cellComparator.compare(cellA, cellB);
+ return dir == SortDir.ASC ? result : -result;
+ }
+ }
+
+ /**
* Model of a row.
*/
public class Row {
@@ -166,4 +255,20 @@
return cells.get(columnId);
}
}
+
+ private static final String DESC = "desc";
+
+ /**
+ * Returns the appropriate sort direction for the given string.
+ * <p>
+ * The expected strings are "asc" for {@link SortDir#ASC ascending} and
+ * "desc" for {@link SortDir#DESC descending}. Any other value will
+ * default to ascending.
+ *
+ * @param s sort direction string encoding
+ * @return sort direction
+ */
+ public static SortDir sortDir(String s) {
+ return !DESC.equals(s) ? SortDir.ASC : SortDir.DESC;
+ }
}