Sync git svn branch with SVN repository r33334.
[simantics/platform.git] / bundles / org.simantics.browsing.ui.nattable / src / org / simantics / browsing / ui / nattable / GETreeRowModel.java
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 expandedIndexes = 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.expandedIndexes.contains(index);\r
63         }\r
64 \r
65         public void clear() {\r
66                 this.expandedIndexes.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.expandedIndexes.remove(index);\r
77                 notifyListeners();\r
78                 List<Integer> list = getChildIndexes(index);\r
79                 //this.parentIndexes.addAll(list);\r
80                 return list;\r
81         }\r
82         \r
83         \r
84 \r
85         @Override\r
86         public List<Integer> expand(int index) {\r
87                 this.expandedIndexes.add(index);\r
88                 notifyListeners();\r
89                 List<Integer> children = getExpandedChildIndexes(index);\r
90                 return children;\r
91         }\r
92         \r
93         @Override\r
94         public List<Integer> collapseAll() {\r
95                 return null;\r
96         }\r
97         \r
98         \r
99         @Override\r
100         public List<Integer> expandToLevel(int level) {\r
101                 // TODO Auto-generated method stub\r
102                 return null;\r
103         }\r
104         \r
105         @Override\r
106         public List<Integer> expandToLevel(T object, int level) {\r
107                 // TODO Auto-generated method stub\r
108                 return null;\r
109         }\r
110         \r
111         @Override\r
112         public List<Integer> expandAll() {\r
113                 // TODO Auto-generated method stub\r
114                 return null;\r
115         }\r
116         \r
117         @Override\r
118         public List<Integer> expandToLevel(int parentIndex, int level) {\r
119                 // TODO Auto-generated method stub\r
120                 return null;\r
121         }\r
122         \r
123         @Override\r
124         public List<T> getChildren(int parentIndex) {\r
125                 T t = treeData.getDataAtIndex(parentIndex);\r
126                 return treeData.getChildren(t,true);\r
127         }\r
128         \r
129         @Override\r
130         public List<T> getDirectChildren(int parentIndex) {\r
131                 return treeData.getChildren(parentIndex);\r
132         }\r
133         \r
134         \r
135         @Override\r
136         public List<Integer> collapse(T object) {\r
137                 int index = treeData.indexOf(object);\r
138                 return collapse(index);\r
139         }\r
140         \r
141         @Override\r
142         public List<Integer> expand(T object) {\r
143                 int index = treeData.indexOf(object);\r
144                 return expand(index);\r
145         }\r
146         \r
147         @Override\r
148         public boolean isCollapsed(T object) {\r
149                 int index = treeData.indexOf(object);\r
150                 return isCollapsed(index);\r
151         }\r
152         \r
153 \r
154         @SuppressWarnings("unchecked")\r
155         public List<Integer> getChildIndexes(int parentIndex) {\r
156                 List<Integer> result = new ArrayList<Integer>();\r
157                 T t = this.treeData.getDataAtIndex(parentIndex);\r
158                 if (t == null)\r
159                         return Collections.EMPTY_LIST;\r
160                 List<T> children = this.treeData.getChildren(t);\r
161                 for (T child : children) {\r
162                         int index = this.treeData.indexOf(child);\r
163                         if (index >= 0) {\r
164                                 result.add(index);\r
165                                 result.addAll(getChildIndexes(index));\r
166                         } else {\r
167                                 result.addAll(getChildIndexes(child));\r
168                         }\r
169                 }\r
170                 return result;\r
171         }\r
172         \r
173         public List<Integer> getChildIndexes(T t) {\r
174                 List<Integer> result = new ArrayList<Integer>();\r
175                 List<T> children = this.treeData.getChildren(t);\r
176                 for (T child : children) {\r
177                         int index = this.treeData.indexOf(child);\r
178                         if (index >= 0) {\r
179                                 result.add(index);\r
180                                 result.addAll(getChildIndexes(index));\r
181                         } else {\r
182                                 result.addAll(getChildIndexes(child));\r
183                         }\r
184                 }\r
185                 return result;\r
186         }\r
187         \r
188         @SuppressWarnings("unchecked")\r
189         public List<Integer> getExpandedChildIndexes(int parentIndex) {\r
190                 List<Integer> result = new ArrayList<Integer>();\r
191                 T t = this.treeData.getDataAtIndex(parentIndex);\r
192                 if (t == null)\r
193                         return Collections.EMPTY_LIST;\r
194                 List<T> children = this.treeData.getChildren(t);\r
195                 for (T child : children) {\r
196                         int index = this.treeData.indexOf(child);\r
197                         if (index >= 0) {\r
198                                 result.add(index);\r
199                                 if (expandedIndexes.contains(index))\r
200                                         result.addAll(getExpandedChildIndexes(index));\r
201                         } else {\r
202                                 result.addAll(getExpandedChildIndexes(child));\r
203                         }\r
204                 }\r
205                 return result;\r
206         }\r
207         \r
208         public List<Integer> getExpandedChildIndexes(T t) {\r
209                 List<Integer> result = new ArrayList<Integer>();\r
210                 List<T> children = this.treeData.getChildren(t);\r
211                 for (T child : children) {\r
212                         int index = this.treeData.indexOf(child);\r
213                         if (index >= 0) {\r
214                                 result.add(index);\r
215                                 if (expandedIndexes.contains(index))\r
216                                         result.addAll(getExpandedChildIndexes(index));\r
217                         } else {\r
218                                 result.addAll(getExpandedChildIndexes(child));\r
219                         }\r
220                 }\r
221                 return result;\r
222         }\r
223         \r
224         @Override\r
225         public List<Integer> getDirectChildIndexes(int parentIndex) {\r
226                 List<Integer> result = new ArrayList<Integer>();\r
227                 T t = this.treeData.getDataAtIndex(parentIndex);\r
228                 if (t == null)\r
229                         return Collections.EMPTY_LIST;\r
230                 List<T> children = this.treeData.getChildren(t);\r
231                 for (T child : children) {\r
232                         int index = this.treeData.indexOf(child);\r
233                         if (index >= 0) {\r
234                                 result.add(index);\r
235                         }\r
236                 }\r
237                 return result;\r
238         }\r
239         \r
240         public ITreeData<T> getTreeData() {\r
241                 return treeData;\r
242         }\r
243 }\r