OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 174
Line 15... Line 15...
15
 
15
 
16
import org.openconcerto.erp.core.sales.pos.model.Article;
16
import org.openconcerto.erp.core.sales.pos.model.Article;
17
import org.openconcerto.erp.core.sales.pos.model.Categorie;
17
import org.openconcerto.erp.core.sales.pos.model.Categorie;
18
import org.openconcerto.ui.touch.ScrollableList;
18
import org.openconcerto.ui.touch.ScrollableList;
19
 
19
 
-
 
20
import java.awt.BorderLayout;
20
import java.awt.Color;
21
import java.awt.Color;
21
import java.awt.FlowLayout;
22
import java.awt.FlowLayout;
22
import java.awt.Font;
23
import java.awt.Font;
23
import java.awt.Graphics;
24
import java.awt.Graphics;
24
import java.awt.Graphics2D;
25
import java.awt.Graphics2D;
25
import java.awt.GridBagConstraints;
26
import java.awt.GridBagConstraints;
26
import java.awt.GridBagLayout;
27
import java.awt.GridBagLayout;
27
import java.awt.Insets;
-
 
28
import java.awt.RenderingHints;
28
import java.awt.RenderingHints;
29
import java.awt.event.ActionEvent;
29
import java.awt.event.ActionEvent;
30
import java.awt.event.ActionListener;
30
import java.awt.event.ActionListener;
31
import java.awt.event.MouseAdapter;
31
import java.awt.event.MouseAdapter;
32
import java.awt.event.MouseEvent;
32
import java.awt.event.MouseEvent;
Line 37... Line 37...
37
 
37
 
38
import javax.swing.BorderFactory;
38
import javax.swing.BorderFactory;
39
import javax.swing.ImageIcon;
39
import javax.swing.ImageIcon;
40
import javax.swing.JLabel;
40
import javax.swing.JLabel;
41
import javax.swing.JPanel;
41
import javax.swing.JPanel;
42
import javax.swing.JSeparator;
-
 
43
import javax.swing.JTextField;
42
import javax.swing.JTextField;
44
import javax.swing.event.DocumentEvent;
43
import javax.swing.event.DocumentEvent;
45
import javax.swing.event.DocumentListener;
44
import javax.swing.event.DocumentListener;
46
import javax.swing.event.ListSelectionEvent;
45
import javax.swing.event.ListSelectionEvent;
47
import javax.swing.event.ListSelectionListener;
46
import javax.swing.event.ListSelectionListener;
48
 
47
 
