OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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