]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphfile.ui/src/org/simantics/graphfile/ui/editor/PlainTextEditorDocumentProvider.java
Move some GraphFile UI from Simupedia to platform
[simantics/platform.git] / bundles / org.simantics.graphfile.ui / src / org / simantics / graphfile / ui / editor / PlainTextEditorDocumentProvider.java
1 package org.simantics.graphfile.ui.editor;
2
3 import java.io.StringWriter;
4 import java.nio.charset.StandardCharsets;
5
6 import org.eclipse.core.runtime.CoreException;
7 import org.eclipse.core.runtime.IProgressMonitor;
8 import org.eclipse.jface.operation.IRunnableContext;
9 import org.eclipse.jface.text.Document;
10 import org.eclipse.jface.text.IDocument;
11 import org.eclipse.jface.text.source.AnnotationModel;
12 import org.eclipse.jface.text.source.IAnnotationModel;
13 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
14 import org.simantics.Simantics;
15 import org.simantics.databoard.Bindings;
16 import org.simantics.db.ReadGraph;
17 import org.simantics.db.Resource;
18 import org.simantics.db.WriteGraph;
19 import org.simantics.db.common.request.UniqueRead;
20 import org.simantics.db.common.request.WriteRequest;
21 import org.simantics.db.common.utils.NameUtils;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.document.DocumentResource;
24 import org.simantics.graphfile.ontology.GraphFileResource;
25 import org.simantics.ui.workbench.ResourceEditorInput;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class PlainTextEditorDocumentProvider extends AbstractDocumentProvider {
30
31     private static final Logger LOGGER = LoggerFactory.getLogger(PlainTextEditor.class);
32
33     protected String currentText;
34
35     @Override
36     protected IDocument createDocument(Object element) throws CoreException {
37         ResourceEditorInput input = (ResourceEditorInput)element;
38         final Resource resource = input.getResource();
39         try {
40             return Simantics.getSession().syncRequest(new UniqueRead<Document>() {
41                 @Override
42                 public Document perform(ReadGraph graph) throws DatabaseException {
43                     
44                     GraphFileResource GF = GraphFileResource.getInstance(graph);
45                     DocumentResource DOC = DocumentResource.getInstance(graph);
46                     if (!graph.isInstanceOf(resource, DOC.FileDocument))
47                         throw new DatabaseException("Invalid input resource for PlainTextEditor: " + NameUtils.getSafeName(graph, resource));
48
49                     byte[] bytes = graph.getPossibleRelatedValue(resource, GF.HasFiledata, Bindings.BYTE_ARRAY);
50                     
51                     currentText = bytes != null ? new String(bytes, StandardCharsets.UTF_8) : "";
52                     
53                     return new Document(currentText);
54                     
55                 }
56             });
57         } catch (DatabaseException e) {
58             StringWriter sw = new StringWriter();
59             LOGGER.error("Failed to create document", e);
60             return new Document(sw.toString());
61         }
62     }
63
64     @Override
65     protected IAnnotationModel createAnnotationModel(Object element)
66             throws CoreException {
67         return new AnnotationModel();
68     }
69
70     @Override
71     protected void doSaveDocument(IProgressMonitor monitor, Object element,
72             IDocument document, boolean overwrite) throws CoreException {
73         ResourceEditorInput input = (ResourceEditorInput)element;
74         final Resource resource = input.getResource();
75         final String text = document.get();
76         currentText = text;
77         Simantics.getSession().asyncRequest(new WriteRequest() {
78             @Override
79             public void perform(WriteGraph graph) throws DatabaseException {
80                 graph.markUndoPoint();
81                 GraphFileResource GF = GraphFileResource.getInstance(graph);
82                 graph.claimLiteral(resource, GF.HasFiledata, text.getBytes(StandardCharsets.UTF_8));
83                 graph.claimLiteral(resource, GF.LastModified, System.currentTimeMillis());
84             }
85         });
86     }
87
88     @Override
89     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
90         return null;
91     }
92     
93     @Override
94     public boolean isModifiable(Object element) {
95         return true;
96     }
97     
98     @Override
99     public boolean isReadOnly(Object element) {
100         return false;
101     }
102     
103     @Override
104     public boolean canSaveDocument(Object element) {
105         return !getDocument(element).get().equals(currentText);
106     }
107
108 }