]> gerrit.simantics Code Review - simantics/platform.git/blob
be2d5e33ead0bd51d851f8a938f6f4f063277f9c
[simantics/platform.git] /
1 package org.simantics.document.linking.ge;
2
3 import java.util.Arrays;
4 import java.util.Comparator;
5
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.Simantics;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.common.request.ReadRequest;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.db.layer0.adapter.ActionFactory;
21 import org.simantics.db.layer0.variable.Variable;
22 import org.simantics.document.linking.ontology.DocumentLink;
23 import org.simantics.document.linking.utils.SourceLinkUtil;
24 import org.simantics.ui.workbench.editor.EditorAdapter;
25 import org.simantics.ui.workbench.editor.EditorRegistry;
26 import org.simantics.utils.ui.ExceptionUtils;
27
28 public class ShowDocumentWithAction implements ActionFactory{
29         
30         
31         @Override
32         public Runnable create(Object target) {
33                 if (!(target instanceof Variable))
34                         return null;
35                 final Variable v = (Variable)target;
36                 
37
38                 return new Runnable() {
39                         
40                         @Override
41                         public void run() {
42                                 Simantics.getSession().asyncRequest(new ReadRequest() {
43                                         
44                                         @Override
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))
49                                                         return;
50                                                 final Resource reference = SourceLinkUtil.getReferredDocument(graph, t);
51                                                 if (reference == null)
52                                                         return;
53                                                 final EditorAdapter[] adapters = EditorRegistry.getInstance().getAdaptersFor(graph, reference);
54                                                 if (adapters.length == 0)
55                                                         return;
56                                                 Arrays.sort(adapters, new Comparator<EditorAdapter>() {
57                                                         @Override
58                                                         public int compare(EditorAdapter o1, EditorAdapter o2) {
59                                                                 return o1.getPriority() - o2.getPriority();
60                                                         }
61                                                 });
62                                                 
63                                                 Display.getDefault().asyncExec(new Runnable() {
64                                                         
65                                                         @Override
66                                                         public void run() {
67                                                                 try {
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)
74                                                                                 return;
75                                                                         EditorAdapter adapter = (EditorAdapter)dialog.getResult()[0];
76                                                                         
77                                                                         adapter.openEditor(reference);
78                                                                 } catch (Exception e) {
79                                                                         ExceptionUtils.logAndShowError("Cannot open editor", e);
80                                                                 }
81                                                         }
82                                                 });
83
84                                                 
85                                         }
86                                 });
87                                 
88                         }
89                 };
90
91                 
92         }
93         
94         private static class ArrayContentProvider implements IStructuredContentProvider {
95                 public Object[] getElements(Object inputElement) {
96                         return (Object[])inputElement;
97                 };
98                 
99                 @Override
100                 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
101                         
102                 }
103                 @Override
104                 public void dispose() {
105                         
106                 }
107         }
108         
109         private static class EditorAdapterLabelProvider extends BaseLabelProvider implements ILabelProvider {
110                 private LocalResourceManager localResourceManager;
111                 
112                 public EditorAdapterLabelProvider() {
113                         localResourceManager = new LocalResourceManager(JFaceResources.getResources());
114                 }
115                 @Override
116                 public Image getImage(Object element) {
117                         EditorAdapter adapter = (EditorAdapter)element;
118                         return localResourceManager.createImage(adapter.getIcon());
119                 }
120                 
121                 @Override
122                 public String getText(Object element) {
123                         EditorAdapter adapter = (EditorAdapter)element;
124                         return adapter.getName();
125                 }
126                 
127                 @Override
128                 public void dispose() {
129                         localResourceManager.dispose();
130                         super.dispose();
131                 }
132         }
133
134 }