OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
149 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.ui;
15
 
16
import javax.swing.DefaultCellEditor;
17
import javax.swing.JTextField;
18
 
19
public class RangedIntegerTableCellEditor extends DefaultCellEditor {
20
    private int min, max;
21
 
22
    public RangedIntegerTableCellEditor(int min, int max) {
23
        super(new JTextField());
156 ilm 24
        ((JTextField) getComponent()).setBorder(null);
149 ilm 25
        this.min = min;
26
        this.max = max;
27
    }
28
 
29
    @Override
30
    public boolean stopCellEditing() {
31
        try {
32
            // try to get the value
33
            this.getCellEditorValue();
34
            return super.stopCellEditing();
35
        } catch (Exception ex) {
36
            return false;
37
        }
38
 
39
    }
40
 
41
    @Override
42
    public Object getCellEditorValue() {
43
        final String str = (String) super.getCellEditorValue();
44
        if (str == null || str.length() == 0) {
45
            return min;
46
        }
47
        try {
48
            int i = Integer.parseInt(str);
49
            if (i < min) {
50
                i = min;
51
            } else if (i > max) {
52
                i = max;
53
            }
54
            return i;
55
        } catch (Exception ex) {
56
            throw new IllegalStateException();
57
        }
58
 
59
    }
60
 
61
    public void setMax(int max) {
62
        this.max = max;
63
    }
64
 
65
    public void setMin(int min) {
66
        this.min = min;
67
    }
68
}