Skip to content

Commit 09c88b0

Browse files
committed
Resolve #159
1 parent 556f9a7 commit 09c88b0

File tree

6 files changed

+88
-8
lines changed

6 files changed

+88
-8
lines changed

gwt-material/src/main/java/gwt/material/design/client/base/helper/ColorHelper.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,29 @@
2020
* #L%
2121
*/
2222

23-
2423
import com.google.gwt.dom.client.Document;
2524
import com.google.gwt.dom.client.Element;
25+
import com.google.gwt.dom.client.Style;
2626
import com.google.gwt.user.client.ui.RootPanel;
2727
import gwt.material.design.client.base.MaterialWidget;
2828
import gwt.material.design.client.constants.Color;
2929

3030
public class ColorHelper {
3131

32+
/**
33+
* Returns first enum constant found..
34+
*
35+
* @param styleName Space-separated list of styles
36+
* @param enumClass Type of enum
37+
* @param defaultValue Default value of no match was found
38+
* @return First enum constant found or default value
39+
*/
40+
public static <E extends Enum<? extends Style.HasCssName>> E fromStyleName(final String styleName,
41+
final Class<E> enumClass,
42+
final E defaultValue) {
43+
return EnumHelper.fromStyleName(styleName, enumClass, defaultValue, true);
44+
}
45+
3246
public static String setupComputedBackgroundColor(Color color) {
3347
MaterialWidget temp = new MaterialWidget(Document.get().createDivElement());
3448
temp.setBackgroundColor(color);
@@ -45,5 +59,4 @@ protected static native String getComputedBackgroundColor(Element e)/*-{
4559
var cs = $wnd.document.defaultView.getComputedStyle(e, null);
4660
return cs.getPropertyValue('background-color');
4761
}-*/;
48-
4962
}

gwt-material/src/main/java/gwt/material/design/client/base/helper/EnumHelper.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,25 @@ public final class EnumHelper {
3535
* @param defaultValue Default value of no match was found
3636
* @return First enum constant found or default value
3737
*/
38-
@SuppressWarnings("unchecked")
3938
public static <E extends Enum<? extends Style.HasCssName>> E fromStyleName(final String styleName,
4039
final Class<E> enumClass,
4140
final E defaultValue) {
41+
return EnumHelper.fromStyleName(styleName, enumClass, defaultValue, false);
42+
}
43+
44+
/**
45+
* Returns first enum constant found in at space-separated list of style names.
46+
*
47+
* @param styleName Space-separated list of styles
48+
* @param enumClass Type of enum
49+
* @param defaultValue Default value of no match was found
50+
* @return First enum constant found or default value
51+
*/
52+
@SuppressWarnings("unchecked")
53+
public static <E extends Enum<? extends Style.HasCssName>> E fromStyleName(final String styleName,
54+
final Class<E> enumClass,
55+
final E defaultValue,
56+
final boolean ignoreSpaces) {
4257
if (styleName == null || enumClass == null) {
4358
return defaultValue;
4459
}
@@ -47,8 +62,16 @@ public static <E extends Enum<? extends Style.HasCssName>> E fromStyleName(final
4762
final Style.HasCssName anEnum = (Style.HasCssName) constant;
4863
final String cssClass = anEnum.getCssName();
4964

50-
if (cssClass != null && StyleHelper.containsStyle(styleName, cssClass)) {
51-
return (E) anEnum;
65+
if(cssClass != null) {
66+
boolean contains;
67+
if (ignoreSpaces) {
68+
contains = styleName.equals(cssClass);
69+
} else {
70+
contains = StyleHelper.containsStyle(styleName, cssClass);
71+
}
72+
if (contains) {
73+
return (E) anEnum;
74+
}
5275
}
5376
}
5477
return defaultValue;

gwt-material/src/main/java/gwt/material/design/client/base/helper/StyleHelper.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public static <E extends Style.HasCssName> void removeEnumStyleName(final UIObje
105105
*/
106106
public static boolean containsStyle(final String styleNames,
107107
final String style) {
108-
109108
if (styleNames == null || style == null) {
110109
return false;
111110
}

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialTooltip.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,12 @@ public void setHtml(String html) {
320320

321321
Element element = widget.getElement();
322322
if (widget.isAttached()) {
323-
JsMaterialElement.$("#" + element.getAttribute("data-tooltip-id"))
323+
$("#" + element.getAttribute("data-tooltip-id"))
324324
.find("span")
325325
.html(html != null ? html : "");
326326
} else {
327327
htmlAttachHandler = widget.addAttachHandler(attachEvent -> {
328-
JsMaterialElement.$("#" + element.getAttribute("data-tooltip-id"))
328+
$("#" + element.getAttribute("data-tooltip-id"))
329329
.find("span")
330330
.html(html != null ? html : "");
331331
});

gwt-material/src/test/java/gwt/material/design/client/GwtMaterialTestComponent.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import gwt.material.design.client.resources.WithJQueryResources;
2525
import gwt.material.design.client.ui.*;
2626
import gwt.material.design.client.ui.animation.AnimationTest;
27+
import gwt.material.design.client.ui.base.helper.ColorHelperTest;
2728
import org.junit.Test;
2829

2930
import static gwt.material.design.jquery.client.api.JQuery.$;
@@ -58,6 +59,11 @@ public void setup() {
5859
assertNotNull($("body"));
5960
}
6061

62+
@Test
63+
public void testHelpers() {
64+
new ColorHelperTest();
65+
}
66+
6167
@Test
6268
public void testAnimation() {
6369
new AnimationTest().init();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* #%L
3+
* GwtMaterial
4+
* %%
5+
* Copyright (C) 2015 - 2016 GwtMaterialDesign
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package gwt.material.design.client.ui.base.helper;
21+
22+
import gwt.material.design.client.base.helper.ColorHelper;
23+
import gwt.material.design.client.constants.Color;
24+
import junit.framework.TestCase;
25+
26+
/**
27+
* Test case for {@link gwt.material.design.client.base.helper.ColorHelper}.
28+
*/
29+
public class ColorHelperTest extends TestCase {
30+
31+
public ColorHelperTest() {
32+
checkFromStyleName();
33+
}
34+
35+
private void checkFromStyleName() {
36+
assertEquals(Color.PINK_LIGHTEN_1,
37+
ColorHelper.fromStyleName("pink lighten-1", Color.class, Color.DEFAULT));
38+
}
39+
}

0 commit comments

Comments
 (0)