Skip to content

Commit 38dd5c9

Browse files
Enhance documentation for EnumIconImage with detailed descriptions, usage examples, and method explanations
1 parent c2fd157 commit 38dd5c9

File tree

1 file changed

+144
-18
lines changed

1 file changed

+144
-18
lines changed

platform/ui/zk/src/main/java/tools/dynamia/zk/ui/EnumIconImage.java

Lines changed: 144 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,50 +29,126 @@
2929
import tools.dynamia.zk.ComponentAliasIndex;
3030
import tools.dynamia.zk.util.ZKUtil;
3131

32-
public class EnumIconImage extends Span implements LoadableOnly{
32+
/**
33+
* A ZK component that displays an icon representation for enum values.
34+
* This component extends Span and provides a visual way to render enum constants
35+
* using icons instead of plain text. Each enum ordinal can be mapped to a specific icon name.
36+
*
37+
* <p>The component supports both image-based and font-based icons through the IconsTheme system.
38+
* If no icon mapping is found for a given enum value, it falls back to displaying the enum name as text.</p>
39+
*
40+
* <p>Example usage in ZUL:</p>
41+
* <pre>{@code
42+
* <enumIconImage value="@bind(vm.status)"
43+
* iconsNames="check-circle,clock,times-circle"
44+
* size="LARGE"/>
45+
* }</pre>
46+
*
47+
* <p>This component is registered in both ComponentAliasIndex and BindingComponentIndex
48+
* for easy integration with ZK's data binding system.</p>
49+
*
50+
* @see IconsTheme
51+
* @see IconSize
52+
* @see LoadableOnly
53+
*/
54+
public class EnumIconImage extends Span implements LoadableOnly {
3355

3456
/**
35-
*
57+
* Serial version UID for serialization compatibility.
3658
*/
3759
private static final long serialVersionUID = -5975771607086380537L;
3860

61+
/**
62+
* Static initializer block that registers this component in the framework's index systems.
63+
* Adds the component to ComponentAliasIndex for alias resolution and to BindingComponentIndex
64+
* for data binding support on the "value" property.
65+
*/
3966
static {
4067
ComponentAliasIndex.getInstance().add(EnumIconImage.class);
4168
BindingComponentIndex.getInstance().put("value", EnumIconImage.class);
4269
}
4370

71+
/**
72+
* The size of the icon to be displayed.
73+
*/
4474
private IconSize size = IconSize.NORMAL;
75+
76+
/**
77+
* The enum value to be displayed as an icon.
78+
*/
4579
private Enum value;
80+
81+
/**
82+
* Array of icon names mapped to enum ordinals.
83+
* Each position in the array corresponds to an enum ordinal value.
84+
*/
4685
private String[] iconsNames;
4786

87+
/**
88+
* Sets the value to be displayed as an icon.
89+
* This method accepts an Object parameter but only processes Enum values.
90+
* Non-enum values are ignored.
91+
*
92+
* @param value the enum value to display. If not an Enum instance, the method does nothing
93+
*
94+
* Example:
95+
* <pre>{@code
96+
* EnumIconImage iconImage = new EnumIconImage();
97+
* iconImage.setValue(Status.ACTIVE);
98+
* }</pre>
99+
*/
48100
public void setValue(Object value) {
49101
if (value instanceof Enum) {
50102
this.value = (Enum) value;
51103
render();
52104
}
53105
}
54106

107+
/**
108+
* Renders the icon representation of the current enum value.
109+
* This method clears existing children and creates the appropriate component
110+
* based on the icon type (Image or font-based icon). If no icon mapping exists,
111+
* it displays the enum name as a Label.
112+
*
113+
* <p>The rendering process:</p>
114+
* <ul>
115+
* <li>Clears all existing child components</li>
116+
* <li>Retrieves the icon name mapped to the enum's ordinal</li>
117+
* <li>Creates an Image component for image-based icons</li>
118+
* <li>Creates an I (italic) component for font-based icons</li>
119+
* <li>Falls back to a Label with the enum name if no icon is found</li>
120+
* </ul>
121+
*/
55122
private void render() {
56123
getChildren().clear();
57-
setTooltiptext(null);
58-
String iconName = getIconName();
59-
if (iconName != null) {
60-
Icon icon = IconsTheme.get().getIcon(iconName);
61-
setTooltiptext(value.name());
62-
if (icon.getType() == IconType.IMAGE) {
63-
Image image = new Image();
64-
image.setParent(this);
65-
ZKUtil.configureComponentIcon(icon, image, size);
124+
if (value != null) {
125+
setTooltiptext(null);
126+
String iconName = getIconName();
127+
if (iconName != null) {
128+
Icon icon = IconsTheme.get().getIcon(iconName);
129+
setTooltiptext(value.name());
130+
if (icon.getType() == IconType.IMAGE) {
131+
Image image = new Image();
132+
image.setParent(this);
133+
ZKUtil.configureComponentIcon(icon, image, size);
134+
} else {
135+
I i = new I();
136+
i.setParent(this);
137+
ZKUtil.configureComponentIcon(icon, i, size);
138+
}
66139
} else {
67-
I i = new I();
68-
i.setParent(this);
69-
ZKUtil.configureComponentIcon(icon, i, size);
140+
appendChild(new Label(value.name()));
70141
}
71-
} else {
72-
appendChild(new Label(value.name()));
73142
}
74143
}
75144

145+
/**
146+
* Retrieves the icon name mapped to the current enum value's ordinal.
147+
* Returns null if the value is null, the iconsNames array is not set,
148+
* or if the ordinal is out of bounds.
149+
*
150+
* @return the icon name corresponding to the enum's ordinal position, or null if not found
151+
*/
76152
private String getIconName() {
77153
try {
78154
if (value != null) {
@@ -85,34 +161,84 @@ private String getIconName() {
85161
return null;
86162
}
87163

164+
/**
165+
* Gets the current icon size.
166+
*
167+
* @return the size of the icon
168+
*/
88169
public IconSize getSize() {
89170
return size;
90171
}
91172

173+
/**
174+
* Sets the icon size.
175+
*
176+
* @param size the desired icon size
177+
*/
92178
public void setSize(IconSize size) {
93179
this.size = size;
94180
}
95181

182+
/**
183+
* Gets the current enum value being displayed.
184+
*
185+
* @return the enum value, or null if not set
186+
*/
96187
public Enum getValue() {
97188
return value;
98189
}
99190

191+
/**
192+
* Sets the icon size using a string representation.
193+
* The string is converted to uppercase and parsed as an IconSize enum value.
194+
*
195+
* @param size the icon size as a string (e.g., "small", "normal", "large")
196+
* @throws IllegalArgumentException if the size string does not match any IconSize value
197+
*/
100198
public void setSize(String size) {
101199
setSize(IconSize.valueOf(size.toUpperCase()));
102200
}
103201

202+
/**
203+
* Gets the array of icon names mapped to enum ordinals.
204+
*
205+
* @return the array of icon names
206+
*/
104207
public String[] getIconsNames() {
105208
return iconsNames;
106209
}
107210

108-
public void setIconsNames(String[] iconsNames) {
211+
/**
212+
* Sets the array of icon names and triggers a re-render of the component.
213+
* Each position in the array corresponds to an enum ordinal value.
214+
*
215+
* @param iconsNames array of icon names to map to enum ordinals
216+
*
217+
* Example:
218+
* <pre>{@code
219+
* String[] icons = {"check", "clock", "times"};
220+
* iconImage.setIconsNamesValues(icons);
221+
* }</pre>
222+
*/
223+
public void setIconsNamesValues(String[] iconsNames) {
109224
this.iconsNames = iconsNames;
110225
render();
111226
}
112227

228+
/**
229+
* Sets the icon names from a comma-separated string and triggers a re-render.
230+
* Spaces are automatically removed from the input string.
231+
*
232+
* @param iconsNames comma-separated string of icon names (e.g., "check,clock,times")
233+
*
234+
* Example:
235+
* <pre>{@code
236+
* iconImage.setIconsNames("check-circle, clock, times-circle");
237+
* }</pre>
238+
*/
113239
public void setIconsNames(String iconsNames) {
114240
if (iconsNames != null) {
115-
setIconsNames(iconsNames.replace(" ", "").split(","));
241+
setIconsNamesValues(iconsNames.replace(" ", "").split(","));
116242
}
117243
}
118244
}

0 commit comments

Comments
 (0)