1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.sysdyn.ui.modelica;
\r
14 import org.eclipse.core.runtime.CoreException;
\r
15 import org.eclipse.core.runtime.IProgressMonitor;
\r
16 import org.eclipse.jface.operation.IRunnableContext;
\r
17 import org.eclipse.jface.text.Document;
\r
18 import org.eclipse.jface.text.IDocument;
\r
19 import org.eclipse.jface.text.PaintManager;
\r
20 import org.eclipse.jface.text.source.AnnotationModel;
\r
21 import org.eclipse.jface.text.source.AnnotationPainter;
\r
22 import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
\r
23 import org.eclipse.jface.text.source.IAnnotationModel;
\r
24 import org.eclipse.jface.text.source.MatchingCharacterPainter;
\r
25 import org.eclipse.swt.graphics.Color;
\r
26 import org.eclipse.swt.graphics.RGB;
\r
27 import org.eclipse.swt.widgets.Display;
\r
28 import org.eclipse.ui.PlatformUI;
\r
29 import org.eclipse.ui.editors.text.TextEditor;
\r
30 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
\r
31 import org.simantics.db.exception.DatabaseException;
\r
32 import org.simantics.sysdyn.modelica.ModelicaWriter;
\r
33 import org.simantics.sysdyn.representation.Configuration;
\r
34 import org.simantics.sysdyn.representation.LoadRepresentation;
\r
35 import org.simantics.ui.SimanticsUI;
\r
36 import org.simantics.ui.workbench.ResourceEditorInput;
\r
39 public class SysdynModelicaEditor extends TextEditor {
\r
41 AnnotationModel annotationModel = new AnnotationModel();
\r
42 AnnotationPainter apainter;
\r
44 public SysdynModelicaEditor() {
\r
46 showOverviewRuler();
\r
47 setDocumentProvider(new AbstractDocumentProvider() {
\r
50 protected IAnnotationModel createAnnotationModel(Object element)
\r
51 throws CoreException {
\r
52 return annotationModel;
\r
56 protected IDocument createDocument(Object element)
\r
57 throws CoreException {
\r
59 Configuration configuration =
\r
60 LoadRepresentation.loadConfiguration(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource());
\r
61 ModelicaWriter writer = new ModelicaWriter();
\r
62 writer.write(configuration);
\r
64 return new Document(writer.toString());
\r
65 } catch (DatabaseException e) {
\r
66 e.printStackTrace();
\r
67 throw new CoreException(STATUS_ERROR);
\r
72 protected void doSaveDocument(IProgressMonitor monitor,
\r
73 Object element, IDocument document, boolean overwrite)
\r
74 throws CoreException {
\r
78 protected IRunnableContext getOperationRunner(
\r
79 IProgressMonitor monitor) {
\r
80 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
\r
84 public boolean isModifiable(Object element) {
\r
89 public boolean isReadOnly(Object element) {
\r
95 setSourceViewerConfiguration(new ModelicaSourceViewerConfiguration());
\r
100 protected void createActions() {
\r
101 super.createActions();
\r
103 PaintManager paintManager = new PaintManager(getSourceViewer());
\r
104 MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(getSourceViewer(),
\r
105 new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} ));
\r
106 matchingCharacterPainter.setColor(new Color(Display.getCurrent(), new RGB(160, 160, 160)));
\r
107 paintManager.addPainter(matchingCharacterPainter);
\r