OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 94 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 94 Rev 156
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 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.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.ui;
14
 package org.openconcerto.ui;
15
 
15
 
16
import java.awt.event.KeyAdapter;
16
import java.awt.event.KeyAdapter;
17
import java.awt.event.KeyEvent;
17
import java.awt.event.KeyEvent;
18
import java.util.Calendar;
18
import java.util.Calendar;
19
 
19
 
20
import javax.swing.JFrame;
20
import javax.swing.JFrame;
21
import javax.swing.JTextField;
21
import javax.swing.JTextField;
22
import javax.swing.SwingConstants;
22
import javax.swing.SwingConstants;
23
import javax.swing.event.DocumentEvent;
23
import javax.swing.event.DocumentEvent;
24
import javax.swing.event.DocumentListener;
24
import javax.swing.event.DocumentListener;
25
import javax.swing.text.AbstractDocument;
25
import javax.swing.text.AbstractDocument;
26
import javax.swing.text.AttributeSet;
26
import javax.swing.text.AttributeSet;
27
import javax.swing.text.BadLocationException;
27
import javax.swing.text.BadLocationException;
28
import javax.swing.text.Document;
28
import javax.swing.text.Document;
29
import javax.swing.text.DocumentFilter;
29
import javax.swing.text.DocumentFilter;
30
import javax.swing.text.JTextComponent;
30
import javax.swing.text.JTextComponent;
31
import javax.swing.text.NavigationFilter;
31
import javax.swing.text.NavigationFilter;
32
import javax.swing.text.Position.Bias;
32
import javax.swing.text.Position.Bias;
33
 
33
 
