All Downloads are FREE. Search and download functionalities are using the official Maven repository.

se.wfh.libs.common.gui.widgets.WTimeTextField Maven / Gradle / Ivy

There is a newer version: 0.17
Show newest version
package se.wfh.libs.common.gui.widgets;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import se.wfh.libs.common.gui.AbstractWTextField;
import se.wfh.libs.common.utils.DateTools;
import se.wfh.libs.common.utils.DateTools.ConvertType;

public class WTimeTextField extends AbstractWTextField {
	private static final long serialVersionUID = 1L;

	public WTimeTextField() {
		this(null);
	}

	public WTimeTextField(final Date defval) {
		this(defval, 10);
	}

	public WTimeTextField(final Date defval, final int columns) {
		super(defval, columns);

		if (defval != null) {
			getComponent().setText(DateTools.toString(defval, ConvertType.TIME));
		}

		changeBackground(getValueInternal() != null);
	}

	@Override
	public Date getValue() {
		return getValueInternal();
	}

	private Date getValueInternal() {
		Calendar cal = null;

		try {
			cal = DateTools.fromString(getComponent().getText(), ConvertType.TIME);
		} catch (ParseException pex) {
			// Ignore
			pex.getErrorOffset(); // for PMD
		}

		return cal == null ? null : cal.getTime();
	}

	@Override
	public boolean isInputValid(final Date value) {
		return value != null;
	}

	@Override
	public void setValue(final Date value) {
		getComponent().setText(DateTools.toString(value, ConvertType.TIME));
		changeBackground(isValidValue(value));
		updatePreviousValue(value);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy