/** * Returns a boolean indicating whether the current text is valid for * instantiating the expected Number type. * * @return true if text is valid, false otherwise. */ protected boolean isValid() { if (!getComponent().isEditValid()) return false; try { if (!hasStrictFormatter()) getNumber(); return true; } catch (Exception ex) { } return false; }
@Override public boolean stopCellEditing() { if (!isValid()) return false; return super.stopCellEditing(); }
/** * {@inheritDoc} <p> * * Overridden to instantiate a Number of the expected type. Note that this * may throw a IllegalStateException if invoked without querying * for a valid value with stopCellEditing. This should not happen during * normal usage. * * @throws IllegalStateException if current value invalid * */ @Override public Number getCellEditorValue() throws IllegalStateException { try { return getNumber(); } catch (Exception ex) { throw new IllegalStateException("Number conversion not possible from " + "current string " + getComponent().getText()); } }
super(useStrictFormatter ? createFormattedTextFieldX(format) : createFormattedTextField(format)); this.useStrictFormatter = useStrictFormatter; final JFormattedTextField textField = getComponent();
/** Override and set the border back to normal in case there was an error previously */ @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { ((JComponent)getComponent()).setBorder(new LineBorder(Color.black)); try { final Class<?> type = table.getColumnClass(column); if (hasStrictFormatter()) { // delegate to formatter which decides at parsing time // then either handles or throws ((NumberFormatter) getComponent().getFormatter()).setValueClass(type); } else { // Assume that the Number object we are dealing with has a constructor which takes // a single string parameter. if (!Number.class.isAssignableFrom(type)) { throw new IllegalStateException("NumberEditor can only handle subclasses of java.lang.Number"); } constructor = type.getConstructor(argTypes); } // JW: in strict mode this may fail in setting the value in the formatter return super.getTableCellEditorComponent(table, value, isSelected, row, column); } catch (Exception ex) { // PENDING JW: super generic editor swallows all failures and returns null // should we do so as well? throw new IllegalStateException("value/type not compatible with Number", ex); } }
@Override public Object getCellEditorValue() { try { getComponent().commitEdit(); return getComponent().getValue(); } catch (ParseException ex) { return null; } } };
public NumberEditorExt(NumberFormat formatter) { super(createFormattedTextField(formatter)); final JFormattedTextField textField = ((JFormattedTextField)getComponent()); textField.setName("Table.editor"); textField.setHorizontalAlignment(JTextField.RIGHT); // remove action listener added in DefaultCellEditor textField.removeActionListener(delegate); // replace the delegate created in DefaultCellEditor delegate = new EditorDelegate() { @Override public void setValue(Object value) { ((JFormattedTextField)getComponent()).setValue(value); } @Override public Object getCellEditorValue() { JFormattedTextField textField = ((JFormattedTextField)getComponent()); try { textField.commitEdit(); return textField.getValue(); } catch (ParseException ex) { return null; } } }; textField.addActionListener(delegate); }
/** * Returns the editor value as number. May fail for a variety of reasons, * as it forces parsing of the current text as well as reflective construction * of the target type. * * @return the editor value or null * @throws Exception if creation of the expected type fails in some way. */ protected Number getNumber() throws Exception { Number number = (Number) super.getCellEditorValue(); if (number==null) return null; return hasStrictFormatter() ? number : (Number) constructor.newInstance(new Object[]{number.toString()}); }
/** * Creates default cell editors for objects, numbers, and boolean values. * <p> * Overridden to hook enhanced editors (f.i. <code>NumberEditorExt</code> * * @see DefaultCellEditor */ @SuppressWarnings("unchecked") @Override protected void createDefaultEditors() { defaultEditorsByColumnClass = new UIDefaults(3, 0.75f); defaultEditorsByColumnClass.put(Object.class, new GenericEditor()); // Numbers // JW: fix for // Issue #1183-swingx: NumberEditorExt throws in getCellEditorValue if // Integer (short, byte..) below/above min/max. // Issue #1236-swingx: NumberEditorExt cannot be used in columns with Object type defaultEditorsByColumnClass.put(Number.class, new NumberEditorExt(true)); // Booleans defaultEditorsByColumnClass.put(Boolean.class, new BooleanEditor()); }
super(useStrictFormatter ? createFormattedTextFieldX(format) : createFormattedTextField(format)); this.useStrictFormatter = useStrictFormatter; final JFormattedTextField textField = getComponent();
/** * {@inheritDoc} <p> * * Overridden to instantiate a Number of the expected type. Note that this * may throw a IllegalStateException if invoked without querying * for a valid value with stopCellEditing. This should not happen during * normal usage. * * @throws IllegalStateException if current value invalid * */ @Override public Number getCellEditorValue() throws IllegalStateException { try { return getNumber(); } catch (Exception ex) { throw new IllegalStateException("Number conversion not possible from " + "current string " + getComponent().getText()); } }
/** Override and set the border back to normal in case there was an error previously */ @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { ((JComponent)getComponent()).setBorder(new LineBorder(Color.black)); try { final Class<?> type = table.getColumnClass(column); if (hasStrictFormatter()) { // delegate to formatter which decides at parsing time // then either handles or throws ((NumberFormatter) getComponent().getFormatter()).setValueClass(type); } else { // Assume that the Number object we are dealing with has a constructor which takes // a single string parameter. if (!Number.class.isAssignableFrom(type)) { throw new IllegalStateException("NumberEditor can only handle subclasses of java.lang.Number"); } constructor = type.getConstructor(argTypes); } // JW: in strict mode this may fail in setting the value in the formatter return super.getTableCellEditorComponent(table, value, isSelected, row, column); } catch (Exception ex) { // PENDING JW: super generic editor swallows all failures and returns null // should we do so as well? throw new IllegalStateException("value/type not compatible with Number", ex); } }
@Override public Object getCellEditorValue() { try { getComponent().commitEdit(); return getComponent().getValue(); } catch (ParseException ex) { return null; } } };
/** * Returns the editor value as number. May fail for a variety of reasons, * as it forces parsing of the current text as well as reflective construction * of the target type. * * @return the editor value or null * @throws Exception if creation of the expected type fails in some way. */ protected Number getNumber() throws Exception { Number number = (Number) super.getCellEditorValue(); if (number==null) return null; return hasStrictFormatter() ? number : (Number) constructor.newInstance(new Object[]{number.toString()}); }
/** * Creates default cell editors for objects, numbers, and boolean values. * <p> * Overridden to hook enhanced editors (f.i. <code>NumberEditorExt</code> * * @see DefaultCellEditor */ @SuppressWarnings("unchecked") @Override protected void createDefaultEditors() { defaultEditorsByColumnClass = new UIDefaults(3, 0.75f); defaultEditorsByColumnClass.put(Object.class, new GenericEditor()); // Numbers // JW: fix for // Issue #1183-swingx: NumberEditorExt throws in getCellEditorValue if // Integer (short, byte..) below/above min/max. // Issue #1236-swingx: NumberEditorExt cannot be used in columns with Object type defaultEditorsByColumnClass.put(Number.class, new NumberEditorExt(true)); // Booleans defaultEditorsByColumnClass.put(Boolean.class, new BooleanEditor()); }
super(useStrictFormatter ? createFormattedTextFieldX(format) : createFormattedTextField(format)); this.useStrictFormatter = useStrictFormatter; final JFormattedTextField textField = getComponent();
/** * Returns a boolean indicating whether the current text is valid for * instantiating the expected Number type. * * @return true if text is valid, false otherwise. */ protected boolean isValid() { if (!getComponent().isEditValid()) return false; try { if (!hasStrictFormatter()) getNumber(); return true; } catch (Exception ex) { } return false; }
/** * {@inheritDoc} <p> * * Overridden to instantiate a Number of the expected type. Note that this * may throw a IllegalStateException if invoked without querying * for a valid value with stopCellEditing. This should not happen during * normal usage. * * @throws IllegalStateException if current value invalid * */ @Override public Number getCellEditorValue() throws IllegalStateException { try { return getNumber(); } catch (Exception ex) { throw new IllegalStateException("Number conversion not possible from " + "current string " + getComponent().getText()); } }
/** Override and set the border back to normal in case there was an error previously */ @Override public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) { ((JComponent)getComponent()).setBorder(new LineBorder(Color.black)); try { final Class<?> type = table.getColumnClass(column); if (hasStrictFormatter()) { // delegate to formatter which decides at parsing time // then either handles or throws ((NumberFormatter) getComponent().getFormatter()).setValueClass(type); } else { // Assume that the Number object we are dealing with has a constructor which takes // a single string parameter. if (!Number.class.isAssignableFrom(type)) { throw new IllegalStateException("NumberEditor can only handle subclasses of java.lang.Number"); } constructor = type.getConstructor(argTypes); } // JW: in strict mode this may fail in setting the value in the formatter return super.getTableCellEditorComponent(table, value, isSelected, row, column); } catch (Exception ex) { // PENDING JW: super generic editor swallows all failures and returns null // should we do so as well? throw new IllegalStateException("value/type not compatible with Number", ex); } }
@Override public void setValue(Object value) { getComponent().setValue(value); }