]> gerrit.simantics Code Review - simantics/platform.git/blob
ebfb962db7a129cf7af9cdc93e7f64f433d2585c
[simantics/platform.git] /
1 package org.simantics.browsing.ui.nattable;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 import java.util.HashSet;\r
7 import java.util.List;\r
8 \r
9 import org.eclipse.nebula.widgets.nattable.tree.ITreeData;\r
10 import org.eclipse.nebula.widgets.nattable.tree.ITreeRowModel;\r
11 import org.eclipse.nebula.widgets.nattable.tree.ITreeRowModelListener;\r
12 \r
13 import it.unimi.dsi.fastutil.ints.IntOpenHashSet;\r
14 \r
15 /**\r
16  * ITreeRowModel that does not automatically expand all child nodes (as TreeRowModel does). \r
17  * \r
18  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
19  *\r
20  * @param <T>\r
21  */\r
22 public class GETreeRowModel<T> implements ITreeRowModel<T>{\r
23         //private final HashSet<Integer> parentIndexes = new HashSet<Integer>();\r
24         //private final TIntHashSet parentIndexes = new TIntHashSet(1000, 0.8f);\r
25         private final IntOpenHashSet parentIndexes = new IntOpenHashSet();\r
26 \r
27         private final Collection<ITreeRowModelListener> listeners = new HashSet<ITreeRowModelListener>();\r
28 \r
29         private final ITreeData<T> treeData;\r
30 \r
31         public GETreeRowModel(ITreeData<T> treeData) {\r
32                 this.treeData = treeData;\r
33         }\r
34 \r
35         public void registerRowGroupModelListener(ITreeRowModelListener listener) {\r
36                 this.listeners.add(listener);\r
37         }\r
38 \r
39         public void notifyListeners() {\r
40                 for (ITreeRowModelListener listener : this.listeners) {\r
41                         listener.treeRowModelChanged();\r
42                 }\r
43         }\r
44 \r
45         public int depth(int index) {\r
46                 return this.treeData.getDepthOfData(this.treeData.getDataAtIndex(index));\r
47         }\r
48 \r
49         public boolean isLeaf(int index) {\r
50                 return !hasChildren(index);\r
51         }\r
52 \r
53         public String getObjectAtIndexAndDepth(int index, int depth) {\r
54                 return this.treeData.formatDataForDepth(depth,this.treeData.getDataAtIndex(index));\r
55         }\r
56 \r
57         public boolean hasChildren(int index) {\r
58                 return this.treeData.hasChildren(this.treeData.getDataAtIndex(index));\r
59         }\r
60         \r
61         public boolean isCollapsed(int index) {\r
62                 return this.parentIndexes.contains(index);\r
63         }\r
64 \r
65         public void clear() {\r
66                 this.parentIndexes.clear();\r
67         }\r
68         \r
69         @Override\r
70         public boolean isCollapsible(int index) {\r
71                 return hasChildren(index);\r
72         }\r
73 \r
74         @Override\r
75         public List<Integer> collapse(int index) {\r
76                 this.parentIndexes.add(index);\r
77                 notifyListeners();\r
78                 return getChildIndexes(index);\r
79         }\r
80         \r
81         \r
82 \r
83         @Override\r
84         public List<Integer> expand(int index) {\r
85                 this.parentIndexes.remove(index);\r
86                 notifyListeners();\r
87                 List<Integer> children = getExpandedChildIndexes(index);\r
88                 return children;\r
89         }\r
90         \r
91         @Override\r
92         public List<Integer> collapseAll() {\r
93                 // TODO Auto-generated method stub\r
94                 return null;\r
95         }\r
96         \r
97         \r
98         @Override\r
99         public List<Integer> expandToLevel(int level) {\r
100                 // TODO Auto-generated method stub\r
101                 return null;\r
102         }\r
103         \r
104         @Override\r
105         public List<Integer> expandToLevel(T object, int level) {\r
106                 // TODO Auto-generated method stub\r
107                 return null;\r
108         }\r
109         \r
110         @Override\r
111         public List<Integer> expandAll() {\r
112                 // TODO Auto-generated method stub\r
113                 return null;\r
114         }\r
115         \r
116         @Override\r
117         public List<Integer> expandToLevel(int parentIndex, int level) {\r
118                 // TODO Auto-generated method stub\r
119                 return null;\r
120         }\r
121         \r
122         @Override\r
123         public List<T> getChildren(int parentIndex) {\r
124                 T t = treeData.getDataAtIndex(parentIndex);\r
125                 return treeData.getChildren(t,true);\r
126         }\r
127         \r
128         @Override\r
129         public List<T> getDirectChildren(int parentIndex) {\r
130                 return treeData.getChildren(parentIndex);\r
131         }\r
132         \r
133         \r
134         @Override\r
135         public List<Integer> collapse(T object) {\r
136                 int index = treeData.indexOf(object);\r
137                 return collapse(index);\r
138         }\r
139         \r
140         @Override\r
141         public List<Integer> expand(T object) {\r
142                 int index = treeData.indexOf(object);\r
143                 return expand(index);\r
144         }\r
145         \r
146         @Override\r
147         public boolean isCollapsed(T object) {\r
148                 int index = treeData.indexOf(object);\r
149                 return isCollapsed(index);\r
150         }\r
151         \r
152 \r
153         @SuppressWarnings("unchecked")\r
154         public List<Integer> getChildIndexes(int parentIndex) {\r
155                 List<Integer> result = new ArrayList<Integer>();\r
156                 T t = this.treeData.getDataAtIndex(parentIndex);\r
157                 if (t == null)\r
158                         return Collections.EMPTY_LIST;\r
159                 List<T> children = this.treeData.getChildren(t);\r
160                 for (T child : children) {\r
161                         int index = this.treeData.indexOf(child);\r
162                         if (index >= 0) {\r
163                                 result.add(index);\r
164                                 result.addAll(getChildIndexes(index));\r
165                         } else {\r
166                                 result.addAll(getChildIndexes(child));\r
167                         }\r
168                 }\r
169                 return result;\r
170         }\r
171         \r
172         public List<Integer> getChildIndexes(T t) {\r
173                 List<Integer> result = new ArrayList<Integer>();\r
174                 List<T> children = this.treeData.getChildren(t);\r
175                 for (T child : children) {\r
176                         int index = this.treeData.indexOf(child);\r
177                         if (index >= 0) {\r
178                                 result.add(index);\r
179                                 result.addAll(getChildIndexes(index));\r
180                         } else {\r
181                                 result.addAll(getChildIndexes(child));\r
182                         }\r
183                 }\r
184                 return result;\r
185         }\r
186         \r
187         @SuppressWarnings("unchecked")\r
188         public List<Integer> getExpandedChildIndexes(int parentIndex) {\r
189                 List<Integer> result = new ArrayList<Integer>();\r
190                 T t = this.treeData.getDataAtIndex(parentIndex);\r
191                 if (t == null)\r
192                         return Collections.EMPTY_LIST;\r
193                 List<T> children = this.treeData.getChildren(t);\r
194                 for (T child : children) {\r
195                         int index = this.treeData.indexOf(child);\r
196                         if (index >= 0) {\r
197                                 result.add(index);\r
198                                 if (!parentIndexes.contains(index))\r
199                                         result.addAll(getExpandedChildIndexes(index));\r
200                         } else {\r
201                                 result.addAll(getExpandedChildIndexes(child));\r
202                         }\r
203                 }\r
204                 return result;\r
205         }\r
206         \r
207         public List<Integer> getExpandedChildIndexes(T t) {\r
208                 List<Integer> result = new ArrayList<Integer>();\r
209                 List<T> children = this.treeData.getChildren(t);\r
210                 for (T child : children) {\r
211                         int index = this.treeData.indexOf(child);\r
212                         if (index >= 0) {\r
213                                 result.add(index);\r
214                                 if (!parentIndexes.contains(index))\r
215                                         result.addAll(getExpandedChildIndexes(index));\r
216                         } else {\r
217                                 result.addAll(getExpandedChildIndexes(child));\r
218                         }\r
219                 }\r
220                 return result;\r
221         }\r
222         \r
223         @Override\r
224         public List<Integer> getDirectChildIndexes(int parentIndex) {\r
225                 List<Integer> result = new ArrayList<Integer>();\r
226                 T t = this.treeData.getDataAtIndex(parentIndex);\r
227                 if (t == null)\r
228                         return Collections.EMPTY_LIST;\r
229                 List<T> children = this.treeData.getChildren(t);\r
230                 for (T child : children) {\r
231                         int index = this.treeData.indexOf(child);\r
232                         if (index >= 0) {\r
233                                 result.add(index);\r
234                         }\r
235                 }\r
236                 return result;\r
237         }\r
238         \r
239         public ITreeData<T> getTreeData() {\r
240                 return treeData;\r
241         }\r
242 }\r