1 /*******************************************************************************
2 * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.image.ui;
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;
25 * @author Tuukka Lehtonen
27 public class CreateImage extends WriteRequest {
29 private Resource container;
30 private ImageSource source;
32 public CreateImage(Resource container, ImageSource source) {
33 this.container = container;
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; //$NON-NLS-1$
41 else if(name.endsWith("png")) return IMAGE.PngImage; //$NON-NLS-1$
42 else if(name.endsWith("jpg") || name.endsWith("jpeg")) return IMAGE.JpegImage; //$NON-NLS-1$ //$NON-NLS-2$
43 else if(name.endsWith("gif")) return IMAGE.GifImage; //$NON-NLS-1$
44 else throw new DatabaseException("Unsupported image format " + source.name); //$NON-NLS-1$
47 public static void claimLiteral(WriteGraph graph, Resource image, ImageSource source) throws DatabaseException {
48 String name = source.name.toLowerCase();
49 if (name.endsWith("svg")) //$NON-NLS-1$
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")) //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
52 graph.claimValue(image, source.data, Bindings.BYTE_ARRAY);
53 else throw new DatabaseException("Unsupported image format " + source.name); //$NON-NLS-1$
57 public void perform(WriteGraph graph) throws DatabaseException {
58 doImage(graph, container, source);
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()); //$NON-NLS-1$ //$NON-NLS-2$
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);
87 // Layer0 l0 = Layer0.getInstance(graph);
88 // ImageResource img = ImageResource.getInstance(graph);
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);