Added getBounds() to TextComponent
[Mograsim.git] / net.mograsim.logic.ui / src / net / mograsim / logic / ui / model / components / TextComponent.java
1 package net.mograsim.logic.ui.model.components;\r
2 \r
3 import org.eclipse.swt.graphics.Color;\r
4 \r
5 import net.haspamelodica.swt.helper.gcs.GeneralGC;\r
6 import net.haspamelodica.swt.helper.swtobjectwrappers.Point;\r
7 import net.haspamelodica.swt.helper.swtobjectwrappers.Rectangle;\r
8 import net.mograsim.logic.ui.model.ViewModelModifiable;\r
9 import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;\r
10 import net.mograsim.logic.ui.modeladapter.componentadapters.NoLogicAdapter;\r
11 import net.mograsim.preferences.Preferences;\r
12 \r
13 public class TextComponent extends GUIComponent\r
14 {\r
15         private final String text;\r
16         private Point textExtent;\r
17 \r
18         public TextComponent(ViewModelModifiable model, String text)\r
19         {\r
20                 super(model);\r
21                 this.text = text;\r
22         }\r
23 \r
24         @Override\r
25         public void render(GeneralGC gc, Rectangle visibleRegion)\r
26         {\r
27                 textExtent = gc.textExtent(text);\r
28                 Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");\r
29                 if (textColor != null)\r
30                         gc.setForeground(textColor);\r
31                 gc.drawText(text, getPosX() - textExtent.x / 2, getPosY() - textExtent.y / 2, true);\r
32         }\r
33 \r
34         @Override\r
35         public Rectangle getBounds()\r
36         {\r
37                 if (textExtent == null)\r
38                         return super.getBounds();\r
39                 return new Rectangle(getPosX() - textExtent.x / 2, getPosY() - textExtent.y / 2, textExtent.x, textExtent.y);\r
40         }\r
41 \r
42         static\r
43         {\r
44                 ViewLogicModelAdapter.addComponentAdapter(NoLogicAdapter.forClass(TextComponent.class));\r
45         }\r
46 }\r