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.supplychain.stock.element;

import org.openconcerto.erp.preferences.GestionArticleGlobalPreferencePanel;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.preferences.SQLPreferences;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.table.JTreeTable;
import org.openconcerto.ui.table.MultiLineHeaderRenderer;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

public class StockConsultPanel extends JPanel {

    private JTable tableStock;
    private StockTreeModel stockModel;
    private JLabel code = new JLabel();
    private JLabel nom = new JLabel();
    private JLabel taille = new JLabel();
    private JLabel couleur = new JLabel();
    private final boolean hasDeclinaison;

    public StockConsultPanel(SQLElement eltArt, SQLRowAccessor sqlRowAccessor) {
        super(new GridBagLayout());
        final SQLPreferences prefs = SQLPreferences.getMemCached(eltArt.getTable().getDBRoot());
        this.hasDeclinaison = prefs.getBoolean(GestionArticleGlobalPreferencePanel.ACTIVER_DECLINAISON, false);

        final GridBagConstraints c = new DefaultGridBagConstraints();
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.weightx = 0;
        this.add(new JLabel("Code : ", SwingConstants.RIGHT), c);
        c.gridx++;
        c.weightx = 1;
        this.add(this.code, c);
        c.gridy++;
        c.gridx = 0;
        c.weightx = 0;
        this.add(new JLabel("Désignation : ", SwingConstants.RIGHT), c);
        c.weightx = 0;
        c.gridx++;
        c.weightx = 1;
        this.add(this.nom, c);

        if (this.hasDeclinaison) {
            // Taille
            c.gridy++;
            c.gridx = 0;
            c.weightx = 0;
            this.add(new JLabel("Taille : ", SwingConstants.RIGHT), c);            
            c.weightx = 0;
            c.gridx++;
            c.weightx = 1;
            this.add(this.taille, c);
            // Couleur
            c.gridy++;
            c.gridx = 0;
            c.weightx = 0;
            this.add(new JLabel("Couleur : ", SwingConstants.RIGHT), c);
            c.gridx++;
            c.weightx = 0;
            c.weightx = 1;
            this.add(this.couleur, c);
        }
        c.gridy++;
        c.gridx = 0;
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = 2;
        c.weighty = 1;
        this.stockModel = new StockTreeModel(new StockRootNode(eltArt.getForeignElement("ID_STOCK")));
        load(sqlRowAccessor);
        this.tableStock = new JTreeTable(this.stockModel);

        MultiLineHeaderRenderer renderer = new MultiLineHeaderRenderer();
        Enumeration e = this.tableStock.getColumnModel().getColumns();
        while (e.hasMoreElements()) {
            final TableColumn tableColumn = (TableColumn) e.nextElement();
            tableColumn.setHeaderRenderer(renderer);

        }
        this.tableStock.getColumnModel().getColumn(0).setWidth(260);
        this.tableStock.getColumnModel().getColumn(0).setMinWidth(200);
        if (this.tableStock.getColumnModel().getColumnCount() > 5) {
            this.tableStock.getColumnModel().getColumn(5).setCellRenderer(new DefaultTableCellRenderer() {
                @Override
                public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                    if (value != null) {
                        value = new SimpleDateFormat("dd/MM/yyyy").format((Date) value);
                    }
                    return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                }
            });
        }
        this.add(new JScrollPane(this.tableStock), c);
        c.fill = GridBagConstraints.NONE;
        c.anchor = GridBagConstraints.SOUTHEAST;
        final JButton close = new JButton("Fermer");
        close.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                ((JFrame) SwingUtilities.getRoot(StockConsultPanel.this)).dispose();
            }
        });
        this.add(close, c);
        this.setMinimumSize(new Dimension(960, 400));
        this.setPreferredSize(new Dimension(960, 400));
    }

    public void load(SQLRowAccessor article) {
        this.code.setText(article.getString("CODE"));
        this.nom.setText(article.getString("NOM"));
        if (this.hasDeclinaison) {
            final SQLRowAccessor nonEmptyForeignTaille = article.getNonEmptyForeign("ID_ARTICLE_DECLINAISON_TAILLE");
            if (nonEmptyForeignTaille != null) {
                this.taille.setText(nonEmptyForeignTaille.getString("NOM"));
            } else {
                this.taille.setText("");
            }

            final SQLRowAccessor nonEmptyForeignCouleur = article.getNonEmptyForeign("ID_ARTICLE_DECLINAISON_COULEUR");
            if (nonEmptyForeignCouleur != null) {
                this.couleur.setText(nonEmptyForeignCouleur.getString("NOM"));
            } else {
                this.couleur.setText("");
            }

        }
        this.stockModel.load(article);

    }

}