]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.ui/src/org/simantics/document/ui/actions/FolderDropAction.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.document.ui / src / org / simantics / document / ui / actions / FolderDropAction.java
1 package org.simantics.document.ui.actions;
2
3 import java.util.List;
4
5 import org.simantics.Simantics;
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.WriteRequest;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.SelectionHints;
12 import org.simantics.db.layer0.adapter.DropActionFactory;
13 import org.simantics.document.DocumentResource;
14 import org.simantics.utils.ui.ISelectionUtils;
15
16 public class FolderDropAction implements DropActionFactory {
17         
18         private Resource folderType;
19         private Resource relation;
20         private Resource inverse;
21         
22         public FolderDropAction(ReadGraph graph, String folderUri, String relationUri) throws DatabaseException {
23                 folderType = graph.getResource(folderUri);
24                 relation = graph.getResource(relationUri);
25                 inverse = graph.getPossibleInverse(relation);
26         }
27         
28         @Override
29         public Runnable create(ReadGraph g, Object target, Object source, int operation) throws DatabaseException {
30                 final Resource lib = ISelectionUtils.getSinglePossibleKey(target, SelectionHints.KEY_MAIN, Resource.class);
31
32                 if (lib == null)
33                         return null;
34
35                 final List<Resource> selections = ISelectionUtils.getPossibleKeys(source, SelectionHints.KEY_MAIN, Resource.class);
36
37                 if (selections.isEmpty()) {
38                         return null;
39                 }
40                 if (!g.isInstanceOf(lib, folderType))
41                         return null;
42                 DocumentResource doc = DocumentResource.getInstance(g);
43                 for (Resource r : selections) {
44                         if (!g.isInstanceOf(r, doc.Document))
45                                 return null;
46                 }
47                 Runnable runnable = new Runnable() {
48
49                         @Override
50                         public void run() {
51                                 Simantics.getSession().asyncRequest(new WriteRequest() {
52
53                                         @Override
54                                         public void perform(WriteGraph graph) throws DatabaseException {
55                                                 
56                                                 for (Resource r : selections) {
57                                                         if (!graph.hasStatement(lib,relation,r)) {
58                                                                 if (inverse != null) {
59                                                                         // check if moved document is children of another document (tree revisions)
60                                                                         // if it is, prevent moving it.
61                                                                         Resource r2 = graph.getPossibleObject(r, inverse);
62                                                                         if (r2 != null && !graph.isInstanceOf(r2, folderType))
63                                                                                 continue;
64                                                                         graph.deny(r,inverse);
65                                                                 }
66                                                                 graph.claim(lib, relation,r);
67                                                         }
68                                                 }
69                                         }
70                                 });
71                         }
72                 };
73                 return runnable;
74                 
75         }
76
77 }