]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/AnnotationUtils.java
3bc50dae9b5fed14f871136a05f4f425d34e7fb9
[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, final Resource parent, Resource model) throws DatabaseException {\r
105 \r
106         final Map<Resource, Pair<String, ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>();\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, final Variable position, Resource model) throws DatabaseException {\r
134 \r
135         final Map<Resource, Pair<String, ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>();\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(final Resource parent, final 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(final 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(final 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(final Resource parent, final 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                 \r
206                 final Map<Resource, Pair<String, ImageDescriptor>> map = new HashMap<Resource, Pair<String,ImageDescriptor>>();\r
207                 findAnnotationTypes(graph, model, map);\r
208                 queryUserSelectedAnnotationType(map, selected -> {\r
209                     Simantics.getSession().async(new WriteRequest() {\r
210                         @Override\r
211                         public void perform(WriteGraph g) throws DatabaseException {\r
212                             g.markUndoPoint();\r
213                             newAnnotationInstance(g, parent, selected);\r
214                         }\r
215                     });\r
216                 });\r
217             }\r
218         });\r
219     }\r
220 \r
221         /**\r
222          * @param graph\r
223          * @param model\r
224          * @return\r
225          * @throws DatabaseException \r
226          */\r
227         protected static void findAnnotationTypes(ReadGraph graph, Resource model, Map<Resource, Pair<String, ImageDescriptor>> map) throws DatabaseException {\r
228 \r
229                 Layer0 L0 = Layer0.getInstance(graph);\r
230                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
231 \r
232                 Instances query = graph.adapt(ANNO.AnnotationType, Instances.class);\r
233 \r
234                 String modelURI = graph.getURI(model);\r
235                 \r
236         ImageDescriptor descriptor = graph.adapt(ANNO.Images_AnnotationType, ImageDescriptor.class);\r
237 \r
238                 for(Resource _res : query.find(graph, model)) {\r
239                         // Don't allow instantiation of abstract annotation types.\r
240                         if (graph.hasStatement(_res, L0.Abstract))\r
241                                 continue;\r
242 \r
243                         Resource res = graph.getPossibleObject(_res, L0.HasRange_Inverse);\r
244                         if(res == null) {\r
245                                 \r
246                                 // Entry type\r
247 \r
248                                 String name = graph.getPossibleRelatedValue(_res, L0.HasName, Bindings.STRING);\r
249                                 if (name == null)\r
250                                         continue;\r
251                                 String label = graph.getPossibleRelatedValue2(_res, L0.HasLabel, Bindings.STRING);\r
252 \r
253                                 if (label != null && !name.equals(label)) {\r
254                                         name = label + " (" + name + ")";\r
255                                 }\r
256 \r
257                                 Resource parent = graph.getPossibleObject(_res, L0.PartOf);\r
258                                 if(parent == null) continue;\r
259 \r
260                                 String parentURI = graph.getURI(parent);\r
261                                 if(parentURI.startsWith(modelURI)) {\r
262                                         parentURI = parentURI.substring(modelURI.length());\r
263                                         if(parentURI.startsWith("/")) parentURI = parentURI.substring(1);\r
264                                 }\r
265 \r
266                                 name = name + " - " + URIStringUtils.unescape(parentURI);\r
267 \r
268                                 map.put(_res, new Pair<String, ImageDescriptor>(name, descriptor));\r
269                                 \r
270                         } else {\r
271                                 \r
272                                 // Property type\r
273                         \r
274                                 String name = graph.getPossibleRelatedValue(res, L0.HasName, Bindings.STRING);\r
275                                 if (name == null)\r
276                                         continue;\r
277                                 String label = graph.getPossibleRelatedValue2(res, L0.HasLabel, Bindings.STRING);\r
278 \r
279                                 if (label != null && !name.equals(label)) {\r
280                                         name = label + " (" + name + ")";\r
281                                 }\r
282 \r
283                                 Resource parent = graph.getPossibleObject(_res, L0.PartOf);\r
284                                 if(parent == null) continue;\r
285 \r
286                                 String parentURI = graph.getURI(parent);\r
287                                 if(parentURI.startsWith(modelURI)) {\r
288                                         parentURI = parentURI.substring(modelURI.length());\r
289                                         if(parentURI.startsWith("/")) parentURI = parentURI.substring(1);\r
290                                 }\r
291 \r
292                                 name = name + " - " + URIStringUtils.unescape(parentURI);\r
293 \r
294                                 map.put(_res, new Pair<String, ImageDescriptor>(name, descriptor));\r
295                         \r
296                         }\r
297                         \r
298                 }\r
299 \r
300         }\r
301 \r
302     protected static void findAnnotations(ReadGraph graph, Resource model, Map<Resource, Pair<String, ImageDescriptor>> map) throws DatabaseException {\r
303 \r
304         Layer0 L0 = Layer0.getInstance(graph);\r
305         AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
306 \r
307         Instances query = graph.adapt(ANNO.Annotation, Instances.class);\r
308         \r
309         String modelURI = graph.getURI(model);\r
310         \r
311         ImageDescriptor descriptor = graph.adapt(ANNO.Images_Annotation, ImageDescriptor.class);\r
312 \r
313         for(Resource _res : query.find(graph, model)) {\r
314             // Don't allow instantiation of abstract annotation types.\r
315 \r
316             String name = graph.getPossibleRelatedValue(_res, L0.HasName, Bindings.STRING);\r
317             if (name == null)\r
318                 continue;\r
319             String label = graph.getPossibleRelatedValue2(_res, L0.HasLabel, Bindings.STRING);\r
320             if (label != null && !name.equals(label)) {\r
321                 name = label + " (" + name + ")";\r
322             }\r
323 \r
324             Resource parent = graph.getPossibleObject(_res, L0.PartOf);\r
325             if(parent == null) continue;\r
326 \r
327             String parentURI = graph.getPossibleURI(parent);\r
328             if(parentURI == null) continue;\r
329              \r
330             if(parentURI.startsWith(modelURI)) {\r
331                 parentURI = parentURI.substring(modelURI.length());\r
332                 if(parentURI.startsWith("/")) parentURI = parentURI.substring(1);\r
333             }\r
334 \r
335             Resource type = graph.getPossibleType(_res, ANNO.Annotation);\r
336             if(type != null) {\r
337                 Resource relation = graph.getPossibleObject(type, L0.HasRange_Inverse);\r
338                 if(relation != null) {\r
339                     String rName = graph.getPossibleRelatedValue(relation, L0.HasName, Bindings.STRING);\r
340                     if(rName != null) {\r
341                         name = name + " - " + rName;\r
342                     }\r
343                 }\r
344             }\r
345             \r
346             name = name + " - " + URIStringUtils.unescape(parentURI);\r
347             \r
348             map.put(_res, new Pair<String, ImageDescriptor>(name, descriptor));\r
349             \r
350         }\r
351 \r
352     }\r
353 \r
354     protected static boolean isAnnotation(Variable variable) {\r
355         if (variable == null)\r
356                 return false;\r
357         try {\r
358                 return Simantics.sync(new VariableRead<Boolean>(variable) {\r
359 \r
360                         @Override\r
361                         public Boolean perform(ReadGraph graph) throws DatabaseException {\r
362                                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
363                                 Resource represents = variable.getPossibleRepresents(graph);\r
364                                 if(represents == null) return false;\r
365                                 return graph.isInstanceOf(represents, ANNO.Annotation);\r
366                         }\r
367 \r
368                 });\r
369         } catch (DatabaseException e) {\r
370                 return false;\r
371         }\r
372     }\r
373     \r
374     protected static Map<Resource, Pair<String, ImageDescriptor>> findLibraries(Variable variable) {\r
375 \r
376         try {\r
377 \r
378                 return Simantics.sync(new VariableRead<Map<Resource, Pair<String, ImageDescriptor>>>(variable) {\r
379 \r
380                         @Override\r
381                         public Map<Resource, Pair<String, ImageDescriptor>> perform(ReadGraph graph) throws DatabaseException {\r
382 \r
383                                 Map<Resource, Pair<String, ImageDescriptor>> result = new HashMap<Resource, Pair<String,ImageDescriptor>>();\r
384 \r
385                                 Layer0 L0 = Layer0.getInstance(graph);\r
386                                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
387 \r
388                                 Resource model = Variables.getModel(graph, variable);\r
389                                 Instances query = graph.adapt(L0.Library, Instances.class);\r
390 \r
391                                 String modelURI = graph.getURI(model);\r
392                                 int modelPos = modelURI.length();\r
393 \r
394                                 ImageDescriptor descriptor = graph.adapt(ANNO.Images_Annotation, ImageDescriptor.class);\r
395 \r
396                                 for(Resource lib : query.find(graph, model)) {\r
397 \r
398                                         String path = graph.getURI(lib);\r
399                                         if(!path.startsWith(modelURI)) continue;\r
400                                         String suffix = URIStringUtils.unescape(path.substring(modelPos));\r
401                                         if(suffix.startsWith("/")) suffix = suffix.substring(1);\r
402                                         result.put(lib, new Pair<String, ImageDescriptor>(suffix, descriptor));\r
403 \r
404                                 }\r
405 \r
406                                 return result;\r
407                                 \r
408                         }\r
409                 });\r
410 \r
411         } catch (DatabaseException e) {\r
412                 Logger.defaultLogError(e);\r
413         }\r
414         \r
415         return null;\r
416 \r
417     }\r
418 \r
419     public static Resource newAnnotation(WriteGraph graph, Resource container, Resource valueOrProperty) throws DatabaseException {\r
420             graph.markUndoPoint();\r
421                 Layer0 L0 = Layer0.getInstance(graph);\r
422                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
423 \r
424                 if(graph.isInstanceOf(valueOrProperty, ANNO.Annotation)) {\r
425 \r
426                     Resource type = graph.getPossibleType(valueOrProperty, ANNO.Annotation);\r
427                     if(type == null) return null;\r
428                     Resource property = graph.getPossibleObject(type, L0.HasRange_Inverse);\r
429             if(property == null) {\r
430                 graph.claim(container, ANNO.Annotation_HasEntry, valueOrProperty);\r
431             } else {\r
432                 graph.deny(container, property);\r
433                 graph.claim(container, property, valueOrProperty);\r
434             }\r
435             Layer0Utils.addCommentMetadata(graph, "Created new annotation value/property " + valueOrProperty + " to " + graph.getRelatedValue2(container, L0.HasName, Bindings.STRING));\r
436                 return valueOrProperty;\r
437             \r
438                 } else if (graph.isInstanceOf(valueOrProperty, ANNO.AnnotationType)) {\r
439                     \r
440                         Resource predicate = graph.getPossibleObject(valueOrProperty, L0.HasRange_Inverse);\r
441                         if(predicate != null) {\r
442                             Resource value = graph.newResource();\r
443                             graph.claim(value, L0.InstanceOf, valueOrProperty);\r
444                             graph.deny(container, predicate);\r
445                             graph.claim(container, predicate, value);\r
446                             Layer0Utils.addCommentMetadata(graph, "Created new annotation type " + value + " to " + graph.getRelatedValue2(container, L0.HasName, Bindings.STRING));\r
447                             return value;\r
448                         } else {\r
449                             Resource value = graph.newResource();\r
450                             graph.claim(value, L0.InstanceOf, valueOrProperty);\r
451                             String name = NameUtils.findFreshEscapedName(graph, "Value", container, ANNO.Annotation_HasEntry);\r
452                             graph.addLiteral(value, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
453                             graph.claim(container, ANNO.Annotation_HasEntry, value);\r
454                             Layer0Utils.addCommentMetadata(graph, "Created new annotation entry " + value + " to " + graph.getRelatedValue2(container, L0.HasName, Bindings.STRING));\r
455                             return value;\r
456                         }\r
457                         \r
458                 } else {\r
459                 \r
460                     Resource valueType = graph.getSingleObject(valueOrProperty, L0.HasRange);\r
461                     Resource value = graph.newResource();\r
462                     graph.claim(value, L0.InstanceOf, valueType);\r
463                     graph.deny(container, valueOrProperty);\r
464                     graph.claim(container, valueOrProperty, value);\r
465                     return value;\r
466                 \r
467                 }\r
468                 \r
469         }\r
470 \r
471     public static Resource newAnnotationInstance(WriteGraph graph, Resource container, Resource annotationProperty) throws DatabaseException {\r
472         return newAnnotationInstance(graph, container, null, annotationProperty);\r
473     }\r
474 \r
475     public static Resource newAnnotationInstance(WriteGraph graph, Resource container, String name, Resource annotationProperty) throws DatabaseException {\r
476         return newAnnotationInstance(graph, container, name, annotationProperty, true);\r
477     }\r
478     \r
479     public static Resource newAnnotationInstance(WriteGraph graph, Resource container, String name, Resource annotationProperty, boolean addCommentMetadata) throws DatabaseException {\r
480         //graph.markUndoPoint();\r
481         Layer0 L0 = Layer0.getInstance(graph);\r
482         AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
483 \r
484         if(graph.isInstanceOf(annotationProperty, ANNO.AnnotationType)) {\r
485 \r
486                 Resource predicate = graph.getPossibleObject(annotationProperty, L0.HasRange_Inverse);\r
487 \r
488                 String proposition = predicate != null ?  (String)graph.getRelatedValue(predicate, L0.HasName, Bindings.STRING) :\r
489                         (String)graph.getRelatedValue(annotationProperty, L0.HasName, Bindings.STRING);\r
490                 \r
491                 Resource value = graph.newResource();\r
492                 graph.claim(value, L0.InstanceOf, annotationProperty);\r
493                 if(name == null)\r
494                         name = NameUtils.findFreshName(graph, proposition + " value", container);\r
495                 graph.addLiteral(value, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
496 \r
497                 graph.claim(container, L0.ConsistsOf, value);\r
498                 \r
499                 if (addCommentMetadata) {\r
500                         CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
501                         graph.addMetadata(cm.add("Added new annotationValue named " + name + ", resource " + value));\r
502                 }\r
503                 \r
504                 return value;\r
505                 \r
506         } else {\r
507 \r
508             String propertyName = graph.getRelatedValue(annotationProperty, L0.HasName, Bindings.STRING);\r
509             \r
510             Resource valueType = graph.getSingleObject(annotationProperty, L0.HasRange);\r
511             Resource value = graph.newResource();\r
512             graph.claim(value, L0.InstanceOf, valueType);\r
513                 if(name == null)\r
514                         name = NameUtils.findFreshName(graph, propertyName + " value", container);\r
515             graph.addLiteral(value, L0.HasName, L0.NameOf, L0.String, name, Bindings.STRING);\r
516 \r
517             graph.claim(container, L0.ConsistsOf, value);\r
518             \r
519             if (addCommentMetadata) {\r
520                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);\r
521                 graph.addMetadata(cm.add("Added new annotationValue named " + name + ", resource " + value));\r
522             }\r
523 \r
524             return value;\r
525                 \r
526         }\r
527         \r
528     }\r
529 \r
530         /**\r
531          * @param map\r
532          * @param selectionCallback\r
533          */\r
534         public static void queryUserSelectedAnnotationType(\r
535                         final Map<Resource, Pair<String, ImageDescriptor>> map,\r
536                         final Consumer<Resource> selectionCallback)\r
537         {\r
538                 Display.getDefault().asyncExec(new Runnable() {\r
539                         @Override\r
540                         public void run() {\r
541                                 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
542                                 ResourceSelectionDialog3<Resource> dialog = new ResourceSelectionDialog3<Resource>(shell, map, "Select annotation type from list") {\r
543                                         @Override\r
544                                         protected IDialogSettings getBaseDialogSettings() {\r
545                                                 return Activator.getDefault().getDialogSettings();\r
546                                         }\r
547                                 };\r
548                                 if (dialog.open() == Window.OK) {\r
549                                         Object[] result = dialog.getResult();\r
550                                         if (result != null && result.length == 1) {\r
551                                                 final Resource res = (Resource)result[0];\r
552                                                 selectionCallback.accept(res);\r
553                                         }\r
554                                 }\r
555                         }\r
556                 });\r
557         }\r
558 \r
559         public static void queryLibrary(\r
560                         final Map<Resource, Pair<String, ImageDescriptor>> map,\r
561                         final Consumer<Pair<Resource,String>> selectionCallback)\r
562         {\r
563                 Display.getDefault().asyncExec(new Runnable() {\r
564                         @Override\r
565                         public void run() {\r
566                                 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();\r
567                                 SaveAnnotationDialog page = new SaveAnnotationDialog(shell, map, "Select library");\r
568                                 if (page.open() == Window.OK) {\r
569                                         Object[] result = page.getResult();\r
570                                         if (result != null && result.length == 1) {\r
571                                                 final Resource res = (Resource)result[0];\r
572                                                 selectionCallback.accept(Pair.make(res, page.getName()));\r
573                                         }\r
574                                 }\r
575                         }\r
576                 });\r
577         }\r
578         \r
579 }