Package jmandelbrotviewer.util

Source Code of jmandelbrotviewer.util.NumberEditorFormatter

package jmandelbrotviewer.util;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import javax.swing.JFormattedTextField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;


class NumberEditorFormatter extends NumberFormatter {
    private final SpinnerBigDecimalModel model;

    NumberEditorFormatter(SpinnerBigDecimalModel model, NumberFormat format) {
        super(format);
        this.model = model;
        setValueClass(model.getValue().getClass());
    }

    @SuppressWarnings("unchecked")
    @Override
    public void setMinimum(Comparable min) {
        model.setMinimum(min);
    }

    @Override
    public Comparable<? super BigDecimal> getMinimum() {
        return model.getMinimum();
    }

    @SuppressWarnings("unchecked")
    @Override
    public void setMaximum(Comparable max) {
        model.setMaximum(max);
    }

    @Override
    public Comparable getMaximum() {
        return model.getMaximum();
    }
}


/**
*
* @author Laszlo Zsolt Kustra <kl223 AT ludens.elte.hu>
* @author Hans Muller
* @author Lynn Monsanto (accessibility)
*/
public class SpinnerBigDecimalEditor extends JSpinner.DefaultEditor
{
    public SpinnerBigDecimalEditor(JSpinner spinner, String decimalFormatPattern) {
        this(spinner, new DecimalFormat(decimalFormatPattern));
    }

    private SpinnerBigDecimalEditor(JSpinner spinner, DecimalFormat format) {
        super(spinner);
        if (!(spinner.getModel() instanceof SpinnerBigDecimalModel)) {
            throw new IllegalArgumentException(
                      "model not a SpinnerBigNumberModel");
        }

        SpinnerBigDecimalModel model = (SpinnerBigDecimalModel)spinner.getModel();
        NumberFormatter formatter = new NumberEditorFormatter(model, format);
        DefaultFormatterFactory factory = new DefaultFormatterFactory(formatter);
        JFormattedTextField ftf = getTextField();
        ftf.setEditable(true);
        ftf.setFormatterFactory(factory);
        ftf.setHorizontalAlignment(JTextField.RIGHT);

        /* TBD - initializing the column width of the text field
         * is imprecise and doing it here is tricky because
         * the developer may configure the formatter later.
         */
        try {
            String maxString = formatter.valueToString(model.getMinimum());
            String minString = formatter.valueToString(model.getMaximum());
            ftf.setColumns(Math.max(maxString.length(), minString.length()));
        }
        catch (ParseException e) {
            // TBD should throw a chained error here
        }

    }

    public DecimalFormat getFormat() {
        return (DecimalFormat)((NumberFormatter)(getTextField().getFormatter())).getFormat();
    }

    public SpinnerNumberModel getModel() {
        return (SpinnerNumberModel)(getSpinner().getModel());
    }
}
TOP

Related Classes of jmandelbrotviewer.util.NumberEditorFormatter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.