1 package org.simantics.document.linking.ge;
3 import org.eclipse.swt.widgets.Display;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.request.ReadRequest;
7 import org.simantics.db.exception.DatabaseException;
8 import org.simantics.db.layer0.adapter.ActionFactory;
9 import org.simantics.db.layer0.variable.Variable;
10 import org.simantics.document.linking.ontology.DocumentLink;
11 import org.simantics.document.linking.utils.SourceLinkUtil;
12 import org.simantics.ui.SimanticsUI;
13 import org.simantics.ui.workbench.editor.EditorAdapter;
14 import org.simantics.ui.workbench.editor.EditorRegistry;
15 import org.simantics.utils.ui.ExceptionUtils;
17 public class ShowDocumentAction implements ActionFactory{
21 public Runnable create(Object target) {
22 if (!(target instanceof Variable))
24 final Variable v = (Variable)target;
27 return new Runnable() {
31 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
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))
39 final Resource reference = SourceLinkUtil.getReferredDocument(graph, t);
40 if (reference == null)
42 EditorAdapter[] adapters = EditorRegistry.getInstance().getAdaptersFor(graph, reference);
43 if (adapters.length == 0)
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) {
54 final EditorAdapter adapter = highPri;
55 Display.getDefault().asyncExec(new Runnable() {
60 adapter.openEditor(reference);
61 } catch (Exception e) {
62 ExceptionUtils.logAndShowError("Cannot open editor", e);