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 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 174
Line 34... Line 34...
34
 
34
 
35
import javax.swing.JPanel;
35
import javax.swing.JPanel;
36
import javax.swing.event.ListSelectionEvent;
36
import javax.swing.event.ListSelectionEvent;
37
import javax.swing.event.ListSelectionListener;
37
import javax.swing.event.ListSelectionListener;
38
 
38
 
39
public class ArticleSelector extends JPanel implements ListSelectionListener, CaisseListener {
39
public class ArticleSelector extends JPanel implements CaisseListener {
40
    private ArticleModel model;
40
    private ArticleModel model;
41
    private ScrollableList list;
41
    private ScrollableList list;
42
    private StatusBar comp;
42
    private StatusBar comp;
43
    private CaisseControler controller;
43
    private CaisseControler controller;
44
 
44
 
Line 50... Line 50...
50
        GridBagConstraints c = new GridBagConstraints();
50
        GridBagConstraints c = new GridBagConstraints();
51
        c.gridx = 0;
51
        c.gridx = 0;
52
        c.gridy = 0;
52
        c.gridy = 0;
53
        c.weightx = 1;
53
        c.weightx = 1;
54
        c.fill = GridBagConstraints.BOTH;
54
        c.fill = GridBagConstraints.BOTH;
55
        comp = new StatusBar();
55
        this.comp = new StatusBar();
56
        comp.setLayout(new FlowLayout(FlowLayout.LEFT));
56
        this.comp.setLayout(new FlowLayout(FlowLayout.LEFT));
57
        comp.setTitle("Articles");
57
        this.comp.setTitle("Articles");
58
        final POSButton bSwitch = new POSButton("-");
58
        final POSButton bSwitch = new POSButton("-");
59
        bSwitch.setForeground(Color.WHITE);
59
        bSwitch.setForeground(Color.WHITE);
60
        bSwitch.setBackground(CaissePanel.DARK_BLUE);
60
        bSwitch.setBackground(CaissePanel.DARK_BLUE);
61
        comp.add(bSwitch);
61
        this.comp.add(bSwitch);
62
        this.add(comp, c);
62
        this.add(this.comp, c);
63
 
63
 
64
        c.weighty = 1;
64
        c.weighty = 1;
65
        c.gridy++;
65
        c.gridy++;
66
        model = new ArticleModel();
66
        this.model = new ArticleModel();
67
        model.setCategorie(null);
67
        this.model.setCategorie(null);
68
 
68
 
-
 
69
        final Font f;
-
 
70
        if (controller.getPOSConf().getScreenWidth() < 1280) {
-
 
71
            f = new Font("Arial", Font.PLAIN, 18);
-
 
72
        } else {
69
        final Font f = new Font("Arial", Font.PLAIN, 21);
73
            f = new Font("Arial", Font.PLAIN, 21);
-
 
74
        }
-
 
75
 
70
        list = new ScrollableList(model) {
76
        this.list = new ScrollableList(this.model) {
71
            int maxStringWidth = 0;
77
            int maxStringWidth = 0;
72
 
78
 
73
            @Override
79
            @Override
74
            public void paint(Graphics g) {
80
            public void paint(Graphics g) {
75
 
81
 
76
                if (maxStringWidth == 0) {
82
                if (this.maxStringWidth == 0) {
77
                    g.setFont(f);
83
                    g.setFont(f);
78
                    int w = this.getWidth();
84
                    int w = this.getWidth();
79
                    int priceWidth = (int) g.getFontMetrics(f).getStringBounds(getPrice(BigDecimal.valueOf(999)), g).getWidth();
85
                    int priceWidth = (int) g.getFontMetrics(f).getStringBounds(getPrice(BigDecimal.valueOf(999)), g).getWidth();
80
                    int maxW = w - priceWidth - getLeftMargin();
86
                    int maxW = w - priceWidth - getLeftMargin();
81
                    String str = "a";
87
                    String str = "a";
Line 83... Line 89...
83
                    do {
89
                    do {
84
                        strW = (int) g.getFontMetrics(f).getStringBounds(str, g).getWidth();
90
                        strW = (int) g.getFontMetrics(f).getStringBounds(str, g).getWidth();
85
                        str += "a";
91
                        str += "a";
86
                    } while (strW < maxW);
92
                    } while (strW < maxW);
87
 
93
 
88
                    maxStringWidth = Math.max(1, str.length() - 1);
94
                    this.maxStringWidth = Math.max(1, str.length() - 1);
89
                    System.out.println(w + " " + priceWidth + " " + maxStringWidth);
-
 
90
 
95
 
91
                }
96
                }
92
                super.paint(g);
97
                super.paint(g);
93
                g.setColor(Color.GRAY);
98
                g.setColor(Color.GRAY);
94
                g.drawLine(0, 0, 0, this.getHeight());
99
                g.drawLine(0, 0, 0, this.getHeight());
95
            }
100
            }
96
 
101
 
97
            @Override
102
            @Override
98
            public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
103
            public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
99
                Article article = (Article) object;
104
                Article article = (Article) object;
100
                paintArticle(f, g, article, isSelected, posY, this.getWidth(), this.getCellHeight(), maxStringWidth, getLeftMargin());
105
                paintArticle(f, g, article, isSelected, posY, this.getWidth(), this.getCellHeight(), this.maxStringWidth, getLeftMargin());
101
 
106
 
102
            }
107
            }
103
        };
108
        };
104
 
109
 
105
        list.setFixedCellHeight(64);
110
        this.list.setFixedCellHeight(64);
106
        list.setOpaque(true);
111
        this.list.setOpaque(true);
107
        this.add(list, c);
112
        this.add(this.list, c);
108
        list.addListSelectionListener(this);
