lphystudio.app.graphicalmodelpanel.ArgumentInput Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lphy-studio Show documentation
Show all versions of lphy-studio Show documentation
The GUI for LPhy language.
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();
}
}