OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | 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
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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;
19
 
20
import java.awt.Color;
21
import java.awt.Component;
22
import java.awt.Font;
23
import java.awt.Graphics;
83 ilm 24
import java.awt.Graphics2D;
18 ilm 25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.Insets;
83 ilm 28
import java.awt.RenderingHints;
142 ilm 29
import java.math.BigDecimal;
67 ilm 30
import java.math.RoundingMode;
18 ilm 31
 
32
import javax.swing.JLabel;
33
import javax.swing.JList;
34
import javax.swing.JPanel;
35
import javax.swing.ListCellRenderer;
36
import javax.swing.SwingConstants;
37
 
174 ilm 38
public class TicketCellRenderer implements ListCellRenderer<TicketItem> {
18 ilm 39
 
40
    @Override
174 ilm 41
    public Component getListCellRendererComponent(JList<? extends TicketItem> list, TicketItem item, int index, boolean isSelected, boolean cellHasFocus) {
42
 
18 ilm 43
        JPanel p = new JPanel();
44
        p.setLayout(new GridBagLayout());
45
        GridBagConstraints c = new GridBagConstraints();
46
        c.gridx = 0;
47
 
48
        c.fill = GridBagConstraints.HORIZONTAL;
49
        c.insets = new Insets(5, 5, 5, 5);
174 ilm 50
        final JLabel l1 = new JLabel(item.getArticle().toString(), SwingConstants.RIGHT);
18 ilm 51
 
52
        p.add(l1, c);
53
        c.gridx++;
54
        c.weightx = 1;
174 ilm 55
        Article article = item.getArticle();
67 ilm 56
        final JLabel l2 = new JLabel(article.getName().toUpperCase(), SwingConstants.LEFT);
18 ilm 57
        p.add(l2, c);
58
        c.gridx++;
59
        c.weightx = 0;
67 ilm 60
 
142 ilm 61
        final BigDecimal priceWithTax = article.getPriceWithTax();
62
        final JLabel l3 = new JLabel(toString(priceWithTax), SwingConstants.RIGHT);
18 ilm 63
        p.add(l3, c);
64
 
67 ilm 65
        //
18 ilm 66
        l1.setOpaque(false);
67
        l2.setOpaque(false);
68
        l3.setOpaque(false);
69
 
70
        if (isSelected) {
71
            p.setOpaque(true);
72
            p.setBackground(new Color(232, 242, 254));
73
        } else {
74
            p.setOpaque(false);
75
        }
76
        // l2.setFont(f);
77
        l1.setFont(new Font("Arial", Font.PLAIN, 18));
78
        l2.setFont(new Font("Arial", Font.PLAIN, 18));
79
        l3.setFont(new Font("Arial", Font.PLAIN, 18));
80
 
81
        return p;
82
    }
83
 
174 ilm 84
    public void paint(Graphics g, ScrollableList list, TicketItem item, int index, boolean isSelected) {
18 ilm 85
 
86
        if (isSelected) {
87
            g.setColor(new Color(232, 242, 254));
88
            g.fillRect(0, 0, list.getWidth(), list.getCellHeight());
89
        }
90
        g.setColor(Color.BLACK);
91
 
92
        final int inset = 5;
83 ilm 93
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
94
        g.setFont(new Font("Arial", Font.PLAIN, 16));
18 ilm 95
        final int height = g.getFontMetrics().getMaxAscent() + g.getFontMetrics().getMaxDescent() + inset;
96
 
174 ilm 97
        System.err.println("TicketCellRenderer.paint()" + item);
98
 
99
        Article article = item.getArticle();
100
        final BigDecimal qty = item.getQty();
101
        String s1 = qty.toString();
102
        if (article.getSalesUnit() != null) {
103
            s1 = qty.toString() + article.getSalesUnit();
104
        }
18 ilm 105
        g.drawString(s1, inset, height);
174 ilm 106
        final int width1 = (int) g.getFontMetrics().getStringBounds("1999kg ", g).getWidth() + inset * 2;
18 ilm 107
 
174 ilm 108
        String s2 = article.getName().toUpperCase().trim();
109
 
110
        final int maxLength = 15;
18 ilm 111
        if (s2.length() > maxLength)
112
            s2 = s2.substring(0, maxLength + 1) + '…';
113
        g.drawString(s2, width1 + inset, height);
114
 
174 ilm 115
        final String s3 = centsToString(article.getPriceWithTax().multiply(qty).movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue()) + "€";
116
        final int width3 = (int) g.getFontMetrics().getStringBounds(s3, g).getWidth() + +inset;
117
        g.drawString(s3, list.getWidth() - width3, height - 4);
118
 
119
        g.setFont(g.getFont().deriveFont(12f));
120
        final String s4 = qty.toPlainString() + " x " + centsToString(article.getPriceWithTax().movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
121
 
122
        final int width4 = (int) g.getFontMetrics().getStringBounds(s4, g).getWidth() + +inset;
123
        g.drawString(s4, list.getWidth() - width4, height + 11);
124
 
18 ilm 125
    }
126
 
127
    public static String centsToString(int cents) {
151 ilm 128
        final int c = Math.abs(cents) % 100;
18 ilm 129
        String sc = String.valueOf(c);
130
        if (c < 10) {
131
            sc = "0" + sc;
132
        }
133
        return cents / 100 + "." + sc;
134
    }
135
 
142 ilm 136
    public static String toString(final BigDecimal p) {
137
        return centsToString(p.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue());
138
    }
174 ilm 139
 
18 ilm 140
}