OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011 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.ui;

import org.openconcerto.erp.core.common.ui.DeviseNiceTableCellRenderer;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.view.list.RowValuesTable;
import org.openconcerto.sql.view.list.RowValuesTableModel;
import org.openconcerto.ui.table.AlternateTableCellRenderer;
import org.openconcerto.ui.table.XTableColumnModel;
import org.openconcerto.utils.CollectionUtils;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;

public class DeliveredQtyRowValuesRenderer extends DeviseNiceTableCellRenderer {

    // Red
    public static final Color red = new Color(255, 31, 52);
    public static final Color redLightGrey = new Color(240, 65, 85);

    // Orange
    public final static Color orange = new Color(243, 125, 75);
    public final static Color orangeGrey = new Color(222, 107, 47);

    // Blue
    public final static Color light = new Color(232, 238, 250);
    public final static Color lightGrey = new Color(211, 220, 222);

    // Black
    public final static Color lightBlack = new Color(192, 192, 192);
    public final static Color lightBlackGrey = new Color(155, 155, 155);

    private final boolean customer;

    public DeliveredQtyRowValuesRenderer() {
        this(true);
    }

    public DeliveredQtyRowValuesRenderer(boolean customer) {
        AlternateTableCellRenderer.setBGColorMap(this, CollectionUtils.createMap(lightBlack, lightBlackGrey, red, redLightGrey, orange, orangeGrey));
        this.customer = customer;
    }

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        Component comp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if (table instanceof RowValuesTable) {

            ((JLabel) comp).setHorizontalAlignment(SwingConstants.RIGHT);
            RowValuesTable rowValuesTable = (RowValuesTable) table;
            XTableColumnModel columnModel = rowValuesTable.getColumnModel();
            RowValuesTableModel model = rowValuesTable.getRowValuesTableModel();
            boolean qteALivrerVisible = columnModel.isColumnVisible(columnModel.getColumnByModelIndex(model.getColumnForField("QTE_A_LIVRER")));
            if (qteALivrerVisible) {
                SQLRowValues rowVals = model.getRowValuesAt(row);

                Number qte;
                Number qteL;
                if (this.customer) {
                    qte = (Number) rowVals.getObject("QTE");
                    qteL = (Number) rowVals.getObject("QTE_LIVREE");
                } else {
                    qte = (Number) rowVals.getObject("QTE_ORIGINE");
                    qteL = (Number) rowVals.getObject("QTE");
                }
                if (qte != null && qteL != null) {
                    if (qte.intValue() < qteL.intValue()) {
                        comp.setBackground(red);
                    } else if (qteL.intValue() <= 0) {
                        comp.setBackground(lightBlack);
                    } else if (qteL.intValue() != qte.intValue()) {
                        comp.setBackground(orange);
                    }
                }
            }
        }
        return comp;
    }
}