Losed Focus

Hi, i’ve attached a Video that shows how i lose the focus. (change extension to mp4)

Pay attention:

I insert new line, and can navigate with TAB key for all cells.

If i modify a price (then starts a listener) they modify prices, and we cannot navigate to the next cell.

How can i Solve this?

This is the code of the screen:


package com.company.imgestion.gui.presupuestos;

import com.company.imgestion.entity.*;
import com.company.imgestion.service.CalculoslineaService;
import com.haulmont.chile.core.common.ValueListener;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.LoadContext;
import com.haulmont.cuba.core.global.Metadata;
import com.haulmont.cuba.core.global.PersistenceHelper;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.EditAction;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;

import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.Column;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class PresupuestosEdit extends AbstractEditor<Presupuestos> {

    /*VARIABLES NECESARIAS PARA TRABAJAR CON LA BDD Y TRATAR OTRAS TABLAS EN EL ENTORNO*/
    @Inject
    private DataManager dataManager;
    @Inject
    private Metadata metadata;

    @Named("fieldGroup.clientes")
    private PickerField clientes;

    @Named("fieldGroup.numero")
    private TextField numero;

    @Inject
    Column Cabecera;

    @Inject
    private Table<PresupuestosLineas> lineasTable;

    @Inject
    private CalculoslineaService calculoslineaService;


    @Inject
    private LookupPickerField selectorarticulo;

    /*DEFINIMOS LA TABLA CON LA QUE TRABAJAREMOS, ESTA TABLA ES LA QUE TIENE EL XML VINCULADO EN LA PANTALLA*/
    @Inject
    private CollectionDatasource<PresupuestosLineas, UUID> lineasDs;


    @Override
    protected void postInit() {
        clientes.addValueChangeListener(e -> {
            if (e.getValue() !=null) {
                showNotification("Cliente seleccionado", NotificationType.HUMANIZED);
                getItem().setVendedor(getItem().getClientes().getVendedor());
                getItem().setFormapago(getItem().getClientes().getFormaPago());
            }
        });

        //detectamos si la pantalla es de un producto/entidad nueva o no
        if (PersistenceHelper.isNew(getItem())) {
            getItem().setNumero(999999);
            numero.setEditable(false);
            numero.setVisible(false);
        }
    }

    /*LE AÑADISMO EL LISTENER DE CAMBIOS*/
    @Override
    public void init(Map<String, Object> params) {

        lineasDs.addCollectionChangeListener(e -> calculartotal());
        //lineasDs.addItemPropertyChangeListener(e -> calculartotallinea());
        lineasDs.addItemPropertyChangeListener(e -> {
            if (e.getProperty().equals("articulo")){
                if ((lineasDs.getItem() != null) && (lineasDs.getItem().getArticulo() !=null))
                lineasDs.getItem().setDescripcion(lineasDs.getItem().getArticulo().getNombre());
                lineasDs.getItem().setTipoIva(lineasDs.getItem().getArticulo().getIva());
                lineasDs.getItem().setPorcentajeIva(lineasDs.getItem().getArticulo().getIva().getIva());
                calculartotallinea();
            }
            calculartotallinea();
        });
    }

    /*CALCULAMOS VALORES*/
    private void calculartotal() {
        /*IMPORTE TOTAL PRESUPUESTO*/
        /*DEFINIMOS VARIABLES NECESARIAS*/
        BigDecimal importe = BigDecimal.ZERO;
        BigDecimal importeiva = BigDecimal.ZERO;
        BigDecimal importerecargo = BigDecimal.ZERO;

        /*VAMOS RECALCULANDO LOS TOTALES POR CADA LINEA Y ASIGNANDOLOS A LAS VARIABLES*/
        /*SIEMPRE SUMAMOS EN LAS VARIABLES*/
        for (PresupuestosLineas linea : lineasDs.getItems()) {
            importe = importe.add(linea.getImportebases());
            if (getItem().getClientes() != null && getItem().getClientes().getRecargo()) {
                importerecargo = importerecargo.add(linea.getImporterecargo());
            }
            importeiva = importeiva.add(linea.getImportecuotas());
        }

        /*ASIGNAMOS VALORES A LOS CAMPOS*/
        getItem().setImporteBase(importe);
        getItem().setImporteCuotas(importeiva);
        getItem().setImporteRecargos(importerecargo);

        /*SUMAMOS LAS TRES VARIABLES EN EL MISMO PROCESO.*/
        getItem().setImporteTotal(importe.add(importeiva.add(importerecargo)));
    }

    private void calculartotallinea() {



        if (lineasDs.isModified()) {

            if ((lineasDs.getItem() != null) && (lineasDs.getItem().getArticulo() !=null) && (lineasDs.getItem().getDescripcion() == "")) {
                lineasDs.getItem().setDescripcion(lineasDs.getItem().getArticulo().getNombre());
            }

            if ((lineasDs.getItem() != null) && (lineasDs.getItem().getArticulo() !=null)){
                lineasDs.getItem().setTipoIva(lineasDs.getItem().getArticulo().getIva());
                lineasDs.getItem().setPorcentajeIva(lineasDs.getItem().getArticulo().getIva().getIva());

                //-----------------
                if (lineasDs.getItem().getPrecio() == BigDecimal.ZERO) {
                    LoadContext<ArticulosPvp> context = LoadContext.create(ArticulosPvp.class).setQuery(
                            LoadContext.createQuery("select pp from imgestion$ArticulosPvp pp where pp.articulo.id = :productid and pp.tarifa.id = :tarifa")
                                    .setParameter("productid", lineasDs.getItem().getArticulo().getId())
                                    .setParameter("tarifa", getItem().getClientes().getTarifa().getId()));
                    ArticulosPvp pp = dataManager.load(context);

                    if ((pp != null) && (pp.getPvp() != null)) {
                        BigDecimal price = pp.getPvp();
                        lineasDs.getItem().setPrecio(price);
                    }
                }
            }

            lineasDs.getItem().setImportebases(calculoslineaService.totalbruto(lineasDs.getItem()));
            lineasDs.getItem().setImportetotal(calculoslineaService.totallineaivaincluido(lineasDs.getItem()));
            lineasDs.getItem().setImportecuotas(calculoslineaService.importecuotas(lineasDs.getItem()));
            lineasDs.getItem().setImporteDescuento(calculoslineaService.importedescuento(lineasDs.getItem()));
        }

        calculartotal();
    }

    public void addlinedirectly(Component source) {
        BigDecimal cero = BigDecimal.ZERO;
        Integer numerolinea;

        // buscamos cuantos registros hay en la tabla de productos.
        numerolinea = lineasDs.getItems().size();

        if (numerolinea == 0) {
            numerolinea = 1;
        } else {
            int max = 0;
            for (PresupuestosLineas details : lineasDs.getItems()) {
                max = Math.max(max, details .getLinea());
            }
            numerolinea = max + 1;
        }

        PresupuestosLineas lineas = metadata.create(PresupuestosLineas.class);
        lineas.setDescripcion("");
        if ((selectorarticulo.getValue()) != null) {
            lineas.setArticulo(selectorarticulo.getValue());
        }
        if (lineas.getArticulo() != null) {
            lineas.setDescripcion(lineas.getArticulo().getNombre());
            lineas.setTipoIva(lineas.getArticulo().getIva());
            lineas.setPorcentajeIva(lineas.getArticulo().getIva().getIva());

            //-----------------
            if (lineas.getPrecio() == BigDecimal.ZERO) {
                LoadContext<ArticulosPvp> context = LoadContext.create(ArticulosPvp.class).setQuery(
                        LoadContext.createQuery("select pp from imgestion$ArticulosPvp pp where pp.articulo.id = :productid and pp.tarifa.id = :tarifa")
                                .setParameter("productid", lineas.getArticulo().getId())
                                .setParameter("tarifa", getItem().getClientes().getTarifa().getId()));
                ArticulosPvp pp = dataManager.load(context);

                if ((pp != null) && (pp.getPvp() != null)) {
                    BigDecimal price = pp.getPvp();
                    lineas.setPrecio(price);
                }
            }
            lineas.setImportebases(calculoslineaService.totallineaivaincluido(lineas));
            lineas.setImportebases(calculoslineaService.totalbruto(lineas));
            lineas.setImportetotal(calculoslineaService.totallineaivaincluido(lineas));
            lineas.setImportecuotas(calculoslineaService.importecuotas(lineas));
            lineas.setImporteDescuento(calculoslineaService.importedescuento(lineas));
        }

        //-----------------
        lineas.setCabecera(getItem());
        lineas.setLinea(numerolinea);

        lineasDs.addItem(lineas);
        lineasTable.setSelected(lineas);
        lineasTable.requestFocus();

        selectorarticulo.setValue(null);
        calculartotal();
    }

    @Override
    protected void initNewItem(Presupuestos item) {
        List<Empresas> empresas = dataManager.loadList(
                LoadContext.create(Empresas.class).setQuery(
                        LoadContext.createQuery("select c from imgestion$Empresas c")));
                if (empresas.size() == 1) {
                    item.setEmpresa(empresas.get(0));
                }
                item.setNumero(99999999);
        item.setEstado(EstadoPresupuesto.Nuevo);
    }

    public void seleccionarlineas(Component source) {
        lineasTable.selectAll();
    }
}

capture-2.zip (264.2K)

Hi Ivan,

for now, if you change items of a Table from your code then corresponding rows be updated in the Table and focus will be lost due to rows HTML update.

In the current version of CUBA you cannot control focus inside a Table. I think you will be able to fix this behavior using the new Table.requestFocus(entity, columnId) method that we will introduce in release 6.3.