OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 144 Rev 156
Line 22... Line 22...
22
import java.awt.GridBagLayout;
22
import java.awt.GridBagLayout;
23
import java.awt.KeyEventDispatcher;
23
import java.awt.KeyEventDispatcher;
24
import java.awt.KeyboardFocusManager;
24
import java.awt.KeyboardFocusManager;
25
import java.awt.event.KeyEvent;
25
import java.awt.event.KeyEvent;
26
import java.util.ArrayList;
26
import java.util.ArrayList;
-
 
27
import java.util.HashMap;
27
import java.util.List;
28
import java.util.List;
-
 
29
import java.util.Map;
-
 
30
import java.util.Map.Entry;
28
import java.util.Timer;
31
import java.util.Timer;
29
import java.util.TimerTask;
32
import java.util.TimerTask;
30
 
33
 
31
import javax.swing.JFrame;
34
import javax.swing.JFrame;
32
import javax.swing.JLabel;
35
import javax.swing.JLabel;
Line 49... Line 52...
49
    private long firstTime = -1;
52
    private long firstTime = -1;
50
    private Timer timer;
53
    private Timer timer;
51
    // non final car un TimerTask n'est pas reutilisable
54
    // non final car un TimerTask n'est pas reutilisable
52
    private TimerTask task;
55
    private TimerTask task;
53
    private boolean enable = true;
56
    private boolean enable = true;
-
 
57
    private boolean debug = false;
-
 
58
    Map<Integer, String> mapCharacterFR = new HashMap<>();
54
 
59
 
55
    public BarcodeReader(int maxInterKeyDelay) {
60
    public BarcodeReader(int maxInterKeyDelay) {
56
        this.timer = null;
61
        this.timer = null;
57
        this.task = null;
62
        this.task = null;
58
        this.maxInterKeyDelay = maxInterKeyDelay;
63
        this.maxInterKeyDelay = maxInterKeyDelay;
-
 
64
        mapCharacterFR.put((int) '&', "1");
-
 
65
        mapCharacterFR.put((int) 'é', "2");
-
 
66
        mapCharacterFR.put((int) '"', "3");
-
 
67
        mapCharacterFR.put((int) '\'', "4");
-
 
68
        mapCharacterFR.put((int) '(', "5");
-
 
69
        mapCharacterFR.put((int) '-', "6");
-
 
70
        mapCharacterFR.put((int) 'è', "7");
-
 
71
        mapCharacterFR.put((int) '_', "8");
-
 
72
        mapCharacterFR.put((int) 'ç', "9");
-
 
73
        mapCharacterFR.put((int) 'à', "0");
59
    }
74
    }
60
 
75
 
