]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/documents/OpenPlainTextDocumentAdapter.java
Improved Copy Visible Data usability in time series chart editor
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / documents / OpenPlainTextDocumentAdapter.java
1 package org.simantics.modeling.ui.documents;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.eclipse.core.runtime.IPath;
7 import org.eclipse.core.runtime.Platform;
8 import org.eclipse.ui.IEditorPart;
9 import org.eclipse.ui.IPartListener;
10 import org.eclipse.ui.IWorkbenchPage;
11 import org.eclipse.ui.IWorkbenchPart;
12 import org.eclipse.ui.PlatformUI;
13 import org.eclipse.ui.editors.text.EditorsUI;
14 import org.eclipse.ui.ide.IDE;
15 import org.simantics.Simantics;
16 import org.simantics.databoard.Bindings;
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19 import org.simantics.db.common.request.PossibleTypedParent;
20 import org.simantics.db.common.request.ReadRequest;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.document.DocumentResource;
23 import org.simantics.layer0.Layer0;
24 import org.simantics.modeling.ui.Activator;
25 import org.simantics.simulation.ontology.SimulationResource;
26 import org.simantics.ui.workbench.editor.AbstractResourceEditorAdapter;
27 import org.simantics.utils.FileUtils;
28 import org.simantics.utils.ui.ErrorLogger;
29
30 /**
31  * @author Tuukka Lehtonen
32  */
33 public class OpenPlainTextDocumentAdapter extends AbstractResourceEditorAdapter {
34
35     public OpenPlainTextDocumentAdapter() {
36         super("Text Editor", Activator.COMPOSITE_ICON);
37     }
38
39     @Override
40     public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
41         DocumentResource DOC = DocumentResource.getInstance(g);
42         return g.isInstanceOf(r, DOC.PlainTextDocument);
43     }
44
45     @Override
46     public void openEditor(final Resource r) throws Exception {
47         Simantics.getSession().asyncRequest(new ReadRequest() {
48             @Override
49             public void run(ReadGraph g) throws DatabaseException {
50                 Layer0 L0 = Layer0.getInstance(g);
51                 DocumentResource DOC = DocumentResource.getInstance(g);
52                 String documentName = g.getPossibleRelatedValue(r, L0.HasName);
53                 if (documentName == null)
54                     documentName = "";
55                 final String text = g.getPossibleRelatedValue(r, DOC.PlainTextDocument_text, Bindings.STRING);
56                 if (text == null)
57                     return;
58
59                 try {
60                         final Resource model = g.syncRequest(new PossibleTypedParent(r, SimulationResource.getInstance(g).Model));
61                         final String dir = model == null ? "" + r.getResourceId() : (String)(g.getRelatedValue(model, L0.HasName));
62                     IPath state = Platform.getStateLocation(Platform.getBundle(Activator.PLUGIN_ID));
63                     final File stateDir = new File(state.toFile(), dir);
64                     stateDir.mkdirs();
65                     final File tempFile = new File(stateDir, FileUtils.escapeFileName(documentName) + ".txt");
66                     FileUtils.writeFile(tempFile, text.getBytes("US-ASCII"));
67
68                     PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
69                         @Override
70                         public void run() {
71                             try {
72                                 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
73                                 final IEditorPart editor = IDE.openEditor(page, tempFile.toURI(), EditorsUI.DEFAULT_TEXT_EDITOR_ID, true);
74                                 page.addPartListener(new IPartListener() {
75                                     @Override
76                                     public void partOpened(IWorkbenchPart part) {
77                                     }
78                                     @Override
79                                     public void partDeactivated(IWorkbenchPart part) {
80                                     }
81                                     @Override
82                                     public void partClosed(IWorkbenchPart part) {
83                                         if (part == editor) {
84                                             page.removePartListener(this);
85                                             if (!PlatformUI.getWorkbench().isClosing()) {
86                                                 try {
87                                                     FileUtils.deleteAll(stateDir);
88                                                 } catch (IOException e) {
89                                                     ErrorLogger.defaultLogError(e);
90                                                 }
91                                             }
92                                         }
93                                     }
94                                     @Override
95                                     public void partBroughtToTop(IWorkbenchPart part) {
96                                     }
97                                     @Override
98                                     public void partActivated(IWorkbenchPart part) {
99                                     }
100                                 });
101                             } catch (Exception e) {
102                                 ErrorLogger.defaultLogError(e);
103                             }
104                         }
105                     });
106                 } catch (IOException e1) {
107                     ErrorLogger.defaultLogError(e1);
108                 }
109             }
110         });
111     }
112
113 }