OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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