]> gerrit.simantics Code Review - simantics/platform.git/blob
47d59bc604d3df5871edeac4def7bfe41e0d0746
[simantics/platform.git] /
1 package org.simantics.document.linking.ge;
2
3 import org.eclipse.swt.widgets.Display;
4 import org.simantics.Simantics;
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.Resource;
7 import org.simantics.db.common.request.ReadRequest;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.adapter.ActionFactory;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.document.linking.ontology.DocumentLink;
12 import org.simantics.document.linking.utils.SourceLinkUtil;
13 import org.simantics.ui.workbench.editor.EditorAdapter;
14 import org.simantics.ui.workbench.editor.EditorRegistry;
15 import org.simantics.utils.ui.ExceptionUtils;
16
17 public class ShowDocumentAction implements ActionFactory{
18         
19         
20         @Override
21         public Runnable create(Object target) {
22                 if (!(target instanceof Variable))
23                         return null;
24                 final Variable v = (Variable)target;
25                 
26
27                 return new Runnable() {
28                         
29                         @Override
30                         public void run() {
31                                 Simantics.getSession().asyncRequest(new ReadRequest() {
32                                         
33                                         @Override
34                                         public void run(ReadGraph graph) throws DatabaseException {
35                                                 DocumentLink sl = DocumentLink.getInstance(graph);
36                                                 Resource t = v.getPossibleRepresents(graph);
37                                                 if (!graph.isInstanceOf(t, sl.Source))
38                                                         return;
39                                                 final Resource reference = SourceLinkUtil.getReferredDocument(graph, t);
40                                                 if (reference == null)
41                                                         return;
42                                                 EditorAdapter[] adapters = EditorRegistry.getInstance().getAdaptersFor(graph, reference);
43                                                 if (adapters.length == 0)
44                                                         return;
45                                                 EditorAdapter highPri = null;
46                                                 int pri = Integer.MIN_VALUE;
47                                                 for (EditorAdapter a : adapters) {
48                                                         int p = a.getPriority();
49                                                         if (highPri == null || p > pri) {
50                                                                 highPri = a;
51                                                                 pri = p;
52                                                         } 
53                                                 }
54                                                 final EditorAdapter adapter = highPri;
55                                                 Display.getDefault().asyncExec(new Runnable() {
56                                                         
57                                                         @Override
58                                                         public void run() {
59                                                                 try {
60                                                                         adapter.openEditor(reference);
61                                                                 } catch (Exception e) {
62                                                                         ExceptionUtils.logAndShowError("Cannot open editor", e);
63                                                                 }
64                                                         }
65                                                 });
66
67                                                 
68                                         }
69                                 });
70                                 
71                         }
72                 };
73
74                 
75         }
76
77 }