All Downloads are FREE. Search and download functionalities are using the official Maven repository.

lphystudio.app.graphicalmodelpanel.ArgumentInput Maven / Gradle / Ivy

The newest version!
package lphystudio.app.graphicalmodelpanel;

import lphy.core.model.Value;
import lphy.core.parser.LPhyParserDictionary;
import lphy.core.parser.argument.Argument;
import lphystudio.core.swing.BoundsPopupMenuListener;
import lphystudio.core.valueeditor.FieldComboBoxEditor;

import javax.swing.*;
import java.util.List;
import java.util.Vector;
import java.util.stream.Collectors;

public class ArgumentInput extends JPanel {

    Argument argument;

    JComboBox valueComboBox;

    public ArgumentInput(Argument argument, LPhyParserDictionary parser) {
        this.argument = argument;

        List names = parser.getNamedValuesByType(argument.type).stream().map(Value::getId).collect(Collectors.toList());
        names.addAll(parser.getNamedValuesByType(argument.type.arrayType()).stream().map(Value::getId).collect(Collectors.toList()));

        Vector eligibleValues = new Vector<>(names);
        // allow arrays as well for vectorization
        eligibleValues.sort(String::compareTo);

        BoundsPopupMenuListener boundsPopupMenuListener = new BoundsPopupMenuListener(true, false);
        valueComboBox = new JComboBox<>(eligibleValues);
        valueComboBox.addPopupMenuListener(boundsPopupMenuListener);
        valueComboBox.setPrototypeDisplayValue((String)valueComboBox.getSelectedItem());
        valueComboBox.setFont(StudioConsoleInterpreter.interpreterFont);

        valueComboBox.addActionListener(e -> valueComboBox.setPrototypeDisplayValue((String)valueComboBox.getSelectedItem()));

        if (argument.type == Double.class || argument.type == Integer.class || argument.type == Number.class) {
            valueComboBox.setEditable(true);
            valueComboBox.setEditor(new FieldComboBoxEditor(parser, argument.type));
        }
        add(valueComboBox);
    }

    public Value getValue() {
        return (Value) valueComboBox.getSelectedItem();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy