X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.browsing.ui.nattable%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fnattable%2FGETreeRowModel.java;fp=bundles%2Forg.simantics.browsing.ui.nattable%2Fsrc%2Forg%2Fsimantics%2Fbrowsing%2Fui%2Fnattable%2FGETreeRowModel.java;h=ebfb962db7a129cf7af9cdc93e7f64f433d2585c;hp=0000000000000000000000000000000000000000;hb=96bb7ef9cbe42d82eb58306d8f9b62392cc29ba8;hpb=ae5bb63c5c88f6569518fed2a24df86fbd0570ff diff --git a/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GETreeRowModel.java b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GETreeRowModel.java new file mode 100644 index 000000000..ebfb962db --- /dev/null +++ b/bundles/org.simantics.browsing.ui.nattable/src/org/simantics/browsing/ui/nattable/GETreeRowModel.java @@ -0,0 +1,242 @@ +package org.simantics.browsing.ui.nattable; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.List; + +import org.eclipse.nebula.widgets.nattable.tree.ITreeData; +import org.eclipse.nebula.widgets.nattable.tree.ITreeRowModel; +import org.eclipse.nebula.widgets.nattable.tree.ITreeRowModelListener; + +import it.unimi.dsi.fastutil.ints.IntOpenHashSet; + +/** + * ITreeRowModel that does not automatically expand all child nodes (as TreeRowModel does). + * + * @author Marko Luukkainen + * + * @param + */ +public class GETreeRowModel implements ITreeRowModel{ + //private final HashSet parentIndexes = new HashSet(); + //private final TIntHashSet parentIndexes = new TIntHashSet(1000, 0.8f); + private final IntOpenHashSet parentIndexes = new IntOpenHashSet(); + + private final Collection listeners = new HashSet(); + + private final ITreeData treeData; + + public GETreeRowModel(ITreeData treeData) { + this.treeData = treeData; + } + + public void registerRowGroupModelListener(ITreeRowModelListener listener) { + this.listeners.add(listener); + } + + public void notifyListeners() { + for (ITreeRowModelListener listener : this.listeners) { + listener.treeRowModelChanged(); + } + } + + public int depth(int index) { + return this.treeData.getDepthOfData(this.treeData.getDataAtIndex(index)); + } + + public boolean isLeaf(int index) { + return !hasChildren(index); + } + + public String getObjectAtIndexAndDepth(int index, int depth) { + return this.treeData.formatDataForDepth(depth,this.treeData.getDataAtIndex(index)); + } + + public boolean hasChildren(int index) { + return this.treeData.hasChildren(this.treeData.getDataAtIndex(index)); + } + + public boolean isCollapsed(int index) { + return this.parentIndexes.contains(index); + } + + public void clear() { + this.parentIndexes.clear(); + } + + @Override + public boolean isCollapsible(int index) { + return hasChildren(index); + } + + @Override + public List collapse(int index) { + this.parentIndexes.add(index); + notifyListeners(); + return getChildIndexes(index); + } + + + + @Override + public List expand(int index) { + this.parentIndexes.remove(index); + notifyListeners(); + List children = getExpandedChildIndexes(index); + return children; + } + + @Override + public List collapseAll() { + // TODO Auto-generated method stub + return null; + } + + + @Override + public List expandToLevel(int level) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List expandToLevel(T object, int level) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List expandAll() { + // TODO Auto-generated method stub + return null; + } + + @Override + public List expandToLevel(int parentIndex, int level) { + // TODO Auto-generated method stub + return null; + } + + @Override + public List getChildren(int parentIndex) { + T t = treeData.getDataAtIndex(parentIndex); + return treeData.getChildren(t,true); + } + + @Override + public List getDirectChildren(int parentIndex) { + return treeData.getChildren(parentIndex); + } + + + @Override + public List collapse(T object) { + int index = treeData.indexOf(object); + return collapse(index); + } + + @Override + public List expand(T object) { + int index = treeData.indexOf(object); + return expand(index); + } + + @Override + public boolean isCollapsed(T object) { + int index = treeData.indexOf(object); + return isCollapsed(index); + } + + + @SuppressWarnings("unchecked") + public List getChildIndexes(int parentIndex) { + List result = new ArrayList(); + T t = this.treeData.getDataAtIndex(parentIndex); + if (t == null) + return Collections.EMPTY_LIST; + List children = this.treeData.getChildren(t); + for (T child : children) { + int index = this.treeData.indexOf(child); + if (index >= 0) { + result.add(index); + result.addAll(getChildIndexes(index)); + } else { + result.addAll(getChildIndexes(child)); + } + } + return result; + } + + public List getChildIndexes(T t) { + List result = new ArrayList(); + List children = this.treeData.getChildren(t); + for (T child : children) { + int index = this.treeData.indexOf(child); + if (index >= 0) { + result.add(index); + result.addAll(getChildIndexes(index)); + } else { + result.addAll(getChildIndexes(child)); + } + } + return result; + } + + @SuppressWarnings("unchecked") + public List getExpandedChildIndexes(int parentIndex) { + List result = new ArrayList(); + T t = this.treeData.getDataAtIndex(parentIndex); + if (t == null) + return Collections.EMPTY_LIST; + List children = this.treeData.getChildren(t); + for (T child : children) { + int index = this.treeData.indexOf(child); + if (index >= 0) { + result.add(index); + if (!parentIndexes.contains(index)) + result.addAll(getExpandedChildIndexes(index)); + } else { + result.addAll(getExpandedChildIndexes(child)); + } + } + return result; + } + + public List getExpandedChildIndexes(T t) { + List result = new ArrayList(); + List children = this.treeData.getChildren(t); + for (T child : children) { + int index = this.treeData.indexOf(child); + if (index >= 0) { + result.add(index); + if (!parentIndexes.contains(index)) + result.addAll(getExpandedChildIndexes(index)); + } else { + result.addAll(getExpandedChildIndexes(child)); + } + } + return result; + } + + @Override + public List getDirectChildIndexes(int parentIndex) { + List result = new ArrayList(); + T t = this.treeData.getDataAtIndex(parentIndex); + if (t == null) + return Collections.EMPTY_LIST; + List children = this.treeData.getChildren(t); + for (T child : children) { + int index = this.treeData.indexOf(child); + if (index >= 0) { + result.add(index); + } + } + return result; + } + + public ITreeData getTreeData() { + return treeData; + } +}