b8010ef828a23868ba0b24b7820c53be38ef2dfa
[simantics/platform.git] / bundles / org.simantics.scl.ui / src / org / simantics / scl / ui / editor2 / SCLAnnotationModel.java
1 package org.simantics.scl.ui.editor2;
2
3 import java.util.Arrays;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.eclipse.jface.text.IDocument;
8 import org.eclipse.jface.text.Position;
9 import org.eclipse.jface.text.source.Annotation;
10 import org.eclipse.jface.text.source.AnnotationModel;
11 import org.simantics.scl.compiler.errors.CompilationError;
12 import org.simantics.scl.compiler.errors.DoesNotExist;
13 import org.simantics.scl.compiler.errors.ErrorSeverity;
14 import org.simantics.scl.compiler.errors.Failable;
15 import org.simantics.scl.compiler.errors.Failure;
16 import org.simantics.scl.compiler.errors.Locations;
17 import org.simantics.scl.compiler.errors.Success;
18 import org.simantics.scl.compiler.module.Module;
19 import org.simantics.scl.compiler.module.repository.ModuleRepository;
20 import org.simantics.scl.compiler.module.repository.UpdateListener;
21
22 public class SCLAnnotationModel extends AnnotationModel {
23     private final SCLModuleEditorInput input;
24     private final ModuleRepository repository;
25     private volatile boolean connected = false;
26     
27     public SCLAnnotationModel(SCLModuleEditorInput input,
28             ModuleRepository repository) {
29         this.input = input;
30         this.repository = repository;
31     }
32     
33     UpdateListener updateListener = new UpdateListener() {
34         @Override
35         public void notifyAboutUpdate() {
36             if(connected)
37                 updateAnnotations();
38         }
39     };
40     
41     private void updateAnnotations() {
42         Failable<Module> result = repository.getModule(input.getModuleName(), updateListener);
43         if(result instanceof Failure) {
44             Failure failure = (Failure)result;
45             setAnnotations(Arrays.asList(failure.errors));
46         }
47         else if(result == DoesNotExist.INSTANCE)
48             setAnnotations(Collections.<CompilationError>emptyList());
49         else
50             setAnnotations(Arrays.asList(result.getResult().getWarnings()));
51     }
52     
53     protected void setAnnotations(List<CompilationError> errors) {
54         synchronized(getLockObject()) {
55             removeAllAnnotations();
56             for(CompilationError error : errors) {
57                 Annotation annotation = new Annotation(
58                         error.severity == ErrorSeverity.ERROR ?
59                                 "org.eclipse.ui.workbench.texteditor.error" :
60                                     "org.eclipse.ui.workbench.texteditor.warning",
61                                     true, error.description);
62                 int begin = Locations.beginOf(error.location);
63                 int end = Locations.endOf(error.location);
64                 Position position = new Position(begin, end - begin);
65                 addAnnotation(annotation, position);
66             }
67         }
68     }
69
70     @Override
71     public void connect(IDocument document) {
72         super.connect(document);
73         connected = true;
74         updateAnnotations();
75     }
76     
77     @Override
78     public void disconnect(IDocument document) {
79         connected = false;
80         updateListener.stopListening();
81         super.disconnect(document);
82     }
83 }