OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | 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
 
149 ilm 16
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
18 ilm 17
import org.openconcerto.erp.core.sales.pos.model.Article;
149 ilm 18
import org.openconcerto.erp.core.sales.pos.model.RegisterState;
19
import org.openconcerto.erp.core.sales.pos.model.RegisterState.Status;
174 ilm 20
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
18 ilm 21
import org.openconcerto.ui.touch.ScrollableList;
22
 
23
import java.awt.Color;
24
import java.awt.Dimension;
25
import java.awt.Font;
26
import java.awt.Graphics;
83 ilm 27
import java.awt.Graphics2D;
18 ilm 28
import java.awt.Image;
83 ilm 29
import java.awt.RenderingHints;
149 ilm 30
import java.text.DateFormat;
31
import java.text.SimpleDateFormat;
18 ilm 32
import java.util.ArrayList;
33
import java.util.List;
149 ilm 34
import java.util.logging.Level;
18 ilm 35
 
36
import javax.swing.ImageIcon;
37
import javax.swing.JLabel;
38
import javax.swing.JPanel;
39
import javax.swing.ListModel;
40
import javax.swing.SwingConstants;
41
import javax.swing.event.ListDataEvent;
42
import javax.swing.event.ListDataListener;
43
import javax.swing.event.ListSelectionEvent;
44
import javax.swing.event.ListSelectionListener;
45
 
