]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/scl/scriptEditor/SCLScriptEditorDocumentProvider.java
(refs #7571) Fixed incorrect handling of effects in edo
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / scl / scriptEditor / SCLScriptEditorDocumentProvider.java
1 /*******************************************************************************
2  * Copyright (c) 2017 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.scl.scriptEditor;
13
14 import org.eclipse.core.runtime.CoreException;
15 import org.eclipse.core.runtime.IProgressMonitor;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.jface.operation.IRunnableContext;
18 import org.eclipse.jface.text.Document;
19 import org.eclipse.jface.text.IDocument;
20 import org.eclipse.jface.text.IDocumentPartitioner;
21 import org.eclipse.jface.text.rules.FastPartitioner;
22 import org.eclipse.jface.text.source.AnnotationModel;
23 import org.eclipse.jface.text.source.IAnnotationModel;
24 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
25 import org.simantics.scl.osgi.SCLOsgi;
26 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
27 import org.simantics.scl.ui.editor2.SCLPartitionScanner;
28
29 /**
30  * @author Tuukka Lehtonen
31  * @since 1.31.0
32  */
33 public class SCLScriptEditorDocumentProvider extends AbstractDocumentProvider {
34
35     private SCLSourceViewerConfigurationNew sourceViewer;
36     protected AnnotationModel annotationModel = new AnnotationModel();
37
38     private Object currentElement;
39     private SCLScriptSource currentSource;
40
41     public SCLScriptEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
42         this.sourceViewer = sourceViewer;
43     }
44
45     private void updateScriptSource(Object input) {
46         if (currentElement != null && currentElement.equals(input))
47             return;
48         currentElement = input;
49         if (!(currentElement instanceof SCLScriptEditorInput))
50             return;
51
52         SCLScriptSource source = ((SCLScriptEditorInput) currentElement).getAdapter(SCLScriptSource.class);
53         if (source != null)
54             currentSource = source;
55     }
56
57     @Override
58     protected IDocument createDocument(Object element) throws CoreException {
59         updateScriptSource(element);
60         if (currentSource == null)
61             throw new CoreException(
62                     new Status(Status.ERROR, "org.simantics.scl.ui", "Source for the SCL module could not be found."));
63         Document document = new Document(currentSource.getSourceText());
64         IDocumentPartitioner partitioner = new FastPartitioner(new SCLPartitionScanner(), SCLPartitionScanner.PARTITION_TYPES);
65         partitioner.connect(document);
66         document.setDocumentPartitioner(partitioner);
67         sourceViewer.updateCompletionAssistModuleName(currentSource.getScriptURI());
68         return document;
69     }
70
71     @Override
72     public void changed(Object element) {
73         updateScriptSource(element);
74     }
75
76     @Override
77     public void aboutToChange(Object element) {
78         super.aboutToChange(element);
79     }
80
81     @Override
82     public boolean isModifiable(Object element) {
83         if (currentSource == null)
84             return false;
85         return currentSource.isUpdateable();
86     }
87
88     @Override
89     public boolean isReadOnly(Object element) {
90         return !isModifiable(element);
91     }
92
93     @Override
94     protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
95         SCLScriptEditorInput input = (SCLScriptEditorInput) element;
96         return new SCLScriptAnnotationModel(input, SCLOsgi.MODULE_REPOSITORY);
97     }
98
99     @Override
100     protected void doSaveDocument(IProgressMonitor monitor, Object element,
101             IDocument document, boolean overwrite) throws CoreException
102     {
103         if (currentSource != null)
104             currentSource.update(document.get());
105     }
106
107     @Override
108     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
109         return null;
110     }
111
112 }