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