Skip to content

Commit f607848

Browse files
authored
Merge pull request #384 from scijava/widget-style
Add WidgetStyle utility class to centralize style logic
2 parents 10ac353 + b9e7b95 commit f607848

File tree

4 files changed

+108
-8
lines changed

4 files changed

+108
-8
lines changed

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,7 @@ public String getWidgetLabel() {
129129

130130
@Override
131131
public boolean isStyle(final String style) {
132-
final String widgetStyle = getItem().getWidgetStyle();
133-
if (widgetStyle == null) return style == null;
134-
for (final String s : widgetStyle.split(",")) {
135-
if (s.equals(style)) return true;
136-
}
137-
return false;
132+
return WidgetStyle.isStyle(getItem(), style);
138133
}
139134

140135
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.scijava.widget;
2+
3+
import org.scijava.module.ModuleItem;
4+
5+
public class WidgetStyle {
6+
private WidgetStyle() {
7+
// prevent instantiation of utility class
8+
}
9+
10+
public static boolean isStyle(String widgetStyle, String target) {
11+
if (widgetStyle == null || target == null)
12+
return widgetStyle == target;
13+
for (final String s : widgetStyle.split(",")) {
14+
if (s.trim().toLowerCase().equals(target.toLowerCase())) return true;
15+
}
16+
return false;
17+
}
18+
19+
public static boolean isStyle(ModuleItem<?> item, String target) {
20+
return isStyle(item.getWidgetStyle(), target);
21+
}
22+
23+
public static String[] getStyleModifiers(String widgetStyle, String target) {
24+
if (widgetStyle == null || target == null)
25+
return null;
26+
String[] styles = widgetStyle.split(",");
27+
for (String s : styles) {
28+
if (s.trim().toLowerCase().startsWith(target.toLowerCase())) {
29+
String suffix = s.split(":")[1];
30+
return suffix.split("/");
31+
}
32+
}
33+
return null;
34+
}
35+
}

src/test/java/org/scijava/script/ScriptInfoTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import org.scijava.test.TestUtils;
6666
import org.scijava.util.DigestUtils;
6767
import org.scijava.util.FileUtils;
68+
import org.scijava.widget.WidgetStyle;
6869

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

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

274276
final ModuleItem<?> animal = info.getInput("animal");
275277
final List<String> animalChoices = //
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.scijava.widget;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.util.Arrays;
8+
import java.util.Collection;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Set;
12+
import java.util.stream.Collectors;
13+
import java.util.stream.IntStream;
14+
15+
import org.junit.Test;
16+
import org.junit.experimental.runners.Enclosed;
17+
import org.junit.runner.RunWith;
18+
import org.junit.runners.Parameterized;
19+
import org.junit.runners.Parameterized.Parameter;
20+
import org.junit.runners.Parameterized.Parameters;
21+
22+
@RunWith(Enclosed.class)
23+
public class WidgetStyleTest {
24+
25+
@RunWith(Parameterized.class)
26+
public static class TestIsStyle {
27+
28+
static String[] styleStrings = { "foo, bar, someThing", " FOO, BAR, SOMEthing ", "foo ", " bar",
29+
"trash, sOmEtHiNg", null };
30+
31+
static String[] stylesToTest = { "foo", "bar", "someThing", null };
32+
33+
static boolean[][] stylesToHave = { // foo, bar, someThing
34+
new boolean[] { true, true, true, false }, new boolean[] { true, true, true, false },
35+
new boolean[] { true, false, false, false }, new boolean[] { false, true, false, false },
36+
new boolean[] { false, false, true, false }, new boolean[] { false, false, false, true } };
37+
38+
@Parameters(name = "{0}")
39+
public static List<Object[]> params() {
40+
return IntStream.range(0, styleStrings.length)
41+
.mapToObj(i -> new Object[] { styleStrings[i], stylesToHave[i] }).collect(Collectors.toList());
42+
}
43+
44+
@Parameter
45+
public String styleString;
46+
47+
@Parameter(1)
48+
public boolean[] targetStyles;
49+
50+
@Test
51+
public void testSimpleStyles() {
52+
for (int i = 0; i < stylesToTest.length; i++) {
53+
assertEquals("style: " + stylesToTest[i], targetStyles[i],
54+
WidgetStyle.isStyle(styleString, stylesToTest[i]));
55+
}
56+
}
57+
}
58+
59+
public static class TestStyleModifiers {
60+
@Test
61+
public void testStyleModifiers() {
62+
String style = "open, extensions:tiff/tif/jpeg/jpg";
63+
Set<String> extensions = new HashSet<>(Arrays.asList(WidgetStyle.getStyleModifiers(style, "extensions")));
64+
Set<String> expected = new HashSet<>(Arrays.asList("tiff", "jpg", "jpeg", "tif"));
65+
assertEquals(expected, extensions);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)