summaryrefslogtreecommitdiff
path: root/source/com/c2kernel/gui/tabs/outcome/form/field/DecimalEditField.java
blob: fabaed8e339e3bc7aa625940c31cbcd4074a146b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.c2kernel.gui.tabs.outcome.form.field;

import java.awt.Toolkit;
import java.math.BigDecimal;

import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import javax.swing.text.PlainDocument;

/**************************************************************************
 *
 * $Revision: 1.3 $
 * $Date: 2005/08/16 13:59:56 $
 *
 * Copyright (C) 2003 CERN - European Organization for Nuclear Research
 * All rights reserved.
 **************************************************************************/
public class DecimalEditField extends StringEditField {

    public DecimalEditField() {
        super();
        field.addFocusListener(this);
        field.setToolTipText("This field must contains a decimal number e.g. 3.14159265");
    }

    @Override
	public String getText() {
        return field.getText();
    }

    @Override
	public void setText(String text) {
        field.setText(text);
    }

    @Override
	public String getDefaultValue() {
        return "0.0";
    }

    @Override
	public JTextComponent makeTextField() {
        return new DecimalTextField();
    }

    private class DecimalTextField extends JTextField {

        public DecimalTextField() {
            super();
            setHorizontalAlignment(RIGHT);
        }
        @Override
		protected Document createDefaultModel() {
            return new Decimal();
        }
    }

    private class Decimal extends PlainDocument {

        BigDecimal currentVal = new BigDecimal(0.0);


        @Override
		public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {

            if (str == null || str.equals("")) {
                return;
            }

            String proposedResult = null;

            if (getLength() == 0) {
                proposedResult = str;
            } else {
                StringBuffer currentBuffer = new StringBuffer( this.getText(0, getLength()) );
                currentBuffer.insert(offs, str);
                proposedResult = currentBuffer.toString();
            }

            try {
                currentVal = parse(proposedResult);
                super.insertString(offs, str, a);
            } catch (Exception e) {
                Toolkit.getDefaultToolkit().beep();
            }

        }

        @Override
		public void remove(int offs, int len) throws BadLocationException {

            String currentText = this.getText(0, getLength());
            String beforeOffset = currentText.substring(0, offs);
            String afterOffset = currentText.substring(len + offs, currentText.length());
            String proposedResult = beforeOffset + afterOffset;

            if (proposedResult.length() == 0) { // empty is ok
                super.remove(offs, len);
                return;
            }
            try {
                currentVal = parse(proposedResult);
                super.remove(offs, len);
            } catch (Exception e) {
                Toolkit.getDefaultToolkit().beep();
            }

        }

        public BigDecimal parse(String proposedResult) throws NumberFormatException {

            BigDecimal value = new BigDecimal(0);
            if ( proposedResult.length() != 0) {
                value =  new BigDecimal(proposedResult);
            }
            return value;
        }
    }
}