46
public class TicketPanel extends JPanel implements CaisseListener {
149 ilm 47
 
174 ilm 48
    private final DateFormat df = new SimpleDateFormat("' le' d MMMM à H:mm");
18 ilm 49
    private final Image bg;
174 ilm 50
    private final ListModel<TicketItem> dataModel;
51
    private final List<ListDataListener> listeners = new ArrayList<>();
52
    private final JLabel lTotal = new JLabel("", SwingConstants.RIGHT);
53
    private final JLabel lNumero = new JLabel("", SwingConstants.LEFT);
18 ilm 54
    private final CaisseControler controler;
55
    private final ScrollableList list;
174 ilm 56
    private int xOffset = 0;
18 ilm 57
 
58
    TicketPanel(final CaisseControler controler) {
59
        this.controler = controler;
60
        this.controler.addCaisseListener(this);
174 ilm 61
        if (this.controler.getPOSConf().getScreenWidth() < 1280) {
62
            this.xOffset = -24;
63
        }
18 ilm 64
        this.setOpaque(false);
65
        this.bg = new ImageIcon(TicketPanel.class.getResource("ticket.png")).getImage();
66
        this.setLayout(null);
67
 
174 ilm 68
        this.dataModel = new ListModel<TicketItem>() {
18 ilm 69
 
70
            @Override
71
            public void addListDataListener(final ListDataListener l) {
72
                TicketPanel.this.listeners.add(l);
73
            }
74
 
75
            @Override
174 ilm 76
            public TicketItem getElementAt(final int index) {
18 ilm 77
                return controler.getItems().get(index);
78
            }
79
 
80
            @Override
81
            public int getSize() {
82
                return controler.getItems().size();
83
            }
84
 
85
            @Override
86
            public void removeListDataListener(final ListDataListener l) {
87
                TicketPanel.this.listeners.remove(l);
88
            }
89
 
90
        };
91
 
92
        this.list = new ScrollableList(this.dataModel) {
93
            private final TicketCellRenderer renderer = new TicketCellRenderer();
94
 
95
            @Override
96
            public void paintCell(final Graphics g, final Object value, final int index, final boolean isSelected, final int posY) {
97
                g.translate(0, posY);
174 ilm 98
                this.renderer.paint(g, TicketPanel.this.list, (TicketItem) value, index, isSelected);
18 ilm 99
                g.translate(0, -posY);
100
            }
101
        };
102
        this.list.setOpaque(false);
174 ilm 103
        this.list.setSize(315, 450);
18 ilm 104
        this.list.setFixedCellHeight(40);
174 ilm 105
        this.list.setLocation(30 + this.xOffset, 18);
18 ilm 106
        this.add(this.list);
107
 
108
        this.lTotal.setSize(276 - 10, 32);
174 ilm 109
        this.lTotal.setLocation(68 + this.xOffset, 500 - 32);
18 ilm 110
        this.lTotal.setFont(new Font("Arial", Font.BOLD, 18));
111
        this.add(this.lTotal);
112
        this.lNumero.setSize(276 - 10, 32);
174 ilm 113
        this.lNumero.setLocation(68 + this.xOffset, 500);
18 ilm 114
        this.lNumero.setForeground(Color.DARK_GRAY);
115
        this.lNumero.setFont(new Font("Arial", Font.BOLD, 12));
116
        this.add(this.lNumero);
117
 
118
        this.list.addListSelectionListener(new ListSelectionListener() {
119
 
120
            @Override
121
            public void valueChanged(final ListSelectionEvent e) {
122
                if (!e.getValueIsAdjusting()) {
123
                    final Object selectedValue = TicketPanel.this.list.getSelectedValue();
124
                    if (selectedValue != null) {
174 ilm 125
                        TicketItem item = (TicketItem) selectedValue;
126
                        controler.setTicketItemSelected(item);
156 ilm 127
                        // If the category of the selected article does not match the current
128
                        // category of the categories list,
151 ilm 129
                        // then the corresponding article is not selected.
174 ilm 130
                        controler.setTicketItemSelected(item); // Dirty fix : use two refresh
131
 
18 ilm 132
                    }
133
                }
134
 
135
            }
136
        });
137
 
138
    }
139
 
140
    @Override
141
    protected void paintComponent(final Graphics g) {
174 ilm 142
        g.drawImage(this.bg, this.xOffset, 0, null);
83 ilm 143
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
149 ilm 144
        try {
174 ilm 145
            final RegisterState registerState = this.controler.getCaisseFrame().getFiles().getLastLog().getRegisterState();
146
 
156 ilm 147
            // Display caisse and Vendor ID
174 ilm 148
            final String infoCaisseVendeur = "Caisse " + this.controler.getPOSConf().getPosID() + "  Vendeur " + this.controler.getPOSConf().getUserID();
149
            final String infoCaisse = (registerState.getStatus() == Status.OPEN ? "Ouverte" : "Fermée") + registerState.formatDate(this.df);
151 ilm 150
            g.setColor(new Color(230, 230, 230));
174 ilm 151
            if (this.controler.getPOSConf().getScreenWidth() < 1280) {
152
                g.setFont(getFont().deriveFont(16.0f));
153
            } else {
154
                g.setFont(getFont().deriveFont(14.0f));
155
            }
156
            int w1 = g.getFontMetrics().stringWidth(infoCaisseVendeur);
157
            int w2 = g.getFontMetrics().stringWidth(infoCaisse);
151 ilm 158
 
174 ilm 159
            int x = 330 - Math.max(w1, w2) + this.xOffset;
160
            g.drawString(infoCaisseVendeur, x, this.getHeight() - 40);
149 ilm 161
            g.setColor(Color.LIGHT_GRAY);
174 ilm 162
 
163
            g.drawString(infoCaisse, x, this.getHeight() - 25);
149 ilm 164
        } catch (Exception e) {
165
            POSConfiguration.getLogger().log(Level.WARNING, "Couldn't find register state", e);
166
        }
18 ilm 167
        super.paintComponent(g);
168
    }
169
 
174 ilm 170
    int getMinWidth() {
171
        if (this.controler.getPOSConf().getScreenWidth() < 1280) {
172
            return 330;
173
        }
174
        return 480;
175
    }
176
 
177
    int getMaxHeight() {
178
        if (this.controler.getPOSConf().getScreenHeight() < 1000) {
179
            return 550;
180
        }
181
        return 707;
182
    }
183
 
18 ilm 184
    @Override
185
    public Dimension getMinimumSize() {
174 ilm 186
        return new Dimension(getMinWidth(), 550);
18 ilm 187
    }
188
 
189
    @Override
190
    public Dimension getPreferredSize() {
174 ilm 191
        return new Dimension(getMinWidth(), getMaxHeight());
18 ilm 192
    }
193
 
194
    public void fire() {
195
        for (final ListDataListener l : this.listeners) {
196
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
197
        }
198
 
199
    }
200
 
201
    @Override
202
    public void caisseStateChanged() {
203
        final Article articleSelected = this.controler.getArticleSelected();
204
        for (final ListDataListener l : this.listeners) {
205
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
206
        }
207
        this.lTotal.setText("TOTAL:  " + TicketCellRenderer.centsToString(this.controler.getTotal()) + " €");
208
        this.lNumero.setText("Ticket " + this.controler.getTicketNumber());
209
        // Rien à selectionner
210
        if (articleSelected == null) {
211
            this.list.clearSelection();
212
            return;
213
        }
214
        try {
215
            // Deja selectionné
174 ilm 216
            if (this.list.getSelectedValue() != null && articleSelected.equals(((TicketItem) this.list.getSelectedValue()).getArticle())) {
18 ilm 217
                return;
218
            }
219
        } catch (final Exception e) {
220
            e.printStackTrace();
221
        }
222
 
174 ilm 223
        for (int i = 0; i < this.dataModel.getSize(); i++) {
224
            final TicketItem item = this.dataModel.getElementAt(i);
225
            if (item.getArticle().equals(articleSelected)) {
226
                this.list.setSelectedValue(item, true);
227
                break;
18 ilm 228
            }
229
        }
230
 
231
    }
174 ilm 232
 
18 ilm 233
}