1 package org.simantics.modeling.template2d.ui.function;
3 import java.util.Vector;
5 import org.simantics.databoard.Bindings;
6 import org.simantics.db.ReadGraph;
7 import org.simantics.db.Resource;
8 import org.simantics.db.WriteGraph;
9 import org.simantics.db.common.request.ReadRequest;
10 import org.simantics.db.common.request.WriteRequest;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.db.layer0.variable.Variables;
14 import org.simantics.diagram.function.PredefinedVariables;
15 import org.simantics.diagram.stubs.DiagramResource;
16 import org.simantics.image2.ontology.ImageResource;
17 import org.simantics.modeling.template2d.ontology.Template2dResource;
18 import org.simantics.project.ontology.ProjectResource;
19 import org.simantics.simulation.ontology.SimulationResource;
20 import org.simantics.utils.ui.dialogs.ShowMessage;
22 public class LinkSVGImage {
24 public static void linkSVGImage(WriteGraph graph, final Resource parent, final Resource child) {
26 final String[] uri = {null};
27 final String[] msg = {null};
28 final String errmsg = "Creation of image reference failed. Activate an experiment and try to copy and paste an image again.";
31 graph.syncRequest(new ReadRequest() {
33 public void run(ReadGraph g) throws DatabaseException {
34 if (parent == null || child == null) {
38 //Layer0 L0 = Layer0.getInstance(g);
39 ImageResource IMG = ImageResource.getInstance(g);
40 if (!g.isInstanceOf(child, IMG.SvgImage)){
45 Variable parentV = Variables.getVariable(g, parent);
51 Vector<Variable> grandOfParent = new Vector<Variable>();
52 Variable tmp = parentV;
54 grandOfParent.add(0, tmp);
55 tmp = tmp.getParent(g);
59 // System.err.println(parentV.getURI(g));
60 // String parentUri = parentV.getURI(g);
62 Variable childV = Variables.getVariable(g, child);
63 String childUri = null;
69 Vector<Variable> grandOfChild = new Vector<Variable>();
72 grandOfChild.add(0, tmp);
73 tmp = tmp.getParent(g);
77 int k = -1; // index of the most nearest common parent
78 int maxLen = (grandOfChild.size() < grandOfParent.size())? grandOfChild.size() : grandOfParent.size();
79 for (int i = 0;i < maxLen; i++){
80 if (grandOfChild.get(i).equals(grandOfParent.get(i)))
85 if (k == -1) // no common parent
90 //StructuralResource2 STR = StructuralResource2.getInstance(g);
91 SimulationResource SIMU = SimulationResource.getInstance(g);
92 ProjectResource PROJ = ProjectResource.getInstance(g);
93 Template2dResource TEPLATE2D = Template2dResource.getInstance(g);
94 Resource root = g.getRootLibrary();;
96 String predefined = null;
99 tmp = grandOfChild.get(k);
100 res = tmp.getRepresents(g);
101 if (res.equals(root)){
102 predefined = PredefinedVariables.root;
105 type = g.getPossibleType(res, SIMU.Model);
107 predefined = PredefinedVariables.model;
110 type = g.getPossibleType(res, PROJ.Project);
112 predefined = PredefinedVariables.project;
115 type = g.getPossibleType(res, TEPLATE2D.DrawingTemplate);
117 predefined = PredefinedVariables.template;
121 if (predefined == null) // no predefined common parent
126 Variable resV = Variables.getVariable(g, res);
127 String resUri = resV.getURI(g);
129 childUri = childV.getURI(g);
130 // System.err.println(childUri);
132 Resource childModel = Variables.getModel(g, childV);
133 Resource parentModel = Variables.getModel(g, parentV);
134 if (childModel == null || parentModel == null){
139 if (!parentModel.equals(childModel)){
144 // Variable modelV = Variables.getVariable(g, childModel);
145 // if (modelV == null){
146 // msg[0] = errmsg[0];
150 // String modelUri = modelV.getURI(g);
152 if (childUri.startsWith(resUri)){
153 uri[0] = childUri.substring(resUri.length());
154 uri[0] = "pre:/" + predefined + uri[0];
155 // System.err.println(uri);
159 graph.syncRequest(new WriteRequest() {
161 public void perform(WriteGraph g) throws DatabaseException {
162 if (uri == null || parent == null){
166 DiagramResource DIA = DiagramResource.getInstance(g);
167 Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
169 Resource var = g.getPossibleObject(parent, DIA.Scenegraph_SVGImage_document);
171 msg[0] = "Creation of image reference failed. Only SVG images are supported for now.";
175 g.claimLiteral(var, TEMPLATE2D.Profiles_VariableReference_path, uri[0] + "#data", Bindings.STRING);
179 ShowMessage.showWarning("Warning", msg[0]);
181 } catch (Exception e) {
182 // TODO: handle exception