Skip to content

Commit cdb24ef

Browse files
Refactory of the MaterialNumberBox and related classes.
1 parent d828a27 commit cdb24ef

File tree

9 files changed

+142
-248
lines changed

9 files changed

+142
-248
lines changed

gwt-material/src/main/java/gwt/material/design/client/base/FloatBox.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

gwt-material/src/main/java/gwt/material/design/client/base/FloatParser.java

Lines changed: 0 additions & 77 deletions
This file was deleted.

gwt-material/src/main/java/gwt/material/design/client/base/FloatRenderer.java

Lines changed: 0 additions & 75 deletions
This file was deleted.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package gwt.material.design.client.base;
2+
3+
/*
4+
* #%L
5+
* GwtMaterial
6+
* %%
7+
* Copyright (C) 2015 GwtMaterialDesign
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import gwt.material.design.client.ui.MaterialNumberBox;
24+
25+
import java.io.IOException;
26+
import java.text.ParseException;
27+
28+
import com.google.gwt.dom.client.Document;
29+
import com.google.gwt.text.shared.Parser;
30+
import com.google.gwt.text.shared.Renderer;
31+
import com.google.gwt.user.client.ui.ValueBox;
32+
33+
/**
34+
* Base class to handle number values on {@code <input type="number">} elements.
35+
*
36+
* The parsing and formatting is done by the browser, using the user specific i18n settings.
37+
*
38+
* @author gilberto-torrezan
39+
*
40+
* @see MaterialNumberBox
41+
*/
42+
@SuppressWarnings({"rawtypes", "unchecked"})
43+
public class NumberBox<T> extends ValueBox<T> {
44+
45+
private static final NumberHandler HANDLER = new NumberHandler();
46+
47+
public NumberBox() {
48+
//currently there's no way to create a <input type="number"> directly
49+
super(Document.get().createTextInputElement(), HANDLER, HANDLER);
50+
}
51+
52+
protected static class NumberHandler<T> implements Renderer<T>, Parser<T> {
53+
/*
54+
* Note: this method is NOT called by MaterialIntegerBox, MaterialFloatBox or MaterialDoubleBox, the parsing is done by the browser
55+
*/
56+
@Override
57+
public T parse(CharSequence text) throws ParseException {
58+
return null;
59+
}
60+
61+
@Override
62+
public String render(T object) {
63+
if (object == null){
64+
return "";
65+
}
66+
return object.toString();
67+
}
68+
69+
@Override
70+
public void render(T object, Appendable appendable) throws IOException {
71+
appendable.append(render(object));
72+
}
73+
}
74+
75+
}

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,32 @@
2222

2323
//@formatter:off
2424

25-
import com.google.gwt.user.client.ui.DoubleBox;
26-
2725
/**
28-
* Material Integer Box is an input field that accepts any Double based string from user.
29-
* <h3>UiBinder Usage:</h3>
30-
* <pre>
31-
*{@code <m:MaterialIntegerBox placeholder="Your doble" step=100/>}
32-
* </pre>
33-
* @see <a href="http://gwt-material-demo.herokuapp.com/#forms">Material DoubleBox</a>
34-
* @author paulux84
35-
*/
36-
//@formatter:on
26+
* Material Integer Box is an input field that accepts any Double based string
27+
* from user. <h3>UiBinder Usage:</h3>
28+
*
29+
* <pre>
30+
* {@code <m:MaterialIntegerBox placeholder="Your doble" step=100/>}
31+
* </pre>
32+
*
33+
* The parsing and formatting of the number are done natively by the browser,
34+
* using the i18n settings from the user.
35+
*
36+
* @see <a href="http://gwt-material-demo.herokuapp.com/#forms">Material
37+
* DoubleBox</a>
38+
* @author paulux84
39+
*/
40+
// @formatter:on
3741
public class MaterialDoubleBox extends MaterialNumberBox<Double> {
3842

3943
public MaterialDoubleBox() {
40-
super(new DoubleBox());
4144
setStep("any");
4245
}
43-
46+
4447
@Override
4548
public Double getValue() {
4649
double number = getValueAsNumber();
47-
if (Double.isNaN(number)){
50+
if (Double.isNaN(number)) {
4851
return null;
4952
}
5053
return number;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
//@formatter:off
2424

25-
import gwt.material.design.client.base.FloatBox;
2625

2726
/**
2827
* Material Integer Box is an input field that accepts any Float based string from user.
@@ -38,11 +37,9 @@
3837
* @author paulux84
3938
*/
4039
//@formatter:on
41-
//@formatter:on
4240
public class MaterialFloatBox extends MaterialNumberBox<Float> {
4341

4442
public MaterialFloatBox() {
45-
super(new FloatBox());
4643
setStep("any");
4744
}
4845

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,32 @@
2222

2323
//@formatter:off
2424

25-
import com.google.gwt.user.client.ui.IntegerBox;
26-
2725
/**
28-
* Material Integer Box is an input field that accepts any Integer based string from user.
29-
* <h3>UiBinder Usage:</h3>
30-
* <pre>
31-
*{@code <m:MaterialIntegerBox placeholder="Your integer" step=100/>}
32-
* </pre>
33-
* @see <a href="http://gwt-material-demo.herokuapp.com/#forms">Material IntegerBox</a>
34-
* @author paulux84
35-
*/
36-
//@formatter:on
26+
* Material Integer Box is an input field that accepts any Integer based string
27+
* from user. <h3>UiBinder Usage:</h3>
28+
*
29+
* <pre>
30+
* {@code <m:MaterialIntegerBox placeholder="Your integer" step=100/>}
31+
* </pre>
32+
*
33+
* The parsing and formatting of the number are done natively by the browser,
34+
* using the i18n settings from the user.
35+
*
36+
* @see <a href="http://gwt-material-demo.herokuapp.com/#forms">Material
37+
* IntegerBox</a>
38+
* @author paulux84
39+
*/
40+
// @formatter:on
3741
public class MaterialIntegerBox extends MaterialNumberBox<Integer> {
3842

3943
public MaterialIntegerBox() {
40-
super(new IntegerBox());
4144
setStep("1");
4245
}
43-
46+
4447
@Override
4548
public Integer getValue() {
4649
double number = getValueAsNumber();
47-
if (Double.isNaN(number)){
50+
if (Double.isNaN(number)) {
4851
return null;
4952
}
5053
return (int) Math.rint(number);

0 commit comments

Comments
 (0)