OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.erp.core.sales.product.element;

import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.utils.Tuple2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.swing.table.AbstractTableModel;

public class TailleCouleurTableModel extends AbstractTableModel {
    // Tailles en colonne, Couleurs en lignes
    private final List<SQLRow> tailles;
    private final List<SQLRow> couleurs;
    private final Boolean[][] selected;

    public TailleCouleurTableModel(ReferenceArticleSQLElement element) {
        final SQLTable tTaille = element.getTable().getTable("ARTICLE_DECLINAISON_TAILLE");
        final SQLSelect sTailles = new SQLSelect();
        sTailles.addSelect(tTaille.getKey());
        sTailles.addSelect(tTaille.getField("NOM"));
        sTailles.addFieldOrder(tTaille.getOrderField());
        this.tailles = new ArrayList<>(SQLRowListRSH.execute(sTailles));

        final SQLTable tCouleur = element.getTable().getTable("ARTICLE_DECLINAISON_COULEUR");
        final SQLSelect sCouleurs = new SQLSelect();
        sCouleurs.addSelect(tCouleur.getKey());
        sCouleurs.addSelect(tCouleur.getField("NOM"));
        sCouleurs.addFieldOrder(tCouleur.getOrderField());
        this.couleurs = new ArrayList<>(SQLRowListRSH.execute(sCouleurs));
        final int size1 = this.tailles.size();
        final int size2 = this.couleurs.size();
        this.selected = new Boolean[size1][size2];
        for (int i = 0; i < size1; i++) {
            for (int j = 0; j < size2; j++) {
                this.selected[i][j] = Boolean.FALSE;
            }
        }

    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        if (columnIndex > 0) {
            this.selected[columnIndex - 1][rowIndex] = (Boolean) aValue;
        }
    }

    @Override
    public int getRowCount() {
        return this.couleurs.size();
    }

    @Override
    public int getColumnCount() {
        return this.tailles.size() + 1;
    }

    @Override
    public String getColumnName(int columnIndex) {
        if (columnIndex == 0) {
            return "Couleurs / Tailles";
        }
        return this.tailles.get(columnIndex - 1).getString("NOM");

    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        if (columnIndex == 0) {
            return String.class;
        }
        return Boolean.class;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (columnIndex == 0) {
            return this.couleurs.get(rowIndex).getString("NOM");
        }
        return this.selected[columnIndex - 1][rowIndex];
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return columnIndex > 0;
    }

    /**
     * Tuple d'ids Taille - Couleur
     */
    public List<Tuple2<SQLRow, SQLRow>> getSelected() {
        List<Tuple2<SQLRow, SQLRow>> r = new ArrayList<>();
        final int size1 = this.tailles.size();
        final int size2 = this.couleurs.size();
        for (int i = 0; i < size1; i++) {
            for (int j = 0; j < size2; j++) {
                if (this.selected[i][j].equals(Boolean.TRUE)) {
                    r.add(new Tuple2<SQLRow, SQLRow>(this.tailles.get(i), this.couleurs.get(j)));
                }
            }
        }

        return r;
    }

}