Skip to content

Commit e406d63

Browse files
imagejanhinerm
authored andcommitted
WidgetStyle: add javadoc and new method
While getStyleModifiers() is useful for things like "extensions:png/gif/bmp", this commit adds a new getStyleModifier() method that simply returns the suffix after the colon, for cases like "format:#0.001". See also scijava/scijava-ui-swing#52.
1 parent 77ef6b3 commit e406d63

File tree

1 file changed

+55
-3
lines changed

1 file changed

+55
-3
lines changed

src/main/java/org/scijava/widget/WidgetStyle.java

+55-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ private WidgetStyle() {
3535
// prevent instantiation of utility class
3636
}
3737

38+
/**
39+
* Check whether a given widget style contains the target style.
40+
*
41+
* @param widgetStyle
42+
* The style declaration to test, usually a comma-separated
43+
* {@code String}; trailing spaces are ignored.
44+
* @param target
45+
* The style being checked, case-insensitive.
46+
* @return {@code true} if the target style matches.
47+
*/
3848
public static boolean isStyle(String widgetStyle, String target) {
3949
if (widgetStyle == null || target == null)
4050
return widgetStyle == target;
@@ -44,20 +54,62 @@ public static boolean isStyle(String widgetStyle, String target) {
4454
return false;
4555
}
4656

57+
/**
58+
* Check whether a given {@link ModuleItem} has the target style.
59+
*
60+
* @param item
61+
* The module item to test.
62+
* @param target
63+
* The style being checked, case-insensitive.
64+
* @return {@code true} if the module item has the target style.
65+
*/
4766
public static boolean isStyle(ModuleItem<?> item, String target) {
4867
return isStyle(item.getWidgetStyle(), target);
4968
}
5069

51-
public static String[] getStyleModifiers(String widgetStyle, String target) {
70+
/**
71+
* Get the modifying value for a given style attribute in a style declaration.
72+
*
73+
* <p>
74+
* For example, for {@code style="format:#0.00"}, this will return
75+
* {@code "#0.00"}.
76+
* </p>
77+
*
78+
* @param widgetStyle
79+
* The style declaration string, e.g. <code>"format:#0.00"</code>.
80+
* @param target
81+
* The target style attribute, e.g. <code>"format"</code>.
82+
* @return The modifier for the given target, e.g. <code>"#0.00"</code>.
83+
*/
84+
public static String getStyleModifier(String widgetStyle, String target) {
5285
if (widgetStyle == null || target == null)
5386
return null;
5487
String[] styles = widgetStyle.split(",");
5588
for (String s : styles) {
5689
if (s.trim().toLowerCase().startsWith(target.toLowerCase())) {
57-
String suffix = s.split(":")[1];
58-
return suffix.split("/");
90+
return s.split(":")[1];
5991
}
6092
}
6193
return null;
6294
}
95+
96+
/**
97+
* Get an array of all modifying values for a given style attribute.
98+
*
99+
* <p>
100+
* For example, for {@code style="extensions:png/gif/bmp"}, this will return {@code ["png", "gif", "bmp"]}.
101+
* </p>
102+
*
103+
* @param widgetStyle
104+
* The style declaration string, e.g. <code>"extensions:png/gif/bmp"</code>.
105+
* @param target
106+
* The target style attribute, e.g. <code>"extensions"</code>.
107+
* @return An array of modifiers for the given target, e.g. <code>["png", "gif", "bmp"]</code>.
108+
*/
109+
public static String[] getStyleModifiers(String widgetStyle, String target) {
110+
String suffix = getStyleModifier(widgetStyle, target);
111+
if (suffix == null) return null;
112+
return suffix.split("/");
113+
}
114+
63115
}

0 commit comments

Comments
 (0)