1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management
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
10 * Semantum Oy - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.modeling.ui.scl.scriptEditor;
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;
30 * @author Tuukka Lehtonen
33 public class SCLScriptEditorDocumentProvider extends AbstractDocumentProvider {
35 private SCLSourceViewerConfigurationNew sourceViewer;
36 protected AnnotationModel annotationModel = new AnnotationModel();
38 private Object currentElement;
39 private SCLScriptSource currentSource;
41 public SCLScriptEditorDocumentProvider(SCLSourceViewerConfigurationNew sourceViewer) {
42 this.sourceViewer = sourceViewer;
45 private void updateScriptSource(Object input) {
46 if (currentElement != null && currentElement.equals(input))
48 currentElement = input;
49 if (!(currentElement instanceof SCLScriptEditorInput))
52 SCLScriptSource source = ((SCLScriptEditorInput) currentElement).getAdapter(SCLScriptSource.class);
54 currentSource = source;
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());
72 public void changed(Object element) {
73 updateScriptSource(element);
77 public void aboutToChange(Object element) {
78 super.aboutToChange(element);
82 public boolean isModifiable(Object element) {
83 if (currentSource == null)
85 return currentSource.isUpdateable();
89 public boolean isReadOnly(Object element) {
90 return !isModifiable(element);
94 protected IAnnotationModel createAnnotationModel(Object element) throws CoreException {
95 SCLScriptEditorInput input = (SCLScriptEditorInput) element;
96 return new SCLScriptAnnotationModel(input, SCLOsgi.MODULE_REPOSITORY);
100 protected void doSaveDocument(IProgressMonitor monitor, Object element,
101 IDocument document, boolean overwrite) throws CoreException
103 if (currentSource != null)
104 currentSource.update(document.get());
108 protected IRunnableContext getOperationRunner(IProgressMonitor monitor) {