PageRenderTime 324ms CodeModel.GetById 141ms app.highlight 8ms RepoModel.GetById 173ms app.codeStats 0ms

/src/mpv5/utils/renderer/TableCellEditorForDezimal.java

http://mp-rechnungs-und-kundenverwaltung.googlecode.com/
Java | 122 lines | 75 code | 12 blank | 35 comment | 15 complexity | 108687b4d44544819800d2d509c9f2d1 MD5 | raw file
  1/*
  2 * 
  3This file is part of YaBS.
  4
  5YaBS is free software: you can redistribute it and/or modify
  6it under the terms of the GNU General Public License as published by
  7the Free Software Foundation, either version 3 of the License, or
  8(at your option) any later version.
  9
 10YaBS is distributed in the hope that it will be useful,
 11but WITHOUT ANY WARRANTY; without even the implied warranty of
 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13GNU General Public License for more details.
 14
 15You should have received a copy of the GNU General Public License
 16along with YaBS.  If not, see <http://www.gnu.org/licenses/>.
 17
 18 * 
 19 */
 20package mpv5.utils.renderer;
 21
 22import java.awt.event.MouseEvent;
 23import java.math.BigDecimal;
 24import java.text.NumberFormat;
 25import java.util.EventObject;
 26import javax.swing.JFormattedTextField;
 27import javax.swing.SwingConstants;
 28import javax.swing.SwingUtilities;
 29import mpv5.logging.Log;
 30import mpv5.utils.numberformat.FormatNumber;
 31
 32/**
 33 *
 34 * 
 35 */
 36public class TableCellEditorForDezimal extends LazyCellEditor {
 37
 38    private static final long serialVersionUID = 1L;
 39
 40    /**
 41     * 
 42     * @param tf
 43     */
 44    public TableCellEditorForDezimal(final JFormattedTextField tf) {
 45        this(tf, null);
 46    }
 47
 48    /**
 49     *
 50     * @param tf
 51     * @param format  
 52     */
 53    public TableCellEditorForDezimal(final JFormattedTextField tf, final NumberFormat format) {
 54        super(tf);
 55        super.setClickCountToStart(1);
 56        tf.setFocusLostBehavior(JFormattedTextField.COMMIT);
 57        tf.setHorizontalAlignment(SwingConstants.LEFT);
 58        tf.setBorder(null);
 59        delegate = new EditorDelegate() {
 60
 61            boolean isMousePressed = false;
 62
 63            @Override
 64            public void setValue(Object param) {
 65                if (isMousePressed
 66                        && param != null && (param.getClass() == Double.class || param.getClass() == BigDecimal.class)) {
 67                    SwingUtilities.invokeLater(new Runnable() {
 68
 69                        public void run() {
 70                            tf.selectAll();
 71                        }
 72                    });
 73                    try {
 74                        tf.setText(format == null ? FormatNumber.formatDezimal((Number) param) : format.format((Number) param));
 75                    } catch (Exception e) {
 76                        try {
 77                            param = new BigDecimal(String.valueOf(param));
 78                            tf.setText(format == null ? FormatNumber.formatDezimal((Number) param) : format.format((Number) param));
 79                        } catch (Exception ex) {
 80                            tf.setText(String.valueOf(param));
 81                        }
 82                    }
 83                } else {
 84                    tf.setText("");
 85                }
 86
 87            }
 88
 89            @Override
 90            public Object getCellEditorValue() {
 91                try {
 92                    String _field = tf.getText();
 93                    Number _number = (format == null ? FormatNumber.parseDezimal(_field) : format.parse(_field));
 94
 95                    if (_number != null) {
 96                        double _parsed = _number.doubleValue();
 97                        Double d = new Double(_parsed);
 98//                        tf.setBackground(Color.white);
 99                        return d;
100                    } else {
101//                        tf.setBackground(Color.white);
102                        return new Double(0.0);
103                    }
104                } catch (Exception e) {
105                    Log.Debug(this, e.getMessage());
106//                    tf.setBackground(Color.red);
107                    return new Double(0.0);
108                }
109            }
110
111            @Override
112            public boolean isCellEditable(EventObject anEvent) {
113                if (anEvent instanceof MouseEvent) {
114                    isMousePressed = true;
115                    return ((MouseEvent) anEvent).getClickCount() >= clickCountToStart;
116                }
117                isMousePressed = false;
118                return true;
119            }
120        };
121    }
122}