OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Rev 182 | 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.erp.core.sales.pos.model.Categorie;
18
import org.openconcerto.ui.touch.ScrollableList;
19
 
20
import java.awt.Color;
142 ilm 21
import java.awt.FlowLayout;
18 ilm 22
import java.awt.Font;
23
import java.awt.Graphics;
24
import java.awt.Graphics2D;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.RenderingHints;
142 ilm 28
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionListener;
18 ilm 30
import java.awt.event.MouseAdapter;
31
import java.awt.event.MouseEvent;
83 ilm 32
import java.math.BigDecimal;
67 ilm 33
import java.math.RoundingMode;
18 ilm 34
 
35
import javax.swing.JPanel;
36
import javax.swing.event.ListSelectionEvent;
37
import javax.swing.event.ListSelectionListener;
38
 
174 ilm 39
public class ArticleSelector extends JPanel implements CaisseListener {
18 ilm 40
    private ArticleModel model;
41
    private ScrollableList list;
42
    private StatusBar comp;
43
    private CaisseControler controller;
44
 
45
    ArticleSelector(final CaisseControler controller) {
46
        this.controller = controller;
47
        this.controller.addCaisseListener(this);
48
 
49
        this.setLayout(new GridBagLayout());
50
        GridBagConstraints c = new GridBagConstraints();
51
        c.gridx = 0;
52
        c.gridy = 0;
53
        c.weightx = 1;
54
        c.fill = GridBagConstraints.BOTH;
174 ilm 55
        this.comp = new StatusBar();
56
        this.comp.setLayout(new FlowLayout(FlowLayout.LEFT));
57
        this.comp.setTitle("Articles");
142 ilm 58
        final POSButton bSwitch = new POSButton("-");
59
        bSwitch.setForeground(Color.WHITE);
60
        bSwitch.setBackground(CaissePanel.DARK_BLUE);
174 ilm 61
        this.comp.add(bSwitch);
62
        this.add(this.comp, c);
18 ilm 63
 
64
        c.weighty = 1;
65
        c.gridy++;
174 ilm 66
        this.model = new ArticleModel();
67
        this.model.setCategorie(null);
18 ilm 68
 
174 ilm 69
        final Font f;
70
        if (controller.getPOSConf().getScreenWidth() < 1280) {
71
            f = new Font("Arial", Font.PLAIN, 18);
72
        } else {
73
            f = new Font("Arial", Font.PLAIN, 21);
74
        }
75
 
76
        this.list = new ScrollableList(this.model) {
83 ilm 77
            int maxStringWidth = 0;
78
 
18 ilm 79
            @Override
80
            public void paint(Graphics g) {
83 ilm 81
 
174 ilm 82
                if (this.maxStringWidth == 0) {
83 ilm 83
                    g.setFont(f);
84
                    int w = this.getWidth();
85
                    int priceWidth = (int) g.getFontMetrics(f).getStringBounds(getPrice(BigDecimal.valueOf(999)), g).getWidth();
86
                    int maxW = w - priceWidth - getLeftMargin();
87
                    String str = "a";
88
                    int strW;
89
                    do {
90
                        strW = (int) g.getFontMetrics(f).getStringBounds(str, g).getWidth();
91
                        str += "a";
92
                    } while (strW < maxW);
93
 
174 ilm 94
                    this.maxStringWidth = Math.max(1, str.length() - 1);
83 ilm 95
 
96
                }
18 ilm 97
                super.paint(g);
98
                g.setColor(Color.GRAY);
99
                g.drawLine(0, 0, 0, this.getHeight());
100
            }
101
 
102
            @Override
103
            public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
104
                Article article = (Article) object;
174 ilm 105
                paintArticle(f, g, article, isSelected, posY, this.getWidth(), this.getCellHeight(), this.maxStringWidth, getLeftMargin());
18 ilm 106
 
107
            }
108
        };
109
 
174 ilm 110
        this.list.setFixedCellHeight(64);
111
        this.list.setOpaque(true);
112
        this.add(this.list, c);
113
        this.list.addListSelectionListener(new ListSelectionListener() {
18 ilm 114
 
115
            @Override
174 ilm 116
            public void valueChanged(ListSelectionEvent e) {
117
                Object sel = ArticleSelector.this.list.getSelectedValue();
118
                if (sel != null && !e.getValueIsAdjusting()) {
119
                    Article article = (Article) sel;
120
                    controller.setArticleSelected(article);
121
                }
122
 
123
            }
124
        });
125
 
126
        this.list.addMouseListener(new MouseAdapter() {
127
            @Override
18 ilm 128
            public void mouseClicked(MouseEvent e) {
174 ilm 129
 
130
                Object sel = ArticleSelector.this.list.getSelectedValue();
131
                if (sel != null) {
132
                    int nb = e.getClickCount();
133
                    if (nb == 1) {
18 ilm 134
                        Article article = (Article) sel;
174 ilm 135
                        controller.addArticle(article);
136
                    } else if (nb > 1) {
137
                        Article article = (Article) sel;
18 ilm 138
                        controller.incrementArticle(article);
139
                        controller.setArticleSelected(article);
140
                    }
141
                }
142
            }
143
        });
142 ilm 144
 
145
        bSwitch.addActionListener(new ActionListener() {
146
 
83 ilm 147
            @Override
142 ilm 148
            public void actionPerformed(ActionEvent e) {
83 ilm 149
                controller.switchListMode();
142 ilm 150
 
83 ilm 151
            }
152
        });
174 ilm 153
        this.comp.addMouseListener(new MouseAdapter() {
149 ilm 154
            @Override
155
            public void mousePressed(MouseEvent e) {
174 ilm 156
                ArticleSelector.this.list.scrollToOffset(0);
149 ilm 157
            }
158
        });
18 ilm 159
    }
