1 | /** |
2 | * |
3 | */ |
4 | package de.uka.ipd.sdq.pcm.dialogs.stoex; |
5 | |
6 | import java.util.ArrayList; |
7 | import java.util.Iterator; |
8 | |
9 | import org.eclipse.jface.text.IDocument; |
10 | import org.eclipse.jface.text.IRegion; |
11 | import org.eclipse.jface.text.ITextHover; |
12 | import org.eclipse.jface.text.ITextViewer; |
13 | import org.eclipse.jface.text.contentassist.ContentAssistant; |
14 | import org.eclipse.jface.text.contentassist.IContentAssistant; |
15 | import org.eclipse.jface.text.presentation.IPresentationReconciler; |
16 | import org.eclipse.jface.text.presentation.PresentationReconciler; |
17 | import org.eclipse.jface.text.rules.DefaultDamagerRepairer; |
18 | import org.eclipse.jface.text.rules.ITokenScanner; |
19 | import org.eclipse.jface.text.source.Annotation; |
20 | import org.eclipse.jface.text.source.IAnnotationHover; |
21 | import org.eclipse.jface.text.source.IAnnotationModel; |
22 | import org.eclipse.jface.text.source.ISourceViewer; |
23 | import org.eclipse.jface.text.source.SourceViewerConfiguration; |
24 | |
25 | import de.uka.ipd.sdq.pcm.repository.Parameter; |
26 | |
27 | /** |
28 | * @author Snowball |
29 | * |
30 | */ |
31 | public class AbstractGrammarBasedViewerConfiguration extends SourceViewerConfiguration { |
32 | |
33 | private IAnnotationModel annotationModel; |
34 | private Class myLexerClass; |
35 | private ITokenMapper myMapper; |
36 | private Parameter[] context = null; |
37 | private ContentAssistant myAssistant; |
38 | |
39 | /** |
40 | * |
41 | */ |
42 | public AbstractGrammarBasedViewerConfiguration(IAnnotationModel annotationModel, Parameter[] context, Class lexerClass, ITokenMapper myMapper) { |
43 | this.annotationModel = annotationModel; |
44 | this.myLexerClass = lexerClass; |
45 | this.myMapper = myMapper; |
46 | this.context = context; |
47 | } |
48 | |
49 | /* |
50 | * (non-Javadoc) |
51 | * |
52 | * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getPresentationReconciler(org.eclipse.jface.text.source.ISourceViewer) |
53 | */ |
54 | @Override |
55 | public IPresentationReconciler getPresentationReconciler( |
56 | ISourceViewer sourceViewer) { |
57 | PresentationReconciler reconciler = new PresentationReconciler(); |
58 | |
59 | DefaultDamagerRepairer dr = new DefaultDamagerRepairer( |
60 | getKeywordScanner()); |
61 | reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE); |
62 | reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE); |
63 | |
64 | return reconciler; |
65 | } |
66 | |
67 | @Override |
68 | public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) { |
69 | return new AnnotationHover(annotationModel); |
70 | } |
71 | |
72 | private ITokenScanner getKeywordScanner() { |
73 | return new ANTLRTokenScannerAdapter(myLexerClass,myMapper); |
74 | } |
75 | |
76 | @Override |
77 | public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { |
78 | if (myAssistant == null){ |
79 | myAssistant = new ContentAssistant(); |
80 | myAssistant.setContentAssistProcessor(new StoExCompletionProcessor(context), |
81 | IDocument.DEFAULT_CONTENT_TYPE); |
82 | myAssistant.setAutoActivationDelay(1); |
83 | myAssistant.enableAutoActivation(true); |
84 | } |
85 | return myAssistant; |
86 | } |
87 | } |
88 | |
89 | // annotation hover manager |
90 | class AnnotationHover implements IAnnotationHover, ITextHover { |
91 | private IAnnotationModel fAnnotationModel; |
92 | |
93 | public AnnotationHover(IAnnotationModel annotationModel) { |
94 | this.fAnnotationModel = annotationModel; |
95 | } |
96 | |
97 | public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) { |
98 | Iterator ite = fAnnotationModel.getAnnotationIterator(); |
99 | |
100 | ArrayList all = new ArrayList(); |
101 | |
102 | while (ite.hasNext()) { |
103 | Annotation a = (Annotation) ite.next(); |
104 | if (a instanceof Annotation) { |
105 | all.add(((Annotation) a).getText()); |
106 | } |
107 | } |
108 | |
109 | StringBuffer total = new StringBuffer(); |
110 | for (int x = 0; x < all.size(); x++) { |
111 | String str = (String) all.get(x); |
112 | total.append(" " + str + (x == (all.size() - 1) ? "" : "\n")); |
113 | } |
114 | |
115 | return total.toString(); |
116 | } |
117 | |
118 | public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) { |
119 | return null; |
120 | } |
121 | |
122 | public IRegion getHoverRegion(ITextViewer textViewer, int offset) { |
123 | return null; |
124 | } |
125 | } |