The final restructured version for automatic build using maven tycho
[Mograsim.git] / plugins / net.mograsim.logic.model / src / net / mograsim / logic / model / model / components / atomic / ModelTextComponent.java
1 package net.mograsim.logic.model.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.model.model.LogicModelModifiable;
9 import net.mograsim.logic.model.model.components.ModelComponent;
10 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
11 import net.mograsim.logic.model.modeladapter.componentadapters.NoLogicAdapter;
12 import net.mograsim.logic.model.serializing.IdentifyParams;
13 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
14 import net.mograsim.preferences.Preferences;
15
16 //TODO clean size calculation mess
17 public class ModelTextComponent extends ModelComponent
18 {
19         private final String text;
20         private boolean calculatedSize;
21
22         public ModelTextComponent(LogicModelModifiable model, String text)
23         {
24                 this(model, text, null);
25         }
26
27         public ModelTextComponent(LogicModelModifiable model, String text, String name)
28         {
29                 super(model, name, false);
30                 this.text = text;
31                 // If size is unset, it defaults to 0, which could prohibit this component from being rendered, which would prohibit the size being
32                 // set to a better guess
33                 setSize(1, 1);
34
35                 init();
36         }
37
38         @Override
39         public void render(GeneralGC gc, Rectangle visibleRegion)
40         {
41                 if (!calculatedSize)
42                 {
43                         calculatedSize = true;
44                         Point textExtent = gc.textExtent(text);
45                         setSize(textExtent.x, textExtent.y);
46                 }
47
48                 Color textColor = Preferences.current().getColor("net.mograsim.logic.model.color.text");
49                 if (textColor != null)
50                         gc.setForeground(textColor);
51                 gc.drawText(text, getPosX(), getPosY(), true);
52         }
53
54         // serializing
55
56         @Override
57         public String getIDForSerializing(IdentifyParams idParams)
58         {
59                 return "TextComponent";
60         }
61
62         @Override
63         public String getParamsForSerializing(IdentifyParams idParams)
64         {
65                 return text;
66         }
67
68         static
69         {
70                 LogicCoreAdapter.addComponentAdapter(new NoLogicAdapter<>(ModelTextComponent.class));
71                 IndirectModelComponentCreator.setComponentSupplier(ModelTextComponent.class.getName(),
72                                 (m, p, n) -> new ModelTextComponent(m, p.getAsString(), n));
73         }
74 }