import java.util.Random;
import java.util.Set;
import java.util.UUID;
+import java.util.function.Function;
import java.util.stream.Collectors;
import javax.xml.transform.OutputKeys;
import org.simantics.datatypes.literal.GUID;
import org.simantics.db.ReadGraph;
import org.simantics.db.Resource;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.ResourceRead;
import org.simantics.db.common.request.UnaryRead;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.exception.RuntimeDatabaseException;
+import org.simantics.db.layer0.variable.Variables;
+import org.simantics.diagram.elements.DecorationSVGNode;
import org.simantics.diagram.elements.DiagramNodeUtil;
import org.simantics.diagram.elements.TextGridNode;
import org.simantics.diagram.elements.TextNode;
import org.simantics.scenegraph.g2d.nodes.BoundsNode;
import org.simantics.scenegraph.g2d.nodes.ConnectionNode;
import org.simantics.scenegraph.g2d.nodes.DataNode;
-import org.simantics.scenegraph.g2d.nodes.DecorationSVGNode;
import org.simantics.scenegraph.g2d.nodes.NavigationNode;
import org.simantics.scenegraph.g2d.nodes.SVGNode;
import org.simantics.scenegraph.g2d.nodes.SelectionNode;
import org.simantics.scenegraph.g2d.nodes.spatial.RTreeNode;
import org.simantics.scenegraph.utils.NodeUtil;
import org.simantics.scl.runtime.function.Function1;
+import org.simantics.scl.runtime.tuple.Tuple2;
import org.simantics.trend.impl.ItemNode;
import org.simantics.utils.threads.ThreadUtils;
import org.slf4j.Logger;
private static final String[] ALL_SECTIONS = { MAIN_SECTION, SELECTION_SECTION, SELECTION_MASK_SECTION };
-
+ @Deprecated
public static ICanvasSceneGraphProvider getICanvasSceneGraphProvider(Resource model, Resource diagram, String diagramRVI) throws DatabaseException, InterruptedException {
ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider(model, diagram, diagramRVI);
return provider;
}
+ @Deprecated
public static void disposeSceneGraphProvider(ICanvasSceneGraphProvider provider) {
provider.dispose();
}
-
+
+ public static <T> T doWithICanvasSceneGraphProvider(Resource diagram, Function1<ICanvasSceneGraphProvider, T> func) throws DatabaseException {
+ return doWithICanvasSceneGraphProvider(diagram, (Function<ICanvasSceneGraphProvider, T>) provider -> func.apply(provider));
+ }
+
+ public static <T> T doWithICanvasSceneGraphProvider(Resource diagram, Function<ICanvasSceneGraphProvider, T> func) throws DatabaseException {
+ Tuple2 result = Simantics.getSession().syncRequest(new ResourceRead<Tuple2>(diagram) {
+
+ @Override
+ public Tuple2 perform(ReadGraph graph) throws DatabaseException {
+ Resource indexRoot = graph.syncRequest(new IndexRoot(resource));
+ String diagramRVI = Variables.getRVI(graph, resource);
+ return new Tuple2(indexRoot, diagramRVI);
+ }
+ });
+ ICanvasSceneGraphProvider provider = DiagramNodeUtil.loadSceneGraphProvider((Resource) result.c0, diagram, (String) result.c1);
+ try {
+ return func.apply(provider);
+ } finally {
+ provider.dispose();
+ }
+ }
+
+ public static <T> T doWithCanvasContext(Resource diagram, Function1<ICanvasContext, T> func) throws DatabaseException {
+ return doWithCanvasContext(diagram, (Function<ICanvasContext, T>) canvasContext -> func.apply(canvasContext));
+ }
+
+ public static <T> T doWithCanvasContext(Resource diagram, Function<ICanvasContext, T> func) throws DatabaseException {
+ return doWithICanvasSceneGraphProvider(diagram, (Function<ICanvasSceneGraphProvider, T>) provider -> func.apply(provider.getCanvasContext()));
+ }
+
public static String getNodeTransform(ICanvasContext ctx, String name) {
Set<TextNode> texts = NodeUtil.collectNodes(ctx.getSceneGraph(), TextNode.class);
}
G2DSceneGraph sg = ctx.getSceneGraph();
+ sg.performCleanup();
G2DParentNode root = (G2DParentNode) sg.getRootNode();
// rtree is the actual content of the diagram
res.append(result.get(MAIN_SECTION));
res.append(result.get(SELECTION_SECTION));
res.append(result.get(SELECTION_MASK_SECTION));
- res.append(result.get("</svg>"));
+ res.append("</svg>");
// System.err.println(" == FINAL RESULT == ");
// System.err.println(res.toString());
private String getKey(SingleElementNode node) {
String key;
- if (mappings.containsKey(node.getKey()))
- key = mappings.get(node.getKey()).toString();
- else
- key = node.getKey().toString();
+ if(node.getKey() != null) {
+ if (mappings.containsKey(node.getKey()))
+ key = mappings.get(node.getKey()).toString();
+ else
+ key = node.getKey().toString();
+ } else {
+ key = Long.toString(node.getId());
+ }
return key;
}
SelectionNode n = (SelectionNode)node;
SingleElementNode parentSEN = (SingleElementNode)NodeUtil.getNearestParentOfType(node, SingleElementNode.class);
- if(parentSEN != null && parentSEN.getKey() != null) {
+ if(parentSEN != null) {
RenderSVGContext parentBuilder2 = getParentBuilder(parentSEN);
String key = getKey(parentSEN);
+ n.setIgnore(false);
Element doc = renderSVGNode((IG2DNode)node);
+ n.setIgnore(true);
String svg = printSVGDocument(doc);
parentBuilder2.append(SELECTION_SECTION, "\n<g style=\"visibility:hidden\" class=\"selection\" id=\"" + key + "\">");
parentBuilder2.append(SELECTION_SECTION, svg);
parentBuilder2.append(SELECTION_MASK_SECTION,"></rect>");
}
parentBuilder2.append(SELECTION_MASK_SECTION,"\n</g>");
+
}
} else if (node instanceof SVGNode) {
SVGNode svg = (SVGNode)node;
AffineTransform at = node.getTransform();
if(node instanceof SingleElementNode) {
SingleElementNode sen = (SingleElementNode)node;
- if(sen.getKey() != null) {
- String key = getKey(sen);
- String typeClass = sen.getTypeClass();
- String clazz = "definedElement";
- if(typeClass != null && !typeClass.isEmpty())
- clazz = clazz + " " + typeClass;
-
- parentBuilder.append(MAIN_SECTION, "\n<g class=\""+clazz+"\" id=\"" + key + "\">");
- }
+ String key = getKey(sen);
+ String typeClass = sen.getTypeClass();
+ String clazz = "definedElement";
+ if(typeClass != null && !typeClass.isEmpty())
+ clazz = clazz + " " + typeClass;
+
+ parentBuilder.append(MAIN_SECTION, "\n<g class=\""+clazz+"\" id=\"" + key + "\">");
senBuilders.put(sen, new RenderSVGContext());
}
if(!at.isIdentity()) {
RenderSVGContext b = senBuilders.get(sen);
String content = b.get(MAIN_SECTION);
if(content.isEmpty()) {
- if(sen.getKey() != null) {
-
- for(SelectionNode n : NodeUtil.collectNodes(node, SelectionNode.class)) {
- n.setIgnore(true);
- }
-
- Element doc = renderSVGNode((IG2DNode)node);
- String svg = printSVGDocument(doc);
- parentBuilder.append(MAIN_SECTION, svg);
+ for(SelectionNode n : NodeUtil.collectNodes(node, SelectionNode.class)) {
+ n.setIgnore(true);
}
+ Element doc = renderSVGNode((IG2DNode)node);
+ String svg = printSVGDocument(doc);
+ parentBuilder.append(MAIN_SECTION, svg);
} else {
parentBuilder.append(b);
}
}
if(node instanceof SingleElementNode) {
SingleElementNode sen = (SingleElementNode)node;
- if(sen.getKey() != null) {
+ //if(sen.getKey() != null) {
parentBuilder.append(MAIN_SECTION, "</g>");
- }
+ //}
}
}
indent --;