aboutsummaryrefslogtreecommitdiffstats
path: root/Application/org.eclipse.viatra.solver.language.ui/src/org/eclipse/viatra/solver/language/ui/syntaxcoloring/SolverSemanticTextAttributeProvider.xtend
blob: ca74a9a231fcd1308134a90bf7c8956fdeaf7569 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package org.eclipse.viatra.solver.language.ui.syntaxcoloring

import com.google.inject.Inject
import java.util.WeakHashMap
import org.eclipse.jface.text.TextAttribute
import org.eclipse.swt.graphics.RGB
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess
import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration
import org.eclipse.xtext.ui.editor.syntaxcoloring.PreferenceStoreAccessor
import org.eclipse.xtext.ui.editor.syntaxcoloring.TextAttributeProvider
import org.eclipse.xtext.ui.editor.utils.EditorUtils
import org.eclipse.xtext.ui.editor.utils.TextStyle

class SolverSemanticTextAttributeProvider extends TextAttributeProvider {

	val defaultTextStyle = new TextStyle
	val colorID2TextAttribute = new WeakHashMap<String, TextAttribute>

	@Inject
	new(IHighlightingConfiguration highlightingConfig, IPreferenceStoreAccess preferenceStoreAccess,
		PreferenceStoreAccessor prefStoreAccessor) {
		super(highlightingConfig, preferenceStoreAccess, prefStoreAccessor)
	}

	override getAttribute(String id) {
		if (isMetamodelElementColorID(id)) {
			if (colorID2TextAttribute.containsKey(id)) {
				return colorID2TextAttribute.get(id)
			} else {
				val style = metamodelElementTextStyle(id)
				colorID2TextAttribute.put(id, style)
				return style
			}
		} else {
			super.getAttribute(id)
		}
	}

	private def isMetamodelElementColorID(String id) {
		id.startsWith(SolverSemanticHighlightCalculator.SYMBOL_CODE)
	}

	private def TextAttribute metamodelElementTextStyle(String id) {
		val texts = id.split(' ')
		val backgroundColor = new RGB(
			Float.parseFloat(texts.get(1)),
			Float.parseFloat(texts.get(2)),
			Float.parseFloat(texts.get(3))
		)
		return new TextAttribute(EditorUtils.colorFromRGB(defaultTextStyle.color),
			EditorUtils.colorFromRGB(backgroundColor), defaultTextStyle.style,
			EditorUtils.fontFromFontData(defaultTextStyle.getFontData()));
	}

}