Search in sources :

Example 1 with AbstractFormatter

use of javax.swing.JFormattedTextField.AbstractFormatter in project knime-core by knime.

the class DomainDialog method createNumericDomainPanel.

private JPanel createNumericDomainPanel() {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Domain values"));
    // Integer column domain panel
    Box nomBox = Box.createHorizontalBox();
    JPanel up = new JPanel();
    up.setLayout(new BoxLayout(up, BoxLayout.X_AXIS));
    JLabel upperBoundLabel = new JLabel("Upper Bound: ");
    up.add(upperBoundLabel);
    m_upperBoundField = new JFormattedTextField(new AbstractFormatter() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Object stringToValue(final String text) throws ParseException {
            try {
                if (m_colProp.getColumnSpec().getType().equals(DoubleCell.TYPE)) {
                    return Double.parseDouble(text);
                }
                if (m_colProp.getColumnSpec().getType().equals(IntCell.TYPE)) {
                    return Integer.parseInt(text);
                }
                // should not happen
                throw new NumberFormatException("Impossible column type.");
            } catch (NumberFormatException nfe) {
                throw new ParseException("Contains non-numeric chars", 0);
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String valueToString(final Object value) throws ParseException {
            return value == null ? null : value.toString();
        }
    });
    m_upperBoundField.setColumns(8);
    up.add(m_upperBoundField);
    JPanel low = new JPanel();
    low.setLayout(new BoxLayout(low, BoxLayout.X_AXIS));
    JLabel lowerBoundLabel = new JLabel("Lower Bound: ");
    low.add(lowerBoundLabel);
    m_lowerBoundField = new JFormattedTextField(new AbstractFormatter() {

        /**
         * {@inheritDoc}
         */
        @Override
        public Object stringToValue(final String text) throws ParseException {
            try {
                if (m_colProp.getColumnSpec().getType().equals(DoubleCell.TYPE)) {
                    return Double.parseDouble(text);
                }
                if (m_colProp.getColumnSpec().getType().equals(IntCell.TYPE)) {
                    return Integer.parseInt(text);
                }
                // should not happen
                throw new NumberFormatException("Impossible column type.");
            } catch (NumberFormatException nfe) {
                throw new ParseException("Contains non-numeric chars", 0);
            }
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public String valueToString(final Object value) throws ParseException {
            return value == null ? null : value.toString();
        }
    });
    m_lowerBoundField.setColumns(8);
    low.add(m_lowerBoundField);
    if (m_colProp.getColumnSpec().getDomain().getUpperBound() != null) {
        if (m_colProp.getColumnSpec().getType().equals(IntCell.TYPE)) {
            m_upperBoundField.setValue(((IntValue) m_colProp.getColumnSpec().getDomain().getUpperBound()).getIntValue());
            m_lowerBoundField.setValue(((IntValue) m_colProp.getColumnSpec().getDomain().getLowerBound()).getIntValue());
        }
    }
    if (m_colProp.getColumnSpec().getDomain().getLowerBound() != null) {
        if (m_colProp.getColumnSpec().getType().equals(DoubleCell.TYPE)) {
            m_upperBoundField.setValue(((DoubleValue) m_colProp.getColumnSpec().getDomain().getUpperBound()).getDoubleValue());
            m_lowerBoundField.setValue(((DoubleValue) m_colProp.getColumnSpec().getDomain().getLowerBound()).getDoubleValue());
        }
    }
    panel.add(low);
    panel.add(up);
    panel.add(nomBox);
    return panel;
}
Also used : JPanel(javax.swing.JPanel) BoxLayout(javax.swing.BoxLayout) JFormattedTextField(javax.swing.JFormattedTextField) JLabel(javax.swing.JLabel) Box(javax.swing.Box) JCheckBox(javax.swing.JCheckBox) ParseException(java.text.ParseException) AbstractFormatter(javax.swing.JFormattedTextField.AbstractFormatter)

Aggregations

ParseException (java.text.ParseException)1 Box (javax.swing.Box)1 BoxLayout (javax.swing.BoxLayout)1 JCheckBox (javax.swing.JCheckBox)1 JFormattedTextField (javax.swing.JFormattedTextField)1 AbstractFormatter (javax.swing.JFormattedTextField.AbstractFormatter)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1