Skip to content

Commit 8fc229e

Browse files
committed
#537 clean up embed control code
1 parent 609151b commit 8fc229e

File tree

2 files changed

+75
-4
lines changed

2 files changed

+75
-4
lines changed

embedCONTROLCore/src/main/java/com/thecoderscorner/embedcontrol/core/controlmgr/ComponentSettingsBuilder.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@
1616
* justification, position, control type, drawing mode, and custom drawing configuration.
1717
*/
1818
public class ComponentSettingsBuilder {
19+
public enum BuildingMode { MENU, TEXT, IMAGE }
20+
1921
private final static Set<EditItemType> POSSIBLE_TIME_TYPES = Set.of(
2022
EditItemType.TIME_12H,
2123
EditItemType.TIME_24_HUNDREDS,
2224
EditItemType.TIME_24H,
2325
EditItemType.TIME_12H_HHMM,
2426
EditItemType.TIME_24H_HHMM);
2527

28+
private BuildingMode mode;
29+
private String text;
2630
private MenuItem item;
2731
private FontInformation fontInfo = FONT_100_PERCENT;
2832
private ConditionalColoring colors;
@@ -39,13 +43,24 @@ public class ComponentSettingsBuilder {
3943
/// @param color the colors to use for the control
4044
public static ComponentSettingsBuilder forMenuItem(MenuItem item, ConditionalColoring color) {
4145
var b = new ComponentSettingsBuilder();
46+
b.mode = BuildingMode.MENU;
4247
b.colors = color;
4348
b.item = item;
4449
b.withControlType(defaultControlForType(item));
4550
b.withJustification(defaultJustificationForType(b.controlType));
4651
return b;
4752
}
4853

54+
public static ComponentSettingsBuilder forText(String text, ConditionalColoring color) {
55+
var b = new ComponentSettingsBuilder();
56+
b.mode = BuildingMode.TEXT;
57+
b.colors = color;
58+
b.text = text;
59+
b.withControlType(ControlType.TEXT_CONTROL);
60+
b.withJustification(PortableAlignment.LEFT);
61+
return b;
62+
}
63+
4964
private static PortableAlignment defaultJustificationForType(ControlType controlType) {
5065
return switch(controlType) {
5166
case HORIZONTAL_SLIDER, UP_DOWN_CONTROL -> PortableAlignment.LEFT_VAL_RIGHT;
@@ -89,22 +104,34 @@ public ComponentSettingsBuilder withJustification(PortableAlignment justificatio
89104
return this;
90105
}
91106

92-
/// Set the position of the control in the grid. Pretty much must always be set
107+
/// Set the position of the control in the grid. Must always be set, for simpler cases with
108+
/// no span you can use `withRowCol`
93109
/// @param position the position and span in the grid to create with
94110
public ComponentSettingsBuilder withPosition(ComponentPositioning position) {
95111
this.position = position;
96112
return this;
97113
}
98114

115+
/// Set the position of the control in the grid. Must always be set
116+
/// @param row the zero based row
117+
/// @param col the zero based column
118+
public ComponentSettingsBuilder withRowCol(int row, int col) {
119+
this.position = new ComponentPositioning(row, col);
120+
return this;
121+
}
122+
99123
/// Override the control type that was guessed during `forMenuItem`. You should be careful that the control type
100124
/// you choose is compatible with the menu item type.
101125
/// @param controlType the control type to use
102126
/// @throws IllegalArgumentException if the control type is invalid for the menu item
103127
public ComponentSettingsBuilder withControlType(ControlType controlType) {
104-
if(!controlType.isSupportedFor(item)) {
128+
if(mode != BuildingMode.MENU) {
129+
controlType = ControlType.TEXT_CONTROL;
130+
} else if(!controlType.isSupportedFor(item)) {
105131
throw new IllegalArgumentException("Control type %s cannot render %s".formatted(controlType, item.getClass().getSimpleName()));
132+
} else {
133+
this.controlType = controlType;
106134
}
107-
this.controlType = controlType;
108135
return this;
109136
}
110137

@@ -132,6 +159,18 @@ public MenuItem getItem() {
132159
return item;
133160
}
134161

162+
/// Get the static text associated with this builder
163+
/// @return the static text
164+
public String getText() {
165+
return text;
166+
}
167+
168+
/// Get the mode of the building, IE text, menu item etc.
169+
/// @return the building mode
170+
public BuildingMode getMode() {
171+
return mode;
172+
}
173+
135174
/// Creates the component settings
136175
/// @return the built object
137176
public ComponentSettings build() {

embedCONTROLCore/src/main/java/com/thecoderscorner/embedcontrol/jfx/controlmgr/panels/BaseCustomMenuPanel.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@
22

33
import com.thecoderscorner.embedcontrol.core.controlmgr.*;
44
import com.thecoderscorner.embedcontrol.core.controlmgr.color.ConditionalColoring;
5+
import com.thecoderscorner.embedcontrol.customization.FontInformation;
56
import com.thecoderscorner.embedcontrol.jfx.controlmgr.UpdatablePanel;
67
import com.thecoderscorner.menu.domain.MenuItem;
78
import com.thecoderscorner.menu.domain.state.MenuTree;
89
import com.thecoderscorner.menu.remote.commands.AckStatus;
910
import com.thecoderscorner.menu.remote.protocol.CorrelationId;
1011
import javafx.scene.Node;
12+
import javafx.scene.control.Label;
1113
import javafx.scene.layout.Background;
1214
import javafx.scene.layout.BackgroundFill;
1315
import javafx.scene.layout.GridPane;
16+
import javafx.scene.text.Font;
17+
import javafx.scene.text.TextAlignment;
1418

1519
import java.util.HashMap;
1620

@@ -59,8 +63,36 @@ public Node getPanelToPresent(double width) throws Exception {
5963
}
6064
protected abstract void populateGrid();
6165

66+
private Font calculateFont(FontInformation fontInfo, Font current) {
67+
if(fontInfo.sizeMeasurement() == FontInformation.SizeMeasurement.PERCENT) {
68+
return Font.font(current.getSize() * (fontInfo.fontSize() / 100.0));
69+
} else {
70+
return Font.font(fontInfo.fontSize());
71+
}
72+
}
73+
74+
private TextAlignment toTextAlign(EditorComponent.PortableAlignment justification) {
75+
return switch (justification) {
76+
case LEFT, LEFT_VAL_RIGHT -> TextAlignment.LEFT;
77+
case RIGHT -> TextAlignment.RIGHT;
78+
case CENTER -> TextAlignment.CENTER;
79+
};
80+
}
81+
6282
protected void putIntoGrid(ComponentSettingsBuilder builder) {
63-
putIntoGrid(builder.getItem(), builder.build());
83+
if(builder.getMode() == ComponentSettingsBuilder.BuildingMode.TEXT) {
84+
var settings = builder.build();
85+
var comp = new Label(builder.getText());
86+
comp.setFont(calculateFont(settings.getFontInfo(), comp.getFont()));
87+
comp.setTextAlignment(toTextAlign(settings.getJustification()));
88+
comp.setTextFill(asFxColor(settings.getColors().foregroundFor(NORMAL, TEXT_FIELD)));
89+
var pos = settings.getPosition();
90+
gridPane.add(comp, pos.getCol(), pos.getRow(), pos.getColSpan(), pos.getRowSpan());
91+
} else if(builder.getMode() == ComponentSettingsBuilder.BuildingMode.MENU){
92+
putIntoGrid(builder.getItem(), builder.build());
93+
} else {
94+
throw new IllegalArgumentException("Unsupported mode " + builder.getMode());
95+
}
6496
}
6597

6698
protected void putIntoGrid(MenuItem item, ComponentSettings componentSettings) {

0 commit comments

Comments
 (0)