OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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.pos.ui;

import org.openconcerto.erp.core.sales.pos.POSConfiguration;
import org.openconcerto.erp.core.sales.pos.model.Article;
import org.openconcerto.erp.core.sales.pos.model.RegisterState;
import org.openconcerto.erp.core.sales.pos.model.RegisterState.Status;
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
import org.openconcerto.ui.touch.ScrollableList;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class TicketPanel extends JPanel implements CaisseListener {

    private final DateFormat df = new SimpleDateFormat("' le' d MMMM à H:mm");
    private final Image bg;
    private final ListModel<TicketItem> dataModel;
    private final List<ListDataListener> listeners = new ArrayList<>();
    private final JLabel lTotal = new JLabel("", SwingConstants.RIGHT);
    private final JLabel lNumero = new JLabel("", SwingConstants.LEFT);
    private final CaisseControler controler;
    private final ScrollableList list;
    private int xOffset = 0;

    TicketPanel(final CaisseControler controler) {
        this.controler = controler;
        this.controler.addCaisseListener(this);
        if (this.controler.getPOSConf().getScreenWidth() < 1280) {
            this.xOffset = -24;
        }
        this.setOpaque(false);
        this.bg = new ImageIcon(TicketPanel.class.getResource("ticket.png")).getImage();
        this.setLayout(null);

        this.dataModel = new ListModel<TicketItem>() {

            @Override
            public void addListDataListener(final ListDataListener l) {
                TicketPanel.this.listeners.add(l);
            }

            @Override
            public TicketItem getElementAt(final int index) {
                return controler.getItems().get(index);
            }

            @Override
            public int getSize() {
                return controler.getItems().size();
            }

            @Override
            public void removeListDataListener(final ListDataListener l) {
                TicketPanel.this.listeners.remove(l);
            }

        };

        this.list = new ScrollableList(this.dataModel) {
            private final TicketCellRenderer renderer = new TicketCellRenderer();

            @Override
            public void paintCell(final Graphics g, final Object value, final int index, final boolean isSelected, final int posY) {
                g.translate(0, posY);
                this.renderer.paint(g, TicketPanel.this.list, (TicketItem) value, index, isSelected);
                g.translate(0, -posY);
            }
        };
        this.list.setOpaque(false);
        this.list.setSize(315, 450);
        this.list.setFixedCellHeight(40);
        this.list.setLocation(30 + this.xOffset, 18);
        this.add(this.list);

        this.lTotal.setSize(276 - 10, 32);
        this.lTotal.setLocation(68 + this.xOffset, 500 - 32);
        this.lTotal.setFont(new Font("Arial", Font.BOLD, 18));
        this.add(this.lTotal);
        this.lNumero.setSize(276 - 10, 32);
        this.lNumero.setLocation(68 + this.xOffset, 500);
        this.lNumero.setForeground(Color.DARK_GRAY);
        this.lNumero.setFont(new Font("Arial", Font.BOLD, 12));
        this.add(this.lNumero);

        this.list.addListSelectionListener(new ListSelectionListener() {

            @Override
            public void valueChanged(final ListSelectionEvent e) {
                if (!e.getValueIsAdjusting()) {
                    final Object selectedValue = TicketPanel.this.list.getSelectedValue();
                    if (selectedValue != null) {
                        TicketItem item = (TicketItem) selectedValue;
                        controler.setTicketItemSelected(item);
                        // If the category of the selected article does not match the current
                        // category of the categories list,
                        // then the corresponding article is not selected.
                        controler.setTicketItemSelected(item); // Dirty fix : use two refresh

                    }
                }

            }
        });

    }

    @Override
    protected void paintComponent(final Graphics g) {
        g.drawImage(this.bg, this.xOffset, 0, null);
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        try {
            final RegisterState registerState = this.controler.getCaisseFrame().getFiles().getLastLog().getRegisterState();

            // Display caisse and Vendor ID
            final String infoCaisseVendeur = "Caisse " + this.controler.getPOSConf().getPosID() + "  Vendeur " + this.controler.getPOSConf().getUserID();
            final String infoCaisse = (registerState.getStatus() == Status.OPEN ? "Ouverte" : "Fermée") + registerState.formatDate(this.df);
            g.setColor(new Color(230, 230, 230));
            if (this.controler.getPOSConf().getScreenWidth() < 1280) {
                g.setFont(getFont().deriveFont(16.0f));
            } else {
                g.setFont(getFont().deriveFont(14.0f));
            }
            int w1 = g.getFontMetrics().stringWidth(infoCaisseVendeur);
            int w2 = g.getFontMetrics().stringWidth(infoCaisse);

            int x = 330 - Math.max(w1, w2) + this.xOffset;
            g.drawString(infoCaisseVendeur, x, this.getHeight() - 40);
            g.setColor(Color.LIGHT_GRAY);

            g.drawString(infoCaisse, x, this.getHeight() - 25);
        } catch (Exception e) {
            POSConfiguration.getLogger().log(Level.WARNING, "Couldn't find register state", e);
        }
        super.paintComponent(g);
    }

    int getMinWidth() {
        if (this.controler.getPOSConf().getScreenWidth() < 1280) {
            return 330;
        }
        return 480;
    }

    int getMaxHeight() {
        if (this.controler.getPOSConf().getScreenHeight() < 1000) {
            return 550;
        }
        return 707;
    }

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(getMinWidth(), 550);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(getMinWidth(), getMaxHeight());
    }

    public void fire() {
        for (final ListDataListener l : this.listeners) {
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
        }

    }

    @Override
    public void caisseStateChanged() {
        final Article articleSelected = this.controler.getArticleSelected();
        for (final ListDataListener l : this.listeners) {
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
        }
        this.lTotal.setText("TOTAL:  " + TicketCellRenderer.centsToString(this.controler.getTotal()) + " €");
        this.lNumero.setText("Ticket " + this.controler.getTicketNumber());
        // Rien à selectionner
        if (articleSelected == null) {
            this.list.clearSelection();
            return;
        }
        try {
            // Deja selectionné
            if (this.list.getSelectedValue() != null && articleSelected.equals(((TicketItem) this.list.getSelectedValue()).getArticle())) {
                return;
            }
        } catch (final Exception e) {
            e.printStackTrace();
        }

        for (int i = 0; i < this.dataModel.getSize(); i++) {
            final TicketItem item = this.dataModel.getElementAt(i);
            if (item.getArticle().equals(articleSelected)) {
                this.list.setSelectedValue(item, true);
                break;
            }
        }

    }

}