1 /*******************************************************************************
\r
2 * Copyright (c) 2010, 2012 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 java.io.File;
\r
15 import java.io.IOException;
\r
16 import java.util.HashSet;
\r
17 import java.util.Set;
\r
19 import org.eclipse.core.runtime.CoreException;
\r
20 import org.eclipse.core.runtime.IProgressMonitor;
\r
21 import org.eclipse.core.runtime.preferences.InstanceScope;
\r
22 import org.eclipse.jface.operation.IRunnableContext;
\r
23 import org.eclipse.jface.preference.IPreferenceStore;
\r
24 import org.eclipse.jface.text.Document;
\r
25 import org.eclipse.jface.text.IDocument;
\r
26 import org.eclipse.jface.text.PaintManager;
\r
27 import org.eclipse.jface.text.source.AnnotationModel;
\r
28 import org.eclipse.jface.text.source.AnnotationPainter;
\r
29 import org.eclipse.jface.text.source.DefaultCharacterPairMatcher;
\r
30 import org.eclipse.jface.text.source.IAnnotationModel;
\r
31 import org.eclipse.jface.text.source.MatchingCharacterPainter;
\r
32 import org.eclipse.swt.graphics.Color;
\r
33 import org.eclipse.swt.graphics.RGB;
\r
34 import org.eclipse.swt.widgets.Display;
\r
35 import org.eclipse.ui.IEditorInput;
\r
36 import org.eclipse.ui.IEditorSite;
\r
37 import org.eclipse.ui.PartInitException;
\r
38 import org.eclipse.ui.PlatformUI;
\r
39 import org.eclipse.ui.editors.text.TextEditor;
\r
40 import org.eclipse.ui.preferences.ScopedPreferenceStore;
\r
41 import org.eclipse.ui.texteditor.AbstractDocumentProvider;
\r
42 import org.simantics.db.exception.DatabaseException;
\r
43 import org.simantics.modelica.ModelicaManager;
\r
44 import org.simantics.modelica.preferences.OpenModelicaPreferences;
\r
45 import org.simantics.sysdyn.modelica.ModelicaWriter;
\r
46 import org.simantics.sysdyn.representation.Configuration;
\r
47 import org.simantics.sysdyn.representation.IElement;
\r
48 import org.simantics.sysdyn.representation.LoadRepresentation;
\r
49 import org.simantics.sysdyn.representation.Module;
\r
50 import org.simantics.sysdyn.representation.ModuleType;
\r
51 import org.simantics.sysdyn.representation.utils.RepresentationUtils;
\r
52 import org.simantics.ui.SimanticsUI;
\r
53 import org.simantics.ui.workbench.ResourceEditorInput;
\r
56 * Text editor for displaying Modelica code
\r
58 * @author Teemu Lempinen
\r
61 public class SysdynModelicaEditor extends TextEditor {
\r
63 AnnotationModel annotationModel = new AnnotationModel();
\r
64 AnnotationPainter apainter;
\r
67 public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {
\r
68 super.init(site, input);
\r
70 Configuration configuration =
\r
71 LoadRepresentation.loadConfiguration(SimanticsUI.getSession(), ((ResourceEditorInput)input).getResource());
\r
72 setPartName(configuration.getLabel());
\r
73 } catch (DatabaseException e) {
\r
74 e.printStackTrace();
\r
79 public SysdynModelicaEditor() {
\r
81 showOverviewRuler();
\r
82 setDocumentProvider(new AbstractDocumentProvider() {
\r
85 protected IAnnotationModel createAnnotationModel(Object element)
\r
86 throws CoreException {
\r
87 return annotationModel;
\r
91 protected IDocument createDocument(Object element)
\r
92 throws CoreException {
\r
94 Configuration configuration =
\r
95 LoadRepresentation.loadConfiguration(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource());
\r
97 HashSet<Configuration> configurations = new HashSet<Configuration>();
\r
98 configurations.add(configuration);
\r
99 getConfigurations(configuration, configurations);
\r
101 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE,
\r
102 org.simantics.modelica.Activator.PLUGIN_ID);
\r
103 String omHome = store.getString(OpenModelicaPreferences.OM_HOME);
\r
104 String version = null;
\r
105 if(omHome != null) {
\r
106 File omHomeDir = new File(omHome);
\r
107 if(omHomeDir != null && omHomeDir.isDirectory())
\r
109 version = ModelicaManager.getOMCVersion(omHomeDir);
\r
110 } catch (IOException e) { }
\r
112 return new Document(ModelicaWriter.write(configurations, RepresentationUtils.isGameExperimentActive(), version));
\r
113 } catch (DatabaseException e) {
\r
114 e.printStackTrace();
\r
115 throw new CoreException(STATUS_ERROR);
\r
120 protected void doSaveDocument(IProgressMonitor monitor,
\r
121 Object element, IDocument document, boolean overwrite)
\r
122 throws CoreException {
\r
126 protected IRunnableContext getOperationRunner(
\r
127 IProgressMonitor monitor) {
\r
128 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();
\r
132 public boolean isModifiable(Object element) {
\r
137 public boolean isReadOnly(Object element) {
\r
143 setSourceViewerConfiguration(new ModelicaSourceViewerConfiguration());
\r
147 private void getConfigurations(Configuration configuration, Set<Configuration> configurations) {
\r
148 for(IElement e : configuration.getElements()) {
\r
149 if(e instanceof Module) {
\r
150 ModuleType mt = ((Module) e).getType();
\r
151 configurations.add(mt.getConfiguration());
\r
152 getConfigurations(mt.getConfiguration(), configurations);
\r
158 protected void createActions() {
\r
159 super.createActions();
\r
161 PaintManager paintManager = new PaintManager(getSourceViewer());
\r
162 MatchingCharacterPainter matchingCharacterPainter = new MatchingCharacterPainter(getSourceViewer(),
\r
163 new DefaultCharacterPairMatcher( new char[] {'(', ')', '{', '}', '[', ']'} ));
\r
164 matchingCharacterPainter.setColor(new Color(Display.getCurrent(), new RGB(160, 160, 160)));
\r
165 paintManager.addPainter(matchingCharacterPainter);
\r