OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Rev 182 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 156 Rev 180
Line 14... Line 14...
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.ui.DefaultGridBagConstraints;
17
import org.openconcerto.ui.DefaultGridBagConstraints;
18
import org.openconcerto.ui.component.ITextArea;
18
import org.openconcerto.ui.component.ITextArea;
-
 
19
import org.openconcerto.utils.StringUtils;
19
 
20
 
20
import java.awt.Dimension;
21
import java.awt.Dimension;
21
import java.awt.GridBagConstraints;
22
import java.awt.GridBagConstraints;
22
import java.awt.GridBagLayout;
23
import java.awt.GridBagLayout;
23
import java.awt.KeyEventDispatcher;
24
import java.awt.KeyEventDispatcher;
Line 59... Line 60...
59
 
60
 
60
    public BarcodeReader(int maxInterKeyDelay) {
61
    public BarcodeReader(int maxInterKeyDelay) {
61
        this.timer = null;
62
        this.timer = null;
62
        this.task = null;
63
        this.task = null;
63
        this.maxInterKeyDelay = maxInterKeyDelay;
64
        this.maxInterKeyDelay = maxInterKeyDelay;
64
        mapCharacterFR.put((int) '&', "1");
65
        this.mapCharacterFR.put((int) '&', "1");
65
        mapCharacterFR.put((int) 'é', "2");
66
        this.mapCharacterFR.put((int) 'é', "2");
66
        mapCharacterFR.put((int) '"', "3");
67
        this.mapCharacterFR.put((int) '"', "3");
67
        mapCharacterFR.put((int) '\'', "4");
68
        this.mapCharacterFR.put((int) '\'', "4");
68
        mapCharacterFR.put((int) '(', "5");
69
        this.mapCharacterFR.put((int) '(', "5");
69
        mapCharacterFR.put((int) '-', "6");
70
        this.mapCharacterFR.put((int) '-', "6");
70
        mapCharacterFR.put((int) 'è', "7");
71
        this.mapCharacterFR.put((int) 'è', "7");
71
        mapCharacterFR.put((int) '_', "8");
72
        this.mapCharacterFR.put((int) '_', "8");
72
        mapCharacterFR.put((int) 'ç', "9");
73
        this.mapCharacterFR.put((int) 'ç', "9");
73
        mapCharacterFR.put((int) 'à', "0");
74
        this.mapCharacterFR.put((int) 'à', "0");
74
    }
75
    }
75
 
76
 