34
public class TimeTextField extends JTextField {
34
public class TimeTextField extends JTextField {
35
 
35
 
36
    public TimeTextField() {
36
    public TimeTextField() {
37
        super(6);
37
        super(6);
38
        init(0, 0);
38
        init(0, 0);
39
    }
39
    }
40
 
40
 
41
    public TimeTextField(boolean fillWithCurrentTime) {
41
    public TimeTextField(boolean fillWithCurrentTime) {
42
        super(6);
42
        super(6);
43
        if (fillWithCurrentTime) {
43
        if (fillWithCurrentTime) {
44
            Calendar c = Calendar.getInstance();
44
            Calendar c = Calendar.getInstance();
45
            init(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
45
            init(c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE));
46
        } else {
46
        } else {
47
            init(0, 0);
47
            init(0, 0);
48
        }
48
        }
49
    }
49
    }
50
 
50
 
51
    public TimeTextField(int hour, int minute) {
51
    public TimeTextField(int hour, int minute) {
52
        super(6);
52
        super(6);
53
        init(hour, minute);
53
        init(hour, minute);
54
    }
54
    }
55
 
55
 
56
    private void init(int hour, int minute) {
56
    private void init(int hour, int minute) {
57
        setTime(hour, minute);
57
        setTime(hour, minute);
58
        Document d = getDocument();
58
        Document d = getDocument();
59
 
59
 
60
        this.addKeyListener(new KeyAdapter() {
60
        this.addKeyListener(new KeyAdapter() {
61
 
61
 
62
            @Override
62
            @Override
63
            public void keyPressed(KeyEvent e) {
63
            public void keyPressed(KeyEvent e) {
64
                try {
64
                try {
65
                    if (e.getKeyCode() == KeyEvent.VK_UP) {
65
                    if (e.getKeyCode() == KeyEvent.VK_UP) {
66
                        final int caretPosition = getCaretPosition();
66
                        final int caretPosition = getCaretPosition();
67
 
67
 
68
                        if (caretPosition < 2) {
68
                        if (caretPosition < 2) {
69
                            int i = getHours() + 1;
69
                            int i = getHours() + 1;
70
                            if (i > 23) {
70
                            if (i > 23) {
71
                                i = 0;
71
                                i = 0;
72
                            }
72
                            }
73
                            final String valueOf = fill2Char(i);
73
                            final String valueOf = fill2Char(i);
74
                            ((AbstractDocument) getDocument()).replace(0, 2, valueOf, null);
74
                            ((AbstractDocument) getDocument()).replace(0, 2, valueOf, null);
75
                            setCaretPosition(caretPosition);
75
                            setCaretPosition(caretPosition);
76
                        } else if (caretPosition > 2) {
76
                        } else if (caretPosition > 2) {
77
                            int i = getMinutes() + 1;
77
                            int i = getMinutes() + 1;
78
                            if (i > 59) {
78
                            if (i > 59) {
79
                                i = 0;
79
                                i = 0;
80
                            }
80
                            }
81
                            final String valueOf = fill2Char(i);
81
                            final String valueOf = fill2Char(i);
82
                            ((AbstractDocument) getDocument()).replace(3, 2, valueOf, null);
82
                            ((AbstractDocument) getDocument()).replace(3, 2, valueOf, null);
83
                            setCaretPosition(caretPosition);
83
                            setCaretPosition(caretPosition);
84
                        }
84
                        }
85
 
85
 
86
                    } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
86
                    } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
87
                        final int caretPosition = getCaretPosition();
87
                        final int caretPosition = getCaretPosition();
88
                        if (caretPosition < 2) {
88
                        if (caretPosition < 2) {
89
                            int i = getHours() - 1;
89
                            int i = getHours() - 1;
90
                            if (i < 0) {
90
                            if (i < 0) {
91
                                i = 23;
91
                                i = 23;
92
                            }
92
                            }
93
                            final String valueOf = fill2Char(i);
93
                            final String valueOf = fill2Char(i);
94
                            ((AbstractDocument) getDocument()).replace(0, 2, valueOf, null);
94
                            ((AbstractDocument) getDocument()).replace(0, 2, valueOf, null);
95
                            setCaretPosition(caretPosition);
95
                            setCaretPosition(caretPosition);
96
                        } else if (caretPosition > 2) {
96
                        } else if (caretPosition > 2) {
97
                            int i = getMinutes() - 1;
97
                            int i = getMinutes() - 1;
98
                            if (i < 0) {
98
                            if (i < 0) {
99
                                i = 59;
99
                                i = 59;
100
                            }
100
                            }
101
                            final String valueOf = fill2Char(i);
101
                            final String valueOf = fill2Char(i);
102
                            ((AbstractDocument) getDocument()).replace(3, 2, valueOf, null);
102
                            ((AbstractDocument) getDocument()).replace(3, 2, valueOf, null);
103
                            setCaretPosition(caretPosition);
103
                            setCaretPosition(caretPosition);
104
                        }
104
                        }
105
                    }
105
                    }
106
                } catch (BadLocationException e1) {
106
                } catch (BadLocationException e1) {
107
                    e1.printStackTrace();
107
                    e1.printStackTrace();
108
                }
108
                }
109
 
109
 
110
            }
110
            }
111
        });
111
        });
112
 
112
 
113
        final AbstractDocument doc = (AbstractDocument) d;
113
        final AbstractDocument doc = (AbstractDocument) d;
114
        doc.setDocumentFilter(new DocumentFilter() {
114
        doc.setDocumentFilter(new DocumentFilter() {
115
 
115
 
116
            @Override
116
            @Override
117
            public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
117
            public void remove(FilterBypass fb, int offset, int length) throws BadLocationException {
118
                if (offset == 2) {
118
                if (offset == 2) {
119
                    offset--;
119
                    offset--;
120
                }
120
                }
121
                super.replace(fb, offset + 1, 0, "0", null);
121
                super.replace(fb, offset + 1, 0, "0", null);
122
                super.remove(fb, offset, 1);
122
                super.remove(fb, offset, 1);
123
                setCaretPosition(offset);
123
                setCaretPosition(offset);
124
            }
124
            }
125
 
125
 
126
            @Override
126
            @Override
127
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
127
            public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
128
                if (offset > 4) {
128
                if (offset > 4) {
129
                    return;
129
                    return;
130
                }
130
                }
131
                final String actualText = doc.getText(0, 5);
131
                final String actualText = doc.getText(0, 5);
132
                if (length > 0 && text.length() != length) {
132
                if (length > 0 && text.length() != length) {
133
                    length = 0;
133
                    length = 0;
134
                }
134
                }
135
                final char[] te = actualText.toCharArray();
135
                final char[] te = actualText.toCharArray();
136
                final int stop = text.length();
136
                final int stop = text.length();
137
                for (int i = 0; i < stop; i++) {
137
                for (int i = 0; i < stop; i++) {
138
                    int position = i + offset;
138
                    int position = i + offset;
-
 
139
                    if (position < te.length) {
139
                    te[position] = text.charAt(i);
140
                        te[position] = text.charAt(i);
140
                }
141
                    }
-
 
142
                }
141
                for (int i = 0; i < stop; i++) {
143
                for (int i = 0; i < stop; i++) {
142
                    int position = i + offset;
144
                    int position = i + offset;
143
                    final char c = text.charAt(i);
145
                    final char c = text.charAt(i);
144
                    if (!isCharValid(c, position, te)) {
146
                    if (!isCharValid(c, position, te)) {
145
                        return;
147
                        return;
146
                    }
148
                    }
147
                }
149
                }
148
 
150
 
149
                if (length == 0) {
151
                if (length == 0) {
150
                    super.remove(fb, offset, text.length());
152
                    super.remove(fb, offset, text.length());
151
                }
153
                }
152
                super.replace(fb, offset, length, text, attrs);
154
                super.replace(fb, offset, length, text, attrs);
153
 
155
 
154
            }
156
            }
155
 
157
 
156
        });
158
        });
