1 package org.simantics.modeling.ui.documents;
4 import java.io.IOException;
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;
31 * @author Tuukka Lehtonen
33 public class OpenPlainTextDocumentAdapter extends AbstractResourceEditorAdapter {
35 public OpenPlainTextDocumentAdapter() {
36 super("Text Editor", Activator.COMPOSITE_ICON);
40 public boolean canHandle(ReadGraph g, Resource r) throws DatabaseException {
41 DocumentResource DOC = DocumentResource.getInstance(g);
42 return g.isInstanceOf(r, DOC.PlainTextDocument);
46 public void openEditor(final Resource r) throws Exception {
47 Simantics.getSession().asyncRequest(new ReadRequest() {
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)
55 final String text = g.getPossibleRelatedValue(r, DOC.PlainTextDocument_text, Bindings.STRING);
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);
65 final File tempFile = new File(stateDir, FileUtils.escapeFileName(documentName) + ".txt");
66 FileUtils.writeFile(tempFile, text.getBytes("US-ASCII"));
68 PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
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() {
76 public void partOpened(IWorkbenchPart part) {
79 public void partDeactivated(IWorkbenchPart part) {
82 public void partClosed(IWorkbenchPart part) {
84 page.removePartListener(this);
85 if (!PlatformUI.getWorkbench().isClosing()) {
87 FileUtils.deleteAll(stateDir);
88 } catch (IOException e) {
89 ErrorLogger.defaultLogError(e);
95 public void partBroughtToTop(IWorkbenchPart part) {
98 public void partActivated(IWorkbenchPart part) {
101 } catch (Exception e) {
102 ErrorLogger.defaultLogError(e);
106 } catch (IOException e1) {
107 ErrorLogger.defaultLogError(e1);