/* * 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; import com.google.common.collect.Sets; import org.onosproject.ui.table.cell.DefaultCellComparator; import org.onosproject.ui.table.cell.DefaultCellFormatter; 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; import java.util.Set; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkNotNull; /** * A simple model of table data. *
* 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. *
* 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. *
* Note that it is expected that all values for a particular column will
* be the same class.
*/
public class TableModel {
private static final CellComparator DEF_CMP = DefaultCellComparator.INSTANCE;
private static final CellFormatter DEF_FMT = DefaultCellFormatter.INSTANCE;
private final String[] columnIds;
private final Set
* Implementation note: we are knowingly passing you a reference to
* our internal array to avoid copying. Don't mess with it. It's your
* table you'll break if you do!
*
* @return the column identifiers
*/
public String[] getColumnIds() {
return columnIds;
}
/**
* Returns the raw {@link Row} representation of the rows in this table.
*
* @return raw table rows
*/
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
* @param formatter formatter to use
*/
public void setFormatter(String columnId, CellFormatter formatter) {
checkNotNull(formatter, "must provide a formatter");
checkId(columnId);
formatters.put(columnId, formatter);
}
/**
* Returns the cell formatter to use on values in the specified column.
*
* @param columnId column identifier
* @return an appropriate cell formatter
*/
public CellFormatter getFormatter(String columnId) {
checkId(columnId);
CellFormatter fmt = formatters.get(columnId);
return fmt == null ? DEF_FMT : fmt;
}
/**
* Adds a row to the table model.
*
* @return the row, for chaining
*/
public Row addRow() {
Row r = new Row();
rows.add(r);
return r;
}
/**
* 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
* 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;
}
}