]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.image.ui/src/org/simantics/image/ui/CreateImage.java
f10ada1e154fe375e224f7421206c6997f92068f
[simantics/platform.git] / bundles / org.simantics.image.ui / src / org / simantics / image / ui / CreateImage.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.image.ui;
13
14 import org.simantics.databoard.Bindings;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.request.WriteRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.util.Layer0Utils;
21 import org.simantics.image2.ontology.ImageResource;
22 import org.simantics.layer0.Layer0;
23
24 /**
25  * @author Tuukka Lehtonen
26  */
27 public class CreateImage extends WriteRequest {
28
29     private Resource container;
30     private ImageSource source;
31
32     public CreateImage(Resource container, ImageSource source) {
33         this.container = container;
34         this.source = source;
35     }
36
37     public static Resource getType(ReadGraph graph, ImageSource source) throws DatabaseException {
38         ImageResource IMAGE = ImageResource.getInstance(graph);
39         String name = source.name.toLowerCase();
40         if(name.endsWith("svg")) return IMAGE.SvgImage;
41         else if(name.endsWith("png")) return IMAGE.PngImage;
42         else if(name.endsWith("jpg") || name.endsWith("jpeg")) return IMAGE.JpegImage;
43         else if(name.endsWith("gif")) return IMAGE.GifImage;
44         else throw new DatabaseException("Unsupported image format " + source.name);
45     }
46     
47     public static void claimLiteral(WriteGraph graph, Resource image, ImageSource source) throws DatabaseException {
48         String name = source.name.toLowerCase();
49         if (name.endsWith("svg"))
50             graph.claimValue(image, new String(source.data), Bindings.STRING);
51         else if (name.endsWith("png") || name.endsWith("jpg") || name.endsWith("jpeg") || name.endsWith("gif"))
52             graph.claimValue(image, source.data, Bindings.BYTE_ARRAY);
53         else throw new DatabaseException("Unsupported image format " + source.name);
54     }
55
56     @Override
57     public void perform(WriteGraph graph) throws DatabaseException {
58         doImage(graph, container, source);
59     }
60     
61     public Resource doImage(WriteGraph graph, Resource parent, ImageSource source) throws DatabaseException {
62         graph.markUndoPoint();
63         Layer0 L0 = Layer0.getInstance(graph);
64         Resource image = graph.newResource();
65         graph.claim(image, L0.InstanceOf, null, getType(graph, source));
66         graph.claimLiteral(image, L0.HasName, source.name, Bindings.STRING);
67         claimLiteral(graph, image, source);
68         graph.claim(parent, L0.ConsistsOf, image);
69         Layer0Utils.addCommentMetadata(graph, "Imported image " + source.name + " " + image.toString());
70         return image;
71         
72 //      if(file.getPath().endsWith("svg")) {
73 //            ImageSource src = ImportImagesActionFactory.toImageSource(file);
74 //            Layer0 l0 = Layer0.getInstance(graph);
75 //            ImageResource img = ImageResource.getInstance(graph);
76 //            Resource image = graph.newResource();
77 //            graph.claim(image, l0.InstanceOf, null, img.Image);
78 //            graph.claimLiteral(image, l0.HasName, src.name, Bindings.STRING);
79 //            String document = FileUtils.getContents(file.getAbsolutePath());
80 //            graph.claimLiteral(image, img.HasImageDocument, document, Bindings.STRING);
81 //            graph.claim(container, l0.ConsistsOf, image);
82 //            graph.claim(container, img.HasImage, image);
83 //      } else {
84 //      }
85 //
86 //      
87 //        Layer0 l0 = Layer0.getInstance(graph);
88 //        ImageResource img = ImageResource.getInstance(graph);
89 //
90 //        Resource image = graph.newResource();
91 //        graph.claim(image, l0.InstanceOf, null, img.Image);
92 //        graph.claimLiteral(image, l0.HasName, source.name, Bindings.STRING);
93 //        graph.claimLiteral(image, img.HasImageData, source.data, Bindings.BYTE_ARRAY);
94 //        graph.claim(container, l0.ConsistsOf, image);
95 //        graph.claim(container, img.HasImage, image);
96                 
97     }
98
99 }