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

fr.ird.observe.client.form.spi.init.JXDatePickerInitializer Maven / Gradle / Ivy

package fr.ird.observe.client.form.spi.init;

/*-
 * #%L
 * ObServe Toolkit :: Common Client
 * %%
 * Copyright (C) 2008 - 2017 IRD, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import fr.ird.observe.client.form.FormUI;
import fr.ird.observe.client.form.spi.FormUIComponentInitializerSupport;
import fr.ird.observe.client.form.spi.FormUIInitializerContext;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.text.ParseException;
import java.util.Date;
import java.util.Objects;
import javax.swing.JFormattedTextField;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jdesktop.swingx.JXDatePicker;
import org.jdesktop.swingx.JXMonthView;
import org.nuiton.util.DateUtil;


import static fr.ird.observe.client.form.FormUIHandler.PROPERTY_NAME;

/**
 * Created by tchemit on 20/09/17.
 *
 * @author Tony Chemit - [email protected]
 */
public class JXDatePickerInitializer> extends FormUIComponentInitializerSupport {
    /** Logger. */
    private static final Log log = LogFactory.getLog(JXDatePickerInitializer.class);

    public JXDatePickerInitializer() {
        super(JXDatePicker.class);
    }

    @Override
    public void init(V context, JXDatePicker component) {

        log.debug("init JXDatePicker editor" + component.getName());
        component.setLocale(context.getLocale());
        JFormattedTextField editor = component.getEditor();
        editor.setEditable(true);
        JXMonthView monthView = new JXMonthView();
        monthView.setLowerBound(DateUtil.createDate(1, 1, 1970));
        monthView.setTraversable(true);
        monthView.setZoomable(true);
        component.setMonthView(monthView);

        final String propertyName = (String) editor.getClientProperty(PROPERTY_NAME);
        if (propertyName != null) {
            editor.addActionListener(e -> {
                JXDatePicker source = (JXDatePicker) e.getSource();
                Date date = source.getDate();
                if (Objects.equals(date, context.getBean().get(propertyName))) {
                    // On n'envoie pas la demande, car cela va modifier ensuite le formulaire
                    // car avec oldValue=null et newValue=null, les pcs sont déclanchés...
                    return;
                }
                context.getBean().set(propertyName, date);
            });
        }

        context.addAutoSelectOnFocus(editor);
        editor.addFocusListener(new FocusAdapter() {
            @Override
            public void focusLost(FocusEvent e) {

                JFormattedTextField source = (JFormattedTextField) e.getSource();
                if (source.isEditValid()) {
                    try {
                        editor.commitEdit();
                        log.debug("quit date editor, commit value: " + source.getValue());
                    } catch (ParseException e1) {
                        // l'édition est valide donc pas de problème ici
                    }
                }
                super.focusLost(e);
            }
        });

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy