TextComponent no longer overrides getBounds but rather calls setSize()
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 30 Jun 2019 16:14:24 +0000 (18:14 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Sun, 30 Jun 2019 16:14:24 +0000 (18:14 +0200)
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/GUIComponent.java
net.mograsim.logic.ui/src/net/mograsim/logic/ui/model/components/TextComponent.java

index a80c15c..655422a 100644 (file)
@@ -203,7 +203,7 @@ public abstract class GUIComponent
         * 
         * @author Daniel Kirschten
         */
-       public Rectangle getBounds()
+       public final Rectangle getBounds()
        {
                return new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height);
        }
index 542b7f0..b72c2f2 100644 (file)
@@ -10,35 +10,32 @@ import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
 import net.mograsim.logic.ui.modeladapter.componentadapters.NoLogicAdapter;\r
 import net.mograsim.preferences.Preferences;\r
 \r
+//TODO clean size calculation mess\r
 public class TextComponent extends GUIComponent\r
 {\r
        private final String text;\r
-       private Point textExtent;\r
 \r
        public TextComponent(ViewModelModifiable model, String text)\r
        {\r
                super(model);\r
                this.text = text;\r
+               // If size is unset, it defaults to 0, which could prohibit this component from being rendered, which would prohibit the size being\r
+               // set to a better guess\r
+               setSize(1, 1);\r
        }\r
 \r
        @Override\r
        public void render(GeneralGC gc, Rectangle visibleRegion)\r
        {\r
-               textExtent = gc.textExtent(text);\r
+               Point textExtent = gc.textExtent(text);\r
+               setSize(textExtent.x, textExtent.y);\r
+\r
                Color textColor = Preferences.current().getColor("net.mograsim.logic.ui.color.text");\r
                if (textColor != null)\r
                        gc.setForeground(textColor);\r
                gc.drawText(text, getPosX(), getPosY(), true);\r
        }\r
 \r
-       @Override\r
-       public Rectangle getBounds()\r
-       {\r
-               if (textExtent == null)\r
-                       return super.getBounds();\r
-               return new Rectangle(getPosX(), getPosY(), textExtent.x, textExtent.y);\r
-       }\r
-\r
        static\r
        {\r
                ViewLogicModelAdapter.addComponentAdapter(new NoLogicAdapter<>(TextComponent.class));\r