package lipstone.joshua.parser.plugin.settings;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import lipstone.joshua.parser.Tokenizer;
import lipstone.joshua.parser.exceptions.SyntaxException;
import lipstone.joshua.parser.exceptions.UnbalancedParenthesesException;
import lipstone.joshua.parser.plugin.ParserPlugin;
import lipstone.joshua.parser.util.ConsCell;
public class ParserSettingsComboBox<T> extends ParserSettingsItem<JComboBox<T>> {
public ParserSettingsComboBox(String name, ParserPlugin plugin, boolean editable, T... initialItems) {
super(name, new JComboBox<T>(initialItems), plugin);
guiItem.setEditable(editable);
}
@Override
protected void addListeners() {
guiItem.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
onEnter();
}
});
}
@Override
public ConsCell getValue() {
try {
return Tokenizer.tokenizeString(guiItem.getSelectedItem().toString());
}
catch (SyntaxException | UnbalancedParenthesesException e) {
getPlugin().getParser().getLog().logError("Unable to tokenize the selected item in " + getName() + " in " + getPlugin().getID());
return new ConsCell();
}
}
@Override
public void setValue(ConsCell value) {
guiItem.setSelectedItem(value.toString());
}
}