001package gwt.material.design.client.base; 002 003/* 004 * #%L 005 * GwtMaterial 006 * %% 007 * Copyright (C) 2015 GwtMaterialDesign 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import gwt.material.design.client.ui.MaterialNumberBox; 024 025import java.io.IOException; 026import java.text.ParseException; 027 028import com.google.gwt.dom.client.Document; 029import com.google.gwt.text.shared.Parser; 030import com.google.gwt.text.shared.Renderer; 031import com.google.gwt.user.client.ui.ValueBox; 032 033/** 034 * Base class to handle number values on {@code <input type="number">} elements. 035 * 036 * The parsing and formatting is done by the browser, using the user specific i18n settings. 037 * 038 * @author gilberto-torrezan 039 * 040 * @see MaterialNumberBox 041 */ 042@SuppressWarnings({ "rawtypes", "unchecked" }) 043public class NumberBox<T> extends ValueBox<T> { 044 045 public NumberBox(NumberHandler handler) { 046 // currently there's no way to create a <input type="number"> directly 047 super(Document.get().createTextInputElement(), handler, handler); 048 } 049 050 public static class NumberHandler<T> implements Renderer<T>, Parser<T> { 051 052 MaterialNumberBox<T> numberBox; 053 054 public NumberHandler(MaterialNumberBox<T> numberBox) { 055 this.numberBox = numberBox; 056 } 057 058 /* 059 * Note: This assumes that getValue() from the custom MaterialNumberBox was overridden to avoid using the parser. 060 */ 061 @Override 062 public T parse(CharSequence text) throws ParseException { 063 return numberBox.getValue(); 064 } 065 066 @Override 067 public String render(T object) { 068 if(object == null) { 069 return ""; 070 } 071 return object.toString(); 072 } 073 074 @Override 075 public void render(T object, Appendable appendable) throws IOException { 076 appendable.append(render(object)); 077 } 078 } 079 080}