PIBD-13_Pazushkin_I.P._Hard/ProjectCleaningCar/src/FormCleaningCarCollection.java

275 lines
11 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import CollectionGenericObjects.*;
import Drawnings.DrawningCleaningCar;
import Drawnings.DrawningTruck;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Objects;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormCleaningCarCollection extends JFrame{
private AbstractCompany _company = null;
private StorageCollection<DrawningTruck> _storageCollection;
private LinkedList<DrawningTruck> _resurrected;
private JPanel panel;
private JPanel tools;
private JComboBox comboBoxSelectorCompany;
private JButton buttonAddTruck;
private JPanel pictureBox;
private JFormattedTextField maskedTextBox;
private JButton buttonDel;
private JButton buttonGoToCheck;
private JButton buttonRefresh;
private JButton buttonAdditionalCollection;
private JPanel panelStorage;
private JLabel labelCollectionName;
private JTextField textBoxCollectionName;
private JRadioButton radioButtonMassive;
private JRadioButton radioButtonList;
private JButton buttonCollectionAdd;
private JList listBoxCollection;
private JButton buttonCollectionDel;
private JButton buttonCreateCompany;
private JPanel panelCompanyTools;
private JButton buttonResurrected;
private JButton buttonIndex;
public FormCleaningCarCollection() {
setTitle("Коллекция уборочных машин");
setSize(1000, 600);
setLocation(200, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
_storageCollection = new StorageCollection<>();
_resurrected = new LinkedList<>();
tools.setBackground(new Color(220, 220, 220));
comboBoxSelectorCompany.addItem(new String(""));
comboBoxSelectorCompany.addItem(new String("Автопарк"));
//mask
MaskFormatter maskFormatter = null;
try {
maskFormatter = new MaskFormatter("##");
maskFormatter.setPlaceholder("00");
} catch (ParseException e) {
throw new RuntimeException(e);
}
MaskFormatter finalMaskFormatter = maskFormatter;
maskedTextBox.setFormatterFactory(new JFormattedTextField.AbstractFormatterFactory() {
@Override
public JFormattedTextField.AbstractFormatter getFormatter(JFormattedTextField tf) {
return finalMaskFormatter;
}
});
Init();
}
private void Init() {
//ComboBoxSelectorCompany_SelectedIndexChanged
comboBoxSelectorCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panelCompanyTools.setEnabled(false);
for (Component i : panelCompanyTools.getComponents()) {
i.setBackground(Color.GRAY);
}
}
});
//ButtonAddTruck_Click
buttonAddTruck.addActionListener(e -> {
FormTruckConfig form = new FormTruckConfig();
form.setVisible(true);
form.SetCompany(_company);
});
//ButtonDelTruck_Click
buttonDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || maskedTextBox.getText() == null) return;
switch (JOptionPane.showConfirmDialog(null, "Удалить?", "Удаление", JOptionPane.YES_NO_OPTION)) {
case 0:
int pos = parseInt(maskedTextBox.getText());
DrawningTruck truck = _company._collection.Remove(pos);
if (truck != null) {
_resurrected.addFirst(truck);
JOptionPane.showMessageDialog(null, "Объект удалён");
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
break;
case 1:
default:
return;
}
}
});
//ButtonGoToCheck_Click
buttonGoToCheck.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) return;
DrawningTruck truck = _company.GetRandomObject();
int counter = 100;
while (truck == null) {
truck = _company.GetRandomObject();
counter--;
if (counter <= 0) break;
}
if (truck == null) return;
FormCleaningCar formCleaningCar = new FormCleaningCar();
formCleaningCar.SetTruck(truck);
}
});
//ButtonRefresh_Click
buttonRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) return;
repaint();
}
});
buttonAdditionalCollection.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null) {
return;
}
FormAdditionalCollection formAdditionalCollection = new FormAdditionalCollection();
formAdditionalCollection.SetCompany(_company);
}
});
//ButtonCollectionAdd_Click
buttonCollectionAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textBoxCollectionName.getText().isEmpty() || (!radioButtonMassive.isSelected() && !radioButtonList.isSelected())) {
JOptionPane.showMessageDialog(null, "Не все данные заполнены");
return;
}
CollectionType collectionType = CollectionType.None;
if (radioButtonMassive.isSelected()) {
collectionType = CollectionType.Massive;
} else if (radioButtonList.isSelected()) {
collectionType = CollectionType.List;
}
_storageCollection.AddCollection(textBoxCollectionName.getText(), collectionType);
RefreshListBoxItems();
}
});
//ButtonCollectionDel_Click
buttonCollectionDel.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
return;
}
switch (JOptionPane.showConfirmDialog(null, "Удалить коллекцию?", "Удаление", JOptionPane.YES_NO_OPTION)) {
case 1:
return;
case 0:
_storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString());
break;
}
_storageCollection.DelCollection(listBoxCollection.getSelectedValue().toString());
RefreshListBoxItems();
}
});
//ButtonCreateCompany_Click
buttonCreateCompany.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxCollection.getSelectedIndex() < 0 || listBoxCollection.getSelectedValue() == null) {
JOptionPane.showMessageDialog(null, "Коллекция не выбрана");
return;
}
ICollectionGenericObjects<DrawningTruck> collection = _storageCollection.GetCollectionObject(listBoxCollection.getSelectedValue().toString());
if (collection == null) {
JOptionPane.showMessageDialog(null, "Коллекция не проинициализирована");
return;
}
switch (comboBoxSelectorCompany.getSelectedItem().toString()) {
case "Автопарк":
_company = new AutoParkService(pictureBox.getWidth(), pictureBox.getHeight(), collection);
break;
}
panelCompanyTools.setEnabled(true);
for (Component i : panelCompanyTools.getComponents()) {
i.setBackground(buttonCreateCompany.getBackground());
}
RefreshListBoxItems();
}
});
buttonResurrected.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_resurrected.peek() != null) {
FormCleaningCar form = new FormCleaningCar();
form.SetTruck(_resurrected.pop());
}
}
});
buttonIndex.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (listBoxCollection.getSelectedValue() == null || listBoxCollection.getSelectedIndex() < 0
|| _company == null || maskedTextBox.getText() == null) return;
int pos = parseInt(maskedTextBox.getText());
switch (JOptionPane.showConfirmDialog(null, "Удалить?", "Удаление", JOptionPane.YES_NO_OPTION)) {
case 0:
DrawningTruck truck = _storageCollection.Remove(listBoxCollection.getSelectedValue().toString(), pos);
if (truck != null) {
_resurrected.addFirst(truck);
JOptionPane.showMessageDialog(null, "Объект удалён");
repaint();
} else {
JOptionPane.showMessageDialog(null, "Не удалось удалить объект");
}
break;
case 1:
default:
return;
}
}
});
}
private void RefreshListBoxItems()
{
listBoxCollection.removeAll();
DefaultListModel<String> list = new DefaultListModel<String>();
for (String name : _storageCollection.Keys()) {
if (!Objects.equals(name, ""))
{
list.addElement(name);
}
}
listBoxCollection.setModel(list);
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (_company == null) return;
Graphics e = pictureBox.getGraphics();
e.setColor(pictureBox.getBackground());
e.fillRect(0,0, pictureBox.getWidth(), pictureBox.getHeight());
_company.Show(e);
}
public static void Show() {for (Frame i : Frame.getFrames()) {if (!i.isActive()) {i.repaint();}}}
}