OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev 174 Rev 182
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.
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.erp.core.common.ui;
14
 package org.openconcerto.erp.core.common.ui;
15
 
15
 
16
import org.openconcerto.sql.model.SQLField;
16
import org.openconcerto.sql.model.SQLField;
17
import org.openconcerto.sql.request.SQLRowItemView;
17
import org.openconcerto.sql.request.SQLRowItemView;
18
import org.openconcerto.sql.sqlobject.itemview.RowItemViewComponent;
18
import org.openconcerto.sql.sqlobject.itemview.RowItemViewComponent;
19
import org.openconcerto.ui.valuewrapper.ValueWrapper;
19
import org.openconcerto.ui.valuewrapper.ValueWrapper;
20
import org.openconcerto.utils.checks.ValidListener;
20
import org.openconcerto.utils.checks.ValidListener;
21
import org.openconcerto.utils.checks.ValidState;
21
import org.openconcerto.utils.checks.ValidState;
22
import org.openconcerto.utils.doc.Documented;
22
import org.openconcerto.utils.doc.Documented;
23
import org.openconcerto.utils.text.SimpleDocumentListener;
23
import org.openconcerto.utils.text.SimpleDocumentListener;
24
 
24
 
25
import java.awt.Font;
25
import java.awt.Font;
26
import java.awt.event.FocusAdapter;
26
import java.awt.event.FocusAdapter;
27
import java.awt.event.FocusEvent;
27
import java.awt.event.FocusEvent;
28
import java.awt.event.KeyAdapter;
28
import java.awt.event.KeyAdapter;
29
import java.awt.event.KeyEvent;
29
import java.awt.event.KeyEvent;
30
import java.awt.event.MouseAdapter;
30
import java.awt.event.MouseAdapter;
31
import java.awt.event.MouseEvent;
31
import java.awt.event.MouseEvent;
32
import java.beans.PropertyChangeListener;
32
import java.beans.PropertyChangeListener;
33
import java.beans.PropertyChangeSupport;
33
import java.beans.PropertyChangeSupport;
34
import java.math.BigDecimal;
34
import java.math.BigDecimal;
35
 
35
 
36
import javax.swing.JComponent;
36
import javax.swing.JComponent;
37
import javax.swing.JTextField;
37
import javax.swing.JTextField;
38
import javax.swing.SwingUtilities;
38
import javax.swing.SwingUtilities;
39
import javax.swing.event.DocumentEvent;
39
import javax.swing.event.DocumentEvent;
40
 
40
 
