OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 65 | Rev 132 | 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;
83 ilm 18
import org.openconcerto.ui.touch.ScrollableList;
18 ilm 19
 
83 ilm 20
import java.awt.Color;
21
import java.awt.Font;
22
import java.awt.Graphics;
23
import java.awt.Graphics2D;
18 ilm 24
import java.awt.GridBagConstraints;
25
import java.awt.GridBagLayout;
83 ilm 26
import java.awt.RenderingHints;
18 ilm 27
import java.awt.event.MouseAdapter;
28
import java.awt.event.MouseEvent;
83 ilm 29
import java.math.BigDecimal;
18 ilm 30
 
31
import javax.swing.JList;
32
import javax.swing.JPanel;
33
import javax.swing.ListSelectionModel;
34
import javax.swing.event.ListSelectionEvent;
35
import javax.swing.event.ListSelectionListener;
36
 
37
public class CategorieSelector extends JPanel implements ListSelectionListener, CaisseListener {
38
    private CategorieModel model;
83 ilm 39
    private ScrollableList list;
18 ilm 40
    private StatusBar comp;
41
    private Categorie previous;
42
    private ArticleModel articleModel;
43
    private CaisseControler controller;
44
 
45
    CategorieSelector(CaisseControler controller, final ArticleModel articleModel) {
83 ilm 46
        this.setBackground(Color.WHITE);
18 ilm 47
        this.articleModel = articleModel;
48
        this.controller = controller;
49
 
50
        controller.addCaisseListener(this);
51
 
52
        this.setLayout(new GridBagLayout());
53
        GridBagConstraints c = new GridBagConstraints();
54
        c.gridx = 0;
55
        c.gridy = 0;
56
        c.weightx = 1;
57
        c.fill = GridBagConstraints.BOTH;
58
        this.comp = new StatusBar();
59
        this.comp.setTitle("Catégories");
60
        this.add(this.comp, c);
61
 
62
        c.weighty = 1;
63
        c.gridy++;
64
        this.model = new CategorieModel();
65
        this.model.setRoot(null);
83 ilm 66
        final Font f = new Font("Arial", Font.PLAIN, 36);
67
        final Font smallFont = new Font("Arial", Font.PLAIN, 23);
68
        this.list = new ScrollableList(this.model) {
69
            int maxWidth = 0;
70
 
71
            @Override
72
            public void paint(Graphics g) {
73
                ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
74
                if (maxWidth == 0) {
75
                    g.setFont(f);
76
                    int w = this.getWidth();
77
 
78
                    int maxW = w - 2 * getLeftMargin();
79
                    String str = "a";
80
                    int strW;
81
                    do {
82
                        strW = (int) g.getFontMetrics(f).getStringBounds(str, g).getWidth();
83
                        str += "a";
84
                    } while (strW < maxW);
85
 
86
                    maxWidth = Math.max(1, str.length() - 1);
87
                    System.out.println(w + " " + getLeftMargin() + " " + maxWidth);
88
 
89
                }
90
                super.paint(g);
91
            }
92
 
93
            private int getLeftMargin() {
94
 
95
                return 10;
96
            }
97
 
98
            public void paintCell(Graphics g, Object object, int index, boolean selected, int posY) {
99
 
100
                g.setColor(Color.WHITE);
101
 
102
                g.fillRect(0, posY, this.getWidth(), this.getCellHeight());
103
                g.setColor(Color.GRAY);
104
 
105
                g.drawLine(0, posY + this.getCellHeight() - 1, this.getWidth(), posY + this.getCellHeight() - 1);
106
 
107
                if (selected) {
108
                    g.setColor(Color.BLACK);
109
                } else {
110
                    g.setColor(Color.GRAY);
111
                }
112
                String label = object.toString();
113
 
114
                if (label.length() > maxWidth * 2) {
115
                    label = label.substring(0, maxWidth * 2) + "...";
116
                }
117
                String label2 = null;
118
                if (label.length() > maxWidth) {
119
                    String t = label.substring(0, maxWidth).trim();
120
                    int lastSpace = t.lastIndexOf(' ');
121
                    if (lastSpace <= 0) {
122
                        lastSpace = maxWidth;
123
                    }
124
                    label2 = label.substring(lastSpace).trim();
125
                    label = label.substring(0, lastSpace).trim();
126
                    if (label2.length() > maxWidth) {
127
                        label2 = label2.substring(0, maxWidth) + "...";
128
                    }
129
                }
130
 
131
                if (label2 == null) {
132
                    g.setFont(f);
133
                    g.drawString(label, getLeftMargin(), posY + 44);
134
                } else {
135
                    g.setFont(smallFont);
136
                    g.drawString(label, getLeftMargin(), posY + 26);
137
                    g.drawString(label2, getLeftMargin(), posY + 52);
138
                }
139
 
140
            }
141
 
142
        };
18 ilm 143
        this.list.setFixedCellHeight(64);
83 ilm 144
        this.list.setOpaque(true);
145
        this.list.setBackground(Color.WHITE);
18 ilm 146
        this.add(this.list, c);
83 ilm 147
        this.list.addListSelectionListener(this);
18 ilm 148
 
149
        this.comp.addMouseListener(new MouseAdapter() {
150
 
151
            @Override
152
            public void mousePressed(MouseEvent e) {
65 ilm 153
                // User pressed "Previous" button on category
154
                final Categorie newCategory = CategorieSelector.this.previous;
155
                CategorieSelector.this.model.setRoot(newCategory);
18 ilm 156
                CategorieSelector.this.list.clearSelection();
65 ilm 157
                if (newCategory == null) {
18 ilm 158
                    CategorieSelector.this.comp.setTitle("Catégories");
159
                    CategorieSelector.this.comp.setPrevious(false);
65 ilm 160
                    CategorieSelector.this.previous = null;
18 ilm 161
                } else {
65 ilm 162
                    CategorieSelector.this.comp.setTitle(newCategory.getName());
18 ilm 163
                    CategorieSelector.this.comp.setPrevious(true);
65 ilm 164
                    CategorieSelector.this.previous = newCategory.getParent();
18 ilm 165
                }
65 ilm 166
                articleModel.setCategorie(newCategory);
18 ilm 167
            }
168
 
169
        });
170
    }
171
 
172
    @Override
173
    public void valueChanged(ListSelectionEvent e) {
65 ilm 174
        final Object sel = this.list.getSelectedValue();
18 ilm 175
        if (sel != null && !e.getValueIsAdjusting()) {
65 ilm 176
            final Categorie c = (Categorie) sel;
18 ilm 177
            if (!c.getSubCategories().isEmpty()) {
178
                // Descend la hierarchie
179
                this.previous = this.model.getRoot();
180
                this.model.setRoot(c);
181
                this.comp.setTitle(c.getName());
182
                this.comp.setPrevious(true);
183
                this.list.clearSelection();
184
            }
185
            this.articleModel.setCategorie(c);
186
            this.controller.setArticleSelected(null);
187
        }
188
    }
189
 
190
    @Override
191
    public void caisseStateChanged() {
192
        final Article articleSelected = this.controller.getArticleSelected();
193
        if (articleSelected != null) {
65 ilm 194
            final Categorie c = articleSelected.getCategorie();
18 ilm 195
            if (c.getParent() != null) {
196
                this.previous = c.getParent().getParent();
197
                this.model.setRoot(c.getParent());
198
                this.comp.setTitle(c.getParent().getName());
199
            }
200
            this.comp.setPrevious(true);
201
            this.list.setSelectedValue(c, true);
202
        }
203
 
204
    }
205
}