From 5d0052805c98fd8ea5c190c0b043553eebeaac40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BE=D0=BB=D0=BE=D0=B4=D0=B8=D0=BD=20=D0=93=D0=B5?= =?UTF-8?q?=D1=80=D0=BC=D0=B0=D0=BD?= Date: Fri, 28 Aug 2026 19:37:27 +0400 Subject: [PATCH] refactor: replace current collection window Replace CurrentCollectionWindow with CollectionDisplayWindow. --- .../ui/window/CollectionDisplayWindow.java | 102 ++++++++++++++++++ .../ui/window/CurrentCollectionWindow.java | 41 ------- 2 files changed, 102 insertions(+), 41 deletions(-) create mode 100644 app/src/main/java/ru/project/ui/window/CollectionDisplayWindow.java delete mode 100644 app/src/main/java/ru/project/ui/window/CurrentCollectionWindow.java diff --git a/app/src/main/java/ru/project/ui/window/CollectionDisplayWindow.java b/app/src/main/java/ru/project/ui/window/CollectionDisplayWindow.java new file mode 100644 index 0000000..4ca3c5e --- /dev/null +++ b/app/src/main/java/ru/project/ui/window/CollectionDisplayWindow.java @@ -0,0 +1,102 @@ +package ru.project.ui.window; + +import com.googlecode.lanterna.TerminalSize; +import com.googlecode.lanterna.gui2.Button; +import com.googlecode.lanterna.gui2.Direction; +import com.googlecode.lanterna.gui2.EmptySpace; +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 com.googlecode.lanterna.input.KeyStroke; +import com.googlecode.lanterna.input.KeyType; +import ru.project.collection.MyList; +import ru.project.student.Student; +import ru.project.ui.base.BaseWindow; + +public class CollectionDisplayWindow extends BaseWindow { + + private final MyList collection; + + private TextBox collectionTextBox; + private Button exitButton; + + public CollectionDisplayWindow(WindowBasedTextGUI gui, MyList collection) { + + super("Просмотр коллекции", gui); + + this.collection = collection; + + Panel panel = createPanel(); + + setComponent(panel); + + exitButton.takeFocus(); + } + + @Override + public boolean handleInput(KeyStroke key) { + if (key.getKeyType() == KeyType.Escape && collectionTextBox.isFocused()) { + + exitButton.takeFocus(); + return true; + } + + return super.handleInput(key); + } + + private Panel createPanel() { + Panel panel = new Panel(); + + panel.setLayoutManager(new LinearLayout(Direction.VERTICAL)); + + addCollectionDisplay(panel); + addExitButton(panel); + + return panel; + } + + private void addCollectionDisplay(Panel panel) { + collectionTextBox = createCollectionScrollBox(); + + panel.addComponent(collectionTextBox); + } + + private TextBox createCollectionScrollBox() { + TextBox textBox = new TextBox(new TerminalSize(80, 20), TextBox.Style.MULTI_LINE); + + textBox.setReadOnly(true); + + if (collection == null || collection.isEmpty()) { + textBox.setText("Коллекция пуста."); + } else { + textBox.setText(buildCollectionText()); + } + + return textBox; + } + + private void addExitButton(Panel panel) { + panel.addComponent(new EmptySpace(new TerminalSize(0, 1))); + + exitButton = new Button("Выйти", this::close); + + panel.addComponent(exitButton); + } + + private String buildCollectionText() { + StringBuilder text = new StringBuilder(); + + for (int i = 0; i < collection.size(); i++) { + Student student = collection.get(i); + + text.append("[") + .append(i) + .append("] ") + .append(student == null ? "null" : student) + .append("\n"); + } + + return text.toString(); + } +} diff --git a/app/src/main/java/ru/project/ui/window/CurrentCollectionWindow.java b/app/src/main/java/ru/project/ui/window/CurrentCollectionWindow.java deleted file mode 100644 index 0fd7e29..0000000 --- a/app/src/main/java/ru/project/ui/window/CurrentCollectionWindow.java +++ /dev/null @@ -1,41 +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.WindowBasedTextGUI; -import ru.project.collection.MyList; -import ru.project.model.AppState; -import ru.project.student.Student; -import ru.project.ui.base.BaseWindow; - -public class CurrentCollectionWindow extends BaseWindow { - - public CurrentCollectionWindow(WindowBasedTextGUI gui, AppState state) { - super("Текущая коллекция", gui); - - Panel panel = new Panel(); - panel.setLayoutManager(new LinearLayout()); - - MyList collection = state.getCollection(); - - if (collection == null || collection.isEmpty()) { - panel.addComponent(new Label("Текущая коллекция пуста.")); - } else { - addStudents(panel, collection); - } - - panel.addComponent(new Button("Выйти", this::close)); - - setComponent(panel); - } - - private void addStudents(Panel panel, MyList collection) { - for (int i = 0; i < collection.size(); i++) { - Student student = collection.get(i); - - panel.addComponent(new Label((i + 1) + ". " + student)); - } - } -}