--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.image.ui;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.image2.ontology.ImageResource;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class CreateImage extends WriteRequest {\r
+\r
+ private Resource container;\r
+ private ImageSource source;\r
+\r
+ public CreateImage(Resource container, ImageSource source) {\r
+ this.container = container;\r
+ this.source = source;\r
+ }\r
+\r
+ public static Resource getType(ReadGraph graph, ImageSource source) throws DatabaseException {\r
+ ImageResource IMAGE = ImageResource.getInstance(graph);\r
+ String name = source.name.toLowerCase();\r
+ if(name.endsWith("svg")) return IMAGE.SvgImage;\r
+ else if(name.endsWith("png")) return IMAGE.PngImage;\r
+ else if(name.endsWith("jpg") || name.endsWith("jpeg")) return IMAGE.JpegImage;\r
+ else if(name.endsWith("gif")) return IMAGE.GifImage;\r
+ else throw new DatabaseException("Unsupported image format " + source.name);\r
+ }\r
+ \r
+ public static void claimLiteral(WriteGraph graph, Resource image, ImageSource source) throws DatabaseException {\r
+ String name = source.name.toLowerCase();\r
+ if (name.endsWith("svg"))\r
+ graph.claimValue(image, new String(source.data), Bindings.STRING);\r
+ else if (name.endsWith("png") || name.endsWith("jpg") || name.endsWith("jpeg") || name.endsWith("gif"))\r
+ graph.claimValue(image, source.data, Bindings.BYTE_ARRAY);\r
+ else throw new DatabaseException("Unsupported image format " + source.name);\r
+ }\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ doImage(graph, container, source);\r
+ }\r
+ \r
+ public Resource doImage(WriteGraph graph, Resource parent, ImageSource source) throws DatabaseException {\r
+ graph.markUndoPoint();\r
+ Layer0 L0 = Layer0.getInstance(graph);\r
+ Resource image = graph.newResource();\r
+ graph.claim(image, L0.InstanceOf, null, getType(graph, source));\r
+ graph.claimLiteral(image, L0.HasName, source.name, Bindings.STRING);\r
+ claimLiteral(graph, image, source);\r
+ graph.claim(parent, L0.ConsistsOf, image);\r
+ Layer0Utils.addCommentMetadata(graph, "Imported image " + source.name + " " + image.toString());\r
+ return image;\r
+ \r
+// if(file.getPath().endsWith("svg")) {\r
+// ImageSource src = ImportImagesActionFactory.toImageSource(file);\r
+// Layer0 l0 = Layer0.getInstance(graph);\r
+// ImageResource img = ImageResource.getInstance(graph);\r
+// Resource image = graph.newResource();\r
+// graph.claim(image, l0.InstanceOf, null, img.Image);\r
+// graph.claimLiteral(image, l0.HasName, src.name, Bindings.STRING);\r
+// String document = FileUtils.getContents(file.getAbsolutePath());\r
+// graph.claimLiteral(image, img.HasImageDocument, document, Bindings.STRING);\r
+// graph.claim(container, l0.ConsistsOf, image);\r
+// graph.claim(container, img.HasImage, image);\r
+// } else {\r
+// }\r
+//\r
+// \r
+// Layer0 l0 = Layer0.getInstance(graph);\r
+// ImageResource img = ImageResource.getInstance(graph);\r
+//\r
+// Resource image = graph.newResource();\r
+// graph.claim(image, l0.InstanceOf, null, img.Image);\r
+// graph.claimLiteral(image, l0.HasName, source.name, Bindings.STRING);\r
+// graph.claimLiteral(image, img.HasImageData, source.data, Bindings.BYTE_ARRAY);\r
+// graph.claim(container, l0.ConsistsOf, image);\r
+// graph.claim(container, img.HasImage, image);\r
+ \r
+ }\r
+\r
+}
\ No newline at end of file