76
    public synchronized void removeBarcodeListener(BarcodeListener l) {
77
    public synchronized void removeBarcodeListener(BarcodeListener l) {
77
        this.listeners.remove(l);
78
        this.listeners.remove(l);
78
        if (this.listeners.isEmpty()) {
79
        if (this.listeners.isEmpty()) {
Line 100... Line 101...
100
    public synchronized void start() {
101
    public synchronized void start() {
101
        if (this.timer == null) {
102
        if (this.timer == null) {
102
            // init avant que les listeners s'en servent
103
            // init avant que les listeners s'en servent
103
            this.timer = new Timer(getClass().getName(), true);
104
            this.timer = new Timer(getClass().getName(), true);
104
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
105
            KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(this);
105
            System.err.println("BarcodeReader start : scan delay " + maxInterKeyDelay + " ms");
106
            System.err.println("BarcodeReader start : scan delay " + this.maxInterKeyDelay + " ms");
106
        }
107
        }
107
    }
108
    }
108
 
109
 
109
    /**
110
    /**
110
     * Stoppe l'écoute sur les évenements clavier
111
     * Stoppe l'écoute sur les évenements clavier
Line 118... Line 119...
118
        }
119
        }
119
    }
120
    }
120
 
121
 
121
    @Override
122
    @Override
122
    public boolean dispatchKeyEvent(KeyEvent e) {
123
    public boolean dispatchKeyEvent(KeyEvent e) {
123
        if (!enable)
124
        if (!this.enable) {
124
            return false;
125
            return false;
125
 
126
        }
126
        if (this.task != null)
127
        if (this.task != null)
127
            this.task.cancel();
128
            this.task.cancel();
128
 
129
 
129
        final long t = e.getWhen();
130
        final long t = e.getWhen();
130
        if (this.firstTime < 0) {
131
        if (this.firstTime < 0) {
131
            this.firstTime = t;
132
            this.firstTime = t;
132
        }
133
        }
133
        int keyCode = e.getKeyCode();
134
        int keyCode = e.getKeyCode();
134
 
135
 
135
        final long delay = t - this.firstTime;
136
        final long delay = t - this.firstTime;
136
        if (keyCode == KeyEvent.VK_BACK_SPACE || keyCode == KeyEvent.VK_DELETE || (delay > maxInterKeyDelay && keyCode != KeyEvent.VK_SHIFT)) {
137
        if (keyCode == KeyEvent.VK_BACK_SPACE || keyCode == KeyEvent.VK_DELETE || (delay > this.maxInterKeyDelay && keyCode != KeyEvent.VK_SHIFT)) {
137
            // touche normale
138
            // touche normale
138
            if (this.debug) {
139
            if (this.debug) {
139
                System.err.println("TOuche normale " + keyCode);
140
                System.err.println("Touche normale " + keyCode);
140
            }
141
            }
141
            this.eve.add(e);
142
            this.eve.add(e);
142
            redispatch();
143
            redispatch();
143
            return true;
144
            return true;
144
        }
145
        }
Line 149... Line 150...
149
            if (keyCode == KeyEvent.VK_SHIFT) {
150
            if (keyCode == KeyEvent.VK_SHIFT) {
150
                // rien
151
                // rien
151
                if (this.debug) {
152
                if (this.debug) {
152
                    System.err.println("SHIFT " + keyCode);
153
                    System.err.println("SHIFT " + keyCode);
153
                }
154
                }
-
 
155
            } else if (keyChar == ']') {
-
 
156
                this.value += keyChar;
-
 
157
                if (this.debug) {
-
 
158
                    System.err.println("]");
-
 
159
                }
154
            } else if (keyChar == '*' || keyChar == '$' || keyChar == '+' || keyChar == '/' || keyChar == '%' || keyChar == ' ') {
160
            } else if (keyChar == '*' || keyChar == '$' || keyChar == '+' || keyChar == '/' || keyChar == '%' || keyChar == ' ') {
155
                this.value += keyChar;
161
                this.value += keyChar;
156
                if (this.debug) {
162
                if (this.debug) {
157
                    System.err.println("KEY " + keyCode + " - " + keyChar);
163
                    System.err.println("KEY " + keyCode + " - " + keyChar);
158
                }
164
                }
159
            } else if (keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9 || keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z) {
165
            } else if (keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9 || keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z) {
160
                // from KeyEvent : same as ASCII
166
                // from KeyEvent : same as ASCII
161
                if (this.debug) {
167
                if (this.debug) {
162
                    System.err.println("[0-9] [A-Z] " + keyCode);
168
                    System.err.println("[0-9] [A-Z] " + keyCode + " : " + keyChar);
163
                }
169
                }
164
                this.value += (char) keyCode;
170
                this.value += (char) keyCode;
165
            } else if (keyCode == KeyEvent.VK_ENTER && this.value.length() >= MIN_BARCODE_LENGTH) {
171
            } else if (keyCode == KeyEvent.VK_ENTER && this.value.length() >= MIN_BARCODE_LENGTH) {
166
                // fin de code barre
172
                // fin de code barre
167
                if (this.debug) {
173
                if (this.debug) {
168
                    System.err.println("BARCODE OK ENTER OR LENGHT " + keyCode + " length = " + this.value.length() + " min length =" + MIN_BARCODE_LENGTH);
174
                    System.err.println("BARCODE OK ENTER OR LENGHT " + keyCode + " length = " + this.value.length() + " min length =" + MIN_BARCODE_LENGTH);
169
                }
175
                }
170
                this.value = this.value.trim();
176
                this.value = this.value.trim();
171
                fire(this.value);
177
                fire(this.value);
172
                reset();
178
                reset();
173
            } else if (mapCharacterFR.containsKey((int) keyChar)) {
179
            } else if (this.mapCharacterFR.containsKey((int) keyChar)) {
174
                if (this.debug) {
180
                if (this.debug) {
175
                    System.err.println("MAP DEFAULT FR CHAR " + keyChar + " WITH " + mapCharacterFR.get((int) keyChar));
181
                    System.err.println("MAP DEFAULT FR CHAR " + keyChar + " WITH " + this.mapCharacterFR.get((int) keyChar));
176
                }
182
                }
177
                this.value += mapCharacterFR.get((int) keyChar);
183
                this.value += this.mapCharacterFR.get((int) keyChar);
178
            } else if (Character.isLetter(keyChar) || Character.isDigit(keyChar)) {
184
            } else if (Character.isLetter(keyChar) || Character.isDigit(keyChar)) {
179
                this.value += keyChar;
185
                this.value += keyChar;
180
                if (this.debug) {
186
                if (this.debug) {
181
                    System.err.println("LETTER OR DIGIT " + keyChar);
187
                    System.err.println("LETTER OR DIGIT " + keyChar);
182
                }
188
                }
-
 
189
            } else if (keyChar == 29) {
-
 
190
                this.value += '\u001D';
-
 
191
                if (this.debug) {
-
 
192
                    System.err.println("<GS>");
-
 
193
                }
-
 
194
            } else if (keyChar == KeyEvent.CHAR_UNDEFINED) {
-
 
195
                System.err.println("CHAR_UNDEFINED");
183
            } else {
196
            } else {
184
                // Caractere non code barre
197
                // Caractere non code barre
185
                if (this.debug) {
198
                if (this.debug) {
186
                    System.err.println("CHAR NON CODE BARRE " + e);
199
                    System.err.println("CHAR NON CODE BARRE keyCode:" + keyCode + " keyChar:" + keyChar);
187
                }
200
                }
188
                redispatch();
201
                redispatch();
189
            }
202
            }
190
            // lance un timer s'il reste des evenements non dispatchés
203
            // lance un timer s'il reste des evenements non dispatchés
191
            if (!this.eve.isEmpty()) {
204
            if (!this.eve.isEmpty()) {
Line 194... Line 207...
194
                    @Override
207
                    @Override
195
                    public void run() {
208
                    public void run() {
196
                        redispatchLater();
209
                        redispatchLater();
197
                    }
210
                    }
198
                };
211
                };
199
                this.timer.schedule(this.task, maxInterKeyDelay);
212
                this.timer.schedule(this.task, this.maxInterKeyDelay);
200
            }
213
            }
201
            // si pas d'evenement, pas de temps associé
214
            // si pas d'evenement, pas de temps associé
202
            assert !this.eve.isEmpty() || this.firstTime == -1;
215
            assert !this.eve.isEmpty() || this.firstTime == -1;
203
        }
216
        }
204
        return true;
217
        return true;
-
 
218
 
205
    }
219
    }
206
 
220
 
207
    private void redispatchLater() {
221
    private void redispatchLater() {
208
        SwingUtilities.invokeLater(new Runnable() {
222
        SwingUtilities.invokeLater(new Runnable() {
209
            @Override
223
            @Override
Line 229... Line 243...
229
        this.eve.clear();
243
        this.eve.clear();
230
        this.firstTime = -1;
244
        this.firstTime = -1;
231
    }
245
    }
232
 
246
 
233
    public Map<Integer, String> getMapCharacterFR() {
247
    public Map<Integer, String> getMapCharacterFR() {
234
        return mapCharacterFR;
248
        return this.mapCharacterFR;
235
    }
249
    }
236
 
250
 
237
    public void setDebug(boolean debug) {
251
    public void setDebug(boolean debug) {
238
        this.debug = debug;
252
        this.debug = debug;
239
    }
253
    }
Line 287... Line 301...
287
                    }
301
                    }
288
 
302
 
289
                    @Override
303
                    @Override
290
                    public void barcodeRead(String code) {
304
                    public void barcodeRead(String code) {
291
                        t1.append("Barcode OK : '" + code + "'\n");
305
                        t1.append("Barcode OK : '" + code + "'\n");
-
 
306
                        t1.append("Hex: " + StringUtils.bytesToHexString(code.getBytes()));
292
                    }
307
                    }
293
                });
308
                });
294
 
309
 
295
                f.setSize(new Dimension(640, 480));
310
                f.setSize(new Dimension(640, 480));
296
                f.setLocationRelativeTo(null);
311
                f.setLocationRelativeTo(null);