Search in sources :

Example 1 with ResultOption

use of uk.ac.sussex.gdsc.smlm.results.ResultOption in project GDSC-SMLM by aherbert.

the class ResultsManager method collectOptions.

private static void collectOptions(PeakResultsReader reader, ResultOption[] options) {
    final GenericDialog gd = new GenericDialog(TITLE);
    gd.addMessage("Options required for file format: " + reader.getFormat().getName());
    for (final ResultOption option : options) {
        if (option.hasValues()) {
            final String[] items = new String[option.values.length];
            for (int i = 0; i < items.length; i++) {
                items[i] = option.values[i].toString();
            }
            gd.addChoice(getOptionName(option), items, option.getValue().toString());
        } else if (option.getValue() instanceof Number) {
            final Number n = (Number) option.getValue();
            if (n.doubleValue() == n.intValue()) {
                gd.addNumericField(getOptionName(option), n.intValue(), 0);
            } else {
                final String value = n.toString();
                int sig = 0;
                int index = value.indexOf('.');
                if (index != -1) {
                    // There is a decimal point. Count the digits after it
                    while (++index < value.length()) {
                        if (!Character.isDigit(value.charAt(index))) {
                            // A non-digit character after the decimal point is for scientific notation
                            sig = -sig;
                            break;
                        }
                        sig++;
                    }
                }
                gd.addNumericField(getOptionName(option), n.doubleValue(), sig);
            }
        } else if (option.getValue() instanceof String) {
            gd.addStringField(getOptionName(option), (String) option.getValue());
        } else if (option.getValue() instanceof Boolean) {
            gd.addCheckbox(getOptionName(option), (Boolean) option.getValue());
        } else {
            IJ.log(TITLE + ": Unsupported reader option: " + option.name + "=" + option.getValue().toString());
        }
    }
    gd.showDialog();
    if (gd.wasCanceled()) {
        return;
    }
    try {
        for (final ResultOption option : options) {
            if (option.hasValues()) {
                option.setValue(option.values[gd.getNextChoiceIndex()]);
            } else if (option.getValue() instanceof Number) {
                final double d = gd.getNextNumber();
                // Convert to the correct type using the String value constructor for the number
                option.setValue(option.getValue().getClass().getConstructor(String.class).newInstance(Double.toString(d)));
            } else if (option.getValue() instanceof String) {
                option.setValue(gd.getNextString());
            } else if (option.getValue() instanceof Boolean) {
                option.setValue(gd.getNextBoolean());
            }
        }
        reader.setOptions(options);
    } catch (final Exception ex) {
        // This can occur if the options are not valid
        IJ.log(TITLE + ": Failed to configure reader options: " + ex.getMessage());
    }
}
Also used : ResultOption(uk.ac.sussex.gdsc.smlm.results.ResultOption) ExtendedGenericDialog(uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog) GenericDialog(ij.gui.GenericDialog) IOException(java.io.IOException)

Aggregations

GenericDialog (ij.gui.GenericDialog)1 IOException (java.io.IOException)1 ExtendedGenericDialog (uk.ac.sussex.gdsc.core.ij.gui.ExtendedGenericDialog)1 ResultOption (uk.ac.sussex.gdsc.smlm.results.ResultOption)1