1 package org.simantics.modeling.ui.view;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.Comparator;
9 import org.eclipse.nebula.widgets.nattable.sort.ISortModel;
10 import org.eclipse.nebula.widgets.nattable.sort.SortDirectionEnum;
11 import org.eclipse.nebula.widgets.nattable.sort.command.SortCommandHandler;
12 import org.simantics.utils.strings.AlphanumComparator;
14 public class BeanSortModel implements ISortModel {
17 * Array that contains the sort direction for every column.
18 * Needed to access the current sort state of a column.
20 protected SortDirectionEnum[] sortDirections;
23 * Array that contains the sorted flags for every column.
24 * Needed to access the current sort state of a column.
26 protected boolean[] sorted;
29 * As this implementation only supports single column sorting,
30 * this property contains the the column index of the column that
31 * is currently used for sorting.
32 * Initial value = -1 for no sort column
34 protected int currentSortColumn = -1;
37 * As this implementation only supports single column sorting,
38 * this property contains the current sort direction of the column that
39 * is currently used for sorting.
41 protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;
44 * Data list that is sorted
46 private List<Bean> allBeans;
47 private List<Bean> beans;
49 private static int NUMBER_OF_COLUMNS = 7;
51 public BeanSortModel(List<Bean> allBeans, List<Bean> beans) {
53 this.allBeans = allBeans;
56 sortDirections = new SortDirectionEnum[NUMBER_OF_COLUMNS];
57 Arrays.fill(sortDirections, SortDirectionEnum.NONE);
59 sorted = new boolean[NUMBER_OF_COLUMNS];
60 Arrays.fill(sorted, false);
62 //call initial sorting
63 sort(0, SortDirectionEnum.ASC, false);
68 * As this is a simple implementation of an {@link ISortModel} and we don't
69 * support multiple column sorting, this list returns either a list with one
70 * entry for the current sort column or an empty list.
73 public List<Integer> getSortedColumnIndexes() {
74 List<Integer> indexes = new ArrayList<Integer>();
75 if (currentSortColumn > -1) {
76 indexes.add(Integer.valueOf(currentSortColumn));
82 * @return TRUE if the column with the given index is sorted at the moment.
85 public boolean isColumnIndexSorted(int columnIndex) {
86 return sorted[columnIndex];
90 * @return the direction in which the column with the given index is
94 public SortDirectionEnum getSortDirection(int columnIndex) {
95 return sortDirections[columnIndex];
99 * @return 0 as we don't support multiple column sorting.
102 public int getSortOrder(int columnIndex) {
110 public void clear() {
111 Arrays.fill(sortDirections, SortDirectionEnum.NONE);
112 Arrays.fill(sorted, false);
113 this.currentSortColumn = -1;
117 * This method is called by the {@link SortCommandHandler} in response to a sort command.
118 * It is responsible for sorting the requested column.
121 public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {
122 if (!isColumnIndexSorted(columnIndex)) {
126 if (sortDirection.equals(SortDirectionEnum.NONE)) {
127 //we don't support NONE as user action
128 sortDirection = SortDirectionEnum.ASC;
131 Collections.sort(allBeans, new BeanComparator(columnIndex, sortDirection));
132 Collections.sort(beans, new BeanComparator(columnIndex, sortDirection));
133 sortDirections[columnIndex] = sortDirection;
134 sorted[columnIndex] = sortDirection.equals(SortDirectionEnum.NONE) ? false : true;
136 currentSortColumn = columnIndex;
137 currentSortDirection = sortDirection;
141 public Comparator<?> getColumnComparator(int columnIndex) {
142 // TODO : Hard-coded sort direction
143 return new BeanComparator(columnIndex, SortDirectionEnum.ASC);
146 class BeanComparator implements Comparator<Bean> {
149 SortDirectionEnum sortDirection;
151 public BeanComparator(int columnIndex, SortDirectionEnum sortDirection) {
152 this.colIdx = columnIndex;
153 this.sortDirection = sortDirection;
156 int sort(Bean bean1, Bean bean2) {
160 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getName(), bean2.getName());
162 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getPath(), bean2.getPath());
164 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getTypes(), bean2.getTypes());
166 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getCreatedBy(), bean2.getCreatedBy());
168 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getModifiedBy(), bean2.getModifiedBy());
170 return compareLong(bean1.createdAt, bean2.createdAt);
172 return compareLong(bean1.modifiedAt, bean2.modifiedAt);
179 private int compareLong(long x, long y) {
180 return (x < y) ? -1 : ((x == y) ? 0 : 1);
184 public int compare(Bean bean1, Bean bean2) {
186 if(SortDirectionEnum.ASC == sortDirection)
187 return sort(bean1, bean2);
188 else if(SortDirectionEnum.DESC == sortDirection)
189 return sort(bean2, bean1);
198 * @see org.eclipse.nebula.widgets.nattable.sort.ISortModel#getComparatorsForColumnIndex(int)
200 @SuppressWarnings("rawtypes")
202 public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
206 public void sortCurrent() {
208 if(currentSortColumn < 0) return;
210 sort(currentSortColumn, currentSortDirection, false);