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
 
144 ilm 16
import org.openconcerto.erp.core.sales.pos.TicketPrinterConfiguration;
149 ilm 17
import org.openconcerto.erp.core.sales.pos.io.Printable;
144 ilm 18
import org.openconcerto.erp.core.sales.pos.io.TicketPrinter;
149 ilm 19
import org.openconcerto.erp.core.sales.pos.model.RegisterFiles;
20
import org.openconcerto.erp.core.sales.pos.model.RegisterLog;
144 ilm 21
import org.openconcerto.erp.core.sales.pos.model.Ticket;
22
import org.openconcerto.ui.DefaultListModel;
23
import org.openconcerto.ui.touch.ScrollableList;
24
import org.openconcerto.utils.ExceptionHandler;
25
 
18 ilm 26
import java.awt.Color;
27
import java.awt.Component;
28
import java.awt.Dimension;
142 ilm 29
import java.awt.FlowLayout;
18 ilm 30
import java.awt.Font;
31
import java.awt.Graphics;
32
import java.awt.Graphics2D;
33
import java.awt.GridBagConstraints;
34
import java.awt.GridBagLayout;
35
import java.awt.Insets;
36
import java.awt.RenderingHints;
142 ilm 37
import java.awt.event.ActionEvent;
38
import java.awt.event.ActionListener;
149 ilm 39
import java.io.IOException;
40
import java.text.ParseException;
41
import java.util.Calendar;
42
import java.util.Date;
43
import java.util.List;
18 ilm 44
 
45
import javax.swing.JLabel;
46
import javax.swing.JList;
47
import javax.swing.JPanel;
48
import javax.swing.JScrollPane;
49
import javax.swing.ListCellRenderer;
50
import javax.swing.ListSelectionModel;
51
import javax.swing.event.ListSelectionEvent;
52
import javax.swing.event.ListSelectionListener;
53
 
149 ilm 54
import org.jdom2.JDOMException;
55
 
18 ilm 56
public class ListeDesTicketsPanel extends JPanel implements ListSelectionListener {
57
 
144 ilm 58
    private static final String ARIAL_FONT = "Arial";
149 ilm 59
 
60
    static private final class X extends RegisterSummary {
61
 
62
        private final List<Ticket> receipts;
63
 
64
        protected X(final RegisterLog log, final List<Ticket> receipts) {
65
            super("X", log);
66
            this.receipts = receipts;
67
        }
68
 
69
        @Override
70
        protected final List<Ticket> getReceipts() {
71
            return this.receipts;
72
        }
73
 
74
        @Override
75
        public String getLabel() {
76
            return "Synthèse actuelle (X)";
77
        }
78
    }
79
 
80
    static private final class XLite extends RegisterSummary {
81
 
82
        private final List<Ticket> receipts;
83
 
84
        protected XLite(final RegisterLog log, final List<Ticket> receipts) {
85
            super("X", log);
86
            this.receipts = receipts;
87
        }
88
 
89
        @Override
90
        public boolean isPrintingTicketsList() {
91
            return false;
92
        }
93
 
94
        @Override
95
        protected final List<Ticket> getReceipts() {
96
            return this.receipts;
97
        }
98
 
99
        @Override
100
        public String getLabel() {
101
            return "Synthèse actuelle simplifiée (résumé du X)";
102
        }
103
    }
104
 
105
    static private final class Z extends RegisterSummary {
106
 
107
        protected Z(final RegisterFiles files, final Calendar day) throws IOException, JDOMException {
108
            super("Z", new RegisterLog(files.getLogFile(day)).parse());
109
        }
110
 
111
        @Override
174 ilm 112
        protected final List<Ticket> getReceipts() throws ParseException, IOException {
149 ilm 113
            return this.getLog().parseReceipts();
114
        }
115
 
116
        @Override
117
        public String getLabel() {
118
            return "Synthèse précédente (Z)";
119
        }
120
    }
121
 
18 ilm 122
    private JList l;
123
    private CaisseFrame frame;
142 ilm 124
 
18 ilm 125
    private TextAreaTicketPrinter ticketP;
126
    private ScrollableList ticketList;
127
    private DefaultListModel ticketLlistModel;
128
 
129
    ListeDesTicketsPanel(CaisseFrame caisseFrame) {
130
        this.frame = caisseFrame;
131
        this.setBackground(Color.WHITE);
132
        this.setOpaque(true);
133
        this.setLayout(new GridBagLayout());
134
        GridBagConstraints c = new GridBagConstraints();
135
        c.fill = GridBagConstraints.BOTH;
136
        c.anchor = GridBagConstraints.CENTER;
137
        c.weightx = 1;
138
        c.weighty = 0;
139
        c.gridx = 0;
140
        c.gridy = 0;
141
        c.gridwidth = 2;
142
 
143
        // Place pour le menu
142 ilm 144
        StatusBar p = new StatusBar();
145
        p.setTitle("Liste des tickets");
146
        p.setLayout(new FlowLayout(FlowLayout.RIGHT));
147
        POSButton bBack = new POSButton("Fermer");
148
        p.add(bBack);
149
 
18 ilm 150
        this.add(p, c);
151
 
152
        // Liste des tickets
153
        c.gridy++;
154
        c.gridwidth = 1;
155
        c.weighty = 1;
156
        c.gridheight = 2;
157
 
144 ilm 158
        try {
149 ilm 159
            final RegisterLog lastLog = this.frame.getFiles().getLastLog();
160
            final List<Ticket> receipts = lastLog.parseReceipts();
174 ilm 161
            this.ticketLlistModel = new DefaultListModel();
162
            this.ticketLlistModel.addElement(new X(lastLog, receipts));
163
            this.ticketLlistModel.addElement(new XLite(lastLog, receipts));
149 ilm 164
            final Date previousDate = lastLog.getFirstRegisterEvent().getPreviousDate();
165
            if (previousDate != null) {
166
                final Calendar cal = Calendar.getInstance();
167
                cal.setTime(previousDate);
174 ilm 168
                this.ticketLlistModel.addElement(new Z(this.frame.getFiles(), cal));
149 ilm 169
            }
174 ilm 170
            this.ticketLlistModel.addAll(receipts);
149 ilm 171
 
144 ilm 172
        } catch (Exception exn) {
173
            ExceptionHandler.handle(this.frame, "Impossible de charger les tickets", exn);
174
        }
175
        final Font f = new Font(ARIAL_FONT, Font.PLAIN, 24);
174 ilm 176
        this.ticketList = new ScrollableList(this.ticketLlistModel) {
18 ilm 177
            @Override
178
            public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
179
                g.setFont(f);
180
 
181
                if (isSelected) {
182
                    g.setColor(new Color(232, 242, 254));
183
                } else {
184
                    g.setColor(Color.WHITE);
185
                }
186
                g.fillRect(0, posY, getWidth(), getCellHeight());
187
 
188
                //
189
                g.setColor(Color.GRAY);
190
                g.drawLine(0, posY + this.getCellHeight() - 1, this.getWidth(), posY + this.getCellHeight() - 1);
191
 
192
                if (isSelected) {
193
                    g.setColor(Color.BLACK);
194
                } else {
195
                    g.setColor(Color.GRAY);
196
                }
197
                ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
149 ilm 198
                if (object instanceof Ticket) {
199
                    Ticket article = (Ticket) object;
200
                    String label = "Ticket " + article.getCode();
18 ilm 201
 
149 ilm 202
                    String euro = TicketCellRenderer.centsToString(article.getTotalInCents()) + "€";
18 ilm 203
 
149 ilm 204
                    int wEuro = (int) g.getFontMetrics().getStringBounds(euro, g).getWidth();
205
                    g.drawString(label, 10, posY + 39);
206
                    g.drawString(euro, getWidth() - 5 - wEuro, posY + 39);
207
                } else {
208
                    g.drawString(((RegisterSummary) object).getLabel(), 10, posY + 39);
209
                }
18 ilm 210
            }
211
        };
174 ilm 212
        this.add(this.ticketList, c);
18 ilm 213
        // Ticket
214
        c.fill = GridBagConstraints.VERTICAL;
174 ilm 215
        c.weightx = 0;
18 ilm 216
        c.gridx++;
217
        c.gridheight = 1;
218
        c.insets = new Insets(10, 10, 10, 10);
174 ilm 219
        this.ticketP = new TextAreaTicketPrinter();
18 ilm 220
 
174 ilm 221
        JScrollPane scrollPane = new JScrollPane(this.ticketP);
18 ilm 222
        scrollPane.setPreferredSize(new Dimension(400, 200));
223
        scrollPane.setMinimumSize(new Dimension(400, 200));
224
        this.add(scrollPane, c);
225
 
174 ilm 226
        this.ticketList.addListSelectionListener(new ListSelectionListener() {
18 ilm 227
 
228
            @Override
229
            public void valueChanged(ListSelectionEvent e) {
174 ilm 230
                Object selectedValue = ListeDesTicketsPanel.this.ticketList.getSelectedValue();
18 ilm 231
                setSelectedTicket(selectedValue);
232
            }
233
        });
234
 
235
        // Menu
236
 
237
        c.gridy++;
238
        c.weighty = 0;
239
        c.fill = GridBagConstraints.NONE;
144 ilm 240
        final Font font = new Font(ARIAL_FONT, Font.PLAIN, 46);
174 ilm 241
        this.l = new JList(new String[] { "Imprimer", "Annuler le ticket" });
242
        this.l.setCellRenderer(new ListCellRenderer() {
18 ilm 243
 
244
            @Override
245
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
246
                JLabel l = new JLabel(value.toString()) {
247
                    @Override
248
                    public void paint(Graphics g) {
249
 
250
                        super.paint(g);
251
 
252
                        g.setColor(Color.LIGHT_GRAY);
253
                        g.drawLine(0, 0, this.getWidth(), 0);
254
                    }
255
                };
256
                l.setFont(font);
257
                return l;
258
            }
259
 
260
        });
174 ilm 261
        this.l.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
262
        this.l.getSelectionModel().addListSelectionListener(this);
18 ilm 263
 
174 ilm 264
        this.l.setFixedCellHeight(100);
265
        this.add(this.l, c);
142 ilm 266
 
144 ilm 267
        setFont(new Font(ARIAL_FONT, Font.BOLD, 24));
142 ilm 268
 
269
        // Listeners
270
        bBack.addActionListener(new ActionListener() {
271
 
272
            @Override
273
            public void actionPerformed(ActionEvent e) {
174 ilm 274
                ListeDesTicketsPanel.this.frame.showCaisse();
142 ilm 275
 
276
            }
277
        });
278
 
18 ilm 279
    }
280
 
281
    @Override
282
    public void valueChanged(ListSelectionEvent e) {
283
        if (e.getValueIsAdjusting()) {
284
            return;
285
        }
174 ilm 286
        Object selectedValue = this.ticketList.getSelectedValue();
287
        int selectedIndex = this.l.getSelectedIndex();
144 ilm 288
        if (selectedIndex == 0 && selectedValue != null) {
156 ilm 289
            this.frame.getPOSConf().printOnceOnFirstPrinter(((Printable) selectedValue));
174 ilm 290
        } else if (selectedIndex == 1 && selectedValue != null && selectedValue instanceof Ticket) {
151 ilm 291
            Ticket t = (Ticket) selectedValue;
174 ilm 292
            this.frame.getControler().cancel(t);
293
 
18 ilm 294
        }
174 ilm 295
        this.l.clearSelection();
18 ilm 296
    }
297
 
298
    public void setSelectedTicket(Object selectedValue) {
174 ilm 299
        this.ticketP.clear();
18 ilm 300
        if (selectedValue != null) {
156 ilm 301
            this.frame.getPOSConf().print(((Printable) selectedValue), new TicketPrinterConfiguration() {
142 ilm 302
                @Override
303
                public TicketPrinter createTicketPrinter() {
174 ilm 304
                    return ListeDesTicketsPanel.this.ticketP;
142 ilm 305
                }
306
 
307
                @Override
308
                public int getCopyCount() {
309
                    return 1;
310
                }
311
 
312
                @Override
313
                public boolean isValid() {
314
                    return true;
315
                }
316
            });
18 ilm 317
            try {
174 ilm 318
                this.ticketP.printBuffer();
18 ilm 319
            } catch (Exception e1) {
320
                e1.printStackTrace();
321
            }
322
        }
174 ilm 323
        this.ticketList.setSelectedValue(selectedValue, true);
18 ilm 324
    }
325
}