67 |
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;
|
80 |
ilm |
17 |
import org.openconcerto.utils.StringUtils;
|
67 |
ilm |
18 |
|
|
|
19 |
import java.awt.Color;
|
|
|
20 |
import java.awt.Component;
|
|
|
21 |
import java.awt.event.FocusAdapter;
|
|
|
22 |
import java.awt.event.FocusEvent;
|
|
|
23 |
import java.awt.event.KeyAdapter;
|
|
|
24 |
import java.awt.event.KeyEvent;
|
|
|
25 |
import java.awt.event.KeyListener;
|
|
|
26 |
import java.awt.event.MouseEvent;
|
|
|
27 |
import java.math.BigDecimal;
|
73 |
ilm |
28 |
import java.text.DecimalFormat;
|
|
|
29 |
import java.text.DecimalFormatSymbols;
|
67 |
ilm |
30 |
import java.util.EventObject;
|
|
|
31 |
|
|
|
32 |
import javax.swing.AbstractCellEditor;
|
|
|
33 |
import javax.swing.JTable;
|
|
|
34 |
import javax.swing.JTextField;
|
|
|
35 |
import javax.swing.border.LineBorder;
|
|
|
36 |
import javax.swing.table.TableCellEditor;
|
|
|
37 |
|
|
|
38 |
/**
|
|
|
39 |
* Editeur de devise On saisi un nombre avec 2 decimal representant une valeur en € et retourne un
|
|
|
40 |
* long representant des cents
|
|
|
41 |
*
|
|
|
42 |
*/
|
73 |
ilm |
43 |
public class DeviseNumericCellEditor extends AbstractCellEditor implements TableCellEditor {
|
|
|
44 |
protected JTextField textField = new JTextField();
|
67 |
ilm |
45 |
|
73 |
ilm |
46 |
protected int precision;
|
|
|
47 |
protected final DecimalFormat decimalFormat = new DecimalFormat("##,##0.00#######");
|
|
|
48 |
|
67 |
ilm |
49 |
public DeviseNumericCellEditor(SQLField field) {
|
73 |
ilm |
50 |
final DecimalFormatSymbols symbol = DecimalFormatSymbols.getInstance();
|
|
|
51 |
symbol.setDecimalSeparator('.');
|
|
|
52 |
decimalFormat.setDecimalFormatSymbols(symbol);
|
|
|
53 |
|
67 |
ilm |
54 |
// Mimic JTable.GenericEditor behavior
|
|
|
55 |
this.textField.setBorder(new LineBorder(Color.black));
|
|
|
56 |
this.textField.setHorizontalAlignment(JTextField.RIGHT);
|
|
|
57 |
this.precision = field.getType().getDecimalDigits();
|
|
|
58 |
// On ne peut saisir qu'un chiffre à 2 décimales
|
|
|
59 |
textField.addKeyListener(new KeyAdapter() {
|
|
|
60 |
public void keyTyped(java.awt.event.KeyEvent keyEvent) {
|
|
|
61 |
|
|
|
62 |
final char keychar = keyEvent.getKeyChar();
|
|
|
63 |
|
|
|
64 |
if (keychar == KeyEvent.VK_BACK_SPACE) {
|
|
|
65 |
return;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
int pointPosition = textField.getText().indexOf('.');
|
|
|
69 |
if (Character.isDigit(keychar)) {
|
|
|
70 |
if (pointPosition > -1) {
|
|
|
71 |
if (textField.getSelectedText() == null) {
|
|
|
72 |
if (textField.getCaretPosition() <= pointPosition) {
|
|
|
73 |
return;
|
|
|
74 |
} else {
|
|
|
75 |
if (textField.getText().substring(pointPosition).length() <= precision) {
|
|
|
76 |
return;
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
} else {
|
|
|
80 |
return;
|
|
|
81 |
}
|
|
|
82 |
} else {
|
|
|
83 |
return;
|
|
|
84 |
}
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0)
|
|
|
88 |
return;
|
|
|
89 |
if (keychar == KeyEvent.VK_MINUS && (textField.getText().indexOf('-') < 0) && textField.getCaretPosition() == 0)
|
|
|
90 |
return;
|
|
|
91 |
|
|
|
92 |
keyEvent.consume();
|
|
|
93 |
}
|
|
|
94 |
});
|
|
|
95 |
|
|
|
96 |
// on sélectionne tout lors de la selection
|
|
|
97 |
textField.addFocusListener(new FocusAdapter() {
|
|
|
98 |
public void focusGained(FocusEvent e) {
|
73 |
ilm |
99 |
// Force lineborder to black (may be red if a wrong value was previously hit)
|
|
|
100 |
textField.setBorder(new LineBorder(Color.black));
|
67 |
ilm |
101 |
textField.selectAll();
|
|
|
102 |
}
|
|
|
103 |
});
|
|
|
104 |
}
|
|
|
105 |
|
73 |
ilm |
106 |
@Override
|
|
|
107 |
public boolean stopCellEditing() {
|
|
|
108 |
// Check if the value is correct (may be wrong if we use copy/paste)
|
|
|
109 |
try {
|
|
|
110 |
getCellEditorValue();
|
|
|
111 |
} catch (Exception e) {
|
80 |
ilm |
112 |
e.printStackTrace();
|
73 |
ilm |
113 |
this.textField.setBorder(new LineBorder(Color.RED));
|
|
|
114 |
return false;
|
|
|
115 |
}
|
|
|
116 |
return super.stopCellEditing();
|
|
|
117 |
}
|
|
|
118 |
|
67 |
ilm |
119 |
public void addKeyListener(KeyListener l) {
|
|
|
120 |
this.textField.addKeyListener(l);
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public boolean isCellEditable(EventObject e) {
|
|
|
124 |
if (e instanceof MouseEvent) {
|
|
|
125 |
return ((MouseEvent) e).getClickCount() >= 2;
|
|
|
126 |
}
|
|
|
127 |
return super.isCellEditable(e);
|
|
|
128 |
}
|
|
|
129 |
|
|
|
130 |
public Object getCellEditorValue() {
|
80 |
ilm |
131 |
final String text = StringUtils.removeNonDecimalChars(this.textField.getText());
|
|
|
132 |
if (text.length() > 0) {
|
|
|
133 |
return new BigDecimal(text);
|
67 |
ilm |
134 |
} else {
|
|
|
135 |
return BigDecimal.ZERO;
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
80 |
ilm |
140 |
this.textField.setText((value == null ? "0" : this.decimalFormat.format((BigDecimal) value)));
|
67 |
ilm |
141 |
this.textField.selectAll();
|
|
|
142 |
this.textField.grabFocus();
|
|
|
143 |
return this.textField;
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
}
|