41
public class NumericTextField extends JTextField implements ValueWrapper<BigDecimal>, Documented, RowItemViewComponent {
41
public class NumericTextField extends JTextField implements ValueWrapper<BigDecimal>, Documented, RowItemViewComponent {
42
 
42
 
43
    private SQLField field;
43
    private SQLField field;
44
 
44
 
45
    private final PropertyChangeSupport supp;
45
    private final PropertyChangeSupport supp;
46
    // does this component just gained focus
46
    // does this component just gained focus
47
    private boolean gained;
47
    private boolean gained;
48
    private boolean mousePressed;
48
    private boolean mousePressed;
49
    // the content of this text field when it gained focus
49
    // the content of this text field when it gained focus
50
    private String initialText;
50
    private String initialText;
51
 
51
 
52
    public NumericTextField() {
52
    public NumericTextField() {
53
        this(15);
53
        this(15);
54
    }
54
    }
55
 
55
 
56
    public NumericTextField(int columns) {
56
    public NumericTextField(int columns) {
57
        super(columns);
57
        super(columns);
58
 
58
 
59
        this.supp = new PropertyChangeSupport(this);
59
        this.supp = new PropertyChangeSupport(this);
60
        this.gained = false;
60
        this.gained = false;
61
        this.getDocument().addDocumentListener(new SimpleDocumentListener() {
61
        this.getDocument().addDocumentListener(new SimpleDocumentListener() {
62
            public void update(DocumentEvent e) {
62
            public void update(DocumentEvent e) {
63
                NumericTextField.this.textModified();
63
                NumericTextField.this.textModified();
64
            }
64
            }
65
        });
65
        });
66
        this.init();
66
        this.init();
67
    }
67
    }
68
 
68
 
69
    /**
69
    /**
70
     * Methode appelée quand le texte est modifié
70
     * Methode appelée quand le texte est modifié
71
     */
71
     */
72
    protected void textModified() {
72
    protected void textModified() {
73
        this.supp.firePropertyChange("value", null, this.getValue());
73
        this.supp.firePropertyChange("value", null, this.getValue());
74
    }
74
    }
75
 
75
 
76
    @Override
76
    @Override
77
    public void init(SQLRowItemView v) {
77
    public void init(SQLRowItemView v) {
78
        this.field = v.getFields().get(0);
78
        this.field = v.getFields().get(0);
79
    }
79
    }
80
 
80
 
81
    private void init() {
81
    private void init() {
82
        // TODO use JFormattedTextField => conflit getValue()
82
        // TODO use JFormattedTextField => conflit getValue()
83
        // DefaultFormatterFactory NumberFormatter (getAllowsInvalid) NumberFormat
83
        // DefaultFormatterFactory NumberFormatter (getAllowsInvalid) NumberFormat
84
 
84
 
85
        addFilteringKeyListener(this);
85
        addFilteringKeyListener(this);
86
 
86
 
87
        // select all on focus gained
87
        // select all on focus gained
88
        // except if the user is selecting with the mouse
88
        // except if the user is selecting with the mouse
89
        this.addFocusListener(new FocusAdapter() {
89
        this.addFocusListener(new FocusAdapter() {
90
            public void focusGained(FocusEvent e) {
90
            public void focusGained(FocusEvent e) {
91
                NumericTextField.this.gained = true;
91
                NumericTextField.this.gained = true;
92
                NumericTextField.this.initialText = getText();
92
                NumericTextField.this.initialText = getText();
93
                if (!NumericTextField.this.mousePressed) {
93
                if (!NumericTextField.this.mousePressed) {
94
                    selectAll();
94
                    selectAll();
95
                }
95
                }
96
            }
96
            }
97
        });
97
        });
98
        this.addMouseListener(new MouseAdapter() {
98
        this.addMouseListener(new MouseAdapter() {
99
            public void mousePressed(MouseEvent e) {
99
            public void mousePressed(MouseEvent e) {
100
                NumericTextField.this.mousePressed = true;
100
                NumericTextField.this.mousePressed = true;
101
            }
101
            }
102
 
102
 
103
            public void mouseReleased(MouseEvent e) {
103
            public void mouseReleased(MouseEvent e) {
104
                // don't override the user selection
104
                // don't override the user selection
105
                if (NumericTextField.this.gained && getSelectedText() == null) {
105
                if (NumericTextField.this.gained && getSelectedText() == null) {
106
                    selectAll();
106
                    selectAll();
107
                }
107
                }
108
                // don't select all for each mouse released
108
                // don't select all for each mouse released
109
                NumericTextField.this.gained = false;
109
                NumericTextField.this.gained = false;
110
                NumericTextField.this.mousePressed = false;
110
                NumericTextField.this.mousePressed = false;
111
            }
111
            }
112
        });
112
        });
113
        this.addKeyListener(new KeyAdapter() {
113
        this.addKeyListener(new KeyAdapter() {
114
            public void keyTyped(KeyEvent keyEvent) {
114
            public void keyTyped(KeyEvent keyEvent) {
115
                // Sert a annuler une saisie
115
                // Sert a annuler une saisie
116
                if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) {
116
                if (keyEvent.getKeyChar() == KeyEvent.VK_ESCAPE) {
117
                    NumericTextField.this.setValue(NumericTextField.this.initialText);
117
                    NumericTextField.this.setValue(NumericTextField.this.initialText);
118
                    selectAll();
118
                    selectAll();
119
                }
119
                }
120
            }
120
            }
121
        });
121
        });
122
    }
122
    }
123
 
123
 
124
    public static void addFilteringKeyListener(final NumericTextField textField) {
124
    public static void addFilteringKeyListener(final NumericTextField textField) {
125
 
125
 
126
        textField.addKeyListener(new KeyAdapter() {
126
        textField.addKeyListener(new KeyAdapter() {
127
            public void keyTyped(java.awt.event.KeyEvent keyEvent) {
127
            public void keyTyped(java.awt.event.KeyEvent keyEvent) {
128
 
128
 
129
                final char keychar = keyEvent.getKeyChar();
129
                final char keychar = keyEvent.getKeyChar();
130
 
130
 
131
                if (keychar == KeyEvent.VK_BACK_SPACE) {
131
                if (keychar == KeyEvent.VK_BACK_SPACE) {
132
                    return;
132
                    return;
133
                }
133
                }
134
 
134
 
135
                // pas plus de 2 chiffres apres la virgule
135
                // pas plus de 2 chiffres apres la virgule
136
                int pointPosition = textField.getText().indexOf('.');
136
                int pointPosition = textField.getText().indexOf('.');
137
                if (Character.isDigit(keychar)) {
137
                if (Character.isDigit(keychar)) {
138
                    if (pointPosition > -1) {
138
                    if (pointPosition > -1) {
139
                        // System.err.println("Text Selected :: " + textField.getSelectedText());
139
                        // System.err.println("Text Selected :: " + textField.getSelectedText());
140
                        if (textField.getSelectedText() == null) {
140
                        if (textField.getSelectedText() == null) {
141
                            if (textField.getCaretPosition() <= pointPosition) {
141
                            if (textField.getCaretPosition() <= pointPosition) {
142
                                return;
142
                                return;
143
                            } else {
143
                            } else {
144
                                if (textField.getText().substring(pointPosition).length() <= 6) {
144
                                if (textField.getText().substring(pointPosition).length() <= 6) {
145
                                    return;
145
                                    return;
146
                                }
146
                                }
147
                            }
147
                            }
148
                        } else {
148
                        } else {
149
                            return;
149
                            return;
150
                        }
150
                        }
151
                    } else {
151
                    } else {
152
                        return;
152
                        return;
153
                    }
153
                    }
154
                }
154
                }
155
 
155
 
156
                if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0)
156
                if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0)
157
                    return;
157
                    return;
158
 
158
 
159
                if (textField.getCaretPosition() > 0 && textField.getCaretPosition() == textField.getText().length())
159
                if (textField.getCaretPosition() > 0 && textField.getCaretPosition() == textField.getText().length())
160
                    return;
160
                    return;
161
 
161
 
162
                keyEvent.consume();
162
                keyEvent.consume();
163
            }
163
            }
164
        });
164
        });
165
    }
