]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/AnnotationUtils.java
Sync git svn branch with SVN repository r33382.
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / AnnotationUtils.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.annotation.ui;\r
13 \r
14 import java.util.HashMap;\r
15 import java.util.Map;\r
16 import java.util.UUID;\r
17 import java.util.function.Consumer;\r
18 \r
19 import org.eclipse.jface.dialogs.IDialogSettings;\r
20 import org.eclipse.jface.resource.ImageDescriptor;\r
21 import org.eclipse.jface.window.Window;\r
22 import org.eclipse.swt.widgets.Display;\r
23 import org.eclipse.swt.widgets.Shell;\r
24 import org.eclipse.ui.PlatformUI;\r
25 import org.simantics.Simantics;\r
26 import org.simantics.annotation.ontology.AnnotationResource;\r
27 import org.simantics.annotation.ui.internal.SaveAnnotationDialog;\r
28 import org.simantics.databoard.Bindings;\r
29 import org.simantics.databoard.util.URIStringUtils;\r
30 import org.simantics.db.ReadGraph;\r
31 import org.simantics.db.Resource;\r
32 import org.simantics.db.WriteGraph;\r
33 import org.simantics.db.common.CommentMetadata;\r
34 import org.simantics.db.common.request.PossibleIndexRoot;\r
35 import org.simantics.db.common.request.ReadRequest;\r
36 import org.simantics.db.common.request.WriteRequest;\r
37 import org.simantics.db.common.utils.Logger;\r
38 import org.simantics.db.common.utils.NameUtils;\r
39 import org.simantics.db.exception.DatabaseException;\r
40 import org.simantics.db.layer0.adapter.Instances;\r
41 import org.simantics.db.layer0.request.PossibleModel;\r
42 import org.simantics.db.layer0.request.VariableRead;\r
43 import org.simantics.db.layer0.util.Layer0Utils;\r
44 import org.simantics.db.layer0.variable.Variable;\r
45 import org.simantics.db.layer0.variable.Variables;\r
46 import org.simantics.layer0.Layer0;\r
47 import org.simantics.selectionview.SelectionViewResources;\r
48 import org.simantics.structural.stubs.StructuralResource2;\r
49 import org.simantics.ui.workbench.dialogs.ResourceSelectionDialog3;\r
50 import org.simantics.utils.datastructures.Pair;\r
51 \r
52 /**\r
53  * @author Teemu Mätäsniemi\r
54  * @author Antti Villberg\r
55  * @author Tuukka Lehtonen\r
56  */\r
57 public class AnnotationUtils {\r
58 \r
59         /**\r
60          * @param graph\r
61          * @param parent\r
62          * @return (r1, r2) pair where r1 is the created annotation property\r
63          *         relation and and r2 is the created annotation type\r
64          * @throws DatabaseException\r
65          */\r
66         public static Pair<Resource, Resource> newAnnotationType(WriteGraph graph, final Resource parent) throws DatabaseException {\r
67             graph.markUndoPoint();\r
68                 Layer0 L0 = Layer0.getInstance(graph);\r
69                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
70                 SelectionViewResources SEL = SelectionViewResources.getInstance(graph);\r
71                 StructuralResource2 STR = StructuralResource2.getInstance(graph);\r
72 \r
73                 Resource indexRoot = graph.sync(new PossibleIndexRoot(parent));\r
74 \r
75                 // Get supertype for annotation type\r
76                 Resource propertySubrelation = graph.getPossibleObject(indexRoot, ANNO.HasAnnotationPropertySubrelation);\r
77                 if (propertySubrelation == null)\r
78                         propertySubrelation = L0.HasProperty;\r
79                 Resource supertype = graph.getPossibleObject(indexRoot, ANNO.HasAnnotationTypeSupertype);\r
80                 if (supertype == null)\r
81                         supertype = ANNO.Annotation;\r
82 \r
83                 Resource property = graph.newResource();\r
84                 String name = NameUtils.findFreshName(graph, "newAnnotationProperty", parent, L0.ConsistsOf);\r
85                 graph.addLiteral(property, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
86                 graph.claim(property, L0.SubrelationOf, propertySubrelation);\r
87 \r
88                 Resource type = graph.newResource();\r
89                 graph.claim(type, L0.Inherits, null, supertype);\r
90                 graph.addLiteral(type, L0.HasName, L0.NameOf, L0.String, UUID.randomUUID().toString(), Bindings.STRING);\r
91                 graph.claim(type, STR.ComponentType_HasDefaultPropertyRelationType, SEL.GenericParameterType);\r
92 \r
93                 graph.claim(property, L0.HasRange, type);\r
94 \r
95                 graph.claim(type, L0.ConsistsOf, property);\r
96                 graph.claim(parent, L0.ConsistsOf, type);\r
97                 \r
98                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
99         graph.addMetadata(cm.add("Added new annotationType " + type + " with property relation " + property + " "));\r
100 \r
101                 return Pair.make(property, type);\r
102         }\r
103 \r
104         public static void newAnnotation(ReadGraph graph, Resource parent, Resource model) throws DatabaseException {\r
105 \r
106         Map<Resource, Pair<String, ImageDescriptor>> map = new HashMap<>();\r
107         findAnnotationTypes(graph, model, map);\r
108         findAnnotations(graph, model, map);\r
109         queryUserSelectedAnnotationType(map, selected -> {\r
110             Simantics.getSession().async(new WriteRequest() {\r
111                 @Override\r
112                 public void perform(WriteGraph g) throws DatabaseException {\r
113                     newAnnotation(g, parent, selected);\r
114                 }\r
115             });\r
116         });\r
117 \r
118         }\r
119 \r
120         private static boolean isEntryAnnotation(ReadGraph graph, Resource selected) throws DatabaseException {\r
121                 Layer0 L0 = Layer0.getInstance(graph);\r
122         AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
123         if(graph.isInstanceOf(selected, ANNO.Annotation)) {\r
124                 Resource type = graph.getSingleType(selected, ANNO.Annotation);\r
125                 return !graph.hasStatement(type, L0.HasRange_Inverse);\r
126         } else if (graph.isInstanceOf(selected, ANNO.AnnotationType)) {\r
127                 return !graph.hasStatement(selected, L0.HasRange_Inverse);\r
128         } else {\r
129                 throw new DatabaseException("Incompatible resource " + selected);\r
130         }\r
131         }\r
132         \r
133         public static void newAnnotation(ReadGraph graph, Variable position, Resource model) throws DatabaseException {\r
134 \r
135         Map<Resource, Pair<String, ImageDescriptor>> map = new HashMap<>();\r
136         findAnnotationTypes(graph, model, map);\r
137         findAnnotations(graph, model, map);\r
138         queryUserSelectedAnnotationType(map, selected -> {\r
139             Simantics.getSession().async(new WriteRequest() {\r
140                 @Override\r
141                 public void perform(WriteGraph g) throws DatabaseException {\r
142                     g.markUndoPoint();\r
143                     if(isEntryAnnotation(g, selected)) {\r
144                         newAnnotation(g, position.getRepresents(g), selected);\r
145                         Layer0Utils.addCommentMetadata(g, "Attached new annotation to " + g.getRelatedValue2(position.getRepresents(g), Layer0.getInstance(g).HasName, Bindings.STRING));\r
146                     } else {\r
147                         newAnnotation(g, position.getParent(g).getRepresents(g), selected);\r
148                         Layer0Utils.addCommentMetadata(g, "Attached new annotation to " + g.getRelatedValue2(position.getParent(g).getRepresents(g), Layer0.getInstance(g).HasName, Bindings.STRING));\r
149                     }\r
150                 }\r
151             });\r
152         });\r
153 \r
154         }\r
155         \r
156         /**\r
157          * Creates a new annotation instance for the specified parent after the user\r
158          * selects the annotation type from the list of all annotation types in the\r
159          * specified model. The annotation types are resolved from the model\r
160          * dependency index.\r
161          * \r
162          * @param parent\r
163          * @param model\r
164          * @throws DatabaseException\r
165          */\r
166         public static void newAnnotation(Resource parent, Resource model) throws DatabaseException {\r
167                 if (model == null)\r
168                         return;\r
169                 Simantics.getSession().syncRequest(new ReadRequest() {\r
170                         @Override\r
171                         public void run(ReadGraph graph) throws DatabaseException {\r
172                             newAnnotation(graph, parent, model);\r
173                         }\r
174                 });\r
175         }\r
176 \r
177         public static void newAnnotation(Resource parent) throws DatabaseException {\r
178         Simantics.getSession().syncRequest(new ReadRequest() {\r
179             @Override\r
180             public void run(ReadGraph graph) throws DatabaseException {\r
181                 Resource model = graph.sync(new PossibleModel(parent));\r
182                 if(model == null) return;\r
183                 newAnnotation(graph, parent, model);\r
184             }\r
185         });\r
186         }\r
187 \r
188         public static void newAnnotation(Variable position) throws DatabaseException {\r
189         Simantics.getSession().syncRequest(new ReadRequest() {\r
190             @Override\r
191             public void run(ReadGraph graph) throws DatabaseException {\r
192                 Resource model = Variables.getModel(graph, position);\r
193                 if(model == null) return;\r
194                 newAnnotation(graph, position, model);\r
195             }\r
196         });\r
197         }\r
198 \r
199         public static void newAnnotationInstance(Resource parent, Resource model) throws DatabaseException {\r
200         if (model == null)\r
201             return;\r
202         Simantics.getSession().syncRequest(new ReadRequest() {\r
203             @Override\r
204             public void run(ReadGraph graph) throws DatabaseException {\r
205                 Map<Resource, Pair<String, ImageDescriptor>> map = new HashMap<>();\r
206                 findAnnotationTypes(graph, model, map);\r
207                 queryUserSelectedAnnotationType(map, selected -> {\r
208                     Simantics.getSession().async(new WriteRequest() {\r
209                         @Override\r
210                         public void perform(WriteGraph g) throws DatabaseException {\r
211                             g.markUndoPoint();\r
212                             newAnnotationInstance(g, parent, selected);\r
213                         }\r
214                     });\r
215                 });\r
216             }\r
217         });\r
218     }\r
219 \r
220         /**\r
221          * @param graph\r
222          * @param model\r
223          * @return\r
224          * @throws DatabaseException \r
225          */\r
226         protected static void findAnnotationTypes(ReadGraph graph, Resource model, Map<Resource, Pair<String, ImageDescriptor>> map) throws DatabaseException {\r
227 \r
228                 Layer0 L0 = Layer0.getInstance(graph);\r
229                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
230 \r
231                 Instances query = graph.adapt(ANNO.AnnotationType, Instances.class);\r
232 \r
233                 String modelURI = graph.getURI(model);\r
234                 \r
235         ImageDescriptor descriptor = graph.adapt(ANNO.Images_AnnotationType, ImageDescriptor.class);\r
236 \r
237                 for(Resource _res : query.find(graph, model)) {\r
238                         // Don't allow instantiation of abstract annotation types.\r
239                         if (graph.hasStatement(_res, L0.Abstract))\r
240                                 continue;\r
241 \r
242                         // Don't allow instantiation of non-user-selectable annotation types\r
243                         Boolean userSelectable = graph.getPossibleRelatedValue2(_res, ANNO.AnnotationType_systemAnnotation, Bindings.BOOLEAN);\r
244                         if (Boolean.TRUE.equals(userSelectable))\r
245                                 continue;\r
246 \r
247                         Resource res = graph.getPossibleObject(_res, L0.HasRange_Inverse);\r
248                         if(res == null) {\r
249                                 \r
250                                 // Entry type\r
251 \r
252                                 String name = graph.getPossibleRelatedValue(_res, L0.HasName, Bindings.STRING);\r
253                                 if (name == null)\r
254                                         continue;\r
255                                 String label = graph.getPossibleRelatedValue2(_res, L0.HasLabel, Bindings.STRING);\r
256 \r
257                                 if (label != null && !name.equals(label)) {\r
258                                         name = label + " (" + name + ")";\r
259                                 }\r
260 \r
261                                 Resource parent = graph.getPossibleObject(_res, L0.PartOf);\r
262                                 if(parent == null) continue;\r
263 \r
264                                 String parentURI = graph.getURI(parent);\r
265                                 if(parentURI.startsWith(modelURI)) {\r
266                                         parentURI = parentURI.substring(modelURI.length());\r
267                                         if(parentURI.startsWith("/")) parentURI = parentURI.substring(1);\r
268                                 }\r
269 \r
270                                 name = name + " - " + URIStringUtils.unescape(parentURI);\r
271 \r
272                                 map.put(_res, Pair.make(name, descriptor));\r
273                                 \r
274                         } else {\r
275                                 \r
276                                 // Property type\r
277                         \r
278                                 String name = graph.getPossibleRelatedValue(res, L0.HasName, Bindings.STRING);\r
279                                 if (name == null)\r
280                                         continue;\r
281                                 String label = graph.getPossibleRelatedValue2(res, L0.HasLabel, Bindings.STRING);\r
282 \r
283                                 if (label != null && !name.equals(label)) {\r
284                                         name = label + " (" + name + ")";\r
285                                 }\r
286 \r
287                                 Resource parent = graph.getPossibleObject(_res, L0.PartOf);\r
288                                 if(parent == null) continue;\r
289 \r
290                                 String parentURI = graph.getURI(parent);\r
291                                 if(parentURI.startsWith(modelURI)) {\r
292                                         parentURI = parentURI.substring(modelURI.length());\r
293                                         if(parentURI.startsWith("/")) parentURI = parentURI.substring(1);\r
294                                 }\r
295 \r
296                                 name = name + " - " + URIStringUtils.unescape(parentURI);\r
297 \r
298                                 map.put(_res, Pair.make(name, descriptor));\r
299                         \r
300                         }\r
301                         \r
302                 }\r
303 \r
304         }\r
305 \r
306     protected static void findAnnotations(ReadGraph graph, Resource model, Map<Resource, Pair<String, ImageDescriptor>> map) throws DatabaseException {\r
307 \r
308         Layer0 L0 = Layer0.getInstance(graph);\r
309         AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
310 \r
311         Instances query = graph.adapt(ANNO.Annotation, Instances.class);\r
312         \r
313         String modelURI = graph.getURI(model);\r
314         \r
315         ImageDescriptor descriptor = graph.adapt(ANNO.Images_Annotation, ImageDescriptor.class);\r
316 \r
317         for(Resource _res : query.find(graph, model)) {\r
318             String name = graph.getPossibleRelatedValue(_res, L0.HasName, Bindings.STRING);\r
319             if (name == null)\r
320                 continue;\r
321             String label = graph.getPossibleRelatedValue2(_res, L0.HasLabel, Bindings.STRING);\r
322             if (label != null && !name.equals(label)) {\r
323                 name = label + " (" + name + ")";\r
324             }\r
325 \r
326             Resource parent = graph.getPossibleObject(_res, L0.PartOf);\r
327             if(parent == null) continue;\r
328 \r
329             String parentURI = graph.getPossibleURI(parent);\r
330             if(parentURI == null) continue;\r
331              \r
332             if(parentURI.startsWith(modelURI)) {\r
333                 parentURI = parentURI.substring(modelURI.length());\r
334                 if(parentURI.startsWith("/")) parentURI = parentURI.substring(1);\r
335             }\r
336 \r
337             Resource type = graph.getPossibleType(_res, ANNO.Annotation);\r
338             if(type != null) {\r
339                 // Don't list instances of non-user-selectable annotation types\r
340                 Boolean userSelectable = graph.getPossibleRelatedValue2(type, ANNO.AnnotationType_systemAnnotation, Bindings.BOOLEAN);\r
341                 if (Boolean.TRUE.equals(userSelectable))\r
342                     continue;\r
343 \r
344                 Resource relation = graph.getPossibleObject(type, L0.HasRange_Inverse);\r
345                 if(relation != null) {\r
346                     String rName = graph.getPossibleRelatedValue(relation, L0.HasName, Bindings.STRING);\r
347                     if(rName != null) {\r
348                         name = name + " - " + rName;\r
349                     }\r
350                 }\r
351             }\r
352             \r
353             name = name + " - " + URIStringUtils.unescape(parentURI);\r
354             \r
355             map.put(_res, Pair.make(name, descriptor));\r
356             \r
357         }\r
358 \r
359     }\r
360 \r
361     protected static boolean isAnnotation(Variable variable) {\r
362         if (variable == null)\r
363                 return false;\r
364         try {\r
365                 return Simantics.sync(new VariableRead<Boolean>(variable) {\r
366 \r
367                         @Override\r
368                         public Boolean perform(ReadGraph graph) throws DatabaseException {\r
369                                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
370                                 Resource represents = variable.getPossibleRepresents(graph);\r
371                                 if(represents == null) return false;\r
372                                 return graph.isInstanceOf(represents, ANNO.Annotation);\r
373                         }\r
374 \r
375                 });\r
376         } catch (DatabaseException e) {\r
377                 return false;\r
378         }\r
379     }\r
380     \r
381     protected static Map<Resource, Pair<String, ImageDescriptor>> findLibraries(Variable variable) {\r
382 \r
383         try {\r
384 \r
385                 return Simantics.sync(new VariableRead<Map<Resource, Pair<String, ImageDescriptor>>>(variable) {\r
386 \r
387                         @Override\r
388                         public Map<Resource, Pair<String, ImageDescriptor>> perform(ReadGraph graph) throws DatabaseException {\r
389 \r
390                                 Map<Resource, Pair<String, ImageDescriptor>> result = new HashMap<>();\r
391 \r
392                                 Layer0 L0 = Layer0.getInstance(graph);\r
393                                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
394 \r
395                                 Resource model = Variables.getModel(graph, variable);\r
396                                 Instances query = graph.adapt(L0.Library, Instances.class);\r
397 \r
398                                 String modelURI = graph.getURI(model);\r
399                                 int modelPos = modelURI.length();\r
400 \r
401                                 ImageDescriptor descriptor = graph.adapt(ANNO.Images_Annotation, ImageDescriptor.class);\r
402 \r
403                                 for(Resource lib : query.find(graph, model)) {\r
404 \r
405                                         String path = graph.getURI(lib);\r
406                                         if(!path.startsWith(modelURI)) continue;\r
407                                         String suffix = URIStringUtils.unescape(path.substring(modelPos));\r
408                                         if(suffix.startsWith("/")) suffix = suffix.substring(1);\r
409                                         result.put(lib, Pair.make(suffix, descriptor));\r
410 \r
411                                 }\r
412 \r
413                                 return result;\r
414                                 \r
415                         }\r
416                 });\r
417 \r
418         } catch (DatabaseException e) {\r
419                 Logger.defaultLogError(e);\r
420         }\r
421         \r
422         return null;\r
423 \r
424     }\r
425 \r
426     public static Resource newAnnotation(WriteGraph graph, Resource container, Resource valueOrProperty) throws DatabaseException {\r
427             graph.markUndoPoint();\r
428                 Layer0 L0 = Layer0.getInstance(graph);\r
429                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
430 \r
431                 if(graph.isInstanceOf(valueOrProperty, ANNO.Annotation)) {\r
432 \r
433                     Resource type = graph.getPossibleType(valueOrProperty, ANNO.Annotation);\r
434                     if(type == null) return null;\r
435                     Resource property = graph.getPossibleObject(type, L0.HasRange_Inverse);\r
436             if(property == null) {\r
437                 graph.claim(container, ANNO.Annotation_HasEntry, valueOrProperty);\r
438             } else {\r
439                 graph.deny(container, property);\r
440                 graph.claim(container, property, valueOrProperty);\r
441             }\r
442             Layer0Utils.addCommentMetadata(graph, "Created new annotation value/property " + valueOrProperty + " to " + graph.getRelatedValue2(container, L0.HasName, Bindings.STRING));\r
443                 return valueOrProperty;\r
444             \r
445                 } else if (graph.isInstanceOf(valueOrProperty, ANNO.AnnotationType)) {\r
446                     \r
447                         Resource predicate = graph.getPossibleObject(valueOrProperty, L0.HasRange_Inverse);\r
448                         if(predicate != null) {\r
449                             Resource value = graph.newResource();\r
450                             graph.claim(value, L0.InstanceOf, valueOrProperty);\r
451                             graph.deny(container, predicate);\r
452                             graph.claim(container, predicate, value);\r
453                             Layer0Utils.addCommentMetadata(graph, "Created new annotation type " + value + " to " + graph.getRelatedValue2(container, L0.HasName, Bindings.STRING));\r
454                             return value;\r
455                         } else {\r
456                             Resource value = graph.newResource();\r
457                             graph.claim(value, L0.InstanceOf, valueOrProperty);\r
458                             String name = NameUtils.findFreshEscapedName(graph, "Value", container, ANNO.Annotation_HasEntry);\r
459                             graph.addLiteral(value, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
460                             graph.claim(container, ANNO.Annotation_HasEntry, value);\r
461                             Layer0Utils.addCommentMetadata(graph, "Created new annotation entry " + value + " to " + graph.getRelatedValue2(container, L0.HasName, Bindings.STRING));\r
462                             return value;\r
463                         }\r
464                         \r
465                 } else {\r
466                 \r
467                     Resource valueType = graph.getSingleObject(valueOrProperty, L0.HasRange);\r
468                     Resource value = graph.newResource();\r
469                     graph.claim(value, L0.InstanceOf, valueType);\r
470                     graph.deny(container, valueOrProperty);\r
471                     graph.claim(container, valueOrProperty, value);\r
472                     return value;\r
473                 \r
474                 }\r
475                 \r
476         }\r
477 \r
478     public static Resource newAnnotationInstance(WriteGraph graph, Resource container, Resource annotationProperty) throws DatabaseException {\r
479         return newAnnotationInstance(graph, container, null, annotationProperty);\r
480     }\r
481 \r
482     public static Resource newAnnotationInstance(WriteGraph graph, Resource container, String name, Resource annotationProperty) throws DatabaseException {\r
483         return newAnnotationInstance(graph, container, name, annotationProperty, true);\r
484     }\r
485     \r
486     public static Resource newAnnotationInstance(WriteGraph graph, Resource container, String name, Resource annotationProperty, boolean addCommentMetadata) throws DatabaseException {\r
487         //graph.markUndoPoint();\r
488         Layer0 L0 = Layer0.getInstance(graph);\r
489         AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
490 \r
491         if(graph.isInstanceOf(annotationProperty, ANNO.AnnotationType)) {\r
492 \r
493                 Resource predicate = graph.getPossibleObject(annotationProperty, L0.HasRange_Inverse);\r
494 \r
495                 String proposition = predicate != null ?  (String)graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING) :\r
496                         (String)graph.getRelatedValue(annotationProperty, L0.HasName, Bindings.STRING);\r
497                 \r
498                 Resource value = graph.newResource();\r
499                 graph.claim(value, L0.InstanceOf, annotationProperty);\r
500                 if(name == null)\r
501                         name = NameUtils.findFreshName(graph, proposition + " value", container);\r
502                 graph.addLiteral(value, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
503 \r
504                 graph.claim(container, L0.ConsistsOf, value);\r
505                 \r
506                 if (addCommentMetadata) {\r
507                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
508                         graph.addMetadata(cm.add("Added new annotationValue named " + name + ", resource " + value));\r
509                 }\r
510                 \r
511                 return value;\r
512                 \r
513         } else {\r
514 \r
515             String propertyName = graph.getRelatedValue(annotationProperty, L0.HasName, Bindings.STRING);\r
516             \r
517             Resource valueType = graph.getSingleObject(annotationProperty, L0.HasRange);\r
518             Resource value = graph.newResource();\r
519             graph.claim(value, L0.InstanceOf, valueType);\r
520                 if(name == null)\r
521                         name = NameUtils.findFreshName(graph, propertyName + " value", container);\r
522             graph.addLiteral(value, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
523 \r
524             graph.claim(container, L0.ConsistsOf, value);\r
525             \r
526             if (addCommentMetadata) {\r
527                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
528                 graph.addMetadata(cm.add("Added new annotationValue named " + name + ", resource " + value));\r
529             }\r
530 \r
531             return value;\r
532                 \r
533         }\r
534         \r
535     }\r
536 \r
537         /**\r
538          * @param map\r
539          * @param selectionCallback\r
540          */\r
541         public static void queryUserSelectedAnnotationType(\r
542                         Map<Resource, Pair<String, ImageDescriptor>> map,\r
543                         Consumer<Resource> selectionCallback)\r
544         {\r
545                 Display.getDefault().asyncExec(() -> {\r
546                         Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
547                         ResourceSelectionDialog3<Resource> dialog = new ResourceSelectionDialog3<Resource>(shell, map, "Select annotation type from list") {\r
548                                 @Override\r
549                                 protected IDialogSettings getBaseDialogSettings() {\r
550                                         return Activator.getDefault().getDialogSettings();\r
551                                 }\r
552                         };\r
553                         if (dialog.open() == Window.OK) {\r
554                                 Object[] result = dialog.getResult();\r
555                                 if (result != null && result.length == 1) {\r
556                                         selectionCallback.accept((Resource) result[0]);\r
557                                 }\r
558                         }\r
559                 });\r
560         }\r
561 \r
562         public static void queryLibrary(\r
563                         Map<Resource, Pair<String, ImageDescriptor>> map,\r
564                         Consumer<Pair<Resource,String>> selectionCallback)\r
565         {\r
566                 Display.getDefault().asyncExec(() -> {\r
567                         Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
568                         SaveAnnotationDialog page = new SaveAnnotationDialog(shell, map, "Select library");\r
569                         if (page.open() == Window.OK) {\r
570                                 Object[] result = page.getResult();\r
571                                 if (result != null && result.length == 1) {\r
572                                         selectionCallback.accept(Pair.make((Resource)result[0], page.getName()));\r
573                                 }\r
574                         }\r
575                 });\r
576         }\r
577         \r
578 }\r