|
| 1 | +package com.thecoderscorner.embedcontrol.core.controlmgr; |
| 2 | + |
| 3 | +import com.thecoderscorner.embedcontrol.core.controlmgr.color.ConditionalColoring; |
| 4 | +import com.thecoderscorner.embedcontrol.customization.FontInformation; |
| 5 | +import com.thecoderscorner.embedcontrol.customization.customdraw.CustomDrawingConfiguration; |
| 6 | +import com.thecoderscorner.menu.domain.*; |
| 7 | + |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +import static com.thecoderscorner.embedcontrol.core.controlmgr.EditorComponent.PortableAlignment; |
| 11 | +import static com.thecoderscorner.embedcontrol.customization.MenuFormItem.FONT_100_PERCENT; |
| 12 | + |
| 13 | +/** |
| 14 | + * ComponentSettingsBuilder is a builder class for creating instances of ComponentSettings. |
| 15 | + * It provides various methods to customize the settings of a component, such as font, colors, |
| 16 | + * justification, position, control type, drawing mode, and custom drawing configuration. |
| 17 | + */ |
| 18 | +public class ComponentSettingsBuilder { |
| 19 | + public enum BuildingMode { MENU, TEXT, IMAGE } |
| 20 | + |
| 21 | + private final static Set<EditItemType> POSSIBLE_TIME_TYPES = Set.of( |
| 22 | + EditItemType.TIME_12H, |
| 23 | + EditItemType.TIME_24_HUNDREDS, |
| 24 | + EditItemType.TIME_24H, |
| 25 | + EditItemType.TIME_12H_HHMM, |
| 26 | + EditItemType.TIME_24H_HHMM); |
| 27 | + |
| 28 | + private BuildingMode mode; |
| 29 | + private String text; |
| 30 | + private MenuItem item; |
| 31 | + private FontInformation fontInfo = FONT_100_PERCENT; |
| 32 | + private ConditionalColoring colors; |
| 33 | + private PortableAlignment justification = PortableAlignment.LEFT_VAL_RIGHT; |
| 34 | + private ComponentPositioning position = new ComponentPositioning(0, 0); |
| 35 | + private ControlType controlType = ControlType.TEXT_CONTROL; |
| 36 | + private RedrawingMode drawMode = RedrawingMode.SHOW_NAME_VALUE; |
| 37 | + private CustomDrawingConfiguration customDrawing = CustomDrawingConfiguration.NO_CUSTOM_DRAWING; |
| 38 | + |
| 39 | + /// Create component settings builder object from a menu item. It defaults the fields to reasonable values as |
| 40 | + /// much as possible by setting the font to 100% size, setting the control type to the default, and setting the |
| 41 | + /// justification to the default too. |
| 42 | + /// @param item the menu item to build for |
| 43 | + /// @param color the colors to use for the control |
| 44 | + public static ComponentSettingsBuilder forMenuItem(MenuItem item, ConditionalColoring color) { |
| 45 | + var b = new ComponentSettingsBuilder(); |
| 46 | + b.mode = BuildingMode.MENU; |
| 47 | + b.colors = color; |
| 48 | + b.item = item; |
| 49 | + b.withControlType(defaultControlForType(item)); |
| 50 | + b.withJustification(defaultJustificationForType(b.controlType)); |
| 51 | + return b; |
| 52 | + } |
| 53 | + |
| 54 | + /// Create a component settings builder object that will represent some text on the display at a |
| 55 | + /// particular grid position, will default to the text color, 100% font size, and left justification. |
| 56 | + /// @param text the text to present |
| 57 | + /// @param color the color set to use |
| 58 | + public static ComponentSettingsBuilder forText(String text, ConditionalColoring color) { |
| 59 | + var b = new ComponentSettingsBuilder(); |
| 60 | + b.mode = BuildingMode.TEXT; |
| 61 | + b.colors = color; |
| 62 | + b.text = text; |
| 63 | + b.withControlType(ControlType.TEXT_CONTROL); |
| 64 | + b.withJustification(PortableAlignment.LEFT); |
| 65 | + return b; |
| 66 | + } |
| 67 | + |
| 68 | + private static PortableAlignment defaultJustificationForType(ControlType controlType) { |
| 69 | + return switch(controlType) { |
| 70 | + case HORIZONTAL_SLIDER, UP_DOWN_CONTROL -> PortableAlignment.LEFT_VAL_RIGHT; |
| 71 | + case BUTTON_CONTROL, VU_METER, ROTARY_METER -> PortableAlignment.CENTER; |
| 72 | + default -> PortableAlignment.LEFT; |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + public static ControlType defaultControlForType(MenuItem item) { |
| 77 | + return switch(item) { |
| 78 | + case SubMenuItem _, BooleanMenuItem _, ActionMenuItem _ -> ControlType.BUTTON_CONTROL; |
| 79 | + case AnalogMenuItem _ -> ControlType.HORIZONTAL_SLIDER; |
| 80 | + case EnumMenuItem _, ScrollChoiceMenuItem _ -> ControlType.UP_DOWN_CONTROL; |
| 81 | + case Rgb32MenuItem _ -> ControlType.RGB_CONTROL; |
| 82 | + case RuntimeListMenuItem _ -> ControlType.LIST_CONTROL; |
| 83 | + case CustomBuilderMenuItem _ -> ControlType.AUTH_IOT_CONTROL; |
| 84 | + case EditableTextMenuItem txt when txt.getItemType() == EditItemType.GREGORIAN_DATE -> ControlType.DATE_CONTROL; |
| 85 | + case EditableTextMenuItem txt when POSSIBLE_TIME_TYPES.contains(txt.getItemType()) -> ControlType.TIME_CONTROL; |
| 86 | + default -> ControlType.TEXT_CONTROL; |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + /// Override the font from the default 100% size to another value |
| 91 | + /// @param fontInfo the font to override with. |
| 92 | + public ComponentSettingsBuilder withFont(FontInformation fontInfo) { |
| 93 | + this.fontInfo = fontInfo; |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /// Change the conditional coloring from the default one chosen. |
| 98 | + /// @param colors the conditional colors |
| 99 | + public ComponentSettingsBuilder withColors(ConditionalColoring colors) { |
| 100 | + this.colors = colors; |
| 101 | + return this; |
| 102 | + } |
| 103 | + |
| 104 | + /// Change the justification from the default value which is guessed during `forMenuItem` based on the control. |
| 105 | + /// @param justification the justification to use |
| 106 | + public ComponentSettingsBuilder withJustification(PortableAlignment justification) { |
| 107 | + this.justification = justification; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + /// Set the position of the control in the grid. Must always be set, for simpler cases with |
| 112 | + /// no span you can use `withRowCol` |
| 113 | + /// @param position the position and span in the grid to create with |
| 114 | + public ComponentSettingsBuilder withPosition(ComponentPositioning position) { |
| 115 | + this.position = position; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + /// Set the position of the control in the grid. Must always be set |
| 120 | + /// @param row the zero based row |
| 121 | + /// @param col the zero based column |
| 122 | + public ComponentSettingsBuilder withRowCol(int row, int col) { |
| 123 | + this.position = new ComponentPositioning(row, col); |
| 124 | + return this; |
| 125 | + } |
| 126 | + |
| 127 | + /// Override the control type that was guessed during `forMenuItem`. You should be careful that the control type |
| 128 | + /// you choose is compatible with the menu item type. |
| 129 | + /// @param controlType the control type to use |
| 130 | + /// @throws IllegalArgumentException if the control type is invalid for the menu item |
| 131 | + public ComponentSettingsBuilder withControlType(ControlType controlType) { |
| 132 | + if(mode != BuildingMode.MENU) { |
| 133 | + controlType = ControlType.TEXT_CONTROL; |
| 134 | + } else if(!controlType.isSupportedFor(item)) { |
| 135 | + throw new IllegalArgumentException("Control type %s cannot render %s".formatted(controlType, item.getClass().getSimpleName())); |
| 136 | + } else { |
| 137 | + this.controlType = controlType; |
| 138 | + } |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + /// Sets the drawing mode for the item, defaults to show name and item. |
| 143 | + /// @param drawMode the drawing mode |
| 144 | + public ComponentSettingsBuilder withDrawMode(RedrawingMode drawMode) { |
| 145 | + this.drawMode = drawMode; |
| 146 | + return this; |
| 147 | + } |
| 148 | + |
| 149 | + /// Configure a custom drawing for the item, again make sure it is compatible with the menu type your using. |
| 150 | + /// @param customDrawing the custom drawing to be used, must be compatible with the menu item type. |
| 151 | + /// @throws IllegalArgumentException if the custom drawing is incompatible with the menu item type |
| 152 | + public ComponentSettingsBuilder withCustomDrawing(CustomDrawingConfiguration customDrawing) { |
| 153 | + if(!customDrawing.isSupportedFor(item)) { |
| 154 | + throw new IllegalArgumentException("Custom drawing %s cannot render %s".formatted(customDrawing, item.getClass().getSimpleName())); |
| 155 | + } |
| 156 | + this.customDrawing = customDrawing; |
| 157 | + return this; |
| 158 | + } |
| 159 | + |
| 160 | + /// Get the menuitem for this builder |
| 161 | + /// @return menu item |
| 162 | + public MenuItem getItem() { |
| 163 | + return item; |
| 164 | + } |
| 165 | + |
| 166 | + /// Get the static text associated with this builder |
| 167 | + /// @return the static text |
| 168 | + public String getText() { |
| 169 | + return text; |
| 170 | + } |
| 171 | + |
| 172 | + /// Get the mode of the building, IE text, menu item etc. |
| 173 | + /// @return the building mode |
| 174 | + public BuildingMode getMode() { |
| 175 | + return mode; |
| 176 | + } |
| 177 | + |
| 178 | + /// Creates the component settings |
| 179 | + /// @return the built object |
| 180 | + public ComponentSettings build() { |
| 181 | + return new ComponentSettings(colors, fontInfo, justification, position, drawMode, controlType, customDrawing, true); |
| 182 | + } |
| 183 | +} |
0 commit comments