113
        this.list.addListSelectionListener(new ListSelectionListener() {
-
 
114
 
-
 
115
            @Override
-
 
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
        });
109
 
125
 
110
        list.addMouseListener(new MouseAdapter() {
126
        this.list.addMouseListener(new MouseAdapter() {
111
            @Override
127
            @Override
112
            public void mouseClicked(MouseEvent e) {
128
            public void mouseClicked(MouseEvent e) {
113
                int nb = e.getClickCount();
-
 
114
                if (nb > 1) {
-
 
-
 
129
 
115
                    Object sel = list.getSelectedValue();
130
                Object sel = ArticleSelector.this.list.getSelectedValue();
116
                    if (sel != null) {
131
                if (sel != null) {
-
 
132
                    int nb = e.getClickCount();
-
 
133
                    if (nb == 1) {
-
 
134
                        Article article = (Article) sel;
-
 
135
                        controller.addArticle(article);
-
 
136
                    } else if (nb > 1) {
117
                        Article article = (Article) sel;
137
                        Article article = (Article) sel;
118
                        controller.incrementArticle(article);
138
                        controller.incrementArticle(article);
119
                        controller.setArticleSelected(article);
139
                        controller.setArticleSelected(article);
120
                    }
140
                    }
121
                }
141
                }
Line 128... Line 148...
128
            public void actionPerformed(ActionEvent e) {
148
            public void actionPerformed(ActionEvent e) {
129
                controller.switchListMode();
149
                controller.switchListMode();
130
 
150
 
131
            }
151
            }
132
        });
152
        });
133
        comp.addMouseListener(new MouseAdapter() {
153
        this.comp.addMouseListener(new MouseAdapter() {
134
            @Override
154
            @Override
135
            public void mousePressed(MouseEvent e) {
155
            public void mousePressed(MouseEvent e) {
136
                list.scrollToOffset(0);
156
                ArticleSelector.this.list.scrollToOffset(0);
137
            }
157
            }
138
        });
158
        });
139
    }
159
    }
140
 
160
 
141
    @Override
-
 
142
    public void valueChanged(ListSelectionEvent e) {
-
 
143
        Object sel = list.getSelectedValue();
-
 
144
        if (sel != null && !e.getValueIsAdjusting()) {
-
 
145
            Article article = (Article) sel;
-
 
146
            controller.setArticleSelected(article);
-
 
147
            controller.addArticle(article);
-
 
148
        }
-
 
149
    }
-
 
150
 
-
 
151
    public ArticleModel getModel() {
161
    public ArticleModel getModel() {
152
        return this.model;
162
        return this.model;
153
    }
163
    }
154
 
164
 
155
    @Override
165
    @Override
156
    public void caisseStateChanged() {
166
    public void caisseStateChanged() {
157
 
167
 
158
        final Article articleSelected = controller.getArticleSelected();
168
        final Article articleSelected = this.controller.getArticleSelected();
159
        if (articleSelected == null) {
169
        if (articleSelected == null) {
160
            list.clearSelection();
170
            this.list.clearSelection();
161
            return;
171
            return;
162
        }
172
        }
163
 
173
 
164
        Object selectedValue = null;
174
        Object selectedValue = null;
165
        try {
175
        try {
166
            selectedValue = list.getSelectedValue();
176
            selectedValue = this.list.getSelectedValue();
167
        } catch (Exception e) {
177
        } catch (Exception e) {
168
            e.printStackTrace();
178
            e.printStackTrace();
169
        }
179
        }
170
        if (!articleSelected.equals(selectedValue)) {
180
        if (!articleSelected.equals(selectedValue)) {
171
            Categorie c = articleSelected.getCategorie();
181
            Categorie c = articleSelected.getCategorie();
172
            model.setCategorie(c);
182
            this.model.setCategorie(c);
173
            list.setSelectedValue(articleSelected, true);
183
            this.list.setSelectedValue(articleSelected, true);
174
        }
184
        }
175
 
185
 
176
    }
186
    }
177
 
187
 
178
    public static void paintArticle(final Font f, Graphics g, Article article, boolean isSelected, int posY, int cellWidth, int cellHeight, int maxWidth, int leftMargin) {
188
    public static void paintArticle(final Font f, Graphics g, Article article, boolean isSelected, int posY, int cellWidth, int cellHeight, int maxWidth, int leftMargin) {
Line 224... Line 234...
224
            g.drawString(label, leftMargin, posY + 39);
234
            g.drawString(label, leftMargin, posY + 39);
225
        } else {
235
        } else {
226
            g.drawString(label, leftMargin, posY + 26);
236
            g.drawString(label, leftMargin, posY + 26);
227
            g.drawString(label2, leftMargin, posY + 52);
237
            g.drawString(label2, leftMargin, posY + 52);
228
        }
238
        }
-
 
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 {
229
        g.drawString(euro, cellWidth - 5 - wEuro, posY + 39);
259
            g.drawString(euro, cellWidth - 5 - wEuro, posY + 39);
230
    }
260
        }
231
 
261
 
-
 
262
    }
-
 
263
 
232
    public int getLeftMargin() {
264
    public int getLeftMargin() {
-
 
265
        if (this.controller.getPOSConf().getScreenWidth() < 1280) {
-
 
266
            return 3;
-
 
267
        }
233
        return 10;
268
        return 10;
234
    }
269
    }
235
 
270
 
236
    public static String getPrice(final BigDecimal price) {
271
    public static String getPrice(final BigDecimal price) {
237
        return TicketCellRenderer.centsToString(price.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue()) + "€";
272
        return TicketCellRenderer.centsToString(price.movePointRight(2).setScale(0, RoundingMode.HALF_UP).intValue()) + "€";