Move some GraphFile UI from Simupedia to platform
[simantics/platform.git] / bundles / org.simantics.graphfile.ui / src / org / simantics / graphfile / ui / editor / PlainTextEditor.java
1 package org.simantics.graphfile.ui.editor;
2
3 import java.util.function.Supplier;
4
5 import org.eclipse.swt.widgets.Composite;
6 import org.eclipse.ui.IEditorInput;
7 import org.eclipse.ui.IEditorSite;
8 import org.eclipse.ui.PartInitException;
9 import org.eclipse.ui.editors.text.TextEditor;
10 import org.simantics.Simantics;
11 import org.simantics.db.Session;
12 import org.simantics.db.common.request.ParametrizedRead;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.db.layer0.request.combinations.Combinators;
15 import org.simantics.ui.workbench.IResourceEditorInput;
16 import org.simantics.ui.workbench.ResourceEditorSupport;
17 import org.simantics.ui.workbench.TitleRequest;
18 import org.simantics.ui.workbench.TitleUpdater;
19 import org.simantics.ui.workbench.ToolTipRequest;
20 import org.simantics.ui.workbench.editor.input.InputValidationCombinators;
21
22 /**
23  * @author Antti Villberg
24  */
25 public class PlainTextEditor extends TextEditor {
26
27     boolean isDisposed = false;
28
29     ParametrizedRead<IResourceEditorInput, Boolean> INPUT_VALIDATOR =
30             Combinators.compose(
31                     InputValidationCombinators.hasURI(),
32                     InputValidationCombinators.extractInputResource()
33             );
34
35     ResourceEditorSupport support;
36
37     public PlainTextEditor() {
38         super();
39         setDocumentProvider(new PlainTextEditorDocumentProvider());
40         //setSourceViewerConfiguration(new QueueTextEditorViewerConfiguration(new QueueTextEditorColorManager()));
41     }
42
43     @Override
44     public void init(IEditorSite site, IEditorInput input) throws PartInitException {
45         super.init(site, input);
46
47         support = new ResourceEditorSupport(this, INPUT_VALIDATOR);
48         support.activateValidation();
49         try {
50             getResourceEditorInput().init(null);
51         } catch (DatabaseException e) {
52             throw new PartInitException("Failed to initialize " + input, e);
53         }
54     }
55
56     protected IResourceEditorInput getResourceEditorInput() {
57         return (IResourceEditorInput) getEditorInput();
58     }
59
60     @Override
61     public void createPartControl(Composite parent) {
62         super.createPartControl(parent);
63         updatePartName();
64     }
65
66     private void updatePartName() {
67         setPartName(getEditorInput().getName());
68
69         Session session = Simantics.peekSession();
70         if (session != null) {
71             Supplier<Boolean> disposedCallback = () -> isDisposed;
72             session.asyncRequest(
73                     new TitleRequest(null, getResourceEditorInput()),
74                     new TitleUpdater(getSite().getShell().getDisplay(), this::setPartName, disposedCallback));
75             session.asyncRequest(
76                     new ToolTipRequest(getSite().getId(), getResourceEditorInput()),
77                     new TitleUpdater(getSite().getShell().getDisplay(), this::setTitleToolTip, disposedCallback));
78         }
79
80     }
81
82     @Override
83     public void dispose() {
84         isDisposed = true;
85         if (support != null) {
86             support.dispose();
87             support = null;
88         }
89         super.dispose();
90     }
91
92 }