49
public class ArticleSearchPanel extends JPanel implements ListSelectionListener, CaisseListener {
48
public class ArticleSearchPanel extends JPanel implements CaisseListener {
50
    private final ScrollableList list;
49
    private final ScrollableList list;
51
    private final CaisseControler controler;
50
    private final CaisseControler controler;
52
    private final JTextField textField = new JTextField();
51
    private final JTextField textField = new JTextField();
53
 
52
 
54
    public ArticleSearchPanel(final CaisseControler controler) {
53
    public ArticleSearchPanel(final CaisseControler controler) {
55
        this.controler = controler;
54
        this.controler = controler;
56
        this.controler.addCaisseListener(this);
55
        this.controler.addCaisseListener(this);
57
        this.setLayout(new GridBagLayout());
56
        this.setLayout(new GridBagLayout());
58
        final GridBagConstraints c = new GridBagConstraints();
57
        final GridBagConstraints c = new GridBagConstraints();
59
        final FilteredListModel model = new FilteredListModel();
58
        final FilteredListModel model = new FilteredListModel();
-
 
59
        final Font f1;
-
 
60
        final Font f2;
-
 
61
 
-
 
62
        if (this.controler.getPOSConf().getScreenWidth() < 1280) {
-
 
63
            f1 = new Font("Arial", Font.PLAIN, 21);
-
 
64
            f2 = new Font("Arial", Font.PLAIN, 14);
-
 
65
        } else {
60
        final Font f1 = new Font("Arial", Font.PLAIN, 24);
66
            f1 = new Font("Arial", Font.PLAIN, 24);
61
        final Font f2 = new Font("Arial", Font.PLAIN, 16);
67
            f2 = new Font("Arial", Font.PLAIN, 16);
-
 
68
        }
-
 
69
 
62
        setBackground(CaissePanel.LIGHT_BLUE);
70
        setBackground(new Color(230, 230, 230));
63
        list = new ScrollableList(model) {
71
        this.list = new ScrollableList(model) {
64
            @Override
72
            @Override
65
            public void paint(Graphics g) {
73
            public void paint(Graphics g) {
66
                super.paint(g);
74
                super.paint(g);
67
                g.setColor(CaissePanel.LIGHT_BLUE);
75
                g.setColor(Color.LIGHT_GRAY);
68
                g.drawLine(0, 0, 0, this.getHeight());
76
                g.drawLine(0, 0, 0, this.getHeight());
69
            }
77
            }
70
 
78
 
71
            @Override
79
            @Override
72
            public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
80
            public void paintCell(Graphics g, Object object, int index, boolean isSelected, int posY) {
Line 86... Line 94...
86
 
94
 
87
            @Override
95
            @Override
88
            public void mouseReleased(MouseEvent event) {
96
            public void mouseReleased(MouseEvent event) {
89
                if ((event.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
97
                if ((event.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK) {
90
                    final Object obj = getSelectedValue();
98
                    final Object obj = getSelectedValue();
91
                    if (obj != null && obj instanceof Article) {
99
                    if (obj instanceof Article) {
92
                        Article product = (Article) obj;
100
                        Article product = (Article) obj;
93
                        Categorie.toggleFavoriteState(product);
101
                        Categorie.toggleFavoriteState(product);
94
                        controler.saveFavoriteProductsIds(Categorie.getFavoriteProducts());
102
                        controler.saveFavoriteProductsIds(Categorie.getFavoriteProducts());
95
                        model.setFilter(textField.getText());
103
                        model.setFilter(ArticleSearchPanel.this.textField.getText());
96
                        list.scrollToOffset(0);
104
                        ArticleSearchPanel.this.list.scrollToOffset(0);
97
                    }
105
                    }
98
                } else {
106
                } else {
99
                    super.mouseReleased(event);
107
                    super.mouseReleased(event);
100
                }
108
                }
101
            }
109
            }
Line 122... Line 130...
122
 
130
 
123
        bar.setTitle("Articles");
131
        bar.setTitle("Articles");
124
        bar.addMouseListener(new MouseAdapter() {
132
        bar.addMouseListener(new MouseAdapter() {
125
            @Override
133
            @Override
126
            public void mousePressed(MouseEvent e) {
134
            public void mousePressed(MouseEvent e) {
127
                list.scrollToOffset(0);
135
                ArticleSearchPanel.this.list.scrollToOffset(0);
128
            }
136
            }
129
        });
137
        });
130
 
138
 
131
        c.fill = GridBagConstraints.BOTH;
139
        c.fill = GridBagConstraints.BOTH;
132
        c.gridwidth = 2;
140
        c.gridwidth = 2;
133
        this.add(bar, c);
141
        this.add(bar, c);
134
 
142
 
135
        // List
143
        // List
136
        c.weighty = 1;
144
        c.weighty = 1;
137
        c.gridy++;
145
        c.gridy++;
-
 
146
        this.list.setBackground(new Color(240, 240, 240));
138
        this.add(list, c);
147
        this.add(this.list, c);
-
 
148
 
139
        // Separator
149
        // Separator
140
        c.weighty = 0;
150
        c.weighty = 0;
-
 
151
 
-
 
152
        JPanel pBottom = new JPanel();
141
        c.gridy++;
153
        pBottom.setOpaque(true);
142
        this.add(new JSeparator(JSeparator.HORIZONTAL), c);
154
        pBottom.setBackground(CaissePanel.DARK_BLUE);
-
 
155
        pBottom.setLayout(new BorderLayout(3, 3));
143
 
156
 
144
        // Icon and text
157
        // Icon and text
145
        c.weighty = 0;
-
 
146
        c.gridy++;
-
 
147
        c.insets = new Insets(3, 3, 3, 3);
-
 
-
 
158
 
148
        final JLabel label = new JLabel(new ImageIcon(this.getClass().getResource("search.png")));
159
        final JLabel label = new JLabel(new ImageIcon(this.getClass().getResource("search.png")));
149
        c.gridwidth = 1;
-
 
150
        c.weightx = 0;
-
 
151
        this.add(label, c);
-
 
152
 
160
 
-
 
161
        pBottom.add(label, BorderLayout.WEST);
-
 
162
        pBottom.setBorder(BorderFactory.createLineBorder(CaissePanel.DARK_BLUE, 3));
153
        textField.setBorder(BorderFactory.createLineBorder(CaissePanel.DARK_BLUE, 1));
163
        this.textField.setBorder(BorderFactory.createLineBorder(CaissePanel.DARK_BLUE, 1));
-
 
164
        this.textField.setFont(f1);
-
 
165
 
154
        textField.setFont(f1);
166
        this.textField.setFont(f1);
-
 
167
        pBottom.add(this.textField, BorderLayout.CENTER);
-
 
168
        c.gridy++;
155
        c.weightx = 1;
169
        c.weightx = 1;
156
        c.gridx++;
170
        c.gridwidth = 2;
-
 
171
        this.add(pBottom, c);
157
 
172
 
158
        textField.setFont(f1);
-
 
159
        this.add(textField, c);
-
 
160
        textField.getDocument().addDocumentListener(new DocumentListener() {
173
        this.textField.getDocument().addDocumentListener(new DocumentListener() {
161
 
174
 
162
            @Override
175
            @Override
163
            public void removeUpdate(DocumentEvent e) {
176
            public void removeUpdate(DocumentEvent e) {
164
                changedUpdate(e);
177
                changedUpdate(e);
165
            }
178
            }
Line 169... Line 182...
169
                changedUpdate(e);
182
                changedUpdate(e);
170
            }
183
            }
171
 
184
 
172
            @Override
185
            @Override
173
            public void changedUpdate(DocumentEvent e) {
186
            public void changedUpdate(DocumentEvent e) {
174
                model.setFilter(textField.getText());
187
                model.setFilter(ArticleSearchPanel.this.textField.getText());
175
                controler.setArticleSelected(null);
188
                controler.setArticleSelected(null);
176
            }
189
            }
177
        });
190
        });
178
        list.addListSelectionListener(this);
191
        this.list.addListSelectionListener(new ListSelectionListener() {
179
 
192
 
180
        list.addMouseListener(new MouseAdapter() {
-
 
181
            @Override
193
            @Override
182
            public void mouseClicked(MouseEvent e) {
194
            public void valueChanged(ListSelectionEvent e) {
183
                int nb = e.getClickCount();
195
                Object sel = ArticleSearchPanel.this.list.getSelectedValue();
184
                if (nb > 1) {
-
 
185
                    Object sel = list.getSelectedValue();
196
                if (sel != null && !e.getValueIsAdjusting()) {
186
                    if (sel != null) {
197
                    if (sel instanceof Article) {
187
                        Article article = (Article) sel;
198
                        Article article = (Article) sel;
188
                        controler.incrementArticle(article);
-
 
189
                        controler.setArticleSelected(article);
199
                        controler.setArticleSelected(article);
-
 
200
 
190
                    }
201
                    }
191
                }
202
                }
-
 
203
 
192
            }
204
            }
193
        });
205
        });
194
    }
-
 
195
 
206
 
-
 
207
        this.list.addMouseListener(new MouseAdapter() {
196
    @Override
208
            @Override
197
    public void valueChanged(ListSelectionEvent e) {
209
            public void mouseClicked(MouseEvent e) {
198
        Object sel = list.getSelectedValue();
210
                Object sel = ArticleSearchPanel.this.list.getSelectedValue();
-
 
211
                if (sel != null) {
199
        if (sel != null && !e.getValueIsAdjusting()) {
212
                    int nb = e.getClickCount();
-
 
213
                    if (nb == 1) {
200
            if (sel instanceof Article) {
214
                        if (sel instanceof Article) {
201
                Article article = (Article) sel;
215
                            Article article = (Article) sel;
202
                controler.setArticleSelected(article);
216
                            controler.setArticleSelected(article);
203
                controler.addArticle(article);
217
                            controler.addArticle(article);
204
            }
218
                        }
-
 
219
                    } else if (nb > 1) {
-
 
220
                        Article article = (Article) sel;
-
 
221
                        controler.incrementArticle(article);
-
 
222
                        controler.setArticleSelected(article);
-
 
223
                    }
205
        }
224
                }
-
 
225
 
-
 
226
            }
-
 
227
        });
-
 
228
 
206
    }
229
    }
207
 
230
 
208
    public void paintCategorie(final Font f, Font f2, Graphics g, Categorie c, boolean isSelected, int posY, int cellWidth, int cellHeight) {
231
    public void paintCategorie(final Font f, Font f2, Graphics g, Categorie c, boolean isSelected, int posY, int cellWidth, int cellHeight) {
209
        g.setFont(f);
232
        g.setFont(f);
210
 
233
 
Line 246... Line 269...
246
 
269
 
247
    private String getCategoriePath(Categorie c) {
270
    private String getCategoriePath(Categorie c) {
248
        if (c.getParent() == null) {
271
        if (c.getParent() == null) {
249
            return null;
272
            return null;
250
        }
273
        }
251
        String storedString = categoryCache.get(c);
274
        String storedString = this.categoryCache.get(c);
252
        if (storedString != null) {
275
        if (storedString != null) {
253
            return storedString;
276
            return storedString;
254
        }
277
        }
255
 
278
 
256
        Categorie parent = c.getParent();
279
        Categorie parent = c.getParent();
Line 271... Line 294...
271
            }
294
            }
272
 
295
 
273
            parent = c.getParent();
296
            parent = c.getParent();
274
 
297
 
275
        }
298
        }
276
        categoryCache.put(c, s);
299
        this.categoryCache.put(c, s);
277
        return s;
300
        return s;
278
    }
301
    }
279
 
302
 
280
    @Override
303
    @Override
281
    public void caisseStateChanged() {
304
    public void caisseStateChanged() {
282
        final Article articleSelected = controler.getArticleSelected();
305
        final Article articleSelected = this.controler.getArticleSelected();
283
        System.err.println("ArticleSearchPanel.caisseStateChanged() article selected : " + articleSelected);
306
        System.err.println("ArticleSearchPanel.caisseStateChanged() article selected : " + articleSelected);
284
        if (articleSelected == null) {
307
        if (articleSelected == null) {
285
            list.clearSelection();
308
            this.list.clearSelection();
286
            return;
309
            return;
287
        }
310
        }
288
        Object selectedValue = null;
311
        Object selectedValue = null;
289
        try {
312
        try {
290
            selectedValue = list.getSelectedValue();
313
            selectedValue = this.list.getSelectedValue();
291
        } catch (Exception e) {
314
        } catch (Exception e) {
292
            e.printStackTrace();
315
            e.printStackTrace();
293
        }
316
        }
294
        if (selectedValue == null || !articleSelected.equals(selectedValue)) {
317
        if (selectedValue == null || !articleSelected.equals(selectedValue)) {
295
            boolean found = list.setSelectedValue(articleSelected, true);
318
            boolean found = this.list.setSelectedValue(articleSelected, true);
296
            if (!found) {
319
            if (!found) {
297
                list.clearSelection();
320
                this.list.clearSelection();
298
            }
321
            }
299
        }
322
        }
300
    }
323
    }
301
}
324
}