1 package org.simantics.document.linking.ge;
3 import java.util.Arrays;
4 import java.util.Comparator;
6 import org.eclipse.jface.resource.JFaceResources;
7 import org.eclipse.jface.resource.LocalResourceManager;
8 import org.eclipse.jface.viewers.BaseLabelProvider;
9 import org.eclipse.jface.viewers.ILabelProvider;
10 import org.eclipse.jface.viewers.IStructuredContentProvider;
11 import org.eclipse.jface.viewers.Viewer;
12 import org.eclipse.swt.graphics.Image;
13 import org.eclipse.swt.widgets.Display;
14 import org.eclipse.ui.dialogs.ListDialog;
15 import org.simantics.db.ReadGraph;
16 import org.simantics.db.Resource;
17 import org.simantics.db.common.request.ReadRequest;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.layer0.adapter.ActionFactory;
20 import org.simantics.db.layer0.variable.Variable;
21 import org.simantics.document.linking.ontology.DocumentLink;
22 import org.simantics.document.linking.utils.SourceLinkUtil;
23 import org.simantics.ui.SimanticsUI;
24 import org.simantics.ui.workbench.editor.EditorAdapter;
25 import org.simantics.ui.workbench.editor.EditorRegistry;
26 import org.simantics.utils.ui.ExceptionUtils;
28 public class ShowDocumentWithAction implements ActionFactory{
32 public Runnable create(Object target) {
33 if (!(target instanceof Variable))
35 final Variable v = (Variable)target;
38 return new Runnable() {
42 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
45 public void run(ReadGraph graph) throws DatabaseException {
46 DocumentLink sl = DocumentLink.getInstance(graph);
47 Resource t = v.getPossibleRepresents(graph);
48 if (!graph.isInstanceOf(t, sl.Source))
50 final Resource reference = SourceLinkUtil.getReferredDocument(graph, t);
51 if (reference == null)
53 final EditorAdapter[] adapters = EditorRegistry.getInstance().getAdaptersFor(graph, reference);
54 if (adapters.length == 0)
56 Arrays.sort(adapters, new Comparator<EditorAdapter>() {
58 public int compare(EditorAdapter o1, EditorAdapter o2) {
59 return o1.getPriority() - o2.getPriority();
63 Display.getDefault().asyncExec(new Runnable() {
68 ListDialog dialog = new ListDialog(Display.getCurrent().getActiveShell());
69 dialog.setContentProvider(new ArrayContentProvider());
70 dialog.setLabelProvider(new EditorAdapterLabelProvider());
71 dialog.setInput(adapters);
72 dialog.setTitle("Open with");
73 if (dialog.open() != ListDialog.OK)
75 EditorAdapter adapter = (EditorAdapter)dialog.getResult()[0];
77 adapter.openEditor(reference);
78 } catch (Exception e) {
79 ExceptionUtils.logAndShowError("Cannot open editor", e);
94 private static class ArrayContentProvider implements IStructuredContentProvider {
95 public Object[] getElements(Object inputElement) {
96 return (Object[])inputElement;
100 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
104 public void dispose() {
109 private static class EditorAdapterLabelProvider extends BaseLabelProvider implements ILabelProvider {
110 private LocalResourceManager localResourceManager;
112 public EditorAdapterLabelProvider() {
113 localResourceManager = new LocalResourceManager(JFaceResources.getResources());
116 public Image getImage(Object element) {
117 EditorAdapter adapter = (EditorAdapter)element;
118 return localResourceManager.createImage(adapter.getIcon());
122 public String getText(Object element) {
123 EditorAdapter adapter = (EditorAdapter)element;
124 return adapter.getName();
128 public void dispose() {
129 localResourceManager.dispose();