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:
@@ -4,7 +4,7 @@ import com.googlecode.lanterna.gui2.MultiWindowTextGUI;
|
|||||||
import com.googlecode.lanterna.screen.Screen;
|
import com.googlecode.lanterna.screen.Screen;
|
||||||
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
|
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
|
||||||
import ru.project.model.AppState;
|
import ru.project.model.AppState;
|
||||||
import ru.project.ui.main.MainMenu;
|
import ru.project.ui.MainMenu;
|
||||||
|
|
||||||
public final class App {
|
public final class App {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
package ru.project.ui;
|
||||||
|
|
||||||
|
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.CurrentCollectionMenu;
|
||||||
|
import ru.project.ui.creation.CreateCollectionMenu;
|
||||||
|
|
||||||
|
public class MainMenu extends BaseModalWindow {
|
||||||
|
|
||||||
|
private final ActionListBox menu;
|
||||||
|
|
||||||
|
private final BaseModalWindow currentCollectionMenu;
|
||||||
|
private final BaseModalWindow createCollectionMenu;
|
||||||
|
|
||||||
|
public MainMenu(WindowBasedTextGUI gui, AppState state) {
|
||||||
|
super("Главное меню", gui);
|
||||||
|
|
||||||
|
this.currentCollectionMenu = new CurrentCollectionMenu(gui, state);
|
||||||
|
|
||||||
|
this.createCollectionMenu = new CreateCollectionMenu(gui, state);
|
||||||
|
|
||||||
|
menu = new ActionListBox();
|
||||||
|
|
||||||
|
menu.addItem("Меню текущей коллекции", currentCollectionMenu::showModal);
|
||||||
|
|
||||||
|
menu.addItem("Меню создания новой коллекции", createCollectionMenu::showModal);
|
||||||
|
|
||||||
|
menu.addItem("Завершить выполнение программы", this::exit);
|
||||||
|
|
||||||
|
setComponent(menu);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void exit() {
|
||||||
|
close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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, "Не реализовано.");
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package ru.project.ui.sorting;
|
package ru.project.ui.collection.sorting;
|
||||||
|
|
||||||
import com.googlecode.lanterna.gui2.ActionListBox;
|
import com.googlecode.lanterna.gui2.ActionListBox;
|
||||||
import com.googlecode.lanterna.gui2.Button;
|
import com.googlecode.lanterna.gui2.Button;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package ru.project.ui.sorting;
|
package ru.project.ui.collection.sorting;
|
||||||
|
|
||||||
import com.googlecode.lanterna.gui2.Button;
|
import com.googlecode.lanterna.gui2.Button;
|
||||||
import com.googlecode.lanterna.gui2.Label;
|
import com.googlecode.lanterna.gui2.Label;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package ru.project.ui.sorting;
|
package ru.project.ui.collection.sorting;
|
||||||
|
|
||||||
import com.googlecode.lanterna.gui2.Button;
|
import com.googlecode.lanterna.gui2.Button;
|
||||||
import com.googlecode.lanterna.gui2.Label;
|
import com.googlecode.lanterna.gui2.Label;
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package ru.project.ui.sorting;
|
package ru.project.ui.collection.sorting;
|
||||||
|
|
||||||
public enum SortField {
|
public enum SortField {
|
||||||
GROUP_NUMBER,
|
GROUP_NUMBER,
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package ru.project.ui.sorting;
|
package ru.project.ui.collection.sorting;
|
||||||
|
|
||||||
public enum SortMethod {
|
public enum SortMethod {
|
||||||
NORMAL,
|
NORMAL,
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package ru.project.ui.sorting;
|
package ru.project.ui.collection.sorting;
|
||||||
|
|
||||||
import com.googlecode.lanterna.gui2.ActionListBox;
|
import com.googlecode.lanterna.gui2.ActionListBox;
|
||||||
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package ru.project.ui.common;
|
||||||
|
|
||||||
|
import com.googlecode.lanterna.gui2.WindowBasedTextGUI;
|
||||||
|
import com.googlecode.lanterna.gui2.dialogs.FileDialogBuilder;
|
||||||
|
import com.googlecode.lanterna.gui2.dialogs.MessageDialog;
|
||||||
|
import com.googlecode.lanterna.gui2.dialogs.MessageDialogButton;
|
||||||
|
import java.io.BufferedWriter;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
|
import ru.project.collection.MyList;
|
||||||
|
import ru.project.student.Student;
|
||||||
|
|
||||||
|
public class CollectionFileSaveWindow {
|
||||||
|
|
||||||
|
private final WindowBasedTextGUI gui;
|
||||||
|
private final MyList<Student> collection;
|
||||||
|
|
||||||
|
public CollectionFileSaveWindow(WindowBasedTextGUI gui, MyList<Student> collection) {
|
||||||
|
|
||||||
|
this.gui = gui;
|
||||||
|
this.collection = collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void showModal() {
|
||||||
|
File selectedFile = selectFile();
|
||||||
|
|
||||||
|
if (selectedFile == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (selectedFile.exists()) {
|
||||||
|
saveToFile(selectedFile);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageDialogButton result =
|
||||||
|
MessageDialog.showMessageDialog(
|
||||||
|
gui,
|
||||||
|
"Создание файла",
|
||||||
|
"Файл не существует. Создать новый файл?",
|
||||||
|
MessageDialogButton.Yes,
|
||||||
|
MessageDialogButton.No);
|
||||||
|
|
||||||
|
if (result == MessageDialogButton.Yes) {
|
||||||
|
saveToFile(selectedFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private File selectFile() {
|
||||||
|
return new FileDialogBuilder()
|
||||||
|
.setTitle("Сохранение коллекции")
|
||||||
|
.setDescription("Выберите файл:")
|
||||||
|
.setActionLabel("Сохранить")
|
||||||
|
.build()
|
||||||
|
.showDialog(gui);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void saveToFile(File file) {
|
||||||
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
|
||||||
|
|
||||||
|
for (int i = 0; i < collection.size(); i++) {
|
||||||
|
Student student = collection.get(i);
|
||||||
|
|
||||||
|
writer.write(student == null ? "null" : student.toString());
|
||||||
|
|
||||||
|
writer.newLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
MessageWindow.showModal(gui, "Коллекция успешно сохранена.");
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
|
MessageWindow.showModal(gui, "Не удалось сохранить коллекцию.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
package ru.project.ui.main;
|
|
||||||
|
|
||||||
import com.googlecode.lanterna.gui2.ActionListBox;
|
|
||||||
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.BaseModalWindow;
|
|
||||||
import ru.project.ui.common.CollectionDisplayWindow;
|
|
||||||
import ru.project.ui.common.MessageWindow;
|
|
||||||
import ru.project.ui.creation.CreateCollectionMenu;
|
|
||||||
import ru.project.ui.sorting.SortCollectionMenu;
|
|
||||||
|
|
||||||
public class MainMenu extends BaseModalWindow {
|
|
||||||
|
|
||||||
private final ActionListBox menu;
|
|
||||||
|
|
||||||
private final BaseModalWindow createCollectionMenu;
|
|
||||||
private final BaseModalWindow sortCollectionMenu;
|
|
||||||
|
|
||||||
public MainMenu(WindowBasedTextGUI gui, AppState state) {
|
|
||||||
super("Главное меню", gui);
|
|
||||||
|
|
||||||
this.createCollectionMenu = new CreateCollectionMenu(gui, state);
|
|
||||||
this.sortCollectionMenu = new SortCollectionMenu(gui, state);
|
|
||||||
|
|
||||||
menu = new ActionListBox();
|
|
||||||
|
|
||||||
menu.addItem("Показать текущую коллекцию", () -> showMainCollection(state));
|
|
||||||
|
|
||||||
menu.addItem("Меню создания новой коллекции", this::showCreateCollectionMenu);
|
|
||||||
|
|
||||||
menu.addItem("Меню сохранения текущей коллекции в файл", this::saveCollection);
|
|
||||||
|
|
||||||
menu.addItem("Меню сортировки текущей коллекции", this::sortCollection);
|
|
||||||
|
|
||||||
menu.addItem("Меню поиска элемента в текущей коллекции", this::searchCollection);
|
|
||||||
|
|
||||||
menu.addItem("Меню подсчёта количества вхождений элемента", this::countOccurrences);
|
|
||||||
|
|
||||||
menu.addItem("Завершить выполнение программы", this::exit);
|
|
||||||
|
|
||||||
setComponent(menu);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showMainCollection(AppState state) {
|
|
||||||
MyList<Student> mainCollection = state.getMainCollection();
|
|
||||||
|
|
||||||
BaseModalWindow window = new CollectionDisplayWindow(gui, mainCollection);
|
|
||||||
|
|
||||||
window.showModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void showCreateCollectionMenu() {
|
|
||||||
createCollectionMenu.showModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void saveCollection() {
|
|
||||||
// TODO
|
|
||||||
MessageWindow.showModal(gui, "Не реализовано.");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void sortCollection() {
|
|
||||||
sortCollectionMenu.showModal();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void searchCollection() {
|
|
||||||
// TODO
|
|
||||||
MessageWindow.showModal(gui, "Не реализовано.");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void countOccurrences() {
|
|
||||||
// TODO
|
|
||||||
MessageWindow.showModal(gui, "Не реализовано.");
|
|
||||||
}
|
|
||||||
|
|
||||||
private void exit() {
|
|
||||||
close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user