<commit_before>/*
 * DomUI Java User Interface library
 * Copyright (c) 2010 by <NAME>, Itris B.V.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * See the "sponsors" file for a list of supporters.
 *
 * The latest version of DomUI and related code, support and documentation
 * can be found at http://www.domui.org/
 * The contact for the project is <NAME> <<EMAIL>>.
 */
package to.etc.domui.component.input;

import to.etc.domui.component.meta.MetaManager;
import to.etc.domui.component.meta.MetaUtils;
import to.etc.domui.component.meta.NumericPresentation;
import to.etc.domui.component.meta.PropertyMetaModel;
import to.etc.domui.component.meta.PropertyMetaValidator;
import to.etc.domui.component.meta.impl.MetaPropertyValidatorImpl;
import to.etc.domui.converter.ConverterRegistry;
import to.etc.domui.converter.IConvertable;
import to.etc.domui.converter.IConverter;
import to.etc.domui.converter.IValueValidator;
import to.etc.domui.converter.MoneyUtil;
import to.etc.domui.converter.NumericUtil;
import to.etc.domui.converter.ValidatorRegistry;
import to.etc.domui.dom.css.TextAlign;
import to.etc.domui.dom.errors.UIMessage;
import to.etc.domui.dom.html.IControl;
import to.etc.domui.dom.html.IHasModifiedIndication;
import to.etc.domui.dom.html.Input;
import to.etc.domui.parts.MarkerImagePart;
import to.etc.domui.trouble.UIException;
import to.etc.domui.trouble.ValidationException;
import to.etc.domui.util.DomUtil;
import to.etc.domui.util.Msgs;
import to.etc.util.RuntimeConversionException;
import to.etc.util.RuntimeConversions;
import to.etc.webapp.nls.NlsContext;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Use {@code Text2<T>} instead.
 *
 * A single-line input box. This extends the "input" tag with validation ability
 * and methods to handle conversions and labels.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * Created on Jun 11, 2008
 */
@Deprecated
public class Text<T> extends Input implements IControl<T>, IHasModifiedIndication, IConvertable<T>, ITypedControl<T> {
	/** The type of class that is expected. This is the return type of the getValue() call for a validated item */
	@Nonnull
	private Class<T> m_inputClass;

	/**
	 * If the value is to be converted use this converter for it.
	 */
	private IConverter<T> m_converter;

	/** Defined value validators on this field. */
	private List<IValueValidator< ? >> m_validators = Collections.EMPTY_LIST;

	private T m_value;

	/**
	 * This flag gets T if the validate method has been called on the current
	 * input for a control. It gets reset when a control receives a new value
	 * that differs from it's previous value (raw).
	 */
	private boolean m_validated;

	/** If validated this contains the last validation result. */
	private UIException m_validationResult;

	/**
	 * T when this input value is a REQUIRED value.
	 */
	private boolean m_mandatory;

	/**
	 * When T the raw value in the text input thing does not get space trimmed before
	 * it's being returned.
	 */
	private boolean m_untrimmed;

	/**
	 * @see Text#getEmptyMarker()
	 */
	private String m_emptyMarker;

	public enum NumberMode {
		NONE, DIGITS, FLOAT,
	}

	@Nonnull
	private NumberMode m_numberMode = NumberMode.NONE;

	/** Indication if the contents of this thing has been altered by the user. This merely compares any incoming value with the present value and goes "true" when those are not equal. */
	private boolean m_modifiedByUser;

	private String m_validationRegexp;

	private String m_regexpUserString;

	private String m_placeHolder;

	public Text(@Nonnull Class<T> inputClass) {
		m_inputClass = inputClass;

		NumberMode nm = NumberMode.NONE;
		if(BigDecimal.class.isAssignableFrom(inputClass) || DomUtil.isRealType(inputClass))
			nm = NumberMode.FLOAT;
		else if(DomUtil.isIntegerType(inputClass))
			nm = NumberMode.DIGITS;
		setNumberMode(nm);
	}

	/**
	 * Handle the input from the request for this component.
	 * @see to.etc.domui.dom.html.Input#acceptRequestParameter(java.lang.String[])
	 */
	@Override
	public boolean acceptRequestParameter(@Nonnull String[] values) {
		String oldValue = getRawValue();									// Retain previous value,
		super.acceptRequestParameter(values);								// Set the new one;
		String oldTrimmed = oldValue == null ? "" : oldValue.trim();
		String newTrimmed = getRawValue() == null ? "" : getRawValue().trim();
		if(oldTrimmed.equals(newTrimmed)) {
			return false;
		}
		m_validated = false;
		DomUtil.setModifiedFlag(this);

		//-- Handle data updates.
// jal 2014/11/08 This is obviously wrong because there is no way to pass in the bind-time validation failure. Disable for now as hard binding is probably not useful.
//		T old = m_value;
//		if(validate(true)) {
//			fireModified("value", old, m_value);
//		}

		return true;
	}

