Skip to content

Add WidgetStyle utility class to centralize style logic #384

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

Merged
merged 1 commit into from
Oct 29, 2020
Merged
Show file tree
Hide file tree
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
7 changes: 1 addition & 6 deletions src/main/java/org/scijava/widget/DefaultWidgetModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,7 @@ public String getWidgetLabel() {

@Override
public boolean isStyle(final String style) {
final String widgetStyle = getItem().getWidgetStyle();
if (widgetStyle == null) return style == null;
for (final String s : widgetStyle.split(",")) {
if (s.equals(style)) return true;
}
return false;
return WidgetStyle.isStyle(getItem(), style);
}

@Override
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/org/scijava/widget/WidgetStyle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.scijava.widget;

import org.scijava.module.ModuleItem;

public class WidgetStyle {
private WidgetStyle() {
// prevent instantiation of utility class
}

public static boolean isStyle(String widgetStyle, String target) {
if (widgetStyle == null || target == null)
return widgetStyle == target;
for (final String s : widgetStyle.split(",")) {
if (s.trim().toLowerCase().equals(target.toLowerCase())) return true;
}
return false;
}

public static boolean isStyle(ModuleItem<?> item, String target) {
return isStyle(item.getWidgetStyle(), target);
}

public static String[] getStyleModifiers(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 null;
}
}
6 changes: 4 additions & 2 deletions src/test/java/org/scijava/script/ScriptInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import org.scijava.test.TestUtils;
import org.scijava.util.DigestUtils;
import org.scijava.util.FileUtils;
import org.scijava.widget.WidgetStyle;

/**
* Tests {@link ScriptInfo}.
Expand Down Expand Up @@ -251,7 +252,7 @@ public void testParameters() {
final String script = "" + //
"#@ LogService (required = false) log\n" + //
"#@ int (label=\"Slider Value\", softMin=5, softMax=15, " + //
"stepSize=3, value=11, style=\"slider\") sliderValue\n" + //
"stepSize=3, value=11, style=\" slidEr,\") sliderValue\n" + //
"#@ String (persist = false, family='Carnivora', " + //
"choices={'quick brown fox', 'lazy dog'}) animal\n" + //
"#@ Double (autoFill = false) notAutoFilled\n" + //
Expand All @@ -269,7 +270,8 @@ public void testParameters() {

final ModuleItem<?> sliderValue = info.getInput("sliderValue");
assertItem("sliderValue", int.class, "Slider Value", ItemIO.INPUT, true,
true, null, "slider", 11, null, null, 5, 15, 3.0, noChoices, sliderValue);
true, null, " slidEr,", 11, null, null, 5, 15, 3.0, noChoices, sliderValue);
assertTrue("Case-insensitive trimmed style", WidgetStyle.isStyle(sliderValue, "slider"));

final ModuleItem<?> animal = info.getInput("animal");
final List<String> animalChoices = //
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/org/scijava/widget/WidgetStyleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.scijava.widget;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Enclosed.class)
public class WidgetStyleTest {

@RunWith(Parameterized.class)
public static class TestIsStyle {

static String[] styleStrings = { "foo, bar, someThing", " FOO, BAR, SOMEthing ", "foo ", " bar",
"trash, sOmEtHiNg", null };

static String[] stylesToTest = { "foo", "bar", "someThing", null };

static boolean[][] stylesToHave = { // foo, bar, someThing
new boolean[] { true, true, true, false }, new boolean[] { true, true, true, false },
new boolean[] { true, false, false, false }, new boolean[] { false, true, false, false },
new boolean[] { false, false, true, false }, new boolean[] { false, false, false, true } };

@Parameters(name = "{0}")
public static List<Object[]> params() {
return IntStream.range(0, styleStrings.length)
.mapToObj(i -> new Object[] { styleStrings[i], stylesToHave[i] }).collect(Collectors.toList());
}

@Parameter
public String styleString;

@Parameter(1)
public boolean[] targetStyles;

@Test
public void testSimpleStyles() {
for (int i = 0; i < stylesToTest.length; i++) {
assertEquals("style: " + stylesToTest[i], targetStyles[i],
WidgetStyle.isStyle(styleString, stylesToTest[i]));
}
}
}

public static class TestStyleModifiers {
@Test
public void testStyleModifiers() {
String style = "open, extensions:tiff/tif/jpeg/jpg";
Set<String> extensions = new HashSet<>(Arrays.asList(WidgetStyle.getStyleModifiers(style, "extensions")));
Set<String> expected = new HashSet<>(Arrays.asList("tiff", "jpg", "jpeg", "tif"));
assertEquals(expected, extensions);
}
}
}