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