Preventing unnecessary ModuleSource compilation in SCL-editor
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / SCLModuleEditor2DocumentProvider.java
1 package org.simantics.scl.ui.editor2;
2
3 import java.io.IOException;
4
5 import org.eclipse.core.runtime.CoreException;
6 import org.eclipse.core.runtime.IProgressMonitor;
7 import org.eclipse.core.runtime.Status;
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.IDocumentPartitioner;
12 import org.eclipse.jface.text.rules.FastPartitioner;
13 import org.eclipse.jface.text.source.AnnotationModel;
14 import org.eclipse.jface.text.source.IAnnotationModel;
15 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
16 import org.simantics.scl.compiler.source.ModuleSource;
17 import org.simantics.scl.compiler.source.TextualModuleSource;
18 import org.simantics.scl.osgi.SCLOsgi;
19 import org.simantics.scl.ui.editor.SCLSourceViewerConfigurationNew;
20
21 public class SCLModuleEditor2DocumentProvider extends AbstractDocumentProvider {
22
23     private SCLSourceViewerConfigurationNew sourceViewer;
24     protected AnnotationModel annotationModel = new AnnotationModel();
25     
26     private Object currentElement;
27     private TextualModuleSource currentSource;
28     
29     public SCLModuleEditor2DocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
30         this.sourceViewer = sourceViewer;
31     }
32
33     private void updateTextualModuleSource(Object input) {
34         if (currentElement != null && currentElement.equals(input))
35             return;
36         currentElement = input;
37         if(!(currentElement instanceof SCLModuleEditorInput))
38             return;
39         
40         ModuleSource source = ((SCLModuleEditorInput)currentElement).getAdapter(ModuleSource.class);
41         if(source == null)
42             return;
43         if(!(source instanceof TextualModuleSource))
44             return;
45         currentSource = (TextualModuleSource)source;
46     }
47     
48     @Override
49     protected IDocument createDocument(Object element) throws CoreException {
50         updateTextualModuleSource(element);
51         if(currentSource == null)
52             throw new CoreException(
53                     new Status(Status.ERROR, "org.simantics.scl.ui", "Source for the SCL module could not be found.")
54                     );
55         Document document;
56         try {
57             document = new Document(currentSource.getSourceText(null));
58         } catch (IOException e) {
59             throw new CoreException(
60                     new Status(Status.ERROR, "org.simantics.scl.ui", "Reading SCL module failed.", e)
61                     );
62         }
63         IDocumentPartitioner partitioner = new FastPartitioner(new SCLPartitionScanner(), SCLPartitionScanner.PARTITION_TYPES);
64         partitioner.connect(document);
65         document.setDocumentPartitioner(partitioner);
66         sourceViewer.updateCompletionAssistModuleName(currentSource.getModuleName());
67         return document;
68     }
69     
70     @Override
71     public void changed(Object element) {
72         updateTextualModuleSource(element);
73     }
74     
75     @Override
76     public void aboutToChange(Object element) {
77         super.aboutToChange(element);
78     }
79     
80     @Override
81     public boolean isModifiable(Object element) {
82         if(currentSource == null)
83             return false;
84         return currentSource.isUpdateable();
85     }
86     
87     @Override
88     public boolean isReadOnly(Object element) {
89         return !isModifiable(element);
90     }
91
92     @Override
93     protected IAnnotationModel createAnnotationModel(Object element)
94             throws CoreException {
95         SCLModuleEditorInput input = (SCLModuleEditorInput)element;
96         return new SCLAnnotationModel(input, SCLOsgi.MODULE_REPOSITORY);
97     }
98
99     @Override
100     protected void doSaveDocument(IProgressMonitor monitor, Object element,
101             IDocument document, boolean overwrite) throws CoreException {
102         if(currentSource == null)
103             return;
104         currentSource.update(document.get());
105     }
106
107     @Override
108     protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {
109         // TODO Auto-generated method stub
110         return null;
111     }
112
113 }