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