OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Details | Compare with Previous | Last modification | View Log | RSS feed

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