157
        // Move cusor to not be on ':'
159
        // Move cusor to not be on ':'
158
        this.setNavigationFilter(new NavigationFilter() {
160
        this.setNavigationFilter(new NavigationFilter() {
-
 
161
 
159
            @Override
162
            @Override
160
            public int getNextVisualPositionFrom(JTextComponent text, int pos, Bias bias, int direction, Bias[] biasRet) throws BadLocationException {
163
            public int getNextVisualPositionFrom(JTextComponent text, int pos, Bias bias, int direction, Bias[] biasRet) throws BadLocationException {
161
                if (pos == 1 && direction == SwingConstants.EAST) {
164
                if (pos == 1 && direction == SwingConstants.EAST) {
162
                    pos++;
165
                    pos++;
163
                }
166
                }
164
                if (pos == 3 && direction == SwingConstants.WEST) {
167
                if (pos == 3 && direction == SwingConstants.WEST) {
165
                    pos--;
168
                    pos--;
166
                }
169
                }
167
                return super.getNextVisualPositionFrom(text, pos, bias, direction, biasRet);
170
                return super.getNextVisualPositionFrom(text, pos, bias, direction, biasRet);
168
            }
171
            }
169
 
172
 
170
            @Override
173
            @Override
171
            public void moveDot(FilterBypass fb, int dot, Bias bias) {
174
            public void moveDot(FilterBypass fb, int dot, Bias bias) {
172
                if (dot == 2) {
175
                if (dot == 2) {
173
                    dot = 3;
176
                    dot = 3;
174
                }
177
                }
175
                super.moveDot(fb, dot, bias);
178
                super.moveDot(fb, dot, bias);
176
            }
179
            }
177
 
180
 
178
            @Override
181
            @Override
179
            public void setDot(FilterBypass fb, int dot, Bias bias) {
182
            public void setDot(FilterBypass fb, int dot, Bias bias) {
180
                if (dot == 2) {
183
                if (dot == 2) {
181
                    dot = 3;
184
                    dot = 3;
182
                }
185
                }
183
                super.setDot(fb, dot, bias);
186
                super.setDot(fb, dot, bias);
184
            }
187
            }
185
        });
188
        });
186
        doc.addDocumentListener(new DocumentListener() {
189
        doc.addDocumentListener(new DocumentListener() {
187
 
190
 
188
            @Override
191
            @Override
189
            public void removeUpdate(DocumentEvent e) {
192
            public void removeUpdate(DocumentEvent e) {
190
                changedUpdate(e);
193
                changedUpdate(e);
191
 
194
 
192
            }
195
            }
193
 
196
 
194
            @Override
197
            @Override
195
            public void insertUpdate(DocumentEvent e) {
198
            public void insertUpdate(DocumentEvent e) {
196
                changedUpdate(e);
199
                changedUpdate(e);
197
 
200
 
198
            }
201
            }
199
 
202
 
200
            @Override
203
            @Override
201
            public void changedUpdate(DocumentEvent e) {
204
            public void changedUpdate(DocumentEvent e) {
202
                if (getText().length() == 5) {
205
                if (getText().length() == 5) {
203
                    // Fire only on end of value update
206
                    // Fire only on end of value update
204
                    firePropertyChange("value", null, getText());
207
                    firePropertyChange("value", null, getText());
205
                }
208
                }
206
            }
209
            }
207
        });
210
        });
208
    }
211
    }
209
 
212
 
