1 package org.simantics.annotation.ui.actions;
3 import java.util.HashSet;
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;
26 * @author Jani Simomaa
29 public class AnnotationDropActionFactory implements DropActionFactory {
32 public Runnable create(ReadGraph graph, Object target, Object source, int operation)
33 throws DatabaseException {
35 final Resource targetFolder = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
36 if (targetFolder == null) {
39 Resource targetModel = graph.syncRequest(new PossibleModel(targetFolder));
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);
52 if (!movedItems.isEmpty()) {
53 return addAnnotations(targetFolder, movedItems);
60 private Runnable addAnnotations(final Resource targetFolder, final Set<Resource> movedAnnotationItems) {
62 return new Runnable() {
66 Simantics.getSession().asyncRequest(new WriteRequest() {
69 public void perform(WriteGraph graph) throws DatabaseException {
71 Layer0 L0 = Layer0.getInstance(graph);
72 for (Resource annotation : movedAnnotationItems) {
73 graph.deny(annotation, L0.PartOf);
74 graph.claim(targetFolder, L0.ConsistsOf, annotation);
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));
81 }, new Callback<DatabaseException>() {
84 public void run(DatabaseException e) {
86 ErrorLogger.defaultLogError(e);