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;
|
|
|
17 |
|
|
|
18 |
import java.awt.Color;
|
|
|
19 |
import java.awt.Component;
|
|
|
20 |
import java.awt.event.ActionEvent;
|
|
|
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.awt.event.MouseListener;
|
|
|
28 |
import java.math.BigDecimal;
|
|
|
29 |
import java.math.MathContext;
|
|
|
30 |
import java.math.RoundingMode;
|
|
|
31 |
import java.util.EventObject;
|
|
|
32 |
|
|
|
33 |
import javax.swing.AbstractAction;
|
|
|
34 |
import javax.swing.AbstractCellEditor;
|
|
|
35 |
import javax.swing.JPopupMenu;
|
|
|
36 |
import javax.swing.JTable;
|
|
|
37 |
import javax.swing.JTextField;
|
|
|
38 |
import javax.swing.border.LineBorder;
|
|
|
39 |
import javax.swing.table.TableCellEditor;
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* Editeur de devise On saisi un nombre avec 2 decimal representant une valeur en € et retourne un
|
|
|
43 |
* long representant des cents
|
|
|
44 |
*
|
|
|
45 |
*/
|
|
|
46 |
public class DeviseNumericCellEditor extends AbstractCellEditor implements TableCellEditor, MouseListener {
|
|
|
47 |
private JTextField textField = new JTextField();
|
|
|
48 |
private float taxe = 19.6F;
|
|
|
49 |
private int precision;
|
|
|
50 |
|
|
|
51 |
public DeviseNumericCellEditor(SQLField field) {
|
|
|
52 |
// Mimic JTable.GenericEditor behavior
|
|
|
53 |
this.textField.setBorder(new LineBorder(Color.black));
|
|
|
54 |
this.textField.setHorizontalAlignment(JTextField.RIGHT);
|
|
|
55 |
this.precision = field.getType().getDecimalDigits();
|
|
|
56 |
// On ne peut saisir qu'un chiffre à 2 décimales
|
|
|
57 |
textField.addKeyListener(new KeyAdapter() {
|
|
|
58 |
public void keyTyped(java.awt.event.KeyEvent keyEvent) {
|
|
|
59 |
|
|
|
60 |
final char keychar = keyEvent.getKeyChar();
|
|
|
61 |
|
|
|
62 |
if (keychar == KeyEvent.VK_BACK_SPACE) {
|
|
|
63 |
return;
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
int pointPosition = textField.getText().indexOf('.');
|
|
|
67 |
if (Character.isDigit(keychar)) {
|
|
|
68 |
if (pointPosition > -1) {
|
|
|
69 |
// System.err.println("Text Selected :: " + textField.getSelectedText());
|
|
|
70 |
if (textField.getSelectedText() == null) {
|
|
|
71 |
if (textField.getCaretPosition() <= pointPosition) {
|
|
|
72 |
return;
|
|
|
73 |
} else {
|
|
|
74 |
if (textField.getText().substring(pointPosition).length() <= precision) {
|
|
|
75 |
return;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
} else {
|
|
|
79 |
return;
|
|
|
80 |
}
|
|
|
81 |
} else {
|
|
|
82 |
return;
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0)
|
|
|
87 |
return;
|
|
|
88 |
if (keychar == KeyEvent.VK_MINUS && (textField.getText().indexOf('-') < 0) && textField.getCaretPosition() == 0)
|
|
|
89 |
return;
|
|
|
90 |
|
|
|
91 |
keyEvent.consume();
|
|
|
92 |
}
|
|
|
93 |
});
|
|
|
94 |
|
|
|
95 |
// on sélectionne tout lors de la selection
|
|
|
96 |
textField.addFocusListener(new FocusAdapter() {
|
|
|
97 |
public void focusGained(FocusEvent e) {
|
|
|
98 |
|
|
|
99 |
textField.selectAll();
|
|
|
100 |
}
|
|
|
101 |
});
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
public void addKeyListener(KeyListener l) {
|
|
|
105 |
this.textField.addKeyListener(l);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public boolean isCellEditable(EventObject e) {
|
|
|
109 |
|
|
|
110 |
if (e instanceof MouseEvent) {
|
|
|
111 |
return ((MouseEvent) e).getClickCount() >= 2;
|
|
|
112 |
}
|
|
|
113 |
return super.isCellEditable(e);
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
public Object getCellEditorValue() {
|
|
|
117 |
|
|
|
118 |
if (this.textField.getText().trim().length() > 0) {
|
|
|
119 |
return new BigDecimal(this.textField.getText());
|
|
|
120 |
} else {
|
|
|
121 |
return BigDecimal.ZERO;
|
|
|
122 |
}
|
|
|
123 |
}
|
|
|
124 |
|
|
|
125 |
public void setConvertToTTCEnable(boolean b) {
|
|
|
126 |
if (b) {
|
|
|
127 |
this.textField.addMouseListener(this);
|
|
|
128 |
} else {
|
|
|
129 |
this.textField.removeMouseListener(this);
|
|
|
130 |
}
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
public void setTaxe(float d) {
|
|
|
134 |
this.taxe = d;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
|
|
|
138 |
|
|
|
139 |
// System.err.println("Devise cell editor get Component " + value);
|
|
|
140 |
|
|
|
141 |
this.textField.setText((value == null ? "0" : ((BigDecimal) value).toString()));
|
|
|
142 |
this.textField.selectAll();
|
|
|
143 |
this.textField.grabFocus();
|
|
|
144 |
return this.textField;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
public void mouseClicked(MouseEvent e) {
|
|
|
148 |
// TODO Auto-generated method stub
|
|
|
149 |
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
public void mouseEntered(MouseEvent e) {
|
|
|
153 |
// TODO Auto-generated method stub
|
|
|
154 |
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
public void mouseExited(MouseEvent e) {
|
|
|
158 |
// TODO Auto-generated method stub
|
|
|
159 |
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
public void mousePressed(MouseEvent e) {
|
|
|
163 |
// TODO Auto-generated method stub
|
|
|
164 |
if (textField.getText().trim().length() > 0 && e.getButton() == MouseEvent.BUTTON3) {
|
|
|
165 |
JPopupMenu menuDroit = new JPopupMenu();
|
|
|
166 |
menuDroit.add(new AbstractAction("Convertir en HT (TVA " + taxe + ")") {
|
|
|
167 |
|
|
|
168 |
public void actionPerformed(ActionEvent e) {
|
|
|
169 |
|
|
|
170 |
String s = textField.getText().trim();
|
|
|
171 |
if (s.length() > 0) {
|
|
|
172 |
BigDecimal taux = new BigDecimal(taxe).movePointLeft(2).add(BigDecimal.ONE);
|
|
|
173 |
BigDecimal prixTTC = new BigDecimal(s);
|
|
|
174 |
BigDecimal divide = prixTTC.divide(taux, MathContext.DECIMAL128);
|
|
|
175 |
divide = divide.setScale(precision, RoundingMode.HALF_UP);
|
|
|
176 |
textField.setText(divide.toString());
|
|
|
177 |
}
|
|
|
178 |
}
|
|
|
179 |
});
|
|
|
180 |
menuDroit.pack();
|
|
|
181 |
menuDroit.show(e.getComponent(), e.getPoint().x, e.getPoint().y);
|
|
|
182 |
menuDroit.setVisible(true);
|
|
|
183 |
}
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
public void mouseReleased(MouseEvent e) {
|
|
|
187 |
// TODO Auto-generated method stub
|
|
|
188 |
|
|
|
189 |
}
|
|
|
190 |
}
|