<commit_before>/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.desktop.gui.components;

import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.chile.core.model.utils.InstanceUtils;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.desktop.sys.DesktopToolTipManager;
import com.haulmont.cuba.desktop.sys.vcl.Flushable;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.DateField;
import com.haulmont.cuba.gui.components.IFrame;
import com.haulmont.cuba.gui.components.TimeField;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static com.haulmont.bali.util.Preconditions.checkNotNullArgument;

/**
 * @author krivopustov
 * @version $Id$
 */
public class DesktopTimeField extends DesktopAbstractField<JFormattedTextField> implements TimeField {

    private boolean showSeconds;
    private String timeFormat;
    private String mask;
    private MaskFormatter formatter;
    private boolean updatingInstance;
    private Datasource datasource;
    private Object prevValue;
    private String caption;
    private DateField.Resolution resolution;
    private boolean editable = true;

    protected static final int DIGIT_WIDTH = 23;

    public DesktopTimeField() {
        UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);

        timeFormat = Datatypes.getFormatStrings(uss.getLocale()).getTimeFormat();
        resolution = DateField.Resolution.MIN;
        formatter = new MaskFormatter();
        formatter.setPlaceholderCharacter('_');
        impl = new FlushableFormattedTextField(formatter);
        FieldListener listener = new FieldListener();
        impl.addFocusListener(listener);
        impl.addKeyListener(listener);

        setShowSeconds(timeFormat.contains("ss"));
    }

    @Override
    public boolean getShowSeconds() {
        return showSeconds;
    }

    private Object validateRawValue(String value) throws NumberFormatException, ParseException {
        if (value.equals(StringUtils.replaceChars(mask, "#U", "__")))
            return null;

        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
        sdf.setLenient(false);
        try {
            return sdf.parse(value);
        } catch (ParseException e) {
            showValidationMessage();
            return prevValue;
        }
    }

    private void showValidationMessage() {
        Messages messages = AppBeans.get(Messages.NAME);<commit_msg>Unable to use DesktopTimeField with java.sql.Time property #PL-5521<commit_after>/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.desktop.gui.components;

import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.chile.core.model.utils.InstanceUtils;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.MessageTools;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.desktop.sys.DesktopToolTipManager;
import com.haulmont.cuba.desktop.sys.vcl.Flushable;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.components.DateField;
import com.haulmont.cuba.gui.components.IFrame;
import com.haulmont.cuba.gui.components.TimeField;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author krivopustov
 * @version $Id$
 */
public class DesktopTimeField extends DesktopAbstractField<JFormattedTextField> implements TimeField {

    private boolean showSeconds;
    private String timeFormat;
    private String mask;
    private MaskFormatter formatter;
    private boolean updatingInstance;
    private Datasource datasource;
    private Object prevValue;
    private String caption;
    private DateField.Resolution resolution;
    private boolean editable = true;

    protected static final int DIGIT_WIDTH = 23;

    public DesktopTimeField() {
        UserSessionSource uss = AppBeans.get(UserSessionSource.NAME);

        timeFormat = Datatypes.getFormatStringsNN(uss.getLocale()).getTimeFormat();
        resolution = DateField.Resolution.MIN;
        formatter = new MaskFormatter();
        formatter.setPlaceholderCharacter('_');
        impl = new FlushableFormattedTextField(formatter);
        FieldListener listener = new FieldListener();
        impl.addFocusListener(listener);
        impl.addKeyListener(listener);

        setShowSeconds(timeFormat.contains("ss"));
    }

    @Override
    public boolean getShowSeconds() {
        return showSeconds;
    }

    private Object validateRawValue(String value) throws NumberFormatException, ParseException {
        if (value.equals(StringUtils.replaceChars(mask, "#U", "__"))) {
            return null;
        }

        SimpleDateFormat sdf = new SimpleDateFormat(timeFormat);
        sdf.setLenient(false);
        try {
            Date parsedValue = sdf.parse(value);

            Class targetType = null;
            if (datasource != null && metaPropertyPath != null) {
                targetType = metaPropertyPath.getRangeJavaClass();
            }

            if (targetType == java.sql.Time.class) {
                return new Time(parsedValue.getTime());
            }
            if (targetType == java.sql.Date.class) {
                log.warn("Do not use java.sql.Date with time field");
                return new java.sql.Date(parsedValue.getTime());
            }

            return parsedValue;
        } catch (ParseException e) {
            showValidationMessage();
            return prevValue;
        }
    }

    private void showValidationMessage() {
        Messages messages = AppBeans.get(Messages.NAME);
{'fraction_non_alphanumeric': 0.09995794195990466, 'fraction_numerical': 0.0033646432076265245, 'mean_word_length': 3.564299424184261, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6003486', 'n_tokens_mistral': 2116, 'n_tokens_neox': 2013, 'n_words': 413}