X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Frequests%2FCollectionResult.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Frequests%2FCollectionResult.java;h=844524ac167a6e5097a9e07bb86d54cc4b3636bd;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionResult.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionResult.java new file mode 100644 index 000000000..844524ac1 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/requests/CollectionResult.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.modeling.requests; + +import java.util.Collection; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Vector; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListSet; + +import org.eclipse.jface.viewers.IFilter; +import org.simantics.db.Resource; +import org.simantics.scl.runtime.function.Function1; + +/** + * @author Tuukka Lehtonen + */ +public class CollectionResult { + + public class DiagramFilter implements IFilter { + + private final IFilter proxy; + + public DiagramFilter(IFilter proxy) { + this.proxy = proxy; + } + + @Override + public boolean select(Object node) { + return diagramSet.contains(node) && (proxy == null || proxy.select(node)); + } + + } + + final public Set roots = new ConcurrentSkipListSet(); + final private Set diagramSet = new ConcurrentSkipListSet(); + final public List diagramList = new Vector(); + final public Map diagrams = new ConcurrentHashMap(); + + public void addDiagram(Resource r, Node n) { + diagramList.add(n); + diagrams.put(r, n); + diagramSet.add(n); + } + + public Collection breadthFirstFlatten() { + return breadthFirstFlatten(null); + } + + public Collection breadthFirstFlatten(IFilter filter) { + return Nodes.breadthFirstFlatten(new DiagramFilter(filter), roots); + } + + public Collection depthFirstFlatten() { + return depthFirstFlatten(null, null); + } + + public Collection depthFirstFlatten(IFilter filter, Comparator comparator) { + return Nodes.depthFirstFlatten(new DiagramFilter(filter), roots, comparator); + } + + /** + * @param f + * function that takes the walked Node as argument and returns a + * boolean to describe whether to continue the walk or cancel the + * walk. The returned value cannot be null. + * @return true if the walk was completed or false + * if the walk was cancelled + */ + public boolean walkTree(Function1 f) { + return Nodes.walkTree(f, roots); + } + +} \ No newline at end of file