2023-11-24 21:37:08 +04:00
|
|
|
|
package Trolleybus;
|
|
|
|
|
|
|
|
|
|
import javax.swing.*;
|
2023-12-01 20:42:28 +04:00
|
|
|
|
import javax.swing.event.ListSelectionEvent;
|
|
|
|
|
import javax.swing.event.ListSelectionListener;
|
|
|
|
|
|
2023-11-24 21:37:08 +04:00
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.*;
|
2023-12-01 20:42:28 +04:00
|
|
|
|
import java.util.List;
|
2023-11-24 21:37:08 +04:00
|
|
|
|
|
|
|
|
|
public class FormBusesCollection {
|
2023-12-01 20:42:28 +04:00
|
|
|
|
private final BusesGenericStorage _storage;
|
|
|
|
|
private QueueBusesCollection<DrawingBus> _queueCollection;
|
2023-11-24 21:37:08 +04:00
|
|
|
|
private JFrame frameBusesCollection;
|
2023-12-01 20:42:28 +04:00
|
|
|
|
private JPanel panelBusesCollection, panelTools, panelSets;
|
|
|
|
|
private JButton buttonAddBus, buttonRemoveBus, buttonRefreshCollection, buttonAddSet, buttonDelSet, buttonQueue;
|
|
|
|
|
private JTextField positionTextField, nameOfSetTextField; //поля ввода
|
|
|
|
|
private JList<String> listBoxSets;
|
|
|
|
|
private DefaultListModel<String> listBoxModel; //модель для получения/редактирования записей listBoxSets
|
|
|
|
|
|
2023-11-24 21:37:08 +04:00
|
|
|
|
public FormBusesCollection() {
|
|
|
|
|
InitializeComponent();
|
2023-12-01 20:42:28 +04:00
|
|
|
|
_storage = new BusesGenericStorage(panelBusesCollection.getWidth(), panelBusesCollection.getHeight());
|
|
|
|
|
_queueCollection = new QueueBusesCollection<>();
|
2023-11-24 21:37:08 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void InitializeComponent() {
|
|
|
|
|
//Само окно
|
|
|
|
|
frameBusesCollection = new JFrame("Набор автобусов");
|
|
|
|
|
frameBusesCollection.setLayout(new BorderLayout());
|
|
|
|
|
frameBusesCollection.setSize(900, 600);
|
|
|
|
|
frameBusesCollection.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
|
|
|
|
|
//Панель, на которой отрисовывается набор автобусов/троллейбусов
|
|
|
|
|
panelBusesCollection = new JPanel();
|
|
|
|
|
|
|
|
|
|
//Панель, с помощью которой изменяется набор
|
|
|
|
|
panelTools = new JPanel();
|
|
|
|
|
panelTools.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
|
|
|
|
panelTools.setLayout(null);
|
|
|
|
|
panelTools.setPreferredSize(new Dimension(170, 600));
|
|
|
|
|
|
2023-12-01 20:42:28 +04:00
|
|
|
|
//Панель для работы с наборами
|
|
|
|
|
panelSets = new JPanel();
|
|
|
|
|
panelSets.setBorder(BorderFactory.createLineBorder(Color.BLACK));
|
|
|
|
|
panelSets.setLayout(null);
|
|
|
|
|
panelSets.setBounds(10, 10, 150, 300);
|
|
|
|
|
|
2023-11-24 21:37:08 +04:00
|
|
|
|
//Кнопки панели panelTools
|
|
|
|
|
buttonAddBus = new JButton("Добавить автобус");
|
2023-12-01 20:42:28 +04:00
|
|
|
|
buttonAddBus.setBounds(10, 320, 150, 40);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
|
|
|
|
|
buttonRemoveBus = new JButton("Удалить автобус");
|
2023-12-01 20:42:28 +04:00
|
|
|
|
buttonRemoveBus.setBounds(10, 410, 150, 40);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
|
|
|
|
|
buttonRefreshCollection = new JButton("Обновить коллекцию");
|
2023-12-01 20:42:28 +04:00
|
|
|
|
buttonRefreshCollection.setBounds(10, 460, 150, 40);
|
|
|
|
|
|
|
|
|
|
buttonQueue = new JButton("Очередь");
|
|
|
|
|
buttonQueue.setBounds(10, 510, 150, 40);
|
|
|
|
|
|
|
|
|
|
//Элементы панели panelSets
|
|
|
|
|
buttonAddSet = new JButton("Добавить набор");
|
|
|
|
|
buttonAddSet.setBounds(10, 50, 130, 30);
|
|
|
|
|
|
|
|
|
|
listBoxModel = new DefaultListModel<>();
|
|
|
|
|
listBoxSets = new JList<>(listBoxModel);
|
|
|
|
|
listBoxSets.setBounds(10, 90, 130, 100);
|
|
|
|
|
|
|
|
|
|
buttonDelSet = new JButton("Удалить набор");
|
|
|
|
|
buttonDelSet.setBounds(10, 200, 130, 30);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
|
|
|
|
|
//Добавление листенеров к кнопкам
|
|
|
|
|
buttonAddBus.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
buttonAddBus_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
buttonRemoveBus.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
buttonRemoveBus_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
buttonRefreshCollection.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
buttonRefreshCollection_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-12-01 20:42:28 +04:00
|
|
|
|
|
|
|
|
|
buttonAddSet.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
buttonAddSet_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
buttonDelSet.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
buttonDelSet_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
buttonQueue.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
buttonQueue_Click(e);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Листенер изменения индекса в списке
|
|
|
|
|
listBoxSets.addListSelectionListener(new ListSelectionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void valueChanged(ListSelectionEvent e) {
|
|
|
|
|
if (listBoxSets.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
BusesGenericCollection<DrawingBus, DrawingObjectBus> obj = _storage.Get(listBoxSets.getSelectedValue());
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
obj.ShowBuses(panelBusesCollection);
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-11-24 21:37:08 +04:00
|
|
|
|
|
2023-12-01 20:42:28 +04:00
|
|
|
|
//Поля ввода
|
2023-11-24 21:37:08 +04:00
|
|
|
|
positionTextField = new JTextField();
|
2023-12-01 20:42:28 +04:00
|
|
|
|
positionTextField.setBounds(10, 370, 150, 30);
|
|
|
|
|
|
|
|
|
|
nameOfSetTextField = new JTextField();
|
|
|
|
|
nameOfSetTextField.setBounds(10, 10, 130, 30);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
|
2023-12-01 20:42:28 +04:00
|
|
|
|
//Добавление элементов на панель panelTools
|
|
|
|
|
panelTools.add(panelSets);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
panelTools.add(buttonAddBus);
|
|
|
|
|
panelTools.add(positionTextField);
|
|
|
|
|
panelTools.add(buttonRemoveBus);
|
2023-12-01 20:42:28 +04:00
|
|
|
|
panelTools.add(buttonRefreshCollection);
|
|
|
|
|
panelTools.add(buttonQueue);
|
|
|
|
|
|
|
|
|
|
//Добавление элементов на панель panelSets
|
|
|
|
|
panelSets.add(nameOfSetTextField);
|
|
|
|
|
panelSets.add(buttonAddSet);
|
|
|
|
|
panelSets.add(buttonDelSet);
|
|
|
|
|
panelSets.add(listBoxSets);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
|
|
|
|
|
frameBusesCollection.add(panelBusesCollection, BorderLayout.CENTER);
|
|
|
|
|
frameBusesCollection.add(panelTools, BorderLayout.EAST);
|
|
|
|
|
frameBusesCollection.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonAddBus_Click(ActionEvent e) {
|
|
|
|
|
FormTrolleybus form = new FormTrolleybus();
|
2023-12-01 20:42:28 +04:00
|
|
|
|
if (listBoxSets.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
BusesGenericCollection<DrawingBus, DrawingObjectBus> obj = _storage.Get(listBoxSets.getSelectedValue());
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-24 21:37:08 +04:00
|
|
|
|
//Листенер для кнопки выбора автобуса на той форме
|
|
|
|
|
form.buttonSelect.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
|
|
|
|
DrawingBus SelectedBus = form.getBus();
|
2023-12-01 20:42:28 +04:00
|
|
|
|
if (obj.Add(SelectedBus) != -1)
|
2023-11-24 21:37:08 +04:00
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Объект добавлен");
|
2023-12-01 20:42:28 +04:00
|
|
|
|
obj.ShowBuses(panelBusesCollection);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
|
|
|
|
|
}
|
|
|
|
|
form.getFrame().dispose();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-01 20:42:28 +04:00
|
|
|
|
private void buttonRemoveBus_Click(ActionEvent e) {
|
|
|
|
|
if (listBoxSets.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
BusesGenericCollection<DrawingBus, DrawingObjectBus> obj = _storage.Get(listBoxSets.getSelectedValue());
|
|
|
|
|
if (obj == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-24 21:37:08 +04:00
|
|
|
|
String pos_string = positionTextField.getText();
|
|
|
|
|
//Если строка не заполнена (имеет то же значение, что и пустая строка), то считаем, что ввели 0
|
|
|
|
|
if (pos_string.compareTo(" ") == 0) {
|
|
|
|
|
pos_string = "0";
|
|
|
|
|
}
|
|
|
|
|
int pos;
|
|
|
|
|
//Могли ввести не цифры
|
|
|
|
|
try {
|
|
|
|
|
pos = Integer.parseInt(pos_string);
|
|
|
|
|
}
|
|
|
|
|
//Если ввели не цифры, то тоже считаем, что ввели 0
|
|
|
|
|
catch(Exception ex) {
|
|
|
|
|
pos = 0;
|
2023-12-01 20:42:28 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Добавление удаляемого автобуса в очередь перед самим удалением
|
|
|
|
|
_queueCollection.Push(obj.Get(pos));
|
|
|
|
|
if (obj.Remove(pos)) {
|
2023-11-24 21:37:08 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Объект удален");
|
2023-12-01 20:42:28 +04:00
|
|
|
|
obj.ShowBuses(panelBusesCollection);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonRefreshCollection_Click(ActionEvent e) {
|
2023-12-01 20:42:28 +04:00
|
|
|
|
if (listBoxSets.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
BusesGenericCollection<DrawingBus, DrawingObjectBus> obj = _storage.Get(listBoxSets.getSelectedValue());
|
|
|
|
|
if (obj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
obj.ShowBuses(panelBusesCollection);
|
2023-11-24 21:37:08 +04:00
|
|
|
|
}
|
2023-12-01 20:42:28 +04:00
|
|
|
|
|
|
|
|
|
private void buttonAddSet_Click(ActionEvent e) {
|
|
|
|
|
String name = nameOfSetTextField.getText();
|
|
|
|
|
if (name == null || name.trim().length() == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.AddSet(nameOfSetTextField.getText());
|
|
|
|
|
ReloadObjects();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonDelSet_Click(ActionEvent e) {
|
|
|
|
|
if (listBoxSets.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_storage.DelSet(listBoxSets.getSelectedValue());
|
|
|
|
|
ReloadObjects();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void buttonQueue_Click(ActionEvent e) {
|
|
|
|
|
// Если в очередь ничего не добавили, то новую форму открывать не надо
|
|
|
|
|
if (_queueCollection.Size() == 0) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Очередь пустая");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
FormTrolleybus form = new FormTrolleybus();
|
|
|
|
|
// Вывод в форме первого в очереди объекта и удаление его из очереди
|
|
|
|
|
form.ShowBus(_queueCollection.Pop());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ReloadObjects() {
|
|
|
|
|
int index = listBoxSets.getSelectedIndex();
|
|
|
|
|
listBoxModel.clear();
|
|
|
|
|
List<String> keys = _storage.Keys();
|
|
|
|
|
for (int i = 0; i < keys.size(); i++) {
|
|
|
|
|
listBoxModel.addElement(keys.get(i));
|
|
|
|
|
}
|
|
|
|
|
if (listBoxModel.size() > 0 && (index == -1 || index >= listBoxModel.size())) {
|
|
|
|
|
listBoxSets.setSelectedIndex(0);
|
|
|
|
|
}
|
|
|
|
|
else if (listBoxModel.size() > 0) {
|
|
|
|
|
listBoxSets.setSelectedIndex(index);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-24 21:37:08 +04:00
|
|
|
|
}
|