Restructured serializing classes
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / atomic / TextComponent.java
1 package net.mograsim.logic.ui.model.components.atomic;
2
3 import org.eclipse.swt.graphics.Color;
4
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;
8 import net.mograsim.logic.ui.model.ViewModelModifiable;
9 import net.mograsim.logic.ui.model.components.GUIComponent;
10 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
11 import net.mograsim.logic.ui.modeladapter.componentadapters.NoLogicAdapter;
12 import net.mograsim.preferences.Preferences;
13
14 //TODO clean size calculation mess
15 public class TextComponent extends GUIComponent
16 {
17         private final String text;
18
19         public TextComponent(ViewModelModifiable model, String text)
20         {
21                 super(model);
22                 this.text = text;
23                 // If size is unset, it defaults to 0, which could prohibit this component from being rendered, which would prohibit the size being
24                 // set to a better guess
25                 setSize(1, 1);
26         }
27
28         @Override
29         public void render(GeneralGC gc, Rectangle visibleRegion)
30         {
31                 Point textExtent = gc.textExtent(text);
32                 setSize(textExtent.x, textExtent.y);
33
34                 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");
35                 if (textColor != null)
36                         gc.setForeground(textColor);
37                 gc.drawText(text, getPosX(), getPosY(), true);
38         }
39
40         static
41         {
42                 ViewLogicModelAdapter.addComponentAdapter(new NoLogicAdapter<>(TextComponent.class));
43         }
44 }