--- /dev/null
+package org.simantics.modeling.ui.view;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Arrays;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.List;\r
+\r
+import org.eclipse.nebula.widgets.nattable.sort.ISortModel;\r
+import org.eclipse.nebula.widgets.nattable.sort.SortDirectionEnum;\r
+import org.eclipse.nebula.widgets.nattable.sort.command.SortCommandHandler;\r
+import org.simantics.utils.strings.AlphanumComparator;\r
+\r
+public class BeanSortModel implements ISortModel {\r
+\r
+ /**\r
+ * Array that contains the sort direction for every column.\r
+ * Needed to access the current sort state of a column.\r
+ */\r
+ protected SortDirectionEnum[] sortDirections;\r
+ \r
+ /**\r
+ * Array that contains the sorted flags for every column.\r
+ * Needed to access the current sort state of a column.\r
+ */\r
+ protected boolean[] sorted;\r
+\r
+ /**\r
+ * As this implementation only supports single column sorting,\r
+ * this property contains the the column index of the column that\r
+ * is currently used for sorting.\r
+ * Initial value = -1 for no sort column\r
+ */\r
+ protected int currentSortColumn = -1;\r
+ \r
+ /**\r
+ * As this implementation only supports single column sorting,\r
+ * this property contains the current sort direction of the column that\r
+ * is currently used for sorting.\r
+ */\r
+ protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;\r
+\r
+ /**\r
+ * Data list that is sorted\r
+ */\r
+ private List<Bean> allBeans;\r
+ private List<Bean> beans;\r
+\r
+ private static int NUMBER_OF_COLUMNS = 7;\r
+ \r
+ public BeanSortModel(List<Bean> allBeans, List<Bean> beans) {\r
+ \r
+ this.allBeans = allBeans;\r
+ this.beans = beans;\r
+\r
+ sortDirections = new SortDirectionEnum[NUMBER_OF_COLUMNS];\r
+ Arrays.fill(sortDirections, SortDirectionEnum.NONE);\r
+\r
+ sorted = new boolean[NUMBER_OF_COLUMNS];\r
+ Arrays.fill(sorted, false);\r
+\r
+ //call initial sorting\r
+ sort(0, SortDirectionEnum.ASC, false);\r
+ \r
+ }\r
+ \r
+ /**\r
+ * As this is a simple implementation of an {@link ISortModel} and we don't\r
+ * support multiple column sorting, this list returns either a list with one\r
+ * entry for the current sort column or an empty list.\r
+ */\r
+ @Override\r
+ public List<Integer> getSortedColumnIndexes() {\r
+ List<Integer> indexes = new ArrayList<Integer>();\r
+ if (currentSortColumn > -1) {\r
+ indexes.add(Integer.valueOf(currentSortColumn));\r
+ }\r
+ return indexes;\r
+ }\r
+\r
+ /**\r
+ * @return TRUE if the column with the given index is sorted at the moment.\r
+ */\r
+ @Override\r
+ public boolean isColumnIndexSorted(int columnIndex) {\r
+ return sorted[columnIndex];\r
+ }\r
+\r
+ /**\r
+ * @return the direction in which the column with the given index is \r
+ * currently sorted\r
+ */\r
+ @Override\r
+ public SortDirectionEnum getSortDirection(int columnIndex) {\r
+ return sortDirections[columnIndex];\r
+ }\r
+\r
+ /**\r
+ * @return 0 as we don't support multiple column sorting.\r
+ */\r
+ @Override\r
+ public int getSortOrder(int columnIndex) {\r
+ return 0;\r
+ }\r
+\r
+ /**\r
+ * Remove all sorting\r
+ */\r
+ @Override\r
+ public void clear() {\r
+ Arrays.fill(sortDirections, SortDirectionEnum.NONE);\r
+ Arrays.fill(sorted, false);\r
+ this.currentSortColumn = -1;\r
+ }\r
+\r
+ /**\r
+ * This method is called by the {@link SortCommandHandler} in response to a sort command.\r
+ * It is responsible for sorting the requested column.\r
+ */\r
+ @Override\r
+ public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {\r
+ if (!isColumnIndexSorted(columnIndex)) {\r
+ clear();\r
+ }\r
+\r
+ if (sortDirection.equals(SortDirectionEnum.NONE)) {\r
+ //we don't support NONE as user action\r
+ sortDirection = SortDirectionEnum.ASC;\r
+ }\r
+\r
+ Collections.sort(allBeans, new BeanComparator(columnIndex, sortDirection));\r
+ Collections.sort(beans, new BeanComparator(columnIndex, sortDirection));\r
+ sortDirections[columnIndex] = sortDirection;\r
+ sorted[columnIndex] = sortDirection.equals(SortDirectionEnum.NONE) ? false : true;\r
+\r
+ currentSortColumn = columnIndex;\r
+ currentSortDirection = sortDirection;\r
+ }\r
+ \r
+ @Override\r
+ public Comparator<?> getColumnComparator(int columnIndex) {\r
+ // TODO : Hard-coded sort direction\r
+ return new BeanComparator(columnIndex, SortDirectionEnum.ASC);\r
+ }\r
+\r
+ class BeanComparator implements Comparator<Bean> {\r
+\r
+ int colIdx = 0;\r
+ SortDirectionEnum sortDirection;\r
+\r
+ public BeanComparator(int columnIndex, SortDirectionEnum sortDirection) {\r
+ this.colIdx = columnIndex;\r
+ this.sortDirection = sortDirection;\r
+ }\r
+\r
+ int sort(Bean bean1, Bean bean2) {\r
+\r
+ switch(colIdx) {\r
+ case 0: \r
+ return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getName(), bean2.getName()); \r
+ case 1: \r
+ return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getPath(), bean2.getPath()); \r
+ case 2: \r
+ return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getTypes(), bean2.getTypes()); \r
+ case 3:\r
+ return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getCreatedBy(), bean2.getCreatedBy()); \r
+ case 5:\r
+ return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getModifiedBy(), bean2.getModifiedBy()); \r
+ case 4:\r
+ return compareLong(bean1.createdAt, bean2.createdAt);\r
+ case 6:\r
+ return compareLong(bean1.modifiedAt, bean2.modifiedAt);\r
+ }\r
+\r
+ return 0;\r
+\r
+ }\r
+\r
+ private int compareLong(long x, long y) {\r
+ return (x < y) ? -1 : ((x == y) ? 0 : 1);\r
+ }\r
+\r
+ @Override\r
+ public int compare(Bean bean1, Bean bean2) {\r
+\r
+ if(SortDirectionEnum.ASC == sortDirection)\r
+ return sort(bean1, bean2);\r
+ else if(SortDirectionEnum.DESC == sortDirection) \r
+ return sort(bean2, bean1);\r
+ \r
+ return 0;\r
+ \r
+ }\r
+\r
+ }\r
+\r
+ /* (non-Javadoc)\r
+ * @see org.eclipse.nebula.widgets.nattable.sort.ISortModel#getComparatorsForColumnIndex(int)\r
+ */\r
+ @SuppressWarnings("rawtypes")\r
+ @Override\r
+ public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {\r
+ return null;\r
+ }\r
+ \r
+ public void sortCurrent() {\r
+ \r
+ if(currentSortColumn < 0) return;\r
+ \r
+ sort(currentSortColumn, currentSortDirection, false);\r
+ }\r
+ \r
+}\r