Package ro.emilianbold.javaeditor.blinkrate.option

Source Code of ro.emilianbold.javaeditor.blinkrate.option.BlinkratePanel$IntegerEditor

package ro.emilianbold.javaeditor.blinkrate.option;

import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.DocumentFilter.FilterBypass;
import ro.emilianbold.javaeditor.blinkrate.BlinkUtil;

final class BlinkratePanel extends javax.swing.JPanel {

    private final BlinkrateOptionsPanelController controller;

    BlinkratePanel(BlinkrateOptionsPanelController controller) {
        this.controller = controller;
        initComponents();

        // only positive values, and step of 50 milli-sec
        SpinnerNumberModel m = (SpinnerNumberModel)spinner.getModel();
        m.setMinimum(Integer.valueOf(0));
        m.setStepSize(Integer.valueOf(50));

        // This editor rejects non-digit characters
        // and persists itself to the model as valid characters are input.
        spinner.setEditor(new IntegerEditor());

        spinner.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent arg0) {
                BlinkratePanel.this.controller.changed();
            }
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        spinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();

        org.openide.awt.Mnemonics.setLocalizedText(jLabel1, "Blink rate in miliseconds");

        jLabel2.setFont(jLabel2.getFont().deriveFont(jLabel2.getFont().getSize()-1f));
        jLabel2.setForeground(new java.awt.Color(255, 0, 51));
        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(jLabel2, "* A value of 0 (zero) means no blink at all.");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 254, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(spinner, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(spinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .addContainerGap(21, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    void load() {
        ((JTextField)spinner.getEditor()).setText("0");
        spinner.setValue(BlinkUtil.getBlink());
    }

    void store() {
        BlinkUtil.setBlink((Integer) spinner.getValue());
    }

    boolean valid() {
        return ((Integer) spinner.getValue() >= 0);
    }

    private class IntegerEditor extends JTextField {

        @SuppressWarnings({"OverridableMethodCallInConstructor",
                           "LeakingThisInConstructor"})
        public IntegerEditor() {
            setHorizontalAlignment(JTextField.RIGHT);
            updateText();

            String toolTipText = spinner.getToolTipText();
            if (toolTipText != null) {
                //JSpinner has tool tip text.  Use it.
                if (!toolTipText.equals(getToolTipText())) {
                    setToolTipText(toolTipText);
                }
            }

            spinner.addChangeListener(new ChangeListener() {
                public void stateChanged(ChangeEvent e)
                {
                    updateText();
                }
            });

            // Only 0-9 allowed in string
            ((AbstractDocument)getDocument())
                    .setDocumentFilter(new DocumentFilter() {

                private boolean digitsOnly(String s) {
                    for(int i = 0; i < s.length(); i++) {
                        char c = s.charAt(i);
                        if(c < '0' || c > '9')
                            return false;
                    }
                    return true;
                }

                @Override
                public void insertString(FilterBypass fb, int offset,
                                         String string, AttributeSet attr)
                        throws BadLocationException
                {
                    if(digitsOnly(string)) {
                        fb.insertString(offset, string, attr);
                        updateModel();
                    }
                }

                @Override
                public void replace(FilterBypass fb, int offset, int length,
                                    String text, AttributeSet attrs)
                        throws BadLocationException
                {
                    if(digitsOnly(text)) {
                        fb.replace(offset, length, text, attrs);
                        updateModel();
                    }
                }

                @Override
                public void remove(FilterBypass fb, int offset, int length)
                        throws BadLocationException
                {
                    fb.remove(offset, length);
                    updateModel();
                }
            });
        }

        private int getValue() {
            String s = getText();
            int n = -1;
            if(!s.isEmpty()) {
                try {
                    n = Integer.parseInt(s);
                } catch(NumberFormatException ex) {
                }
            } else {
                n = 0;
            }
            return n;
        }

        private void updateModel() {
            int n = getValue();
            if(n >= 0) {
                if(n != (Integer)spinner.getValue())
                    spinner.setValue(n);
            } else
                updateText();
        }

        private void updateText() {
            int n = (Integer)spinner.getValue();
            if(n != getValue())
                setText("" + n);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSpinner spinner;
    // End of variables declaration//GEN-END:variables

}
TOP

Related Classes of ro.emilianbold.javaeditor.blinkrate.option.BlinkratePanel$IntegerEditor

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.