165
    }
166
 
166
 
167
    @Override
167
    @Override
168
    public final void resetValue() {
168
    public final void resetValue() {
169
        this.setValue((BigDecimal) null);
169
        this.setValue((BigDecimal) null);
170
    }
170
    }
171
 
171
 
172
    @Override
172
    @Override
173
    public void setValue(BigDecimal val) {
173
    public void setValue(BigDecimal val) {
174
        this.setValue(val == null ? "" : val.toString());
174
        this.setValue(val == null ? "" : val.toString());
175
    }
175
    }
176
 
176
 
177
    private final void setValue(String val) {
177
    private final void setValue(String val) {
178
        if (!this.getText().equals(val))
178
        if (!this.getText().equals(val))
179
            this.setText(val);
179
            this.setText(val);
180
    }
180
    }
181
 
181
 
182
    public void setBold() {
182
    public void setBold() {
183
        this.setFont(getFont().deriveFont(Font.BOLD));
183
        this.setFont(getFont().deriveFont(Font.BOLD));
184
    }
184
    }
185
 
185
 
186
    @Override
186
    @Override
187
    public String toString() {
187
    public String toString() {
188
        return this.getClass().getSimpleName();
188
        return this.getClass().getSimpleName();
189
    }
189
    }
190
 
190
 
