103 lines
2.2 KiB
Java

package xyz.thastertyn.UserInterface.Dialogs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.ComboBox;
import com.googlecode.lanterna.gui2.EmptySpace;
import com.googlecode.lanterna.gui2.GridLayout;
import com.googlecode.lanterna.gui2.LocalizedString;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.Window;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import com.googlecode.lanterna.gui2.dialogs.DialogWindow;
import xyz.thastertyn.Types.Choice;
import xyz.thastertyn.Types.Option;
import xyz.thastertyn.Types.Options;
public class OptionsDialog extends DialogWindow {
private List<ComboBox<Option>> boxs;
private boolean useData = false;
public OptionsDialog(Options... options)
{
super("Choose from below");
boxs = new ArrayList<>();
for(Options o : options)
{
boxs.add(new ComboBox<Option>(o.getOptions()));
}
Panel mainPanel = new Panel()
.setLayoutManager(new GridLayout(1)
.setLeftMarginSize(1)
.setRightMarginSize(1));
Panel optionPanel = new Panel().setLayoutManager(new GridLayout(3))
.addTo(mainPanel);
for(int i = 0; i < options.length; i++)
{
optionPanel.addComponent(boxs.get(i));
if(i != options.length - 1)
{
optionPanel.addComponent(new EmptySpace());
}
}
new Panel()
.setLayoutManager(
new GridLayout(2).setHorizontalSpacing(1))
.addComponent(
new Button(LocalizedString.OK.toString(), this::onOK)
.setLayoutData(GridLayout.createLayoutData(
GridLayout.Alignment.CENTER,
GridLayout.Alignment.CENTER,
true,
false)))
.addComponent(
new Button(LocalizedString.Cancel.toString(), this::onCancel))
.addTo(mainPanel);
setHints(Arrays.asList(Window.Hint.CENTERED));
setComponent(mainPanel);
}
public void onOK()
{
close();
useData = true;
}
public void onCancel()
{
close();
}
@Override
public Choice showDialog(WindowBasedTextGUI textGUI) {
super.showDialog(textGUI);
return (useData) ?
// User pressed Ok
new Choice(boxs.stream()
.map(box -> box.getSelectedItem().getValue())
.toList())
:
// User pressed Cancel
null;
}
}