	/**
	 * Main handler to validate input. This gets called whenever new input is
	 * present in the control (unvalidated) and:
	 * <ul><commit_msg>Fix for handling properly untrimmed Text control inputs<commit_after>/*
 * DomUI Java User Interface library
 * Copyright (c) 2010 by <NAME>, Itris B.V.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * See the "sponsors" file for a list of supporters.
 *
 * The latest version of DomUI and related code, support and documentation
 * can be found at http://www.domui.org/
 * The contact for the project is <NAME> <<EMAIL>>.
 */
package to.etc.domui.component.input;

import to.etc.domui.component.meta.MetaManager;
import to.etc.domui.component.meta.MetaUtils;
import to.etc.domui.component.meta.NumericPresentation;
import to.etc.domui.component.meta.PropertyMetaModel;
import to.etc.domui.component.meta.PropertyMetaValidator;
import to.etc.domui.component.meta.impl.MetaPropertyValidatorImpl;
import to.etc.domui.converter.ConverterRegistry;
import to.etc.domui.converter.IConvertable;
import to.etc.domui.converter.IConverter;
import to.etc.domui.converter.IValueValidator;
import to.etc.domui.converter.MoneyUtil;
import to.etc.domui.converter.NumericUtil;
import to.etc.domui.converter.ValidatorRegistry;
import to.etc.domui.dom.css.TextAlign;
import to.etc.domui.dom.errors.UIMessage;
import to.etc.domui.dom.html.IControl;
import to.etc.domui.dom.html.IHasModifiedIndication;
import to.etc.domui.dom.html.Input;
import to.etc.domui.parts.MarkerImagePart;
import to.etc.domui.trouble.UIException;
import to.etc.domui.trouble.ValidationException;
import to.etc.domui.util.DomUtil;
import to.etc.domui.util.Msgs;
import to.etc.util.RuntimeConversionException;
import to.etc.util.RuntimeConversions;
import to.etc.webapp.nls.NlsContext;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Use {@code Text2<T>} instead.
 *
 * A single-line input box. This extends the "input" tag with validation ability
 * and methods to handle conversions and labels.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * Created on Jun 11, 2008
 */
@Deprecated
public class Text<T> extends Input implements IControl<T>, IHasModifiedIndication, IConvertable<T>, ITypedControl<T> {
	/** The type of class that is expected. This is the return type of the getValue() call for a validated item */
	@Nonnull
	private Class<T> m_inputClass;

	/**
	 * If the value is to be converted use this converter for it.
	 */
	private IConverter<T> m_converter;

	/** Defined value validators on this field. */
	private List<IValueValidator< ? >> m_validators = Collections.EMPTY_LIST;

	private T m_value;

	/**
	 * This flag gets T if the validate method has been called on the current
	 * input for a control. It gets reset when a control receives a new value
	 * that differs from it's previous value (raw).
	 */
	private boolean m_validated;

	/** If validated this contains the last validation result. */
	private UIException m_validationResult;

	/**
	 * T when this input value is a REQUIRED value.
	 */
	private boolean m_mandatory;

	/**
	 * When T the raw value in the text input thing does not get space trimmed before
	 * it's being returned.
	 */
	private boolean m_untrimmed;

	/**
	 * @see Text#getEmptyMarker()
	 */
	private String m_emptyMarker;

	public enum NumberMode {
		NONE, DIGITS, FLOAT,
	}

	@Nonnull
	private NumberMode m_numberMode = NumberMode.NONE;

	/** Indication if the contents of this thing has been altered by the user. This merely compares any incoming value with the present value and goes "true" when those are not equal. */
	private boolean m_modifiedByUser;

	private String m_validationRegexp;

	private String m_regexpUserString;

	private String m_placeHolder;

	public Text(@Nonnull Class<T> inputClass) {
		m_inputClass = inputClass;

		NumberMode nm = NumberMode.NONE;
		if(BigDecimal.class.isAssignableFrom(inputClass) || DomUtil.isRealType(inputClass))
			nm = NumberMode.FLOAT;
		else if(DomUtil.isIntegerType(inputClass))
			nm = NumberMode.DIGITS;
		setNumberMode(nm);
	}

	/**
	 * Handle the input from the request for this component.
	 * @see to.etc.domui.dom.html.Input#acceptRequestParameter(java.lang.String[])
	 */
	@Override
	public boolean acceptRequestParameter(@Nonnull String[] values) {
		String oldValue = getRawValue();									// Retain previous value,
		super.acceptRequestParameter(values);								// Set the new one;
		oldValue = oldValue == null ? "" : m_untrimmed ? oldValue : oldValue.trim();
		String newValue = getRawValue() == null ? "" : m_untrimmed ? getRawValue() : getRawValue().trim();
		if(oldValue.equals(newValue)) {
			return false;
		}
		m_validated = false;
		DomUtil.setModifiedFlag(this);

		//-- Handle data updates.
// jal 2014/11/08 This is obviously wrong because there is no way to pass in the bind-time validation failure. Disable for now as hard binding is probably not useful.
//		T old = m_value;
//		if(validate(true)) {
//			fireModified("value", old, m_value);
//		}

		return true;
	}

	/**
	 * Main handler to validate input. This gets called whenever new input is
	 * present in the control (unvalidated) and:
	 * <ul>
{'fraction_non_alphanumeric': 0.0983155723616363, 'fraction_numerical': 0.005500171880371262, 'mean_word_length': 4.165113182423435, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6987476', 'n_tokens_mistral': 3690, 'n_tokens_neox': 3457, 'n_words': 1183}