1 package org.simantics.browsing.ui.nattable;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.HashSet;
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;
13 import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
16 * ITreeRowModel that does not automatically expand all child nodes (as TreeRowModel does).
18 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
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();
27 private final Collection<ITreeRowModelListener> listeners = new HashSet<ITreeRowModelListener>();
29 private final ITreeData<T> treeData;
31 public GETreeRowModel(ITreeData<T> treeData) {
32 this.treeData = treeData;
35 public void registerRowGroupModelListener(ITreeRowModelListener listener) {
36 this.listeners.add(listener);
39 public void notifyListeners() {
40 for (ITreeRowModelListener listener : this.listeners) {
41 listener.treeRowModelChanged();
45 public int depth(int index) {
46 return this.treeData.getDepthOfData(this.treeData.getDataAtIndex(index));
49 public boolean isLeaf(int index) {
50 return !hasChildren(index);
53 public String getObjectAtIndexAndDepth(int index, int depth) {
54 return this.treeData.formatDataForDepth(depth,this.treeData.getDataAtIndex(index));
57 public boolean hasChildren(int index) {
58 return this.treeData.hasChildren(this.treeData.getDataAtIndex(index));
61 public boolean isCollapsed(int index) {
62 return !this.expandedIndexes.contains(index);
66 this.expandedIndexes.clear();
70 public boolean isCollapsible(int index) {
71 return hasChildren(index);
75 public List<Integer> collapse(int index) {
76 this.expandedIndexes.remove(index);
78 List<Integer> list = getChildIndexes(index);
79 //this.parentIndexes.addAll(list);
86 public List<Integer> expand(int index) {
87 this.expandedIndexes.add(index);
89 List<Integer> children = getExpandedChildIndexes(index);
94 public List<Integer> collapseAll() {
100 public List<Integer> expandToLevel(int level) {
101 // TODO Auto-generated method stub
106 public List<Integer> expandToLevel(T object, int level) {
107 // TODO Auto-generated method stub
112 public List<Integer> expandAll() {
113 // TODO Auto-generated method stub
118 public List<Integer> expandToLevel(int parentIndex, int level) {
119 // TODO Auto-generated method stub
124 public List<T> getChildren(int parentIndex) {
125 T t = treeData.getDataAtIndex(parentIndex);
126 return treeData.getChildren(t,true);
130 public List<T> getDirectChildren(int parentIndex) {
131 return treeData.getChildren(parentIndex);
136 public List<Integer> collapse(T object) {
137 int index = treeData.indexOf(object);
138 return collapse(index);
142 public List<Integer> expand(T object) {
143 int index = treeData.indexOf(object);
144 return expand(index);
148 public boolean isCollapsed(T object) {
149 int index = treeData.indexOf(object);
150 return isCollapsed(index);
154 @SuppressWarnings("unchecked")
155 public List<Integer> getChildIndexes(int parentIndex) {
156 List<Integer> result = new ArrayList<Integer>();
157 T t = this.treeData.getDataAtIndex(parentIndex);
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);
165 result.addAll(getChildIndexes(index));
167 result.addAll(getChildIndexes(child));
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);
180 result.addAll(getChildIndexes(index));
182 result.addAll(getChildIndexes(child));
188 @SuppressWarnings("unchecked")
189 public List<Integer> getExpandedChildIndexes(int parentIndex) {
190 List<Integer> result = new ArrayList<Integer>();
191 T t = this.treeData.getDataAtIndex(parentIndex);
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);
199 if (expandedIndexes.contains(index))
200 result.addAll(getExpandedChildIndexes(index));
202 result.addAll(getExpandedChildIndexes(child));
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);
215 if (expandedIndexes.contains(index))
216 result.addAll(getExpandedChildIndexes(index));
218 result.addAll(getExpandedChildIndexes(child));
225 public List<Integer> getDirectChildIndexes(int parentIndex) {
226 List<Integer> result = new ArrayList<Integer>();
227 T t = this.treeData.getDataAtIndex(parentIndex);
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);
240 public ITreeData<T> getTreeData() {