1 package org.simantics.browsing.ui.nattable;
\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
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
13 import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
\r
16 * ITreeRowModel that does not automatically expand all child nodes (as TreeRowModel does).
\r
18 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\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
27 private final Collection<ITreeRowModelListener> listeners = new HashSet<ITreeRowModelListener>();
\r
29 private final ITreeData<T> treeData;
\r
31 public GETreeRowModel(ITreeData<T> treeData) {
\r
32 this.treeData = treeData;
\r
35 public void registerRowGroupModelListener(ITreeRowModelListener listener) {
\r
36 this.listeners.add(listener);
\r
39 public void notifyListeners() {
\r
40 for (ITreeRowModelListener listener : this.listeners) {
\r
41 listener.treeRowModelChanged();
\r
45 public int depth(int index) {
\r
46 return this.treeData.getDepthOfData(this.treeData.getDataAtIndex(index));
\r
49 public boolean isLeaf(int index) {
\r
50 return !hasChildren(index);
\r
53 public String getObjectAtIndexAndDepth(int index, int depth) {
\r
54 return this.treeData.formatDataForDepth(depth,this.treeData.getDataAtIndex(index));
\r
57 public boolean hasChildren(int index) {
\r
58 return this.treeData.hasChildren(this.treeData.getDataAtIndex(index));
\r
61 public boolean isCollapsed(int index) {
\r
62 return this.parentIndexes.contains(index);
\r
65 public void clear() {
\r
66 this.parentIndexes.clear();
\r
70 public boolean isCollapsible(int index) {
\r
71 return hasChildren(index);
\r
75 public List<Integer> collapse(int index) {
\r
76 this.parentIndexes.add(index);
\r
78 return getChildIndexes(index);
\r
84 public List<Integer> expand(int index) {
\r
85 this.parentIndexes.remove(index);
\r
87 List<Integer> children = getExpandedChildIndexes(index);
\r
92 public List<Integer> collapseAll() {
\r
93 // TODO Auto-generated method stub
\r
99 public List<Integer> expandToLevel(int level) {
\r
100 // TODO Auto-generated method stub
\r
105 public List<Integer> expandToLevel(T object, int level) {
\r
106 // TODO Auto-generated method stub
\r
111 public List<Integer> expandAll() {
\r
112 // TODO Auto-generated method stub
\r
117 public List<Integer> expandToLevel(int parentIndex, int level) {
\r
118 // TODO Auto-generated method stub
\r
123 public List<T> getChildren(int parentIndex) {
\r
124 T t = treeData.getDataAtIndex(parentIndex);
\r
125 return treeData.getChildren(t,true);
\r
129 public List<T> getDirectChildren(int parentIndex) {
\r
130 return treeData.getChildren(parentIndex);
\r
135 public List<Integer> collapse(T object) {
\r
136 int index = treeData.indexOf(object);
\r
137 return collapse(index);
\r
141 public List<Integer> expand(T object) {
\r
142 int index = treeData.indexOf(object);
\r
143 return expand(index);
\r
147 public boolean isCollapsed(T object) {
\r
148 int index = treeData.indexOf(object);
\r
149 return isCollapsed(index);
\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
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
164 result.addAll(getChildIndexes(index));
\r
166 result.addAll(getChildIndexes(child));
\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
179 result.addAll(getChildIndexes(index));
\r
181 result.addAll(getChildIndexes(child));
\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
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
198 if (!parentIndexes.contains(index))
\r
199 result.addAll(getExpandedChildIndexes(index));
\r
201 result.addAll(getExpandedChildIndexes(child));
\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
214 if (!parentIndexes.contains(index))
\r
215 result.addAll(getExpandedChildIndexes(index));
\r
217 result.addAll(getExpandedChildIndexes(child));
\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
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
239 public ITreeData<T> getTreeData() {
\r