61
    public synchronized void removeBarcodeListener(BarcodeListener l) {
76
    public synchronized void removeBarcodeListener(BarcodeListener l) {
62
        this.listeners.remove(l);
77
        this.listeners.remove(l);
63
        if (this.listeners.isEmpty()) {
78
        if (this.listeners.isEmpty()) {
Line 113... Line 128...
113
 
128
 
114
        final long t = e.getWhen();
129
        final long t = e.getWhen();
115
        if (this.firstTime < 0) {
130
        if (this.firstTime < 0) {
116
            this.firstTime = t;
131
            this.firstTime = t;
117
        }
132
        }
118
        int key = e.getKeyCode();
133
        int keyCode = e.getKeyCode();
119
 
134
 
120
        final long delay = t - this.firstTime;
135
        final long delay = t - this.firstTime;
121
        if (key == KeyEvent.VK_BACK_SPACE || key == KeyEvent.VK_DELETE || (delay > maxInterKeyDelay && key != KeyEvent.VK_SHIFT)) {
136
        if (keyCode == KeyEvent.VK_BACK_SPACE || keyCode == KeyEvent.VK_DELETE || (delay > maxInterKeyDelay && keyCode != KeyEvent.VK_SHIFT)) {
122
            // touche normale
137
            // touche normale
-
 
138
            if (this.debug) {
-
 
139
                System.err.println("TOuche normale " + keyCode);
-
 
140
            }
123
            this.eve.add(e);
141
            this.eve.add(e);
124
            redispatch();
142
            redispatch();
125
            return true;
143
            return true;
126
        }
144
        }
127
 
145
 
128
        final char key2 = e.getKeyChar();
146
        final char keyChar = e.getKeyChar();
129
        this.eve.add(e);
147
        this.eve.add(e);
130
        if (e.getID() == KeyEvent.KEY_RELEASED) {
148
        if (e.getID() == KeyEvent.KEY_RELEASED) {
131
            if (key == KeyEvent.VK_SHIFT) {
149
            if (keyCode == KeyEvent.VK_SHIFT) {
132
                // rien
150
                // rien
-
 
151
                if (this.debug) {
-
 
152
                    System.err.println("SHIFT " + keyCode);
-
 
153
                }
133
            } else if (key2 == '*' || key2 == '$' || key2 == '+' || key2 == '/' || key2 == '%' || key2 == '-' | key2 == ' ') {
154
            } else if (keyChar == '*' || keyChar == '$' || keyChar == '+' || keyChar == '/' || keyChar == '%' || keyChar == ' ') {
134
                this.value += key2;
155
                this.value += keyChar;
-
 
156
                if (this.debug) {
135
            } else if (Character.isLetter(key2) || Character.isDigit(key2)) {
157
                    System.err.println("KEY " + keyCode + " - " + keyChar);
136
                this.value += key2;
158
                }
137
            } else if (key >= KeyEvent.VK_0 && key <= KeyEvent.VK_9 || key >= KeyEvent.VK_A && key <= KeyEvent.VK_Z) {
159
            } else if (keyCode >= KeyEvent.VK_0 && keyCode <= KeyEvent.VK_9 || keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z) {
138
                // from KeyEvent : same as ASCII
160
                // from KeyEvent : same as ASCII
-
 
161
                if (this.debug) {
-
 
162
                    System.err.println("[0-9] [A-Z] " + keyCode);
-
 
163
                }
139
                this.value += (char) key;
164
                this.value += (char) keyCode;
140
            } else if (key == KeyEvent.VK_ENTER && this.value.length() >= MIN_BARCODE_LENGTH) {
165
            } else if (keyCode == KeyEvent.VK_ENTER && this.value.length() >= MIN_BARCODE_LENGTH) {
141
                // fin de code barre
166
                // fin de code barre
-
 
167
                if (this.debug) {
-
 
168
                    System.err.println("BARCODE OK ENTER OR LENGHT " + keyCode + " length = " + this.value.length() + " min length =" + MIN_BARCODE_LENGTH);
-
 
169
                }
142
                this.value = this.value.trim();
170
                this.value = this.value.trim();
143
                fire(this.value);
171
                fire(this.value);
144
                reset();
172
                reset();
-
 
173
            } else if (mapCharacterFR.containsKey((int) keyChar)) {
-
 
174
                if (this.debug) {
-
 
175
                    System.err.println("MAP DEFAULT FR CHAR " + keyChar + " WITH " + mapCharacterFR.get((int) keyChar));
-
 
176
                }
-
 
177
                this.value += mapCharacterFR.get((int) keyChar);
-
 
178
            } else if (Character.isLetter(keyChar) || Character.isDigit(keyChar)) {
-
 
179
                this.value += keyChar;
-
 
180
                if (this.debug) {
-
 
181
                    System.err.println("LETTER OR DIGIT " + keyChar);
-
 
182
                }
145
            } else {
183
            } else {
146
                // Caractere non code barre
184
                // Caractere non code barre
-
 
185
                if (this.debug) {
-
 
186
                    System.err.println("CHAR NON CODE BARRE " + e);
-
 
187
                }
147
                redispatch();
188
                redispatch();
148
            }
189
            }
149
            // lance un timer s'il reste des evenements non dispatchés
190
            // lance un timer s'il reste des evenements non dispatchés
150
            if (!this.eve.isEmpty()) {
191
            if (!this.eve.isEmpty()) {
151
                this.firstTime = t;
192
                this.firstTime = t;
Line 187... Line 228...
187
        this.value = "";
228
        this.value = "";
188
        this.eve.clear();
229
        this.eve.clear();
189
        this.firstTime = -1;
230
        this.firstTime = -1;
190
    }
231
    }
191
 
232
 
-
 
233
    public Map<Integer, String> getMapCharacterFR() {
-
 
234
        return mapCharacterFR;
-
 
235
    }
-
 
236
 
-
 
237
    public void setDebug(boolean debug) {
-
 
238
        this.debug = debug;
-
 
239
    }
-
 
240
 
192
    public static void main(String[] args) {
241
    public static void main(String[] args) {
193
        String delay = "80";
242
        String delay = "80";
194
        if (args.length > 0) {
243
        if (args.length > 0) {
195
            delay = args[0];
244
            delay = args[0];
196
        }
245
        }
Line 221... Line 270...
221
                c.fill = GridBagConstraints.BOTH;
270
                c.fill = GridBagConstraints.BOTH;
222
                final ITextArea t1 = new ITextArea();
271
                final ITextArea t1 = new ITextArea();
223
                panel.add(new JScrollPane(t1), c);
272
                panel.add(new JScrollPane(t1), c);
224
 
273
 
225
                BarcodeReader reader = new BarcodeReader(d);
274
                BarcodeReader reader = new BarcodeReader(d);
-
 
275
                reader.setDebug(true);
-
 
276
 
-
 
277
                System.err.println("FR MAP");
-
 
278
                for (Entry<Integer, String> string : reader.getMapCharacterFR().entrySet()) {
-
 
279
                    System.err.println(string.getKey() + " --> " + string.getValue());
-
 
280
                }
-
 
281
 
226
                reader.addBarcodeListener(new BarcodeListener() {
282
                reader.addBarcodeListener(new BarcodeListener() {
227
 
283
 
228
                    @Override
284
                    @Override
229
                    public void keyReceived(KeyEvent ee) {
285
                    public void keyReceived(KeyEvent ee) {
230
                        System.err.println("BarcodeReader keyReceived() : " + ee);
286
                        System.err.println("BarcodeReader keyReceived() : " + ee);