]> gerrit.simantics Code Review - simantics/platform.git/blob
03b256b28094f23098bb1b2f9f069f3787630ed9
[simantics/platform.git] /
1 package org.simantics.document.ui.graphfile;
2
3 import java.io.File;
4
5 import org.eclipse.swt.widgets.Display;
6 import org.eclipse.ui.PartInitException;
7 import org.simantics.db.ReadGraph;
8 import org.simantics.db.Resource;
9 import org.simantics.db.common.request.ReadRequest;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.document.DocumentResource;
12 import org.simantics.document.ui.Activator;
13 import org.simantics.editors.Editors;
14 import org.simantics.graphfile.ontology.GraphFileResource;
15 import org.simantics.graphfile.util.GraphFileUtil;
16 import org.simantics.ui.SimanticsUI;
17 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
18 import org.simantics.ui.workbench.editor.EditorAdapter;
19 import org.simantics.utils.ui.ExceptionUtils;
20
21 public class ExternalEditorAdapter extends AbstractResourceEditorAdapter implements EditorAdapter {
22         
23         public ExternalEditorAdapter() {
24                 super("External Editor",Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/page.png"));
25         }
26
27         
28         @Override
29         public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
30                 GraphFileResource gf = GraphFileResource.getInstance(g);
31                 DocumentResource doc = DocumentResource.getInstance(g);
32                 if (!g.isInstanceOf(r, doc.FileDocument))
33                         return false;
34                 String filename = g.getPossibleRelatedValue(r, gf.HasResourceName);
35                 return filename != null;
36         }
37         
38         @Override
39         protected void openEditor(final Resource input) throws Exception {
40                 SimanticsUI.getSession().asyncRequest(new ReadRequest() {
41                         
42                         @Override
43                         public void run(ReadGraph graph) throws DatabaseException {
44                                 final File file = GraphFileUtil.toTempFile(graph, input);
45                                 file.deleteOnExit();
46                                 Display.getDefault().asyncExec(new Runnable() {
47                                         
48                                         @Override
49                                         public void run() {
50                                                 
51                                                 if (file.exists() && file.canRead()) {
52                                                         try {
53                                                                 Editors.openExternalEditor(file);
54                                                         } catch (PartInitException e) {
55                                                                 ExceptionUtils.logAndShowError(e);
56                                                         }
57                                                 }
58                                         }
59                                 });
60                         }
61                 });
62                 
63
64         }
65
66 }