feat: add input value window
Add a reusable window for entering and editing values.
This commit is contained in:
@@ -1,54 +0,0 @@
|
|||||||
package ru.project.ui.window;
|
|
||||||
|
|
||||||
import com.googlecode.lanterna.gui2.Button;
|
|
||||||
import com.googlecode.lanterna.gui2.Label;
|
|
||||||
import com.googlecode.lanterna.gui2.LinearLayout;
|
|
||||||
import com.googlecode.lanterna.gui2.Panel;
|
|
||||||
import com.googlecode.lanterna.gui2.TextBox;
|
|
||||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
|
||||||
import java.util.function.IntConsumer;
|
|
||||||
import ru.project.ui.base.BaseWindow;
|
|
||||||
|
|
||||||
public class CollectionSizeInputWindow extends BaseWindow {
|
|
||||||
|
|
||||||
private final TextBox sizeTextBox;
|
|
||||||
private final IntConsumer onSizeEntered;
|
|
||||||
|
|
||||||
public CollectionSizeInputWindow(WindowBasedTextGUI gui, IntConsumer onSizeEntered) {
|
|
||||||
|
|
||||||
super("Задать размер", gui);
|
|
||||||
|
|
||||||
this.onSizeEntered = onSizeEntered;
|
|
||||||
|
|
||||||
Panel panel = new Panel();
|
|
||||||
panel.setLayoutManager(new LinearLayout());
|
|
||||||
|
|
||||||
panel.addComponent(new Label("Введите размер:"));
|
|
||||||
|
|
||||||
sizeTextBox = new TextBox();
|
|
||||||
panel.addComponent(sizeTextBox);
|
|
||||||
|
|
||||||
panel.addComponent(new Button("Подтвердить", this::handleSizeEntered));
|
|
||||||
|
|
||||||
panel.addComponent(new Button("Выйти", this::close));
|
|
||||||
|
|
||||||
setComponent(panel);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void handleSizeEntered() {
|
|
||||||
try {
|
|
||||||
int size = Integer.parseInt(sizeTextBox.getText().trim());
|
|
||||||
|
|
||||||
if (size <= 0) {
|
|
||||||
MessageWindow.create(gui, "Размер должен быть больше 0.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
close();
|
|
||||||
onSizeEntered.accept(size);
|
|
||||||
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
MessageWindow.create(gui, "Введите целое число.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
package ru.project.ui.window;
|
||||||
|
|
||||||
|
import com.googlecode.lanterna.gui2.Button;
|
||||||
|
import com.googlecode.lanterna.gui2.Label;
|
||||||
|
import com.googlecode.lanterna.gui2.LinearLayout;
|
||||||
|
import com.googlecode.lanterna.gui2.Panel;
|
||||||
|
import com.googlecode.lanterna.gui2.TextBox;
|
||||||
|
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import ru.project.ui.base.BaseWindow;
|
||||||
|
|
||||||
|
public class InputValueWindow extends BaseWindow {
|
||||||
|
|
||||||
|
private final TextBox textBox;
|
||||||
|
private final Predicate<String> onSave;
|
||||||
|
|
||||||
|
public InputValueWindow(
|
||||||
|
WindowBasedTextGUI gui,
|
||||||
|
String title,
|
||||||
|
String prompt,
|
||||||
|
String initialValue,
|
||||||
|
Predicate<String> onSave) {
|
||||||
|
|
||||||
|
super(title, gui);
|
||||||
|
|
||||||
|
this.onSave = onSave;
|
||||||
|
|
||||||
|
Panel panel = new Panel();
|
||||||
|
panel.setLayoutManager(new LinearLayout());
|
||||||
|
|
||||||
|
panel.addComponent(new Label(prompt));
|
||||||
|
|
||||||
|
textBox = new TextBox(initialValue == null ? "" : initialValue);
|
||||||
|
|
||||||
|
panel.addComponent(textBox);
|
||||||
|
|
||||||
|
panel.addComponent(new Button("Сохранить", this::save));
|
||||||
|
|
||||||
|
panel.addComponent(new Button("Отмена", this::close));
|
||||||
|
|
||||||
|
setComponent(panel);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void save() {
|
||||||
|
String value = textBox.getText().trim();
|
||||||
|
|
||||||
|
if (value.isEmpty()) {
|
||||||
|
MessageWindow.show(gui, "Значение не может быть пустым.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (onSave.test(value)) {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user