OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | 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.model.Article;
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
import org.openconcerto.ui.touch.ScrollableList;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;

public class TicketCellRenderer implements ListCellRenderer<TicketItem> {

    @Override
    public Component getListCellRendererComponent(JList<? extends TicketItem> list, TicketItem item, int index, boolean isSelected, boolean cellHasFocus) {

        JPanel p = new JPanel();
        p.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridx = 0;

        c.fill = GridBagConstraints.HORIZONTAL;
        c.insets = new Insets(5, 5, 5, 5);
        final JLabel l1 = new JLabel(item.getArticle().toString(), SwingConstants.RIGHT);

        p.add(l1, c);
        c.gridx++;
        c.weightx = 1;
        Article article = item.getArticle();
        final JLabel l2 = new JLabel(article.getName().toUpperCase(), SwingConstants.LEFT);
        p.add(l2, c);
        c.gridx++;
        c.weightx = 0;

        final BigDecimal priceWithTax = article.getPriceWithTax();
        final JLabel l3 = new JLabel(toString(priceWithTax), SwingConstants.RIGHT);
        p.add(l3, c);

        //
        l1.setOpaque(false);
        l2.setOpaque(false);
        l3.setOpaque(false);

        if (isSelected) {
            p.setOpaque(true);
            p.setBackground(new Color(232, 242, 254));
        } else {
            p.setOpaque(false);
        }
        // l2.setFont(f);
        l1.setFont(new Font("Arial", Font.PLAIN, 18));
        l2.setFont(new Font("Arial", Font.PLAIN, 18));
        l3.setFont(new Font("Arial", Font.PLAIN, 18));

        return p;
    }

    public void paint(Graphics g, ScrollableList list, TicketItem item, int index, boolean isSelected) {

        if (isSelected) {
            g.setColor(new Color(232, 242, 254));
            g.fillRect(0, 0, list.getWidth(), list.getCellHeight());
        }
        g.setColor(Color.BLACK);

        final int inset = 5;
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
        g.setFont(new Font("Arial", Font.PLAIN, 16));
        final int height = g.getFontMetrics().getMaxAscent() + g.getFontMetrics().getMaxDescent() + inset;

        System.err.println("TicketCellRenderer.paint()" + item);

        Article article = item.getArticle();
        final BigDecimal qty = item.getQty();
        String s1 = qty.toString();
        if (article.getSalesUnit() != null) {
            s1 = qty.toString() + article.getSalesUnit();
        }
        g.drawString(s1, inset, height);
        final int width1 = (int) g.getFontMetrics().getStringBounds("1999kg ", g).getWidth() + inset * 2;

        String s2 = article.getName().toUpperCase().trim();

        final int maxLength = 15;
        if (s2.length() > maxLength)
            s2 = s2.substring(0, maxLength + 1) + '…';
        g.drawString(s2, width1 + inset, height);

        final String s3 = centsToString(article.getPriceWithTax().multiply(qty).movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue()) + "€";
        final int width3 = (int) g.getFontMetrics().getStringBounds(s3, g).getWidth() + +inset;
        g.drawString(s3, list.getWidth() - width3, height - 4);

        g.setFont(g.getFont().deriveFont(12f));
        final String s4 = qty.toPlainString() + " x " + centsToString(article.getPriceWithTax().movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());

        final int width4 = (int) g.getFontMetrics().getStringBounds(s4, g).getWidth() + +inset;
        g.drawString(s4, list.getWidth() - width4, height + 11);

    }

    public static String centsToString(int cents) {
        final int c = Math.abs(cents) % 100;
        String sc = String.valueOf(c);
        if (c < 10) {
            sc = "0" + sc;
        }
        return cents / 100 + "." + sc;
    }

    public static String toString(final BigDecimal p) {
        return centsToString(p.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
    }

}