]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLTextEditorNew.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor / SCLTextEditorNew.java
diff --git a/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLTextEditorNew.java b/bundles/org.simantics.scl.ui/src/org/simantics/scl/ui/editor/SCLTextEditorNew.java
new file mode 100644 (file)
index 0000000..54afcff
--- /dev/null
@@ -0,0 +1,175 @@
+package org.simantics.scl.ui.editor;\r
+\r
+\r
+import java.util.Iterator;\r
+\r
+import org.eclipse.jface.resource.ImageRegistry;\r
+import org.eclipse.jface.resource.JFaceResources;\r
+import org.eclipse.jface.resource.LocalResourceManager;\r
+import org.eclipse.jface.text.Document;\r
+import org.eclipse.jface.text.IUndoManager;\r
+import org.eclipse.jface.text.Position;\r
+import org.eclipse.jface.text.TextViewerUndoManager;\r
+import org.eclipse.jface.text.source.Annotation;\r
+import org.eclipse.jface.text.source.AnnotationModel;\r
+import org.eclipse.jface.text.source.AnnotationPainter;\r
+import org.eclipse.jface.text.source.IAnnotationModel;\r
+import org.eclipse.jface.text.source.ISharedTextColors;\r
+import org.eclipse.jface.text.source.OverviewRuler;\r
+import org.eclipse.jface.text.source.SourceViewer;\r
+import org.eclipse.jface.text.source.VerticalRuler;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.graphics.Point;\r
+import org.eclipse.swt.graphics.RGB;\r
+import org.eclipse.swt.layout.FillLayout;\r
+import org.eclipse.swt.widgets.Composite;\r
+import org.simantics.scl.compiler.errors.CompilationError;\r
+import org.simantics.scl.compiler.errors.Locations;\r
+\r
+public class SCLTextEditorNew extends Composite {\r
+\r
+    private static final int DELAY_BEFORE_COMPILATION = 500 /*ms*/;\r
+    \r
+    public SourceViewer viewer;\r
+    ImageRegistry imageRegistry;\r
+    SCLAnnotationAccessNew annotationAccess;\r
+    ISharedTextColors sharedTextColors;\r
+    IAnnotationModel annotationModel;    \r
+    \r
+    public SCLTextEditorNew(Composite parent, int style) {\r
+        super(parent, style);\r
+        setLayout(new FillLayout());\r
+        \r
+        imageRegistry = new ImageRegistry(parent.getDisplay());\r
+        annotationAccess = new SCLAnnotationAccessNew(imageRegistry);\r
+        sharedTextColors = new SharedTextColorsNew(getDisplay());\r
+        annotationModel = new AnnotationModel();\r
+        \r
+        VerticalRuler leftRuler = new VerticalRuler(12, annotationAccess);\r
+        leftRuler.setModel(annotationModel);\r
+        \r
+        OverviewRuler rightRuler = \r
+            new OverviewRuler(annotationAccess, 12, sharedTextColors);\r
+        rightRuler.setModel(annotationModel);\r
+        rightRuler.addAnnotationType("error");\r
+        rightRuler.setAnnotationTypeLayer("error", 0);\r
+        rightRuler.setAnnotationTypeColor("error", sharedTextColors.getColor(new RGB(255,0,128)));\r
+        \r
+        viewer = new SourceViewer(this, \r
+                leftRuler, rightRuler,\r
+                true,\r
+                SWT.H_SCROLL | SWT.V_SCROLL);\r
+        Document document = new Document();\r
+        viewer.showAnnotations(false);\r
+        viewer.showAnnotationsOverview(false);\r
+        viewer.setDocument(document, annotationModel);\r
+        viewer.setEditable(true);\r
+        LocalResourceManager resourceManager = new LocalResourceManager(JFaceResources.getResources(), viewer.getControl());\r
+        SCLSourceViewerConfigurationNew sourceViewerConfiguration = new SCLSourceViewerConfigurationNew(resourceManager);\r
+        viewer.configure(sourceViewerConfiguration);\r
+        \r
+        // Annotations to text area\r
+        AnnotationPainter annotationPainter = \r
+            new AnnotationPainter(viewer, annotationAccess);        \r
+        annotationPainter.addAnnotationType("error");\r
+        annotationPainter.setAnnotationTypeColor("error", sharedTextColors.getColor(new RGB(255,0,128)));\r
+        viewer.addPainter(annotationPainter);\r
+        annotationModel.addAnnotationModelListener(annotationPainter);\r
+        \r
+        IUndoManager undoManager = new TextViewerUndoManager(100);\r
+        viewer.setUndoManager(undoManager);\r
+        undoManager.connect(viewer);\r
+        \r
+        // Undo support (maybe not needed in workbench?)\r
+        /*viewer.getTextWidget().addKeyListener(new KeyAdapter() {\r
+            @Override\r
+            public void keyReleased(KeyEvent e) {\r
+            }\r
+            @Override\r
+            public void keyPressed(KeyEvent e) {\r
+                if(e.keyCode=='z'&& e.stateMask == SWT.CTRL) {\r
+                    viewer.getUndoManager().undo();\r
+                }\r
+                else if(e.keyCode=='y'&& e.stateMask == SWT.CTRL) {\r
+                    viewer.getUndoManager().redo();\r
+                }\r
+            }\r
+        });*/\r
+    }\r
+    \r
+    public IUndoManager getUndoManager() {\r
+        return viewer.getUndoManager();\r
+    }\r
+    \r
+    @Override\r
+    public void dispose() {\r
+        super.dispose();\r
+        sharedTextColors.dispose();\r
+    }    \r
+    \r
+    @SuppressWarnings("unchecked")\r
+    private void removeAnnotations() {\r
+        Iterator<Annotation> it = annotationModel.getAnnotationIterator();\r
+        while(it.hasNext()) {\r
+            Annotation annotation = it.next();\r
+            annotationModel.removeAnnotation(annotation);\r
+        }\r
+    }\r
+    \r
+    private void setAnnotations(CompilationError[] errors) {\r
+        removeAnnotations();\r
+        for(CompilationError error : errors) {\r
+            int begin = Locations.beginOf(error.location);\r
+            int end = Locations.endOf(error.location);\r
+            if(begin == end) {\r
+                if(begin > 0)\r
+                    --begin;\r
+                else\r
+                    ++end;\r
+            }\r
+            annotationModel.addAnnotation(\r
+                    new Annotation("error", true, error.description), \r
+                    new Position(begin, end-begin));   \r
+        }\r
+    }\r
+    \r
+    public String getContent() {\r
+        final String[] result = new String[1];\r
+        getDisplay().syncExec(new Runnable() {\r
+            @Override\r
+            public void run() {\r
+                result[0] = viewer.getDocument().get();\r
+            }\r
+        });\r
+        return result[0];\r
+    }\r
+    \r
+    public void setContent(final String content, final CompilationError[] errors) {\r
+        //getDisplay().asyncExec(new Runnable() {\r
+        getDisplay().syncExec(new Runnable() {\r
+            \r
+            @Override\r
+            public void run() {\r
+                if (viewer.getTextWidget().isDisposed()) return;\r
+                if(content != null)\r
+                    viewer.getDocument().set(content);\r
+                if(errors != null)\r
+                    setAnnotations(errors);\r
+            }\r
+        });\r
+    }\r
+\r
+    private Point storedSelectedRange;\r
+\r
+    public void storeSelectedRange() {\r
+        storedSelectedRange = viewer.getSelectedRange();\r
+    }\r
+\r
+    public void restoreSelectedRange() {\r
+        if (storedSelectedRange != null) {\r
+            viewer.setSelectedRange(storedSelectedRange.x, storedSelectedRange.y);\r
+            storedSelectedRange = null;\r
+        }\r
+    }\r
+\r
+}\r