]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/view/BeanSortModel.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / view / BeanSortModel.java
1 package org.simantics.modeling.ui.view;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.List;
8
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;
13
14 public class BeanSortModel implements ISortModel {
15
16         /**
17          * Array that contains the sort direction for every column.
18          * Needed to access the current sort state of a column.
19          */
20         protected SortDirectionEnum[] sortDirections;
21         
22         /**
23          * Array that contains the sorted flags for every column.
24          * Needed to access the current sort state of a column.
25          */
26         protected boolean[] sorted;
27
28         /**
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
33          */
34         protected int currentSortColumn = -1;
35         
36         /**
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.
40          */
41         protected SortDirectionEnum currentSortDirection = SortDirectionEnum.ASC;
42
43         /**
44          * Data list that is sorted
45          */
46         private List<Bean> allBeans;
47         private List<Bean> beans;
48
49         private static int NUMBER_OF_COLUMNS = 7;
50         
51         public BeanSortModel(List<Bean> allBeans, List<Bean> beans) {
52                 
53                 this.allBeans = allBeans;
54                 this.beans = beans;
55
56                 sortDirections = new SortDirectionEnum[NUMBER_OF_COLUMNS];
57                 Arrays.fill(sortDirections, SortDirectionEnum.NONE);
58
59                 sorted = new boolean[NUMBER_OF_COLUMNS];
60                 Arrays.fill(sorted, false);
61
62                 //call initial sorting
63                 sort(0, SortDirectionEnum.ASC, false);
64                 
65         }
66         
67         /**
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.
71          */
72         @Override
73         public List<Integer> getSortedColumnIndexes() {
74                 List<Integer> indexes = new ArrayList<Integer>();
75                 if (currentSortColumn > -1) {
76                         indexes.add(Integer.valueOf(currentSortColumn));
77                 }
78                 return indexes;
79         }
80
81         /**
82          * @return TRUE if the column with the given index is sorted at the moment.
83          */
84         @Override
85         public boolean isColumnIndexSorted(int columnIndex) {
86                 return sorted[columnIndex];
87         }
88
89         /**
90          * @return the direction in which the column with the given index is 
91          * currently sorted
92          */
93         @Override
94         public SortDirectionEnum getSortDirection(int columnIndex) {
95                 return sortDirections[columnIndex];
96         }
97
98         /**
99          * @return 0 as we don't support multiple column sorting.
100          */
101         @Override
102         public int getSortOrder(int columnIndex) {
103                 return 0;
104         }
105
106         /**
107          * Remove all sorting
108          */
109         @Override
110         public void clear() {
111                 Arrays.fill(sortDirections, SortDirectionEnum.NONE);
112                 Arrays.fill(sorted, false);
113                 this.currentSortColumn = -1;
114         }
115
116         /**
117          * This method is called by the {@link SortCommandHandler} in response to a sort command.
118          * It is responsible for sorting the requested column.
119          */
120         @Override
121         public void sort(int columnIndex, SortDirectionEnum sortDirection, boolean accumulate) {
122                 if (!isColumnIndexSorted(columnIndex)) {
123                         clear();
124                 }
125
126                 if (sortDirection.equals(SortDirectionEnum.NONE)) {
127                         //we don't support NONE as user action
128                         sortDirection = SortDirectionEnum.ASC;
129                 }
130
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;
135
136                 currentSortColumn = columnIndex;
137                 currentSortDirection = sortDirection;
138         }
139         
140         @Override
141         public Comparator<?> getColumnComparator(int columnIndex) {
142                 // TODO : Hard-coded sort direction
143                 return new BeanComparator(columnIndex, SortDirectionEnum.ASC);
144         }
145
146         class BeanComparator implements Comparator<Bean> {
147
148                 int colIdx = 0;
149                 SortDirectionEnum sortDirection;
150
151                 public BeanComparator(int columnIndex, SortDirectionEnum sortDirection) {
152                         this.colIdx = columnIndex;
153                         this.sortDirection = sortDirection;
154                 }
155
156                 int sort(Bean bean1, Bean bean2) {
157
158                         switch(colIdx) {
159                         case 0: 
160                                 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getName(), bean2.getName());                        
161                         case 1: 
162                                 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getPath(), bean2.getPath());                        
163                         case 2: 
164                                 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getTypes(), bean2.getTypes());                      
165                         case 3:
166                                 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getCreatedBy(), bean2.getCreatedBy());                      
167                         case 5:
168                                 return AlphanumComparator.CASE_INSENSITIVE_COMPARATOR.compare(bean1.getModifiedBy(), bean2.getModifiedBy());                    
169                         case 4:
170                                 return compareLong(bean1.createdAt, bean2.createdAt);
171                         case 6:
172                                 return compareLong(bean1.modifiedAt, bean2.modifiedAt);
173                         }
174
175                         return 0;
176
177                 }
178
179                 private int compareLong(long x, long y) {
180                         return (x < y) ? -1 : ((x == y) ? 0 : 1);
181                 }
182
183                 @Override
184                 public int compare(Bean bean1, Bean bean2) {
185
186                         if(SortDirectionEnum.ASC == sortDirection)
187                                 return sort(bean1, bean2);
188                         else if(SortDirectionEnum.DESC == sortDirection) 
189                                 return sort(bean2, bean1);
190                         
191                         return 0;
192                 
193                 }
194
195         }
196
197         /* (non-Javadoc)
198          * @see org.eclipse.nebula.widgets.nattable.sort.ISortModel#getComparatorsForColumnIndex(int)
199          */
200         @SuppressWarnings("rawtypes")
201         @Override
202         public List<Comparator> getComparatorsForColumnIndex(int columnIndex) {
203                 return null;
204         }
205         
206         public void sortCurrent() {
207                 
208                 if(currentSortColumn < 0) return;
209                 
210                 sort(currentSortColumn, currentSortDirection, false);
211         }
212         
213 }