OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 156 Rev 174
Line 15... Line 15...
15
 
15
 
16
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
16
import org.openconcerto.erp.core.sales.pos.POSConfiguration;
17
import org.openconcerto.erp.core.sales.pos.model.Article;
17
import org.openconcerto.erp.core.sales.pos.model.Article;
18
import org.openconcerto.erp.core.sales.pos.model.RegisterState;
18
import org.openconcerto.erp.core.sales.pos.model.RegisterState;
19
import org.openconcerto.erp.core.sales.pos.model.RegisterState.Status;
19
import org.openconcerto.erp.core.sales.pos.model.RegisterState.Status;
-
 
20
import org.openconcerto.erp.core.sales.pos.model.TicketItem;
20
import org.openconcerto.ui.touch.ScrollableList;
21
import org.openconcerto.ui.touch.ScrollableList;
21
import org.openconcerto.utils.Pair;
-
 
22
 
22
 
23
import java.awt.Color;
23
import java.awt.Color;
24
import java.awt.Dimension;
24
import java.awt.Dimension;
25
import java.awt.Font;
25
import java.awt.Font;
26
import java.awt.Graphics;
26
import java.awt.Graphics;
Line 43... Line 43...
43
import javax.swing.event.ListSelectionEvent;
43
import javax.swing.event.ListSelectionEvent;
44
import javax.swing.event.ListSelectionListener;
44
import javax.swing.event.ListSelectionListener;
45
 
45
 
46
public class TicketPanel extends JPanel implements CaisseListener {
46
public class TicketPanel extends JPanel implements CaisseListener {
47
 
47
 
48
    static private final DateFormat DF = new SimpleDateFormat("' le' EEEE d MMMM à H:mm:ss");
48
    private final DateFormat df = new SimpleDateFormat("' le' d MMMM à H:mm");
49
 
-
 
50
    private final Image bg;
49
    private final Image bg;
51
 
-
 
52
    private final ListModel dataModel;
50
    private final ListModel<TicketItem> dataModel;
53
    private final List<ListDataListener> listeners = new ArrayList<ListDataListener>();
51
    private final List<ListDataListener> listeners = new ArrayList<>();
54
    JLabel lTotal = new JLabel("", SwingConstants.RIGHT);
52
    private final JLabel lTotal = new JLabel("", SwingConstants.RIGHT);
55
    JLabel lNumero = new JLabel("", SwingConstants.LEFT);
53
    private final JLabel lNumero = new JLabel("", SwingConstants.LEFT);
56
    private final CaisseControler controler;
54
    private final CaisseControler controler;
57
 
-
 
58
    private final ScrollableList list;
55
    private final ScrollableList list;
-
 
56
    private int xOffset = 0;
59
 
57
 
60
    TicketPanel(final CaisseControler controler) {
58
    TicketPanel(final CaisseControler controler) {
61
        this.controler = controler;
59
        this.controler = controler;
62
        this.controler.addCaisseListener(this);
60
        this.controler.addCaisseListener(this);
-
 
61
        if (this.controler.getPOSConf().getScreenWidth() < 1280) {
-
 
62
            this.xOffset = -24;
63
 
63
        }
64
        this.setOpaque(false);
64
        this.setOpaque(false);
65
        this.bg = new ImageIcon(TicketPanel.class.getResource("ticket.png")).getImage();
65
        this.bg = new ImageIcon(TicketPanel.class.getResource("ticket.png")).getImage();
66
        this.setLayout(null);
66
        this.setLayout(null);
67
 
67
 
68
        this.dataModel = new ListModel() {
68
        this.dataModel = new ListModel<TicketItem>() {
69
 
69
 
70
            @Override
70
            @Override
71
            public void addListDataListener(final ListDataListener l) {
71
            public void addListDataListener(final ListDataListener l) {
72
                TicketPanel.this.listeners.add(l);
72
                TicketPanel.this.listeners.add(l);
73
            }
73
            }
74
 
74
 
75
            @Override
75
            @Override
76
            public Object getElementAt(final int index) {
76
            public TicketItem getElementAt(final int index) {
77
                return controler.getItems().get(index);
77
                return controler.getItems().get(index);
78
            }
78
            }
79
 
79
 
80
            @Override
80
            @Override
81
            public int getSize() {
81
            public int getSize() {
Line 93... Line 93...
93
            private final TicketCellRenderer renderer = new TicketCellRenderer();
93
            private final TicketCellRenderer renderer = new TicketCellRenderer();
94
 
94
 
95
            @Override
95
            @Override
96
            public void paintCell(final Graphics g, final Object value, final int index, final boolean isSelected, final int posY) {
96
            public void paintCell(final Graphics g, final Object value, final int index, final boolean isSelected, final int posY) {
97
                g.translate(0, posY);
97
                g.translate(0, posY);
98
                this.renderer.paint(g, TicketPanel.this.list, value, index, isSelected);
98
                this.renderer.paint(g, TicketPanel.this.list, (TicketItem) value, index, isSelected);
99
                g.translate(0, -posY);
99
                g.translate(0, -posY);
100
            }
100
            }
101
        };
101
        };
102
        this.list.setOpaque(false);
102
        this.list.setOpaque(false);
103
        this.list.setSize(276, 450);
103
        this.list.setSize(315, 450);
104
        this.list.setFixedCellHeight(40);
104
        this.list.setFixedCellHeight(40);
105
        this.list.setLocation(68, 18);
105
        this.list.setLocation(30 + this.xOffset, 18);
106
        this.add(this.list);
106
        this.add(this.list);
107
 
107
 
108
        this.lTotal.setSize(276 - 10, 32);
108
        this.lTotal.setSize(276 - 10, 32);
109
        this.lTotal.setLocation(68, 500 - 32);
109
        this.lTotal.setLocation(68 + this.xOffset, 500 - 32);
110
        this.lTotal.setFont(new Font("Arial", Font.BOLD, 18));
110
        this.lTotal.setFont(new Font("Arial", Font.BOLD, 18));
111
        this.add(this.lTotal);
111
        this.add(this.lTotal);
112
        this.lNumero.setSize(276 - 10, 32);
112
        this.lNumero.setSize(276 - 10, 32);
113
        this.lNumero.setLocation(68, 500);
113
        this.lNumero.setLocation(68 + this.xOffset, 500);
114
        this.lNumero.setForeground(Color.DARK_GRAY);
114
        this.lNumero.setForeground(Color.DARK_GRAY);
115
        this.lNumero.setFont(new Font("Arial", Font.BOLD, 12));
115
        this.lNumero.setFont(new Font("Arial", Font.BOLD, 12));
116
        this.add(this.lNumero);
116
        this.add(this.lNumero);
117
 
117
 
118
        this.list.addListSelectionListener(new ListSelectionListener() {
118
        this.list.addListSelectionListener(new ListSelectionListener() {
Line 120... Line 120...
120
            @Override
120
            @Override
121
            public void valueChanged(final ListSelectionEvent e) {
121
            public void valueChanged(final ListSelectionEvent e) {
122
                if (!e.getValueIsAdjusting()) {
122
                if (!e.getValueIsAdjusting()) {
123
                    final Object selectedValue = TicketPanel.this.list.getSelectedValue();
123
                    final Object selectedValue = TicketPanel.this.list.getSelectedValue();
124
                    if (selectedValue != null) {
124
                    if (selectedValue != null) {
125
                        final Article a = ((Pair<Article, Integer>) selectedValue).getFirst();
125
                        TicketItem item = (TicketItem) selectedValue;
126
                        controler.setArticleSelected(a);
126
                        controler.setTicketItemSelected(item);
127
                        // If the category of the selected article does not match the current
127
                        // If the category of the selected article does not match the current
128
                        // category of the categories list,
128
                        // category of the categories list,
129
                        // then the corresponding article is not selected.
129
                        // then the corresponding article is not selected.
130
                        controler.setArticleSelected(a); // Dirty fix : use two refresh
130
                        controler.setTicketItemSelected(item); // Dirty fix : use two refresh
-
 
131
 
131
                    }
132
                    }
132
                }
133
                }
133
 
134
 
134
            }
135
            }
135
        });
136
        });
136
 
137
 
137
    }
138
    }
138
 
139
 
139
    @Override
140
    @Override
140
    protected void paintComponent(final Graphics g) {
141
    protected void paintComponent(final Graphics g) {
141
        g.drawImage(this.bg, 0, 0, null);
142
        g.drawImage(this.bg, this.xOffset, 0, null);
142
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
143
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
143
        try {
144
        try {
-
 
145
            final RegisterState registerState = this.controler.getCaisseFrame().getFiles().getLastLog().getRegisterState();
-
 
146
 
144
            // Display caisse and Vendor ID
147
            // Display caisse and Vendor ID
145
            String InfoCaisseVendeur = "Caisse " + this.controler.getPOSConf().getPosID() + "  Vendeur " + this.controler.getPOSConf().getUserID();
148
            final String infoCaisseVendeur = "Caisse " + this.controler.getPOSConf().getPosID() + "  Vendeur " + this.controler.getPOSConf().getUserID();
-
 
149
            final String infoCaisse = (registerState.getStatus() == Status.OPEN ? "Ouverte" : "Fermée") + registerState.formatDate(this.df);
146
            g.setColor(new Color(230, 230, 230));
150
            g.setColor(new Color(230, 230, 230));
-
 
151
            if (this.controler.getPOSConf().getScreenWidth() < 1280) {
-
 
152
                g.setFont(getFont().deriveFont(16.0f));
-
 
153
            } else {
147
            g.setFont(getFont().deriveFont(28.0f));
154
                g.setFont(getFont().deriveFont(14.0f));
-
 
155
            }
148
            g.drawString(InfoCaisseVendeur, 20, this.getHeight() - 50);
156
            int w1 = g.getFontMetrics().stringWidth(infoCaisseVendeur);
-
 
157
            int w2 = g.getFontMetrics().stringWidth(infoCaisse);
149
 
158
 
-
 
159
            int x = 330 - Math.max(w1, w2) + this.xOffset;
-
 
160
            g.drawString(infoCaisseVendeur, x, this.getHeight() - 40);
150
            g.setColor(Color.LIGHT_GRAY);
161
            g.setColor(Color.LIGHT_GRAY);
-
 
162
 
151
            g.setFont(getFont().deriveFont(18.0f));
163
            g.drawString(infoCaisse, x, this.getHeight() - 25);
152
            final RegisterState registerState = this.controler.getCaisseFrame().getFiles().getLastLog().getRegisterState();
-
 
153
            g.drawString("Caisse " + (registerState.getStatus() == Status.OPEN ? "ouverte" : "fermée") + registerState.formatDate(DF), 20, this.getHeight() - 25);
-
 
154
        } catch (Exception e) {
164
        } catch (Exception e) {
155
            POSConfiguration.getLogger().log(Level.WARNING, "Couldn't find register state", e);
165
            POSConfiguration.getLogger().log(Level.WARNING, "Couldn't find register state", e);
156
        }
166
        }
157
        super.paintComponent(g);
167
        super.paintComponent(g);
158
    }
168
    }
159
 
169
 
-
 
170
    int getMinWidth() {
-
 
171
        if (this.controler.getPOSConf().getScreenWidth() < 1280) {
-
 
172
            return 330;
-
 
173
        }
-
 
174
        return 480;
-
 
175
    }
-
 
176
 
-
 
177
    int getMaxHeight() {
-
 
178
        if (this.controler.getPOSConf().getScreenHeight() < 1000) {
-
 
179
            return 550;
-
 
180
        }
-
 
181
        return 707;
-
 
182
    }
-
 
183
 
160
    @Override
184
    @Override
161
    public Dimension getMinimumSize() {
185
    public Dimension getMinimumSize() {
162
        return new Dimension(480, 550);
186
        return new Dimension(getMinWidth(), 550);
163
    }
187
    }
164
 
188
 
165
    @Override
189
    @Override
166
    public Dimension getPreferredSize() {
190
    public Dimension getPreferredSize() {
167
        return new Dimension(480, 707);
191
        return new Dimension(getMinWidth(), getMaxHeight());
168
    }
192
    }
169
 
193
 
170
    public void fire() {
194
    public void fire() {
171
        for (final ListDataListener l : this.listeners) {
195
        for (final ListDataListener l : this.listeners) {
172
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
196
            l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, this.listeners.size()));
Line 187... Line 211...
187
            this.list.clearSelection();
211
            this.list.clearSelection();
188
            return;
212
            return;
189
        }
213
        }
190
        try {
214
        try {
191
            // Deja selectionné
215
            // Deja selectionné
192
            if (this.list.getSelectedValue() != null && articleSelected != null && articleSelected.equals(((Pair<Article, Integer>) this.list.getSelectedValue()).getFirst())) {
216
            if (this.list.getSelectedValue() != null && articleSelected.equals(((TicketItem) this.list.getSelectedValue()).getArticle())) {
193
                return;
217
                return;
194
            }
218
            }
195
        } catch (final Exception e) {
219
        } catch (final Exception e) {
196
            e.printStackTrace();
220
            e.printStackTrace();
197
        }
221
        }
198
 
222
 
199
        if (articleSelected != null) {
-
 
200
 
-
 
201
            for (int i = 0; i < this.dataModel.getSize(); i++) {
223
        for (int i = 0; i < this.dataModel.getSize(); i++) {
202
                final Pair<Article, Integer> item = (Pair<Article, Integer>) this.dataModel.getElementAt(i);
224
            final TicketItem item = this.dataModel.getElementAt(i);
203
                if (item.getFirst().equals(articleSelected)) {
225
            if (item.getArticle().equals(articleSelected)) {
204
                    this.list.setSelectedValue(item, true);
226
                this.list.setSelectedValue(item, true);
205
                    break;
227
                break;
206
                }
228
            }
207
            }
229
        }
208
        }
-
 
209
 
230
 
210
    }
231
    }
-
 
232
 
211
}
233
}