]> gerrit.simantics Code Review - simantics/platform.git/blob
5d9f939d164efd2128dd021f2e43233fa4acb5c7
[simantics/platform.git] /
1 package org.simantics.annotation.ui.actions;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6
7 import org.simantics.Simantics;
8 import org.simantics.annotation.ontology.AnnotationResource;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.WriteGraph;
12 import org.simantics.db.common.CommentMetadata;
13 import org.simantics.db.common.request.WriteRequest;
14 import org.simantics.db.common.utils.NameUtils;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.SelectionHints;
17 import org.simantics.db.layer0.adapter.DropActionFactory;
18 import org.simantics.db.layer0.request.PossibleModel;
19 import org.simantics.layer0.Layer0;
20 import org.simantics.utils.ObjectUtils;
21 import org.simantics.utils.datastructures.Callback;
22 import org.simantics.utils.ui.ErrorLogger;
23 import org.simantics.utils.ui.ISelectionUtils;
24
25 /**
26  * @author Jani Simomaa
27  */
28
29 public class AnnotationDropActionFactory implements DropActionFactory {
30
31         @Override
32         public Runnable create(ReadGraph graph, Object target, Object source, int operation)
33                         throws DatabaseException {
34
35                 final Resource targetFolder = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
36                 if (targetFolder == null) {
37                         return null;
38                 }
39                 Resource targetModel = graph.syncRequest(new PossibleModel(targetFolder));
40                 
41                 List<Resource> resources = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
42                 AnnotationResource ANNO = AnnotationResource.getInstance(graph);
43                 Set<Resource> movedItems = new HashSet<Resource>();
44                 for (Resource resource : resources) {
45                         if (graph.isInstanceOf(resource, ANNO.AnnotationType) || graph.isInstanceOf(resource, ANNO.Annotation)) {
46                                 Resource model = graph.syncRequest(new PossibleModel(resource));
47                                 if (ObjectUtils.objectEquals(targetModel, model)) {
48                                         movedItems.add(resource);
49                                 }
50                         }
51                 }
52                 if (!movedItems.isEmpty()) {
53                         return addAnnotations(targetFolder, movedItems);
54                 }
55                 
56                 return null;
57         }
58         
59         
60         private Runnable addAnnotations(final Resource targetFolder, final Set<Resource> movedAnnotationItems) {
61                 
62                 return new Runnable() {
63                         
64                         @Override
65                         public void run() {
66                                 Simantics.getSession().asyncRequest(new WriteRequest() {
67                                         
68                                         @Override
69                                         public void perform(WriteGraph graph) throws DatabaseException {
70                                                 
71                                                 Layer0 L0 = Layer0.getInstance(graph);
72                                                 for (Resource annotation : movedAnnotationItems) {
73                                                         graph.deny(annotation, L0.PartOf);
74                                                         graph.claim(targetFolder, L0.ConsistsOf, annotation);
75                                                 }
76                                                         
77                                                 String name = NameUtils.getSafeName(graph, targetFolder);
78                                                 CommentMetadata cm = graph.getMetadata(CommentMetadata.class);
79                                         graph.addMetadata(cm.add("Moved " + movedAnnotationItems.size() + " annotations to folder " + name + ", resource " + targetFolder));
80                                         }
81                                 }, new Callback<DatabaseException>() {
82                                         
83                                         @Override
84                                         public void run(DatabaseException e) {
85                                                 if (e!= null)
86                                                         ErrorLogger.defaultLogError(e);
87                                                 
88                                         }
89                                 });
90                                 
91                         }
92                 };
93         }
94         
95         
96 }