191
    @Override
191
    @Override
192
    public BigDecimal getValue() {
192
    public BigDecimal getValue() {
193
        if (this.getText().trim().length() == 0) {
193
        if (this.getText().trim().length() == 0) {
194
            return null;
194
            return BigDecimal.ZERO;
195
        } else {
195
        } else {
196
            try {
196
            try {
197
                return new BigDecimal(this.getText().trim());
197
                return new BigDecimal(this.getText().trim());
198
            } catch (NumberFormatException ex) {
198
            } catch (NumberFormatException ex) {
199
                SwingUtilities.invokeLater(new Runnable() {
199
                SwingUtilities.invokeLater(new Runnable() {
200
                    @Override
200
                    @Override
201
                    public void run() {
201
                    public void run() {
202
                        setText("0");
202
                        setText("0");
203
                    }
203
                    }
204
                });
204
                });
205
                return BigDecimal.ZERO;
205
                return BigDecimal.ZERO;
206
            }
206
            }
207
        }
207
        }
208
    }
208
    }
209
 
209
 
210
    @Override
210
    @Override
211
    public void addValueListener(PropertyChangeListener l) {
211
    public void addValueListener(PropertyChangeListener l) {
212
        this.supp.addPropertyChangeListener(l);
212
        this.supp.addPropertyChangeListener(l);
213
    }
213
    }
214
 
214
 
215
    @Override
215
    @Override
216
    public void rmValueListener(PropertyChangeListener l) {
216
    public void rmValueListener(PropertyChangeListener l) {
217
        this.supp.removePropertyChangeListener(l);
217
        this.supp.removePropertyChangeListener(l);
218
    }
218
    }
219
 
219
 
220
    public SQLField getField() {
220
    public SQLField getField() {
221
        return this.field;
221
        return this.field;
222
    }
222
    }
223
 
223
 
224
    @Override
224
    @Override
225
    public ValidState getValidState() {
225
    public ValidState getValidState() {
226
        // TODO
226
        // TODO
227
        // return "La valeur saisie n'est pas correcte";
227
        // return "La valeur saisie n'est pas correcte";
228
        return ValidState.getTrueInstance();
228
        return ValidState.getTrueInstance();
229
    }
229
    }
230
 
230
 
231
    @Override
231
    @Override
232
    public void addValidListener(ValidListener l) {
232
    public void addValidListener(ValidListener l) {
233
        // FIXME
233
        // FIXME
234
    }
234
    }
235
 
235
 
236
    @Override
236
    @Override
237
    public void removeValidListener(ValidListener l) {
237
    public void removeValidListener(ValidListener l) {
238
        // FIXME
238
        // FIXME
239
    }
239
    }
240
 
240
 
241
    @Override
241
    @Override
242
    public JComponent getComp() {
242
    public JComponent getComp() {
243
        return this;
243
        return this;
244
    }
244
    }
245
 
245
 
246
    public String getDocId() {
246
    public String getDocId() {
247
        return "";
247
        return "";
248
    }
248
    }
249
 
249
 
250
    public String getGenericDoc() {
250
    public String getGenericDoc() {
251
        return "";
251
        return "";
252
    }
252
    }
253
 
253
 
254
    public boolean onScreen() {
254
    public boolean onScreen() {
255
        return true;
255
        return true;
256
    }
256
    }
257
 
257
 
258
    public boolean isDocTransversable() {
258
    public boolean isDocTransversable() {
259
        return false;
259
        return false;
260
    }
260
    }
261
 
261
 
262
}
262
}