refactor(ui): reorganize collection menus and sorting

- move MainMenu to the root ui package
- add CurrentCollectionMenu for current collection operations
- move sorting components under collection/sorting
- add CollectionFileSaveWindow for saving collections to file
- update imports and menu delegation
This commit is contained in:
2026-08-31 13:52:54 +04:00
parent 7d646c7fa7
commit 32e53d8fc0
11 changed files with 185 additions and 87 deletions
@@ -0,0 +1,64 @@
package ru.project.ui.collection;
import com.googlecode.lanterna.gui2.ActionListBox;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import ru.project.model.AppState;
import ru.project.ui.base.BaseModalWindow;
import ru.project.ui.collection.sorting.SortCollectionMenu;
import ru.project.ui.common.CollectionDisplayWindow;
import ru.project.ui.common.CollectionFileSaveWindow;
import ru.project.ui.common.MessageWindow;
public class CurrentCollectionMenu extends BaseModalWindow {
private final AppState state;
private final SortCollectionMenu sortCollectionMenu;
public CurrentCollectionMenu(WindowBasedTextGUI gui, AppState state) {
super("Меню текущей коллекции", gui);
this.state = state;
this.sortCollectionMenu = new SortCollectionMenu(gui, state);
ActionListBox menu = new ActionListBox();
menu.addItem("Показать коллекцию", this::showCurrentCollection);
menu.addItem("Сохранить коллекцию в файл", this::saveCurrentCollection);
menu.addItem("Сортировать коллекцию", sortCollectionMenu::showModal);
menu.addItem("Поиск элемента в коллекции", this::searchCollection);
menu.addItem("Подсчёт количества вхождений элемента", this::countOccurrences);
menu.addItem("Выйти", this::close);
setComponent(menu);
}
private void showCurrentCollection() {
CollectionDisplayWindow window = new CollectionDisplayWindow(gui, state.getMainCollection());
window.showModal();
}
private void saveCurrentCollection() {
CollectionFileSaveWindow window = new CollectionFileSaveWindow(gui, state.getMainCollection());
window.showModal();
}
private void searchCollection() {
// TODO
MessageWindow.showModal(gui, "Не реализовано.");
}
private void countOccurrences() {
// TODO
MessageWindow.showModal(gui, "Не реализовано.");
}
}
@@ -0,0 +1,45 @@
package ru.project.ui.collection.sorting;
import com.googlecode.lanterna.gui2.ActionListBox;
import com.googlecode.lanterna.gui2.Button;
import com.googlecode.lanterna.gui2.LinearLayout;
import com.googlecode.lanterna.gui2.Panel;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import java.util.function.Consumer;
import ru.project.ui.base.BaseModalWindow;
public class NormalSortSettingsWindow extends BaseModalWindow {
private final Consumer<SortField> onSortFieldSelected;
public NormalSortSettingsWindow(WindowBasedTextGUI gui, Consumer<SortField> onSortFieldSelected) {
super("Обычная сортировка", gui);
this.onSortFieldSelected = onSortFieldSelected;
Panel panel = new Panel();
panel.setLayoutManager(new LinearLayout());
ActionListBox menu = new ActionListBox();
menu.addItem("Сортировать по номеру группы", () -> selectSortField(SortField.GROUP_NUMBER));
menu.addItem("Сортировать по среднему баллу", () -> selectSortField(SortField.AVERAGE_GRADE));
menu.addItem(
"Сортировать по номеру зачётной книжки",
() -> selectSortField(SortField.RECORD_BOOK_NUMBER));
panel.addComponent(menu);
panel.addComponent(new Button("Выйти", this::close));
setComponent(panel);
}
private void selectSortField(SortField sortField) {
onSortFieldSelected.accept(sortField);
close();
}
}
@@ -0,0 +1,24 @@
package ru.project.ui.collection.sorting;
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.ui.base.BaseModalWindow;
public class ParitySortSettingsWindow extends BaseModalWindow {
public ParitySortSettingsWindow(WindowBasedTextGUI gui) {
super("Настройка сортировки по чётности", gui);
Panel panel = new Panel();
panel.setLayoutManager(new LinearLayout());
panel.addComponent(new Label("Сортировка выполняется по номеру зачётной книжки."));
panel.addComponent(new Button("Назад", this::close));
setComponent(panel);
}
}
@@ -0,0 +1,126 @@
package ru.project.ui.collection.sorting;
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.model.AppState;
import ru.project.ui.base.BaseModalWindow;
import ru.project.ui.common.MessageWindow;
public class SortCollectionMenu extends BaseModalWindow {
private final AppState state;
private final Label sortMethodLabel;
private final Label sortFieldLabel;
private final SortMethodSelectionWindow sortMethodSelectionWindow;
private final NormalSortSettingsWindow normalSortSettingsWindow;
private final ParitySortSettingsWindow paritySortSettingsWindow;
private SortMethod selectedSortMethod;
private SortField selectedSortField;
public SortCollectionMenu(WindowBasedTextGUI gui, AppState state) {
super("Меню сортировки текущей коллекции", gui);
this.state = state;
this.selectedSortMethod = SortMethod.NORMAL;
this.selectedSortField = SortField.GROUP_NUMBER;
Panel panel = new Panel();
panel.setLayoutManager(new LinearLayout());
this.sortMethodLabel = new Label("Способ сортировки: " + getSortMethodName(selectedSortMethod));
this.sortFieldLabel = new Label("Сортировать по: " + getSortFieldName(selectedSortField));
this.sortMethodSelectionWindow =
new SortMethodSelectionWindow(
gui,
sortMethod -> {
selectedSortMethod = sortMethod;
sortMethodLabel.setText(
"Способ сортировки: " + getSortMethodName(selectedSortMethod));
});
this.normalSortSettingsWindow =
new NormalSortSettingsWindow(
gui,
sortField -> {
selectedSortField = sortField;
});
this.paritySortSettingsWindow = new ParitySortSettingsWindow(gui);
panel.addComponent(sortMethodLabel);
panel.addComponent(sortFieldLabel);
panel.addComponent(new Button("Выбрать способ сортировки", this::selectSortMethod));
panel.addComponent(
new Button("Настроить текущий способ сортировки", this::setupCurrentSortMethod));
panel.addComponent(new Button("Сортировать текущую коллекцию", this::sortCurrentCollection));
panel.addComponent(new Button("Выйти", this::close));
setComponent(panel);
}
private void setupCurrentSortMethod() {
switch (selectedSortMethod) {
case NORMAL -> normalSortSettingsWindow.showModal();
case PARITY -> paritySortSettingsWindow.showModal();
}
}
private void selectSortMethod() {
sortMethodSelectionWindow.showModal();
}
private void sortCurrentCollection() {
if (state.getMainCollection() == null || state.getMainCollection().isEmpty()) {
MessageWindow.showModal(gui, "Текущая коллекция пуста.");
return;
}
switch (selectedSortMethod) {
case NORMAL -> sortNormally();
case PARITY -> sortByParity();
}
}
private void sortNormally() {
// TODO
MessageWindow.showModal(gui, "Обычная сортировка не реализована.");
}
private void sortByParity() {
// TODO
MessageWindow.showModal(gui, "Сортировка по чётности не реализована.");
}
private String getSortMethodName(SortMethod sortMethod) {
return switch (sortMethod) {
case NORMAL -> "Обычная сортировка";
case PARITY -> "Сортировка по чётности";
};
}
private String getSortFieldName(SortField sortField) {
return switch (sortField) {
case GROUP_NUMBER -> "номеру группы";
case AVERAGE_GRADE -> "среднему баллу";
case RECORD_BOOK_NUMBER -> "номеру зачётной книжки";
};
}
}
@@ -0,0 +1,7 @@
package ru.project.ui.collection.sorting;
public enum SortField {
GROUP_NUMBER,
AVERAGE_GRADE,
RECORD_BOOK_NUMBER
}
@@ -0,0 +1,6 @@
package ru.project.ui.collection.sorting;
public enum SortMethod {
NORMAL,
PARITY
}
@@ -0,0 +1,34 @@
package ru.project.ui.collection.sorting;
import com.googlecode.lanterna.gui2.ActionListBox;
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
import java.util.function.Consumer;
import ru.project.ui.base.BaseModalWindow;
public class SortMethodSelectionWindow extends BaseModalWindow {
private final Consumer<SortMethod> onSortMethodSelected;
public SortMethodSelectionWindow(
WindowBasedTextGUI gui, Consumer<SortMethod> onSortMethodSelected) {
super("Выбрать способ сортировки", gui);
this.onSortMethodSelected = onSortMethodSelected;
ActionListBox menu = new ActionListBox();
menu.addItem("Обычная сортировка", () -> selectSortMethod(SortMethod.NORMAL));
menu.addItem("Сортировка по чётности", () -> selectSortMethod(SortMethod.PARITY));
menu.addItem("Выйти", this::close);
setComponent(menu);
}
private void selectSortMethod(SortMethod sortMethod) {
onSortMethodSelected.accept(sortMethod);
close();
}
}