994d417c99b815ba4c3deae54511777a84e8fc05
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / svg / CreateSVGElement.java
1 package org.simantics.modeling.svg;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.simantics.databoard.Bindings;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.utils.OrderedSetUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.diagram.stubs.DiagramResource;
12 import org.simantics.diagram.stubs.G2DResource;
13 import org.simantics.diagram.synchronization.graph.AddElement;
14 import org.simantics.image2.ontology.ImageResource;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.layer0.utils.direct.GraphUtils;
17 import org.simantics.utils.FileUtils;
18
19 public class CreateSVGElement {
20
21     public static void createSVGElement(WriteGraph g, Resource diagram, String suffix, byte[] data, double mposX, double mposY) throws DatabaseException {
22
23         Layer0 L0 = Layer0.getInstance(g);
24         DiagramResource DIA = DiagramResource.getInstance(g);
25         ImageResource IMAGE = ImageResource.getInstance(g);
26         G2DResource G2D = G2DResource.getInstance(g);
27
28         Resource element;
29         if("svg".equals(suffix)) {
30             Resource transform = GraphUtils.create(g, L0.InstanceOf, G2D.Transform);
31             g.claimValue(transform, new double[] {1.0, 0.0, 0.0, 1.0, mposX, mposY}, Bindings.DOUBLE_ARRAY);
32             element = GraphUtils.create(g,
33                     L0.InstanceOf, DIA.SVGElement,
34                     G2D.HasSVGDocument, new String(data),
35                     DIA.HasTransform, transform);
36             AddElement.claimFreshElementName(g, diagram, element);
37         } else if ("png".equals(suffix)) {
38             Resource transform = GraphUtils.create(g, L0.InstanceOf, G2D.Transform);
39             g.claimValue(transform, new double[] {1.0, 0.0, 0.0, 1.0, mposX, mposY}, Bindings.DOUBLE_ARRAY);
40             Resource image = g.newResource();
41             g.claim(image, L0.InstanceOf, IMAGE.PngImage);
42             g.claimValue(image, data, Bindings.BYTE_ARRAY);
43             element = GraphUtils.create(g,
44                     L0.InstanceOf, DIA.RasterElement,
45                     G2D.HasRasterImage, image,
46                     DIA.HasTransform, transform);
47             AddElement.claimFreshElementName(g, diagram, element);
48         }
49         else
50             throw new DatabaseException("Unknown image format " + suffix);
51         OrderedSetUtils.addFirst(g, diagram, element);
52         g.claim(diagram, L0.ConsistsOf, element);
53
54     }
55     
56     public static void importSVGElement(WriteGraph graph, Resource diagram, File file, double posX, double posY) throws DatabaseException, IOException {
57         
58         final byte[] data = FileUtils.readFile(file);
59         createSVGElement(graph, diagram, suffix(file.getName()), data, posX, posY);
60     }
61     
62     private static String suffix(String fileName) {
63         int len = fileName.length();
64         if(len < 3) return null;
65         else return fileName.substring(len-3,len).toLowerCase();
66     }
67     
68 }