Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WidgetStyle: add javadoc and new method #405

Merged
merged 1 commit into from
Sep 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions src/main/java/org/scijava/widget/WidgetStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ private WidgetStyle() {
// prevent instantiation of utility class
}

/**
* Check whether a given widget style contains the target style.
*
* @param widgetStyle
* The style declaration to test, usually a comma-separated
* {@code String}; trailing spaces are ignored.
* @param target
* The style being checked, case-insensitive.
* @return {@code true} if the target style matches.
*/
public static boolean isStyle(String widgetStyle, String target) {
if (widgetStyle == null || target == null)
return widgetStyle == target;
Expand All @@ -44,20 +54,62 @@ public static boolean isStyle(String widgetStyle, String target) {
return false;
}

/**
* Check whether a given {@link ModuleItem} has the target style.
*
* @param item
* The module item to test.
* @param target
* The style being checked, case-insensitive.
* @return {@code true} if the module item has the target style.
*/
public static boolean isStyle(ModuleItem<?> item, String target) {
return isStyle(item.getWidgetStyle(), target);
}

public static String[] getStyleModifiers(String widgetStyle, String target) {
/**
* Get the modifying value for a given style attribute in a style declaration.
*
* <p>
* For example, for {@code style="format:#0.00"}, this will return
* {@code "#0.00"}.
* </p>
*
* @param widgetStyle
* The style declaration string, e.g. <code>"format:#0.00"</code>.
* @param target
* The target style attribute, e.g. <code>"format"</code>.
* @return The modifier for the given target, e.g. <code>"#0.00"</code>.
*/
public static String getStyleModifier(String widgetStyle, String target) {
if (widgetStyle == null || target == null)
return null;
String[] styles = widgetStyle.split(",");
for (String s : styles) {
if (s.trim().toLowerCase().startsWith(target.toLowerCase())) {
String suffix = s.split(":")[1];
return suffix.split("/");
return s.split(":")[1];
}
}
return null;
}

/**
* Get an array of all modifying values for a given style attribute.
*
* <p>
* For example, for {@code style="extensions:png/gif/bmp"}, this will return {@code ["png", "gif", "bmp"]}.
* </p>
*
* @param widgetStyle
* The style declaration string, e.g. <code>"extensions:png/gif/bmp"</code>.
* @param target
* The target style attribute, e.g. <code>"extensions"</code>.
* @return An array of modifiers for the given target, e.g. <code>["png", "gif", "bmp"]</code>.
*/
public static String[] getStyleModifiers(String widgetStyle, String target) {
String suffix = getStyleModifier(widgetStyle, target);
if (suffix == null) return null;
return suffix.split("/");
}

}