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