OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 174 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
18 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.sales.pos.ui;
15
 
16
import org.openconcerto.erp.core.sales.pos.model.Article;
174 ilm 17
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
18 ilm 18
import org.openconcerto.ui.touch.ScrollableList;
182 ilm 19
import org.openconcerto.utils.QuickOrderedMap;
18 ilm 20
 
21
import java.awt.Color;
22
import java.awt.Component;
23
import java.awt.Font;
24
import java.awt.Graphics;
83 ilm 25
import java.awt.Graphics2D;
18 ilm 26
import java.awt.GridBagConstraints;
27
import java.awt.GridBagLayout;
28
import java.awt.Insets;
83 ilm 29
import java.awt.RenderingHints;
142 ilm 30
import java.math.BigDecimal;
67 ilm 31
import java.math.RoundingMode;
18 ilm 32
 
33
import javax.swing.JLabel;
34
import javax.swing.JList;
35
import javax.swing.JPanel;
36
import javax.swing.ListCellRenderer;
37
import javax.swing.SwingConstants;
38
 
174 ilm 39
public class TicketCellRenderer implements ListCellRenderer<TicketItem> {
18 ilm 40
 
41
    @Override
174 ilm 42
    public Component getListCellRendererComponent(JList<? extends TicketItem> list, TicketItem item, int index, boolean isSelected, boolean cellHasFocus) {
43
 
18 ilm 44
        JPanel p = new JPanel();
45
        p.setLayout(new GridBagLayout());
46
        GridBagConstraints c = new GridBagConstraints();
47
        c.gridx = 0;
48
 
49
        c.fill = GridBagConstraints.HORIZONTAL;
50
        c.insets = new Insets(5, 5, 5, 5);
174 ilm 51
        final JLabel l1 = new JLabel(item.getArticle().toString(), SwingConstants.RIGHT);
18 ilm 52
 
53
        p.add(l1, c);
54
        c.gridx++;
55
        c.weightx = 1;
174 ilm 56
        Article article = item.getArticle();
67 ilm 57
        final JLabel l2 = new JLabel(article.getName().toUpperCase(), SwingConstants.LEFT);
18 ilm 58
        p.add(l2, c);
59
        c.gridx++;
60
        c.weightx = 0;
67 ilm 61
 
182 ilm 62
        final BigDecimal priceWithTax = article.getPriceWithTax(item.getQty());
142 ilm 63
        final JLabel l3 = new JLabel(toString(priceWithTax), SwingConstants.RIGHT);
18 ilm 64
        p.add(l3, c);
65
 
67 ilm 66
        //
18 ilm 67
        l1.setOpaque(false);
68
        l2.setOpaque(false);
69
        l3.setOpaque(false);
70
 
71
        if (isSelected) {
72
            p.setOpaque(true);
73
            p.setBackground(new Color(232, 242, 254));
74
        } else {
75
            p.setOpaque(false);
76
        }
77
        // l2.setFont(f);
78
        l1.setFont(new Font("Arial", Font.PLAIN, 18));
79
        l2.setFont(new Font("Arial", Font.PLAIN, 18));
80
        l3.setFont(new Font("Arial", Font.PLAIN, 18));
81
 
82
        return p;
83
    }
84
 
174 ilm 85
    public void paint(Graphics g, ScrollableList list, TicketItem item, int index, boolean isSelected) {
18 ilm 86
 
87
        if (isSelected) {
88
            g.setColor(new Color(232, 242, 254));
89
            g.fillRect(0, 0, list.getWidth(), list.getCellHeight());
90
        }
91
        g.setColor(Color.BLACK);
92
 
93
        final int inset = 5;
83 ilm 94
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
95
        g.setFont(new Font("Arial", Font.PLAIN, 16));
18 ilm 96
        final int height = g.getFontMetrics().getMaxAscent() + g.getFontMetrics().getMaxDescent() + inset;
97
 
174 ilm 98
        System.err.println("TicketCellRenderer.paint()" + item);
99
 
100
        Article article = item.getArticle();
101
        final BigDecimal qty = item.getQty();
102
        String s1 = qty.toString();
103
        if (article.getSalesUnit() != null) {
104
            s1 = qty.toString() + article.getSalesUnit();
105
        }
18 ilm 106
        g.drawString(s1, inset, height);
174 ilm 107
        final int width1 = (int) g.getFontMetrics().getStringBounds("1999kg ", g).getWidth() + inset * 2;
18 ilm 108
 
174 ilm 109
        String s2 = article.getName().toUpperCase().trim();
110
 
111
        final int maxLength = 15;
182 ilm 112
        if (s2.length() > maxLength) {
18 ilm 113
            s2 = s2.substring(0, maxLength + 1) + '…';
182 ilm 114
        }
115
        QuickOrderedMap<String, String> decls = article.getDeclinaisons();
116
        String textDeclinaisons = null;
117
        if (decls != null && !decls.isEmpty()) {
118
            StringBuilder declinaisons = new StringBuilder();
119
            for (int i = 0; i < decls.size(); i++) {
120
                declinaisons.append(decls.getValue(i));
121
                declinaisons.append("  ");
122
            }
18 ilm 123
 
182 ilm 124
            textDeclinaisons = declinaisons.toString().trim();
125
 
126
        }
127
 
128
        g.drawString(s2, width1 + inset, height - 6);
129
 
130
        final String s3 = centsToString(article.getPriceWithTax(qty, true).multiply(qty).movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue()) + "€";
174 ilm 131
        final int width3 = (int) g.getFontMetrics().getStringBounds(s3, g).getWidth() + +inset;
132
        g.drawString(s3, list.getWidth() - width3, height - 4);
133
 
134
        g.setFont(g.getFont().deriveFont(12f));
182 ilm 135
        final String s4 = qty.toPlainString() + " x " + centsToString(article.getPriceWithTax(qty).movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
174 ilm 136
 
137
        final int width4 = (int) g.getFontMetrics().getStringBounds(s4, g).getWidth() + +inset;
138
        g.drawString(s4, list.getWidth() - width4, height + 11);
182 ilm 139
        if (textDeclinaisons != null) {
140
            g.drawString(textDeclinaisons, width1 + inset, height + 11);
141
        }
174 ilm 142
 
182 ilm 143
        if (!article.getDiscountPct().equals(BigDecimal.ZERO)) {
144
            g.setFont(g.getFont().deriveFont(Font.ITALIC));
145
            g.drawString("Remise: " + article.getDiscountPct().movePointRight(2).setScale(2, BigDecimal.ROUND_HALF_UP) + " %", inset * 2 + 140, height + 12);
146
        }
147
 
18 ilm 148
    }
149
 
150
    public static String centsToString(int cents) {
151 ilm 151
        final int c = Math.abs(cents) % 100;
18 ilm 152
        String sc = String.valueOf(c);
153
        if (c < 10) {
154
            sc = "0" + sc;
155
        }
156
        return cents / 100 + "." + sc;
157
    }
158
 
142 ilm 159
    public static String toString(final BigDecimal p) {
160
        return centsToString(p.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
161
    }
174 ilm 162
 
18 ilm 163
}