210
    public void setTime(int hour, int minute) {
213
    public void setTime(int hour, int minute) {
211
        if (hour < 0 || hour > 23) {
214
        if (hour < 0 || hour > 23) {
212
            throw new IllegalArgumentException("Hour must be betwen 0 and 23 but is " + hour);
215
            throw new IllegalArgumentException("Hour must be betwen 0 and 23 but is " + hour);
213
        }
216
        }
214
        if (minute < 0 || minute > 59) {
217
        if (minute < 0 || minute > 59) {
215
            throw new IllegalArgumentException("Minute must be betwen 0 and 59 but is " + minute);
218
            throw new IllegalArgumentException("Minute must be betwen 0 and 59 but is " + minute);
216
        }
219
        }
217
        String s = "";
220
        String s = "";
218
        if (hour < 10) {
221
        if (hour < 10) {
219
            s += "0";
222
            s += "0";
220
        }
223
        }
221
        s += hour + ":";
224
        s += hour + ":";
222
        if (minute < 10) {
225
        if (minute < 10) {
223
            s += "0";
226
            s += "0";
224
        }
227
        }
225
        s += minute;
228
        s += minute;
226
        setText(s);
229
        setText(s);
227
    }
230
    }
228
 
231
 
229
    public int getHours() {
232
    public int getHours() {
230
        final String s = getText().substring(0, 2);
233
        final String s = getText().substring(0, 2);
231
        return Integer.parseInt(s);
234
        return Integer.parseInt(s);
232
    }
235
    }
233
 
236
 
234
    public int getMinutes() {
237
    public int getMinutes() {
235
        final String s = getText().substring(3, 5);
238
        final String s = getText().substring(3, 5);
236
        return Integer.parseInt(s);
239
        return Integer.parseInt(s);
237
    }
240
    }
238
 
241
 
239
    protected String fill2Char(int i) {
242
    protected String fill2Char(int i) {
240
        if (i < 10) {
243
        if (i < 10) {
241
            return "0" + i;
244
            return "0" + i;
242
        }
245
        }
243
        return String.valueOf(i);
246
        return String.valueOf(i);
244
    }
247
    }
245
 
248
 
246
    @Override
249
    @Override
247
    public void setCaretPosition(int position) {
250
    public void setCaretPosition(int position) {
248
        if (position == 2) {
251
        if (position == 2) {
249
            position++;
252
            position++;
250
        }
253
        }
251
        super.setCaretPosition(position);
254
        super.setCaretPosition(position);
252
    }
255
    }
253
 
256
 
254
    public boolean isCharValid(char c, int position, char[] actualText) {
257
    public boolean isCharValid(char c, int position, char[] actualText) {
255
        switch (position) {
258
        switch (position) {
256
        case 0:
259
        case 0:
257
            if (c >= '0' && c <= '1')
260
            if (c >= '0' && c <= '1')
258
                return true;
261
                return true;
259
            char c1 = actualText[1];
262
            char c1 = actualText[1];
260
            if (c == '2' && c1 <= '3')
263
            if (c == '2' && c1 <= '3')
261
                return true;
264
                return true;
262
            break;
265
            break;
263
        case 1:
266
        case 1:
264
            if (c >= '0' && c <= '3')
267
            if (c >= '0' && c <= '3')
265
                return true;
268
                return true;
266
            char c0 = actualText[0];
269
            char c0 = actualText[0];
267
            if (c >= '4' && c <= '9' && (c0 == '0' || c0 == '1'))
270
            if (c >= '4' && c <= '9' && (c0 == '0' || c0 == '1'))
268
                return true;
271
                return true;
269
            break;
272
            break;
270
        case 2:
273
        case 2:
271
            if (c == ':')
274
            if (c == ':')
272
                return true;
275
                return true;
273
            break;
276
            break;
274
        case 3:
277
        case 3:
275
            if (c >= '0' && c <= '5')
278
            if (c >= '0' && c <= '5')
276
                return true;
279
                return true;
277
            break;
280
            break;
278
        case 4:
281
        case 4:
279
            if (c >= '0' && c <= '9')
282
            if (c >= '0' && c <= '9')
280
                return true;
283
                return true;
281
            break;
284
            break;
282
        }
285
        }
283
        return false;
286
        return false;
284
 
287
 
285
    }
288
    }
286
 
289
 
287
    public static void main(String[] args) {
290
    public static void main(String[] args) {
288
        final JFrame f = new JFrame();
291
        final JFrame f = new JFrame();
289
        final TimeTextField time = new TimeTextField(true);
292
        final TimeTextField time = new TimeTextField(true);
290
        time.setTime(10, 59);
293
        time.setTime(10, 59);
291
        f.setContentPane(time);
294
        f.setContentPane(time);
292
        f.pack();
295
        f.pack();
293
        f.setLocationRelativeTo(null);
296
        f.setLocationRelativeTo(null);
294
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
297
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
295
        f.setVisible(true);
298
        f.setVisible(true);
296
    }
299
    }
297
}
300
}