]> gerrit.simantics Code Review - simantics/platform.git/blob
ec2da54a10f97597396a23747aecb1133d801f66
[simantics/platform.git] /
1 package org.simantics.document.linking.ge;
2
3 import org.eclipse.jface.dialogs.MessageDialog;
4 import org.eclipse.swt.widgets.Display;
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.common.utils.NameUtils;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.layer0.adapter.DropActionFactory;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.document.linking.ontology.DocumentLink;
15 import org.simantics.document.linking.utils.SourceLinkUtil;
16 import org.simantics.layer0.Layer0;
17 import org.simantics.utils.ui.AdaptionUtils;
18
19 public class SourceObjectDropAction implements DropActionFactory {
20         
21         
22         @Override
23         public Runnable create(ReadGraph g, Object target, Object source, int operation)
24                         throws DatabaseException {
25                 Variable t = (Variable)target;
26         Resource s = AdaptionUtils.adaptToSingle(source, Resource.class);
27         if (s == null) {
28                 Variable sv = AdaptionUtils.adaptToSingle(source, Variable.class);
29                 if (sv != null)
30                         s = sv.getRepresents(g);
31         }
32
33         if(t == null || s == null)
34             return null;
35         DocumentLink sl = DocumentLink.getInstance(g);
36         if (g.isInstanceOf(s, sl.Source)) {
37                 return null;
38         }
39                 return getRunnable(t, s);
40         }
41         
42         private Runnable getRunnable(final Variable tv, final Resource s) {
43                 return new Runnable() {
44                         
45                         @Override
46                         public void run() {
47                                 Simantics.getSession().asyncRequest(new WriteRequest() {
48                                         
49                                         @Override
50                                         public void perform(WriteGraph graph) throws DatabaseException {
51                                                 Layer0 l0 = Layer0.getInstance(graph);
52                                                 
53                                                 Resource relation = tv.getPossiblePredicateResource(graph);
54                                                 Resource instance; 
55                                                 if (relation != null && graph.isInstanceOf(relation, l0.FunctionalRelation)) {
56                                                         instance = tv.getParent(graph).getPossibleRepresents(graph);
57                                                 } else {
58                                                         instance = tv.getPossibleRepresents(graph);
59                                                 }
60                                                 if (instance == null)
61                                                         return;
62                                                 
63                                                 try {
64                                                         SourceLinkUtil.createDocumentLink(graph, instance, relation, s);
65                                                 } catch (final DatabaseException e) {
66                                                         String l = NameUtils.getSafeLabel(graph, s);
67                                                         if (l.length() == 0)
68                                                                 l = NameUtils.getSafeName(graph, s);
69                                                         final String label = l;
70                                                         Display.getDefault().asyncExec(new Runnable() {
71                                                                 
72                                                                 @Override
73                                                                 public void run() {
74                                                                         String dialogMessage = "Cannot create document link to \"" + label +"\":" + e.getMessage();
75                                                                         MessageDialog dialog = new MessageDialog(Display.getCurrent().getActiveShell(), "Document Link", null, dialogMessage, MessageDialog.INFORMATION, new String[]{"OK"}, 0); 
76                                                                         dialog.open();
77                                                                 }
78                                                         });
79                                                 }
80                                         }
81                                 });
82                                 
83                         }
84                 };
85                 
86         }
87         
88         
89         
90         
91 }