]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.image.ui/src/org/simantics/image/ui/LinkSvgImage.java
Moved LinkSVGImage implementation to new location.
[simantics/platform.git] / bundles / org.simantics.image.ui / src / org / simantics / image / ui / LinkSvgImage.java
1 package org.simantics.image.ui;
2
3 import java.util.Vector;
4
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;
21
22 public class LinkSvgImage {
23
24     public static void linkSVGImage(WriteGraph graph, final Resource parent, final Resource child) {
25
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.";
29
30         try {
31             graph.syncRequest(new ReadRequest() {
32                 @Override
33                 public void run(ReadGraph g) throws DatabaseException {
34                     if (parent == null || child == null) {
35                         msg[0] = errmsg;
36                         return;
37                     }
38                     //Layer0 L0 = Layer0.getInstance(g);
39                     ImageResource IMG = ImageResource.getInstance(g);
40                     if (!g.isInstanceOf(child, IMG.SvgImage)){
41                         msg[0] = errmsg;
42                         return;
43                     }
44
45                     Variable parentV = Variables.getVariable(g, parent);
46                     if (parentV == null){
47                         msg[0] = errmsg;
48                         return;
49                     }
50
51                     Vector<Variable> grandOfParent = new Vector<Variable>();
52                     Variable tmp = parentV;
53                     do {
54                         grandOfParent.add(0, tmp);
55                         tmp = tmp.getParent(g);
56                     }
57                     while (tmp != null);
58
59 //                  System.err.println(parentV.getURI(g));
60 //                  String parentUri = parentV.getURI(g);
61
62                     Variable childV = Variables.getVariable(g, child);
63                     String childUri = null;
64                     if (childV == null){
65                         msg[0] = errmsg;
66                         return;
67                     }
68
69                     Vector<Variable> grandOfChild = new Vector<Variable>();
70                     tmp = childV;
71                     do {
72                         grandOfChild.add(0, tmp);
73                         tmp = tmp.getParent(g);
74                     }
75                     while (tmp != null);
76
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)))
81                             k++;
82                         else
83                             break;
84                     }
85                     if (k == -1) // no common parent
86                     {
87                         msg[0] = errmsg;
88                         return;
89                     }
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();;
95                     Resource type = null;
96                     String predefined = null;
97                     Resource res = null;
98                     for (;k >= 0;k--){
99                         tmp = grandOfChild.get(k);
100                         res = tmp.getRepresents(g);
101                         if (res.equals(root)){
102                             predefined = PredefinedVariables.root;
103                             break;
104                         }
105                         type = g.getPossibleType(res, SIMU.Model);
106                         if (type != null){
107                             predefined = PredefinedVariables.model;
108                             break;
109                         }
110                         type = g.getPossibleType(res, PROJ.Project);
111                         if (type != null){
112                             predefined = PredefinedVariables.project;
113                             break;
114                         }
115                         type = g.getPossibleType(res, TEPLATE2D.DrawingTemplate);
116                         if (type != null){
117                             predefined = PredefinedVariables.template;
118                             break;
119                         }
120                     }
121                     if (predefined == null) // no predefined common parent
122                     {
123                         msg[0] = errmsg;
124                         return;
125                     }
126                     Variable resV = Variables.getVariable(g, res);
127                     String resUri = resV.getURI(g);
128
129                     childUri = childV.getURI(g);
130 //                  System.err.println(childUri);
131
132                     Resource childModel = Variables.getModel(g, childV);
133                     Resource parentModel = Variables.getModel(g, parentV);
134                     if (childModel == null || parentModel == null){
135                         msg[0] = errmsg;
136                         return;
137                     }
138
139                     if (!parentModel.equals(childModel)){
140                         msg[0] = errmsg;
141                         return;
142                     }
143
144 //                  Variable modelV = Variables.getVariable(g, childModel);
145 //                  if (modelV == null){
146 //                      msg[0] = errmsg[0];
147 //                      return;
148 //                  }
149 //
150 //                  String modelUri = modelV.getURI(g);
151
152                     if (childUri.startsWith(resUri)){
153                         uri[0] = childUri.substring(resUri.length());
154                         uri[0] = "pre:/" + predefined + uri[0];
155 //                      System.err.println(uri);
156                     }
157                 }
158             });
159             graph.syncRequest(new WriteRequest() {
160                 @Override
161                 public void perform(WriteGraph g) throws DatabaseException {
162                     if (uri == null || parent == null){
163                         msg[0] = errmsg;
164                         return;
165                     }
166                     DiagramResource DIA = DiagramResource.getInstance(g);
167                     Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);
168
169                     Resource var = g.getPossibleObject(parent, DIA.Scenegraph_SVGImage_document);
170                     if (var == null){
171                         msg[0] = "Creation of image reference failed. Only SVG images are supported for now.";
172                         return;
173                     }
174
175                     g.claimLiteral(var, TEMPLATE2D.Profiles_VariableReference_path, uri[0] + "#data", Bindings.STRING);
176                 }
177             });
178             if (msg[0] != null){
179                 ShowMessage.showWarning("Warning", msg[0]);
180             }
181         } catch (Exception e) {
182             // TODO: handle exception
183             e.printStackTrace();
184         }
185
186     }
187
188
189 }