OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
67 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
67 ilm 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;
83 ilm 47
    protected final DecimalFormat decimalFormat = new DecimalFormat("0.00#######");
73 ilm 48
 
67 ilm 49
    public DeviseNumericCellEditor(SQLField field) {
182 ilm 50
        this(field.getType().getDecimalDigits());
51
    }
52
 
53
    public DeviseNumericCellEditor(int precision) {
73 ilm 54
        final DecimalFormatSymbols symbol = DecimalFormatSymbols.getInstance();
55
        symbol.setDecimalSeparator('.');
56
        decimalFormat.setDecimalFormatSymbols(symbol);
57
 
67 ilm 58
        // Mimic JTable.GenericEditor behavior
59
        this.textField.setBorder(new LineBorder(Color.black));
60
        this.textField.setHorizontalAlignment(JTextField.RIGHT);
182 ilm 61
        this.precision = precision;
67 ilm 62
        // On ne peut saisir qu'un chiffre à 2 décimales
63
        textField.addKeyListener(new KeyAdapter() {
64
            public void keyTyped(java.awt.event.KeyEvent keyEvent) {
65
 
66
                final char keychar = keyEvent.getKeyChar();
67
 
68
                if (keychar == KeyEvent.VK_BACK_SPACE) {
69
                    return;
70
                }
71
 
72
                int pointPosition = textField.getText().indexOf('.');
73
                if (Character.isDigit(keychar)) {
74
                    if (pointPosition > -1) {
75
                        if (textField.getSelectedText() == null) {
76
                            if (textField.getCaretPosition() <= pointPosition) {
77
                                return;
78
                            } else {
79
                                if (textField.getText().substring(pointPosition).length() <= precision) {
80
                                    return;
81
                                }
82
                            }
83
                        } else {
84
                            return;
85
                        }
86
                    } else {
87
                        return;
88
                    }
89
                }
90
 
91
                if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0)
92
                    return;
93
                if (keychar == KeyEvent.VK_MINUS && (textField.getText().indexOf('-') < 0) && textField.getCaretPosition() == 0)
94
                    return;
95
 
96
                keyEvent.consume();
97
            }
98
        });
99
 
100
        // on sélectionne tout lors de la selection
101
        textField.addFocusListener(new FocusAdapter() {
102
            public void focusGained(FocusEvent e) {
73 ilm 103
                // Force lineborder to black (may be red if a wrong value was previously hit)
104
                textField.setBorder(new LineBorder(Color.black));
67 ilm 105
                textField.selectAll();
106
            }
107
        });
108
    }
109
 
73 ilm 110
    @Override
111
    public boolean stopCellEditing() {
112
        // Check if the value is correct (may be wrong if we use copy/paste)
113
        try {
114
            getCellEditorValue();
115
        } catch (Exception e) {
80 ilm 116
            e.printStackTrace();
73 ilm 117
            this.textField.setBorder(new LineBorder(Color.RED));
118
            return false;
119
        }
120
        return super.stopCellEditing();
121
    }
122
 
67 ilm 123
    public void addKeyListener(KeyListener l) {
124
        this.textField.addKeyListener(l);
125
    }
126
 
127
    public boolean isCellEditable(EventObject e) {
128
        if (e instanceof MouseEvent) {
129
            return ((MouseEvent) e).getClickCount() >= 2;
130
        }
131
        return super.isCellEditable(e);
132
    }
133
 
134
    public Object getCellEditorValue() {
83 ilm 135
        return StringUtils.getBigDecimalFromUserText(this.textField.getText());
67 ilm 136
    }
137
 
138
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
80 ilm 139
        this.textField.setText((value == null ? "0" : this.decimalFormat.format((BigDecimal) value)));
67 ilm 140
        this.textField.selectAll();
141
        this.textField.grabFocus();
142
        return this.textField;
143
    }
144
 
145
}