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

fr.ird.observe.client.form.spi.init.BeanComboBoxInitializer 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.ObserveKeyStrokes;
import fr.ird.observe.client.form.FormUI;
import fr.ird.observe.client.form.spi.FormUIComponentInitializerSupport;
import fr.ird.observe.client.form.spi.FormUIInitializerContext;
import fr.ird.observe.client.util.ComboBoxListCellRenderer;
import fr.ird.observe.dto.decoration.ObserveI18nDecoratorHelper;
import fr.ird.observe.dto.decoration.ObserveI18nLabelsBuilder;
import fr.ird.observe.dto.reference.DataDtoReference;
import fr.ird.observe.dto.reference.ReferentialDtoReference;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.Collections;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.ListCellRenderer;
import javax.swing.SwingUtilities;
import org.nuiton.decorator.MultiJXPathDecorator;
import org.nuiton.jaxx.widgets.select.BeanComboBox;


import static org.nuiton.i18n.I18n.t;

/**
 * Created by tchemit on 20/09/17.
 *
 * @author Tony Chemit - [email protected]
 */
public class BeanComboBoxInitializer> extends FormUIComponentInitializerSupport {

    public BeanComboBoxInitializer() {
        super(BeanComboBox.class);
    }

    @Override
    public void init(V context, BeanComboBox component) {
        log.debug("init beanComboBox for " + component.getBeanType());
        Class referenceType = component.getBeanType();
        context.registerBeanComboBox(component);
        component.setI18nLabelBuilder(new ObserveI18nLabelsBuilder(referenceType));
        component.setI18nPrefix("observe.common.");
        component.setMinimumSize(new Dimension(0, 24));
        component.getResetButton().setToolTipText(component.getResetButton().getToolTipText() + " (ctrl + R)");
        component.getChangeDecorator().setToolTipText(component.getChangeDecorator().getToolTipText() + " (ctrl + F)");
        JComponent editorComponent = (JComponent) component.getCombobox().getEditor().getEditorComponent();

        Action resetAction = new AbstractAction() {
            private static final long serialVersionUID = -2463616162604643653L;

            @Override
            public void actionPerformed(ActionEvent e) {
                component.doActionPerformed__on__resetButton(null);
                SwingUtilities.invokeLater(component::grabFocus);
            }
        };

        editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(ObserveKeyStrokes.KEY_STROKE_RESET, "resetAction");
        editorComponent.getActionMap().put("resetAction", resetAction);

        Action showPopupAction = new AbstractAction() {
            private static final long serialVersionUID = -5863189767390457787L;

            @Override
            public void actionPerformed(ActionEvent e) {
                if (!component.getCombobox().isPopupVisible()) {
                    component.getCombobox().showPopup();
                } else {
                    component.setSelectedItem(component.getCombobox().getEditor().getItem());
                    component.getCombobox().hidePopup();
                }
            }
        };
        editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(ObserveKeyStrokes.KEY_STROKE_ENTER, "showPopupAction");
        editorComponent.getActionMap().put("showPopupAction", showPopupAction);

        Action showDecoratorPopupAction = new AbstractAction() {
            private static final long serialVersionUID = -765620876132416303L;

            @Override
            public void actionPerformed(ActionEvent e) {
                component.getPopup().show(component, 0, 0);
            }
        };
        editorComponent.getInputMap(JComponent.WHEN_FOCUSED).put(ObserveKeyStrokes.KEY_STROKE_SHOW_DECORATOR_POPUP, "showDecoratorPopupAction");
        editorComponent.getActionMap().put("showDecoratorPopupAction", showDecoratorPopupAction);

        if (ReferentialDtoReference.class.isAssignableFrom(referenceType)) {
            prepareReferentialBeanComboBox(context, referenceType, component);
        } else {
            prepareDataBeanComboBox(context, referenceType, component);
        }
    }

    private  void prepareDataBeanComboBox(V context, Class dtoClass, BeanComboBox comboBox) {

        MultiJXPathDecorator decorator = context.getDataReferenceDecorator(dtoClass);

        String entityLabel = t(ObserveI18nDecoratorHelper.getTypeI18nKey(dtoClass));
        comboBox.setPopupTitleText(t("observe.common.DataDto.type", entityLabel));

        comboBox.init(decorator, Collections.emptyList());

        JComboBox combobox = comboBox.getCombobox();

        ListCellRenderer toolTipRenderer = new ComboBoxListCellRenderer<>(combobox.getRenderer());
        combobox.setRenderer(toolTipRenderer);
        log.debug("combo  list [" + dtoClass.getName() + "] : " + comboBox.getData().size());

    }

    private  void prepareReferentialBeanComboBox(V context, Class dtoClass, BeanComboBox comboBox) {

        MultiJXPathDecorator decorator = context.getReferentialReferenceDecorator(dtoClass);

        String entityLabel = t(ObserveI18nDecoratorHelper.getTypeI18nKey(dtoClass));
        comboBox.setPopupTitleText(t("observe.common.ReferentialDto.type", entityLabel));

        comboBox.init(decorator, Collections.emptyList());

        JComboBox combobox = comboBox.getCombobox();

        ListCellRenderer toolTipRenderer = new ComboBoxListCellRenderer<>(combobox.getRenderer());
        combobox.setRenderer(toolTipRenderer);
        log.debug("combo  list [" + dtoClass.getName() + "] : " + comboBox.getData().size());

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy