OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 12... Line 12...
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.sales.pos.io;
14
 package org.openconcerto.erp.core.sales.pos.io;
15
 
15
 
16
import org.openconcerto.erp.core.sales.pos.ui.BarcodeListener;
16
import org.openconcerto.erp.core.sales.pos.ui.BarcodeListener;
-
 
17
import org.openconcerto.erp.gs1.GS1Util;
17
import org.openconcerto.ui.DefaultGridBagConstraints;
18
import org.openconcerto.ui.DefaultGridBagConstraints;
18
import org.openconcerto.ui.component.ITextArea;
19
import org.openconcerto.ui.component.ITextArea;
19
import org.openconcerto.utils.StringUtils;
20
import org.openconcerto.utils.StringUtils;
20
 
21
 
21
import java.awt.Dimension;
22
import java.awt.Dimension;
Line 39... Line 40...
39
import javax.swing.SwingUtilities;
40
import javax.swing.SwingUtilities;
40
import javax.swing.UIManager;
41
import javax.swing.UIManager;
41
 
42
 
42
/**
43
/**
43
 * Lecteur code barres, intercepte les événements clavier pour détecter un scan de code. Le code
44
 * Lecteur code barres, intercepte les événements clavier pour détecter un scan de code. Le code
44
 * barre doit terminer par un retour à la ligne.
45
 * barre doit terminer par un retour à la ligne. Pour les GS1 : Aim Code ID Character AIM ID doit
-
 
46
 * être activé, ]D2 pour databar / ]C1 pour GS1
45
 */
47
 */
46
public class BarcodeReader implements KeyEventDispatcher {
48
public class BarcodeReader implements KeyEventDispatcher {
47
 
49
 
48
    public int maxInterKeyDelay = 80;
50
    public int maxInterKeyDelay = 80;
49
    private static final int MIN_BARCODE_LENGTH = 2;
51
    private static final int MIN_BARCODE_LENGTH = 2;
Line 54... Line 56...
54
    private Timer timer;
56
    private Timer timer;
55
    // non final car un TimerTask n'est pas reutilisable
57
    // non final car un TimerTask n'est pas reutilisable
56
    private TimerTask task;
58
    private TimerTask task;
57
    private boolean enable = true;
59
    private boolean enable = true;
58
    private boolean debug = false;
60
    private boolean debug = false;
59
    Map<Integer, String> mapCharacterFR = new HashMap<>();
61
    private Map<Integer, String> mapCharacterFR = new HashMap<>();
60
 
62
 
61
    public BarcodeReader(int maxInterKeyDelay) {
63
    public BarcodeReader(int maxInterKeyDelay) {
62
        this.timer = null;
64
        this.timer = null;
63
        this.task = null;
65
        this.task = null;
64
        this.maxInterKeyDelay = maxInterKeyDelay;
66
        this.maxInterKeyDelay = maxInterKeyDelay;
Line 191... Line 193...
191
                if (this.debug) {
193
                if (this.debug) {
192
                    System.err.println("<GS>");
194
                    System.err.println("<GS>");
193
                }
195
                }
194
            } else if (keyChar == KeyEvent.CHAR_UNDEFINED) {
196
            } else if (keyChar == KeyEvent.CHAR_UNDEFINED) {
195
                System.err.println("CHAR_UNDEFINED");
197
                System.err.println("CHAR_UNDEFINED");
-
 
198
                this.value += '\u001D';
196
            } else {
199
            } else {
197
                // Caractere non code barre
200
                // Caractere non code barre
198
                if (this.debug) {
201
                if (this.debug) {
199
                    System.err.println("CHAR NON CODE BARRE keyCode:" + keyCode + " keyChar:" + keyChar);
202
                    System.err.println("CHAR NON CODE BARRE keyCode:" + keyCode + " keyChar:" + keyChar);
200
                }
203
                }
Line 204... Line 207...
204
            if (!this.eve.isEmpty()) {
207
            if (!this.eve.isEmpty()) {
205
                this.firstTime = t;
208
                this.firstTime = t;
206
                this.task = new TimerTask() {
209
                this.task = new TimerTask() {
207
                    @Override
210
                    @Override
208
                    public void run() {
211
                    public void run() {
-
 
212
                        SwingUtilities.invokeLater(new Runnable() {
-
 
213
                            @Override
-
 
214
                            public void run() {
209
                        redispatchLater();
215
                                redispatch();
-
 
216
                            }
-
 
217
                        });
210
                    }
218
                    }
211
                };
219
                };
-
 
220
                try {
212
                this.timer.schedule(this.task, this.maxInterKeyDelay);
221
                    this.timer.schedule(this.task, this.maxInterKeyDelay);
-
 
222
                } catch (Exception ex) {
-
 
223
                    ex.printStackTrace();
-
 
224
                }
213
            }
225
            }
214
            // si pas d'evenement, pas de temps associé
226
            // si pas d'evenement, pas de temps associé
215
            assert !this.eve.isEmpty() || this.firstTime == -1;
227
            assert !this.eve.isEmpty() || this.firstTime == -1;
216
        }
228
        }
217
        return true;
229
        return true;
218
 
230
 
219
    }
231
    }
220
 
232
 
221
    private void redispatchLater() {
-
 
222
        SwingUtilities.invokeLater(new Runnable() {
-
 
223
            @Override
-
 
224
            public void run() {
-
 
225
                redispatch();
-
 
226
            }
-
 
227
        });
-
 
228
    }
-
 
229
 
-
 
230
    private void redispatch() {
233
    private void redispatch() {
231
        for (int i = 0; i < this.eve.size(); i++) {
234
        for (int i = 0; i < this.eve.size(); i++) {
232
            final KeyEvent ee = this.eve.get(i);
235
            final KeyEvent ee = this.eve.get(i);
233
            KeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(ee.getComponent(), ee);
236
            KeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(ee.getComponent(), ee);
234
            for (int j = 0; j < this.listeners.size(); j++) {
237
            for (int j = 0; j < this.listeners.size(); j++) {
Line 302... Line 305...
302
 
305
 
303
                    @Override
306
                    @Override
304
                    public void barcodeRead(String code) {
307
                    public void barcodeRead(String code) {
305
                        t1.append("Barcode OK : '" + code + "'\n");
308
                        t1.append("Barcode OK : '" + code + "'\n");
306
                        t1.append("Hex: " + StringUtils.bytesToHexString(code.getBytes()));
309
                        t1.append("Hex: " + StringUtils.bytesToHexString(code.getBytes()));
-
 
310
                        t1.append("\n");
-
 
311
                        GS1Util e = new GS1Util();
-
 
312
                        try {
-
 
313
                            t1.append("GS1: " + e.parseFromScanner(code).formatHumanReadable());
-
 
314
                        } catch (Exception ex) {
-
 
315
                            t1.append("GS1: " + ex.getMessage());
-
 
316
                        }
-
 
317
                        t1.append("\n\n");
307
                    }
318
                    }
308
                });
319
                });
309
 
320
 
310
                f.setSize(new Dimension(640, 480));
321
                f.setSize(new Dimension(640, 480));
311
                f.setLocationRelativeTo(null);
322
                f.setLocationRelativeTo(null);