160
 
161
    public ArticleModel getModel() {
162
        return this.model;
163
    }
164
 
165
    @Override
166
    public void caisseStateChanged() {
167
 
174 ilm 168
        final Article articleSelected = this.controller.getArticleSelected();
18 ilm 169
        if (articleSelected == null) {
174 ilm 170
            this.list.clearSelection();
18 ilm 171
            return;
172
        }
173
 
174
        Object selectedValue = null;
175
        try {
174 ilm 176
            selectedValue = this.list.getSelectedValue();
18 ilm 177
        } catch (Exception e) {
178
            e.printStackTrace();
179
        }
149 ilm 180
        if (!articleSelected.equals(selectedValue)) {
18 ilm 181
            Categorie c = articleSelected.getCategorie();
174 ilm 182
            this.model.setCategorie(c);
183
            this.list.setSelectedValue(articleSelected, true);
18 ilm 184
        }
185
 
186
    }
187
 
83 ilm 188
    public static void paintArticle(final Font f, Graphics g, Article article, boolean isSelected, int posY, int cellWidth, int cellHeight, int maxWidth, int leftMargin) {
189
 
190
        g.setFont(f);
191
 
192
        if (isSelected) {
193
            g.setColor(new Color(232, 242, 254));
194
        } else {
195
            g.setColor(Color.WHITE);
196
        }
197
        g.fillRect(0, posY, cellWidth, cellHeight);
198
 
199
        //
200
        g.setColor(Color.GRAY);
201
        g.drawLine(0, posY + cellHeight - 1, cellWidth, posY + cellHeight - 1);
202
 
203
        if (isSelected) {
204
            g.setColor(Color.BLACK);
205
        } else {
206
            g.setColor(Color.GRAY);
207
        }
208
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
209
 
210
        String label = article.getName();
211
 
212
        if (label.length() > maxWidth * 2) {
213
            label = label.substring(0, maxWidth * 2) + "...";
214
        }
215
        String label2 = null;
216
        if (label.length() > maxWidth) {
217
            String t = label.substring(0, maxWidth).trim();
218
            int lastSpace = t.lastIndexOf(' ');
219
            if (lastSpace <= 0) {
220
                lastSpace = maxWidth;
221
            }
222
            label2 = label.substring(lastSpace).trim();
223
            label = label.substring(0, lastSpace).trim();
224
            if (label2.length() > maxWidth) {
225
                label2 = label2.substring(0, maxWidth) + "...";
226
            }
227
        }
228
 
132 ilm 229
        final BigDecimal priceInCents = article.getPriceWithTax();
83 ilm 230
        String euro = getPrice(priceInCents);
231
 
232
        int wEuro = (int) g.getFontMetrics().getStringBounds(euro, g).getWidth();
233
        if (label2 == null) {
234
            g.drawString(label, leftMargin, posY + 39);
235
        } else {
236
            g.drawString(label, leftMargin, posY + 26);
237
            g.drawString(label2, leftMargin, posY + 52);
238
        }
174 ilm 239
 
240
        if (isSelected) {
241
            g.setColor(new Color(232, 242, 254));
242
        } else {
243
            g.setColor(Color.WHITE);
244
        }
245
        g.fillRect(cellWidth - wEuro - 12, posY + 1, wEuro + 12, cellHeight - 2);
246
 
247
        // Price
248
        if (isSelected) {
249
            g.setColor(Color.BLACK);
250
        } else {
251
            g.setColor(Color.GRAY);
252
        }
253
        if (article.getSalesUnit() != null) {
254
            String unit = "/" + article.getSalesUnit();
255
            int wUnit = (int) g.getFontMetrics().getStringBounds(unit, g).getWidth();
256
            g.drawString(euro, cellWidth - 5 - wEuro, posY + 28);
257
            g.drawString(unit, cellWidth - 5 - wUnit, posY + 50);
258
        } else {
259
            g.drawString(euro, cellWidth - 5 - wEuro, posY + 39);
260
        }
261
 
83 ilm 262
    }
263
 
264
    public int getLeftMargin() {
174 ilm 265
        if (this.controller.getPOSConf().getScreenWidth() < 1280) {
266
            return 3;
267
        }
83 ilm 268
        return 10;
269
    }
270
 
271
    public static String getPrice(final BigDecimal price) {
272
        return TicketCellRenderer.centsToString(price.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue()) + "€";
273
    }
274
 
18 ilm 275
}