Java Code Examples for javax.swing.JFormattedTextField.AbstractFormatter

The following examples show how to use javax.swing.JFormattedTextField.AbstractFormatter. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ghidra   Source File: DoubleRangeConstraintEditor.java    License: Apache License 2.0 6 votes vote down vote up
private boolean hasValidValue(JSpinner spinner) {
	NumberEditor numEditor = (NumberEditor) spinner.getEditor();
	JFormattedTextField textField = numEditor.getTextField();
	AbstractFormatter formatter = textField.getFormatter();
	String text = textField.getText();
	try {
		String roundTrip = formatter.valueToString(formatter.stringToValue(text));

		Double textDouble = Double.valueOf(text);
		Double roundTripDouble = Double.valueOf(roundTrip);

		return Double.compare(textDouble, roundTripDouble) == 0;
	}
	catch (ParseException e) {
		return false;
	}
	catch (NumberFormatException nfe) {
		return false;
	}
}
 
Example 2
Source Project: ghidra   Source File: IntegerInputVerifier.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean verify( JComponent input ) {
    if ( !(input instanceof JFormattedTextField) ) {
        return true;
    }

    JFormattedTextField ftf = (JFormattedTextField)input;
    AbstractFormatter formatter = ftf.getFormatter();
    if ( formatter == null ) {
        return true;
    }

    String text = ftf.getText();
    try {       
        Integer intValue = ((Number) formatter.stringToValue(text)).intValue();
        if ( intValue.compareTo( 0 ) < 0 ) {
            // no negatives or values over 1
            return false;
        }

        return true;
    }
    catch ( ParseException e ) {
        return false;
    }
}
 
Example 3
Source Project: gcs   Source File: FeatureEditor.java    License: Mozilla Public License 2.0 6 votes vote down vote up
/**
 * @param amt The current {@link LeveledAmount}.
 * @param min The minimum value to allow.
 * @param max The maximum value to allow.
 * @return The {@link EditorField} that allows a {@link LeveledAmount} to be changed.
 */
protected EditorField addLeveledAmountField(LeveledAmount amt, int min, int max) {
    AbstractFormatter formatter;
    Object            value;
    Object            prototype;
    if (amt.isIntegerOnly()) {
        formatter = new IntegerFormatter(min, max, true);
        value = Integer.valueOf(amt.getIntegerAmount());
        prototype = Integer.valueOf(max);
    } else {
        formatter = new DoubleFormatter(min, max, true);
        value = Double.valueOf(amt.getAmount());
        prototype = Double.valueOf(max + 0.25);
    }
    EditorField field = new EditorField(new DefaultFormatterFactory(formatter), this, SwingConstants.LEFT, value, prototype, null);
    field.putClientProperty(LeveledAmount.class, amt);
    UIUtilities.setToPreferredSizeOnly(field);
    add(field);
    return field;
}
 
Example 4
Source Project: ghidra   Source File: DoubleValueConstraintEditor.java    License: Apache License 2.0 5 votes vote down vote up
private boolean checkEditorValue() {
	NumberEditor numEditor = (NumberEditor) spinner.getEditor();
	JFormattedTextField textField = numEditor.getTextField();
	AbstractFormatter formatter = textField.getFormatter();

	// to test if the textfield has a valid value, we try and parse it.  There are two ways
	// in which it is invalid - it can't be parsed or when parsed it doesn't match the spinner
	// value.
	String text = textField.getText();
	try {
		Double valueFromTextField = (Double) formatter.stringToValue(text);
		Double spinnerValue = (Double) spinner.getValue();

		// to compare the two values, convert them back to formatted strings to avoid rounding issues
		String valueFromField = formatter.valueToString(valueFromTextField);
		String valueFromSpinner = formatter.valueToString(spinnerValue);

		if (valueFromField.equals(valueFromSpinner)) {
			errorMessage = "";
			return true;
		}
	}
	catch (ParseException e) {
		// Do nothing
	}
	errorMessage = "Invalid Value!";
	return false;
}
 
Example 5
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
	return decimalFormatter;
}
 
Example 6
Source Project: ghidra   Source File: DecimalFormatterFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
	return numberFormatter;
}
 
Example 7
Source Project: ghidra   Source File: IntegerFormatterFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public AbstractFormatter getFormatter(JFormattedTextField tf) {
	return formatter;
}
 
Example 8
Source Project: ghidra   Source File: BoundedRangeInputVerifier.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean verify( JComponent input ) {
    if ( !(input instanceof JFormattedTextField) ) {
        return true;
    }

    JFormattedTextField ftf = (JFormattedTextField)input;
    AbstractFormatter formatter = ftf.getFormatter();
    if ( formatter == null ) {
        return true;
    }

    String text = ftf.getText();
    try {

        //
        // First, make sure we are within bounds
        // 
        Number number = (Number) formatter.stringToValue(text);
        if ( compareNumbers( number, upperRangeValue ) > 0 ||
             compareNumbers( number, lowerRangeValue ) < 0 ) {
            // no values above or below our max
            return false;
        }

        // 
        // Second, don't let any value through that crosses our other field's range
        // 
        boolean result = false;
        Number otherNumber = (Number) otherField.getValue();
        if ( isOtherFieldUpperRange ) {
            // make sure our value is below the upper range value
            result = compareNumbers( number, otherNumber ) <= 0;
        }

        // make sure our value is above the lower range value
        else {
            result = compareNumbers( number, otherNumber ) >= 0;
        }

        return result;
    } catch (ParseException pe) {
        return false;
    }
}
 
Example 9
Source Project: arcusplatform   Source File: Fields.java    License: Apache License 2.0 4 votes vote down vote up
public static FormattedTextFieldBuilder<Object> formattedTextFieldBuilder(AbstractFormatter formatter) {
	return new FormattedTextFieldBuilder<Object>(formatter);
}
 
Example 10
Source Project: arcusplatform   Source File: Fields.java    License: Apache License 2.0 4 votes vote down vote up
public FormattedTextFieldBuilder(AbstractFormatter passedFormat) {
	super(new JFormattedTextField(passedFormat));

	withGetter((field) -> (V) field.getValue());
     withSetter((field, value) -> field.setValue((V) value));
}
 
Example 11
Source Project: netbeans   Source File: SplashUISupport.java    License: Apache License 2.0 4 votes vote down vote up
FontFormatter(AbstractFormatter deleg) {
    setOverwriteMode(false);
    this.deleg = deleg;
}
 
Example 12
/**
 * Creates a {@link JFormattedTextField} for the specified cell. If a formatter is given, will
 * apply it to the field. Does not validate the model, so make sure this call works!
 * 
 * @param model
 * @param rowIndex
 * @param columnIndex
 * @param cellClass
 * @param formatter
 *            the formatter or <code>null</code> if none is required
 * @param hideUnavailableContentAssist
 * @return
 */
public CellTypeTextFieldDefaultImpl(final TablePanelModel model, final int rowIndex, final int columnIndex,
		final Class<? extends CellType> cellClass, AbstractFormatter formatter, boolean hideUnavailableContentAssist) {
	super();

	final JFormattedTextField field = CellTypeImplHelper.createFormattedTextField(model, rowIndex, columnIndex);
	setLayout(new BorderLayout());
	add(field, BorderLayout.CENTER);

	// otherwise 'null' would be restored
	Object value = model.getValueAt(rowIndex, columnIndex);
	String text = value != null ? String.valueOf(value) : "";

	// specical handling when formatter is given
	if (formatter != null) {
		field.setFormatterFactory(new DefaultFormatterFactory(formatter));
	}
	field.setText(text);

	// set syntax assist if available
	String syntaxHelp = model.getSyntaxHelpAt(rowIndex, columnIndex);
	if (syntaxHelp != null && !"".equals(syntaxHelp.trim())) {
		SwingTools.setPrompt(syntaxHelp, field);
	}

	// see if content assist is possible for this field, if so add it
	ImageIcon icon = SwingTools.createIcon("16/"
			+ I18N.getMessageOrNull(I18N.getGUIBundle(), "gui.action.content_assist.icon"));
	JButton contentAssistButton = new JButton();
	contentAssistButton.setIcon(icon);
	if (field.isEnabled() && model.isContentAssistPossibleForCell(rowIndex, columnIndex)) {
		contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
				"gui.action.content_assist_enabled.tip"));
		CellTypeImplHelper.addContentAssist(model, rowIndex, columnIndex, field, contentAssistButton, cellClass);
	} else {
		contentAssistButton.setToolTipText(I18N.getMessageOrNull(I18N.getGUIBundle(),
				"gui.action.content_assist_disabled.tip"));
		contentAssistButton.setEnabled(false);
	}
	if (contentAssistButton.isEnabled() || (!contentAssistButton.isEnabled() && !hideUnavailableContentAssist)) {
		add(contentAssistButton, BorderLayout.EAST);
	}

	// set size so panels don't grow larger when they get the chance
	setPreferredSize(new Dimension(300, 20));
	setMinimumSize(new Dimension(100, 15));
	setMaximumSize(new Dimension(1600, 30));
}