
de.knightsoftnet.mtwidgets.client.ui.widget.ValueBox Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package de.knightsoftnet.mtwidgets.client.ui.widget;
import com.google.gwt.i18n.client.LocaleInfo;
import com.google.gwt.text.shared.Parser;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;
import elemental2.dom.DomGlobal;
import elemental2.dom.HTMLInputElement;
/**
* A text box able to parse its displayed value.
*
* @param the value type
*/
public class ValueBox extends ValueBoxBaseWithEditorErrors {
/**
* Creates a ValueBox widget that wraps an existing <input type='text'> element.
*
*
* This element must already be attached to the document. If the element is removed from the
* document, you must call {@link RootPanel#detachNow(Widget)}.
*
*
* @param the value type
*
* @param element the element to be wrapped
* @param renderer rendering routine
* @param parser parsing routine
* @return value box
*/
public static ValueBox wrap(final HTMLInputElement element, final Renderer renderer,
final Parser parser) {
// Assert that the element is attached.
assert DomGlobal.document.contains(element);
final ValueBox valueBox = new ValueBox<>(element, renderer, parser);
// Mark it attached and remember it for cleanup.
valueBox.onAttach();
RootPanel.detachOnWindowClose(valueBox);
return valueBox;
}
/**
* This constructor may be used by subclasses to explicitly use an existing element. This element
* must be an <input> element whose type is 'text'.
*
* @param element the element to be used
* @param renderer rendering routine
* @param parser parsing routine
*/
protected ValueBox(final HTMLInputElement element, final Renderer renderer,
final Parser parser) {
super(element, renderer, parser);
// BiDi input is not expected - disable direction estimation.
this.setDirectionEstimator(false);
if (LocaleInfo.getCurrentLocale().isRTL()) {
setDirection(Direction.LTR);
}
assert element.type == "text";
}
/**
* This constructor may be used by subclasses to explicitly use an existing element. This element
* must be an <input> element the type is given as separate parameter and is not limited.
*
* @param element the element to be used
* @param inputType input type of the element to set /
* @param renderer rendering routine
* @param parser parsing routine
*/
protected ValueBox(final HTMLInputElement element, final String inputType,
final Renderer renderer, final Parser parser) {
super(element, renderer, parser);
element.setAttribute("type", inputType);
// BiDi input is not expected - disable direction estimation.
this.setDirectionEstimator(false);
if (LocaleInfo.getCurrentLocale().isRTL()) {
setDirection(Direction.LTR);
}
}
/**
* Gets the maximum allowable length.
*
* @return the maximum length, in characters
*/
public int getMaxLength() {
return getInputElement().maxLength;
}
/**
* Gets the number of visible characters.
*
* @return the number of visible characters
*/
public int getVisibleLength() {
return getInputElement().size;
}
/**
* Sets the maximum allowable length.
*
* @param length the maximum length, in characters
*/
public void setMaxLength(final int length) {
getInputElement().maxLength = length;
}
/**
* Sets the number of visible characters.
*
* @param length the number of visible characters
*/
public void setVisibleLength(final int length) {
getInputElement().size = length;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy