]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
5810dd668349532b5240b830ba469d42c23e7adb
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.modelica;\r
13 \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
18 \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
54 \r
55 /**\r
56  * Text editor for displaying Modelica code\r
57  * \r
58  * @author Teemu Lempinen\r
59  *\r
60  */\r
61 public class SysdynModelicaEditor extends TextEditor {\r
62 \r
63     AnnotationModel annotationModel = new AnnotationModel();\r
64     AnnotationPainter apainter;\r
65     \r
66     \r
67         public void init(final IEditorSite site, final IEditorInput input) throws PartInitException {\r
68                 super.init(site, input);\r
69         try {\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
75                 }\r
76 \r
77         }\r
78                 \r
79     public SysdynModelicaEditor() {\r
80         super();\r
81         showOverviewRuler();\r
82         setDocumentProvider(new AbstractDocumentProvider() {\r
83 \r
84             @Override\r
85             protected IAnnotationModel createAnnotationModel(Object element)\r
86                     throws CoreException {\r
87                 return annotationModel;\r
88             }\r
89 \r
90             @Override\r
91             protected IDocument createDocument(Object element)\r
92                     throws CoreException {\r
93                 try {\r
94                     Configuration configuration =\r
95                         LoadRepresentation.loadConfiguration(SimanticsUI.getSession(), ((ResourceEditorInput)element).getResource());\r
96                     \r
97                     HashSet<Configuration> configurations = new HashSet<Configuration>();\r
98                     configurations.add(configuration);\r
99                     getConfigurations(configuration, configurations);\r
100                     \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
108                             try {\r
109                                 version = ModelicaManager.getOMCVersion(omHomeDir);\r
110                             } catch (IOException e) { }\r
111                     }\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
116                 }                \r
117             }\r
118 \r
119             @Override\r
120             protected void doSaveDocument(IProgressMonitor monitor,\r
121                     Object element, IDocument document, boolean overwrite)\r
122                     throws CoreException {\r
123             }\r
124 \r
125             @Override\r
126             protected IRunnableContext getOperationRunner(\r
127                     IProgressMonitor monitor) {\r
128                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
129             }\r
130             \r
131             @Override\r
132             public boolean isModifiable(Object element) {\r
133                 return false;\r
134             }\r
135             \r
136             @Override\r
137             public boolean isReadOnly(Object element) {\r
138                 return true;\r
139             }\r
140             \r
141         }); \r
142         \r
143         setSourceViewerConfiguration(new ModelicaSourceViewerConfiguration());\r
144                 \r
145     }\r
146     \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
153                 }\r
154         }\r
155     }\r
156     \r
157     @Override\r
158     protected void createActions() {\r
159         super.createActions();\r
160         \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
166     }\r
167     \r
168     \r
169 }\r
170 \r