All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jaxx.runtime.swing.editor.cell.NumberCellEditor Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-1
Show newest version
/*
 * #%L
 * JAXX :: Widgets
 * %%
 * Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public 
 * License along with this program.  If not, see
 * .
 * #L%
 */
package jaxx.runtime.swing.editor.cell;

import java.awt.Component;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.TableCellEditor;
import jaxx.runtime.JAXXUtil;
import jaxx.runtime.swing.editor.NumberEditor;

/**
 * @author Sylvain Lletellier
 * @deprecated since 2.17, prefer use {@code org.nuiton.jaxx.widgets.editor.cell.NumberCellEditor}.
 */
@Deprecated
public class NumberCellEditor extends AbstractCellEditor
        implements TableCellEditor, FocusListener, AncestorListener {

    private static final long serialVersionUID = 1L;

    protected final NumberEditor numberEditor;

    /** constructor */
    public NumberCellEditor(Class type, boolean useSign) {
        numberEditor = new NumberEditor();
        numberEditor.getTextField().setHorizontalAlignment(SwingConstants.RIGHT);
        numberEditor.getTextField().setBorder(null);
        numberEditor.getTextField().addFocusListener(this);
        numberEditor.getTextField().addAncestorListener(this);

        numberEditor.setModelType(type);

        numberEditor.setUseSign(useSign);
        numberEditor.init();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value,
                                                 boolean isSelected, int row, int column) {

        E number = (E) value;
        numberEditor.setModel(number);
        numberEditor.setModelText(JAXXUtil.getStringValue(number));
        return numberEditor;
    }

    public NumberEditor getNumberEditor() {
        return numberEditor;
    }

    @Override
    public E getCellEditorValue() {
        return (E)numberEditor.getModel();
    }

    @Override
    public void focusGained(FocusEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                numberEditor.getTextField().requestFocus();
                numberEditor.getTextField().selectAll();
            }
        });
    }

    @Override
    public void focusLost(FocusEvent e) {
        // commenting the next line fixes http://www.nuiton.org/issues/3517 and http://www.nuiton.org/issues/3518
//        cancelCellEditing();
    }

    @Override
    public void ancestorAdded(AncestorEvent event) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                numberEditor.getTextField().requestFocus();
                numberEditor.getTextField().selectAll();
            }
        });
    }

    @Override
    public void ancestorRemoved(AncestorEvent event) {
    }

    @Override
    public void ancestorMoved(AncestorEvent event) {
    }

    @Override
    public boolean stopCellEditing() {
        boolean result = super.stopCellEditing();
        // Reset previous data to avoid keeping it on other cell edition
        if (result) {
            numberEditor.setModel(null);
            // Use empty string, otherwise there is a NPE in NumberEditorHandler
            numberEditor.setModelText("");
            // force binding, I do not know why the textfield text is not emptied
            // if we do not force it
            numberEditor.applyDataBinding(NumberEditor.BINDING_TEXT_FIELD_TEXT);
        }
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy