1 package org.simantics.document.linking.ge;
3 import org.eclipse.jface.dialogs.MessageDialog;
4 import org.eclipse.swt.widgets.Display;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.WriteGraph;
8 import org.simantics.db.common.request.WriteRequest;
9 import org.simantics.db.common.utils.NameUtils;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.adapter.DropActionFactory;
12 import org.simantics.db.layer0.variable.Variable;
13 import org.simantics.document.linking.ontology.DocumentLink;
14 import org.simantics.document.linking.utils.SourceLinkUtil;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.ui.SimanticsUI;
17 import org.simantics.utils.ui.AdaptionUtils;
19 public class SourceObjectDropAction implements DropActionFactory {
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);
28 Variable sv = AdaptionUtils.adaptToSingle(source, Variable.class);
30 s = sv.getRepresents(g);
33 if(t == null || s == null)
35 DocumentLink sl = DocumentLink.getInstance(g);
36 if (g.isInstanceOf(s, sl.Source)) {
39 return getRunnable(t, s);
42 private Runnable getRunnable(final Variable tv, final Resource s) {
43 return new Runnable() {
47 SimanticsUI.getSession().asyncRequest(new WriteRequest() {
50 public void perform(WriteGraph graph) throws DatabaseException {
51 Layer0 l0 = Layer0.getInstance(graph);
53 Resource relation = tv.getPossiblePredicateResource(graph);
55 if (relation != null && graph.isInstanceOf(relation, l0.FunctionalRelation)) {
56 instance = tv.getParent(graph).getPossibleRepresents(graph);
58 instance = tv.getPossibleRepresents(graph);
64 SourceLinkUtil.createDocumentLink(graph, instance, relation, s);
65 } catch (final DatabaseException e) {
66 String l = NameUtils.getSafeLabel(graph, s);
68 l = NameUtils.getSafeName(graph, s);
69 final String label = l;
70 Display.getDefault().asyncExec(new Runnable() {
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);