aboutsummaryrefslogtreecommitdiffstats
path: root/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/MetamodelElementColoringTextAttributeProvider.xtend
blob: 10b96d025f5a846fb77cb7e9158a44ed29971415 (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
56
package hu.bme.mit.inf.dslreasoner.application.ui.highlight

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.TextStyle
import org.eclipse.xtext.ui.editor.utils.EditorUtils
import org.eclipse.swt.SWT

class MetamodelElementColoringTextAttributeProvider 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(ApplicationConfigurationSemanticHighlightingCalculator::MetamodelElementIDPrefix)
	}
	
	private def TextAttribute metamodelElementTextStyle(String id) {
		val texts = id.split(' ')
		val backgroundColor = new RGB(
			Integer.parseInt(texts.get(1)),
			Integer.parseInt(texts.get(2)),
			Integer.parseInt(texts.get(3))
		)
		return new TextAttribute(
			EditorUtils.colorFromRGB(defaultTextStyle.color),
			EditorUtils.colorFromRGB(backgroundColor),
			defaultTextStyle.style.bitwiseAnd(SWT.BORDER),
			EditorUtils.fontFromFontData(defaultTextStyle.getFontData()));
	}
}