refactor: replace current collection window
Replace CurrentCollectionWindow with CollectionDisplayWindow.
This commit is contained in:
@@ -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<Student> collection;
|
||||
|
||||
private TextBox collectionTextBox;
|
||||
private Button exitButton;
|
||||
|
||||
public CollectionDisplayWindow(WindowBasedTextGUI gui, MyList<Student> 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();
|
||||
}
|
||||
}
|
||||
@@ -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<Student> 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<Student> collection) {
|
||||
for (int i = 0; i < collection.size(); i++) {
|
||||
Student student = collection.get(i);
|
||||
|
||||
panel.addComponent(new Label((i + 1) + ". " + student));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user