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