OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 67 | Rev 149 | 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
 *
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;
17
import org.openconcerto.ui.touch.ScrollableList;
18
import org.openconcerto.utils.Pair;
19
 
20
import java.awt.Color;
21
import java.awt.Dimension;
22
import java.awt.Font;
23
import java.awt.Graphics;
83 ilm 24
import java.awt.Graphics2D;
18 ilm 25
import java.awt.Image;
83 ilm 26
import java.awt.RenderingHints;
18 ilm 27
import java.util.ArrayList;
28
import java.util.List;
29
 
30
import javax.swing.ImageIcon;
31
import javax.swing.JLabel;
32
import javax.swing.JPanel;
33
import javax.swing.ListModel;
34
import javax.swing.SwingConstants;
35
import javax.swing.event.ListDataEvent;
36
import javax.swing.event.ListDataListener;
37
import javax.swing.event.ListSelectionEvent;
38
import javax.swing.event.ListSelectionListener;
39
 
40
public class TicketPanel extends JPanel implements CaisseListener {
41
    private final Image bg;
42
 
43
    private final ListModel dataModel;
44
    private final List<ListDataListener> listeners = new ArrayList<ListDataListener>();
45
    JLabel lTotal = new JLabel("", SwingConstants.RIGHT);
46
    JLabel lNumero = new JLabel("", SwingConstants.LEFT);
47
    private final CaisseControler controler;
48
 
49
    private final ScrollableList list;
50
 
51
    TicketPanel(final CaisseControler controler) {
52
        this.controler = controler;
53
        this.controler.addCaisseListener(this);
54
 
55
        this.setOpaque(false);
56
        this.bg = new ImageIcon(TicketPanel.class.getResource("ticket.png")).getImage();
57
        this.setLayout(null);
58
 
59
        this.dataModel = new ListModel() {
60
 
61
            @Override
62
            public void addListDataListener(final ListDataListener l) {
63
                TicketPanel.this.listeners.add(l);
64
            }
65
 
66
            @Override
67
            public Object getElementAt(final int index) {
68
                return controler.getItems().get(index);
69
            }
70
 
71
            @Override
72
            public int getSize() {
73
                return controler.getItems().size();
74
            }
75
 
76
            @Override
77
            public void removeListDataListener(final ListDataListener l) {
78
                TicketPanel.this.listeners.remove(l);
79
            }
80
 
81
        };
82
 
83
        this.list = new ScrollableList(this.dataModel) {
84
            private final TicketCellRenderer renderer = new TicketCellRenderer();
85
 
86
            @Override
87
            public void paintCell(final Graphics g, final Object value, final int index, final boolean isSelected, final int posY) {
88
                g.translate(0, posY);
89
                this.renderer.paint(g, TicketPanel.this.list, value, index, isSelected);
90
                g.translate(0, -posY);
91
            }
92
        };
93
        this.list.setOpaque(false);
94
        this.list.setSize(276, 450);
95
        this.list.setFixedCellHeight(40);
96
        this.list.setLocation(68, 18);
97
        this.add(this.list);
98
 
99
        this.lTotal.setSize(276 - 10, 32);
100
        this.lTotal.setLocation(68, 500 - 32);
101
        this.lTotal.setFont(new Font("Arial", Font.BOLD, 18));
102
        this.add(this.lTotal);
103
        this.lNumero.setSize(276 - 10, 32);
104
        this.lNumero.setLocation(68, 500);
105
        this.lNumero.setForeground(Color.DARK_GRAY);
106
        this.lNumero.setFont(new Font("Arial", Font.BOLD, 12));
107
        this.add(this.lNumero);
108
 
109
        this.list.addListSelectionListener(new ListSelectionListener() {
110
 
111
            @Override
112
            public void valueChanged(final ListSelectionEvent e) {
113
                if (!e.getValueIsAdjusting()) {
114
                    final Object selectedValue = TicketPanel.this.list.getSelectedValue();
115
                    if (selectedValue != null) {
116
                        final Article a = ((Pair<Article, Integer>) selectedValue).getFirst();
117
                        controler.setArticleSelected(a);
118
                    }
119
                }
120
 
121
            }
122
        });
123
 
124
    }
125
 
126
    @Override
127
    protected void paintComponent(final Graphics g) {
128
        g.drawImage(this.bg, 0, 0, null);
83 ilm 129
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
18 ilm 130
        super.paintComponent(g);
131
    }
132
 
133
    @Override
134
    public Dimension getMinimumSize() {
67 ilm 135
        return new Dimension(480, 550);
18 ilm 136
    }
137
 
138
    @Override
139
    public Dimension getPreferredSize() {
140
        return new Dimension(480, 707);
141
    }
142
 
143
    public void fire() {
144
        for (final ListDataListener l : this.listeners) {
145
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
146
        }
147
 
148
    }
149
 
150
    @Override
151
    public void caisseStateChanged() {
152
        final Article articleSelected = this.controler.getArticleSelected();
153
        for (final ListDataListener l : this.listeners) {
154
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
155
        }
156
        this.lTotal.setText("TOTAL:  " + TicketCellRenderer.centsToString(this.controler.getTotal()) + " €");
157
        this.lNumero.setText("Ticket " + this.controler.getTicketNumber());
158
        // Rien à selectionner
159
        if (articleSelected == null) {
160
            this.list.clearSelection();
161
            return;
162
        }
163
        try {
164
            // Deja selectionné
165
            if (this.list.getSelectedValue() != null && articleSelected != null && articleSelected.equals(((Pair<Article, Integer>) this.list.getSelectedValue()).getFirst())) {
166
                return;
167
            }
168
        } catch (final Exception e) {
169
            e.printStackTrace();
170
        }
171
 
172
        if (articleSelected != null) {
173
 
174
            for (int i = 0; i < this.dataModel.getSize(); i++) {
175
                final Pair<Article, Integer> item = (Pair<Article, Integer>) this.dataModel.getElementAt(i);
176
                if (item.getFirst().equals(articleSelected)) {
177
                    this.list.setSelectedValue(item, true);
178
                    break;
179
                }
180
            }
181
        }
182
 
183
    }
184
}