]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
96dffc7480d145e21ce229dbe17756ced59d521e
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 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 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
37 \r
38 \r
39 public class SysdynModelicaEditor extends TextEditor {\r
40 \r
41     AnnotationModel annotationModel = new AnnotationModel();\r
42     AnnotationPainter apainter;\r
43     \r
44     public SysdynModelicaEditor() {\r
45         super();\r
46         showOverviewRuler();\r
47         setDocumentProvider(new AbstractDocumentProvider() {\r
48 \r
49             @Override\r
50             protected IAnnotationModel createAnnotationModel(Object element)\r
51                     throws CoreException {\r
52                 return annotationModel;\r
53             }\r
54 \r
55             @Override\r
56             protected IDocument createDocument(Object element)\r
57                     throws CoreException {\r
58                 try {\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
63                     \r
64                     return new Document(writer.toString());\r
65                 } catch (DatabaseException e) {\r
66                     e.printStackTrace();\r
67                     throw new CoreException(STATUS_ERROR);\r
68                 }                \r
69             }\r
70 \r
71             @Override\r
72             protected void doSaveDocument(IProgressMonitor monitor,\r
73                     Object element, IDocument document, boolean overwrite)\r
74                     throws CoreException {\r
75             }\r
76 \r
77             @Override\r
78             protected IRunnableContext getOperationRunner(\r
79                     IProgressMonitor monitor) {\r
80                 return PlatformUI.getWorkbench().getActiveWorkbenchWindow();\r
81             }\r
82             \r
83             @Override\r
84             public boolean isModifiable(Object element) {\r
85                 return false;\r
86             }\r
87             \r
88             @Override\r
89             public boolean isReadOnly(Object element) {\r
90                 return true;\r
91             }\r
92             \r
93         }); \r
94         \r
95         setSourceViewerConfiguration(new ModelicaSourceViewerConfiguration());\r
96                 \r
97     }\r
98     \r
99     @Override\r
100     protected void createActions() {\r
101         super.createActions();\r
102         \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
108     }\r
109     \r
110     \r
111 }\r
112 \r