diff options
author | 2018-02-01 14:28:01 +0100 | |
---|---|---|
committer | 2018-02-01 14:28:01 +0100 | |
commit | 5cc9e724582ac4e302fad76d380c397ddeb27876 (patch) | |
tree | b51ee133babd08c336e739ae5f23ce68b50428ea /Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit | |
parent | Qualified name Converter (diff) | |
download | VIATRA-Generator-5cc9e724582ac4e302fad76d380c397ddeb27876.tar.gz VIATRA-Generator-5cc9e724582ac4e302fad76d380c397ddeb27876.tar.zst VIATRA-Generator-5cc9e724582ac4e302fad76d380c397ddeb27876.zip |
Type coloring
Diffstat (limited to 'Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit')
4 files changed, 173 insertions, 1 deletions
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/ApplicationConfigurationUiModule.xtend b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/ApplicationConfigurationUiModule.xtend index ad86ba44..6bc65f7b 100644 --- a/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/ApplicationConfigurationUiModule.xtend +++ b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/ApplicationConfigurationUiModule.xtend | |||
@@ -3,11 +3,27 @@ | |||
3 | */ | 3 | */ |
4 | package hu.bme.mit.inf.dslreasoner.application.ui | 4 | package hu.bme.mit.inf.dslreasoner.application.ui |
5 | 5 | ||
6 | import hu.bme.mit.inf.dslreasoner.application.ui.highlight.ApplicationConfigurationSemanticHighlightingCalculator | ||
7 | import hu.bme.mit.inf.dslreasoner.application.ui.highlight.MetamodelElementColoringTextAttributeProvider | ||
6 | import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor | 8 | import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor |
9 | import org.eclipse.xtext.ide.editor.syntaxcoloring.ISemanticHighlightingCalculator | ||
10 | import org.eclipse.xtext.ui.editor.syntaxcoloring.ITextAttributeProvider | ||
7 | 11 | ||
8 | /** | 12 | /** |
9 | * Use this class to register components to be used within the Eclipse IDE. | 13 | * Use this class to register components to be used within the Eclipse IDE. |
10 | */ | 14 | */ |
11 | @FinalFieldsConstructor | 15 | @FinalFieldsConstructor |
12 | class ApplicationConfigurationUiModule extends AbstractApplicationConfigurationUiModule { | 16 | class ApplicationConfigurationUiModule extends AbstractApplicationConfigurationUiModule { |
13 | } | 17 | |
18 | // def public Class<? extends IHighlightingConfiguration> bindIHighlightingConfiguration() { | ||
19 | // ApplicationConfigurationHighlightConfiguration | ||
20 | // } | ||
21 | |||
22 | def public Class<? extends ISemanticHighlightingCalculator> bindISemanticHighlightingCalculator() { | ||
23 | ApplicationConfigurationSemanticHighlightingCalculator | ||
24 | } | ||
25 | |||
26 | def public Class<? extends ITextAttributeProvider> bindITextAttributeProvider() { | ||
27 | MetamodelElementColoringTextAttributeProvider | ||
28 | } | ||
29 | } \ No newline at end of file | ||
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/ApplicationConfigurationSemanticHighlightingCalculator.xtend b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/ApplicationConfigurationSemanticHighlightingCalculator.xtend new file mode 100644 index 00000000..f99105be --- /dev/null +++ b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/ApplicationConfigurationSemanticHighlightingCalculator.xtend | |||
@@ -0,0 +1,62 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.ui.highlight | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.application.applicationConfiguration.MetamodelElement | ||
4 | import java.util.WeakHashMap | ||
5 | import org.eclipse.emf.ecore.EClassifier | ||
6 | import org.eclipse.xtext.ide.editor.syntaxcoloring.DefaultSemanticHighlightingCalculator | ||
7 | import org.eclipse.xtext.ide.editor.syntaxcoloring.IHighlightedPositionAcceptor | ||
8 | import org.eclipse.xtext.nodemodel.util.NodeModelUtils | ||
9 | import org.eclipse.xtext.resource.XtextResource | ||
10 | import org.eclipse.xtext.util.CancelIndicator | ||
11 | |||
12 | class ApplicationConfigurationSemanticHighlightingCalculator extends DefaultSemanticHighlightingCalculator { | ||
13 | public static val MetamodelElementIDPrefix = "MetamodelElementColor"; | ||
14 | |||
15 | val metamodelElement2ID = new WeakHashMap<EClassifier,String> | ||
16 | val colorCalculator = new ColorCalculator | ||
17 | |||
18 | override provideHighlightingFor(XtextResource resource, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) { | ||
19 | if (resource == null || resource.getParseResult() == null) { | ||
20 | return | ||
21 | } else { | ||
22 | val metamodelElements = resource.allContents.filter(MetamodelElement) | ||
23 | // val root = resource.getParseResult().getRootNode(); | ||
24 | // for (node : root.getAsTreeIterable()) { | ||
25 | // | ||
26 | // if (node.getGrammarElement() instanceof CrossReference) { | ||
27 | // acceptor.addPosition(node.getOffset(), node.getLength(), ApplicationConfigurationHighlightConfiguration.CROSS_REF); | ||
28 | // } | ||
29 | // } | ||
30 | while(metamodelElements.hasNext) { | ||
31 | val mentamodelElement = metamodelElements.next | ||
32 | val node = NodeModelUtils::getNode(mentamodelElement) | ||
33 | acceptor.addPosition( | ||
34 | node.getOffset(), | ||
35 | node.getLength(), | ||
36 | getMetamodelElementColorID(mentamodelElement) | ||
37 | ) | ||
38 | } | ||
39 | } | ||
40 | } | ||
41 | |||
42 | def getMetamodelElementColorID(MetamodelElement element) { | ||
43 | val classifier = element.classifier | ||
44 | if(classifier === null) { | ||
45 | return "default" | ||
46 | } else if(classifier.eIsProxy) { | ||
47 | return "default" | ||
48 | } else { | ||
49 | if(metamodelElement2ID.containsKey(classifier)) { | ||
50 | return metamodelElement2ID.get(classifier) | ||
51 | } else { | ||
52 | val id = calculateMetamodelElementID(classifier) | ||
53 | metamodelElement2ID.put(classifier,id) | ||
54 | return id | ||
55 | } | ||
56 | } | ||
57 | } | ||
58 | private def String calculateMetamodelElementID(EClassifier c) { | ||
59 | val randomColor = colorCalculator.getColor(c) | ||
60 | '''«MetamodelElementIDPrefix» «randomColor.get(0)» «randomColor.get(1)» «randomColor.get(2)»''' | ||
61 | } | ||
62 | } | ||
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/ColorCalculator.xtend b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/ColorCalculator.xtend new file mode 100644 index 00000000..d0f9db88 --- /dev/null +++ b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/ColorCalculator.xtend | |||
@@ -0,0 +1,38 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.ui.highlight | ||
2 | |||
3 | import java.util.List | ||
4 | import java.util.Random | ||
5 | import org.eclipse.emf.ecore.EClass | ||
6 | import org.eclipse.emf.ecore.EEnum | ||
7 | |||
8 | class ColorCalculator { | ||
9 | |||
10 | public def dispatch getColor(EClass c) { | ||
11 | val supertypes = (c.getEAllSuperTypes + #[c]) | ||
12 | val typeHashcodes = supertypes.map[(c.getEPackage.nsURI->c.name).hashCode] | ||
13 | return randomColor(typeHashcodes) | ||
14 | } | ||
15 | public def dispatch getColor(EEnum e) { | ||
16 | return randomColor((e.getEPackage.nsURI->e.name).hashCode) | ||
17 | } | ||
18 | |||
19 | protected def randomColor(Integer seed) { | ||
20 | val Random random = new Random(seed) | ||
21 | val rangePicker = [|random.nextInt(128)+128] | ||
22 | return #[rangePicker.apply(), rangePicker.apply(), rangePicker.apply()] | ||
23 | } | ||
24 | protected def randomColor(Iterable<Integer> seeds) { seeds.map[randomColor].averageColor } | ||
25 | private def averageColor(Iterable<List<Integer>> colors) { | ||
26 | if(colors.empty) { | ||
27 | return #[256,256,256] | ||
28 | } else { | ||
29 | return #[ | ||
30 | colors.map[get(0)].average, | ||
31 | colors.map[get(1)].average, | ||
32 | colors.map[get(2)].average | ||
33 | ] | ||
34 | } | ||
35 | } | ||
36 | private def average(Iterable<Integer> doubles) { return doubles.reduce[p1, p2|p1+p2]/doubles.size } | ||
37 | } | ||
38 | |||
diff --git a/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/MetamodelElementColoringTextAttributeProvider.xtend b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/MetamodelElementColoringTextAttributeProvider.xtend new file mode 100644 index 00000000..10b96d02 --- /dev/null +++ b/Application/hu.bme.mit.inf.dslreasoner.application.ui/src/hu/bme/mit/inf/dslreasoner/application/ui/highlight/MetamodelElementColoringTextAttributeProvider.xtend | |||
@@ -0,0 +1,56 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.application.ui.highlight | ||
2 | |||
3 | import com.google.inject.Inject | ||
4 | import java.util.WeakHashMap | ||
5 | import org.eclipse.jface.text.TextAttribute | ||
6 | import org.eclipse.swt.graphics.RGB | ||
7 | import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess | ||
8 | import org.eclipse.xtext.ui.editor.syntaxcoloring.IHighlightingConfiguration | ||
9 | import org.eclipse.xtext.ui.editor.syntaxcoloring.PreferenceStoreAccessor | ||
10 | import org.eclipse.xtext.ui.editor.syntaxcoloring.TextAttributeProvider | ||
11 | import org.eclipse.xtext.ui.editor.utils.TextStyle | ||
12 | import org.eclipse.xtext.ui.editor.utils.EditorUtils | ||
13 | import org.eclipse.swt.SWT | ||
14 | |||
15 | class MetamodelElementColoringTextAttributeProvider extends TextAttributeProvider{ | ||
16 | |||
17 | val defaultTextStyle = new TextStyle | ||
18 | val colorID2TextAttribute = new WeakHashMap<String,TextAttribute> | ||
19 | |||
20 | @Inject | ||
21 | new(IHighlightingConfiguration highlightingConfig, IPreferenceStoreAccess preferenceStoreAccess, PreferenceStoreAccessor prefStoreAccessor) { | ||
22 | super(highlightingConfig, preferenceStoreAccess, prefStoreAccessor) | ||
23 | } | ||
24 | |||
25 | override getAttribute(String id) { | ||
26 | if(isMetamodelElementColorID(id)) { | ||
27 | if(colorID2TextAttribute.containsKey(id)) { | ||
28 | return colorID2TextAttribute.get(id) | ||
29 | } else { | ||
30 | val style = metamodelElementTextStyle(id) | ||
31 | colorID2TextAttribute.put(id,style) | ||
32 | return style | ||
33 | } | ||
34 | } else { | ||
35 | super.getAttribute(id) | ||
36 | } | ||
37 | } | ||
38 | |||
39 | private def isMetamodelElementColorID(String id) { | ||
40 | id.startsWith(ApplicationConfigurationSemanticHighlightingCalculator::MetamodelElementIDPrefix) | ||
41 | } | ||
42 | |||
43 | private def TextAttribute metamodelElementTextStyle(String id) { | ||
44 | val texts = id.split(' ') | ||
45 | val backgroundColor = new RGB( | ||
46 | Integer.parseInt(texts.get(1)), | ||
47 | Integer.parseInt(texts.get(2)), | ||
48 | Integer.parseInt(texts.get(3)) | ||
49 | ) | ||
50 | return new TextAttribute( | ||
51 | EditorUtils.colorFromRGB(defaultTextStyle.color), | ||
52 | EditorUtils.colorFromRGB(backgroundColor), | ||
53 | defaultTextStyle.style.bitwiseAnd(SWT.BORDER), | ||
54 | EditorUtils.fontFromFontData(defaultTextStyle.getFontData())); | ||
55 | } | ||
56 | } | ||