PIbd-14_Antonova_A.A.__Hard/FormDumpTruckCollection.java

226 lines
8.8 KiB
Java
Raw Permalink Normal View History

2024-06-10 13:56:47 +04:00
import CollectionGenericObjects.AbstractCompany;
import CollectionGenericObjects.MassiveGenericObjects;
import CollectionGenericObjects.TruckShearingService;
import Drawnings.CanvasFormDumpTruckCollection;
import Drawnings.DrawningDumpTruck;
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.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.text.ParseException;
import java.util.Random;
import static java.lang.Integer.parseInt;
public class FormDumpTruckCollection extends JFrame {
private String title;
private Dimension dimension;
public static CanvasFormDumpTruckCollection<DrawningTruck> _canvasDumpTruck = new CanvasFormDumpTruckCollection<DrawningTruck>();
private static AbstractCompany _company = null;
private JButton CreateDumpTruckButton = new JButton("Создать самосвал");;
private JButton CreateTruckButton = new JButton("Создать грузовик");
private JButton RemoveButton = new JButton("Удалить");
private JButton GoToCheckButton = new JButton("Передать на тесты");
private JButton RandomButton = new JButton("Рандомный грузовик");
private JButton RefreshButton = new JButton("Обновить");
private JComboBox ComboBoxCollections = new JComboBox(new String[]{"", "Хранилище"});
private JFormattedTextField MaskedTextField;
private Random random = new Random();
public FormDumpTruckCollection(String title, Dimension dimension) {
this.title = title;
this.dimension = dimension;
}
public static void canvasShow() {
_company.SetPosition();
_canvasDumpTruck.SetCollectionToCanvas(_company);
_canvasDumpTruck.repaint();
}
private void CreateObject(String type){
DrawningTruck _drawningTruck;
Color bodyColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
switch(type){
case "DrawningTruck":
_drawningTruck = new DrawningTruck(random.nextInt(100, 300), random.nextInt(1000, 3000),
bodyColor);
break;
case "DrawningDumpTruck":
Color additionalColor = new Color(random.nextInt(0, 256), random.nextInt(0, 256), random.nextInt(0, 256));
_drawningTruck = new DrawningDumpTruck(random.nextInt(100, 300), random.nextInt(1000, 3000),
bodyColor, additionalColor,
random.nextBoolean(), random.nextBoolean());
break;
default:
return;
}
if (_company._collection.Insert(_drawningTruck) >= 0){
JOptionPane.showMessageDialog(null, "Объект добавлен");
canvasShow();
}
else{
JOptionPane.showMessageDialog(null, "Объект не удалось добавить");
}
}
public Color getColor() {
Color initializator = new Color(random.nextInt(0, 256),random.nextInt(0, 256),random.nextInt(0, 256));
Color _color = JColorChooser.showDialog(this, "Выберите цвет", initializator);
return _color;
}
public void Form(){
setTitle(title);
setMinimumSize(dimension);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MaskFormatter _mask = null;
try {
_mask = new MaskFormatter("##");
_mask.setPlaceholder("00");
} catch (ParseException e) {
throw new RuntimeException(e);
}
MaskedTextField = new JFormattedTextField(_mask);
ComboBoxCollections.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
switch (ComboBoxCollections.getSelectedItem().toString()) {
case "Хранилище":
_company = new TruckShearingService(getWidth()-200, getHeight()-70, new MassiveGenericObjects<DrawningTruck>());
break;
}
}
});
CreateTruckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawningTruck");
}
});
CreateDumpTruckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
CreateObject("DrawningDumpTruck");
}
});
RemoveButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null || MaskedTextField.getText() == null) {
return;
}
int pos = parseInt(MaskedTextField.getText());
int resultConfirmDialog = JOptionPane.showConfirmDialog(null,
"Удалить", "Удаление",
JOptionPane.YES_NO_OPTION);
if (resultConfirmDialog == JOptionPane.NO_OPTION) return;
if (_company._collection.Remove(pos) != null) {
JOptionPane.showMessageDialog(null, "Объект удален");
canvasShow();
}
else {
JOptionPane.showMessageDialog(null, "Объект не удалось удалить");
}
}
});
GoToCheckButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
DrawningTruck _truck = null;
int counter = 100;
while (_truck == null)
{
_truck = _company.GetRandomObject();
counter--;
if (counter <= 0)
{
break;
}
}
if (_truck == null)
{
return;
}
FormDumpTruck form = new FormDumpTruck("Самосвал", new Dimension(900,565));
form.Form(_truck);
}
});
RandomButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
FormAdditionalCollection form = new FormAdditionalCollection();
form.setCompany(_company);
}
});
RefreshButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (_company == null)
{
return;
}
canvasShow();
}
});
_canvasDumpTruck.setBounds(0, 0, getWidth()-200, getHeight());
ComboBoxCollections.setBounds(getWidth()-170, 10, 150, 20);
CreateTruckButton.setBounds(getWidth()-170, 60, 160, 30);
CreateDumpTruckButton.setBounds(getWidth()-170, 100, 160, 30);
MaskedTextField.setBounds(getWidth()-170,200,150,30);
RemoveButton.setBounds(getWidth()-170, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-170, 280, 150, 30);
RandomButton.setBounds(getWidth()-170, 320, 150, 30);
RefreshButton.setBounds(getWidth()-170, getHeight()-90, 150, 30);
setSize(dimension.width,dimension.height);
setLayout(null);
add(_canvasDumpTruck);
add(ComboBoxCollections);
add(CreateTruckButton);
add(CreateDumpTruckButton);
add(MaskedTextField);
add(RemoveButton);
add(GoToCheckButton);
add(RandomButton);
add(RefreshButton);
setVisible(true);
addComponentListener(new ComponentAdapter() {
public void componentResized(ComponentEvent e) {
_canvasDumpTruck.setBounds(0, 0, getWidth()-200, getHeight()-70);
ComboBoxCollections.setBounds(getWidth()-170, 10, 150, 20);
CreateTruckButton.setBounds(getWidth()-170, 60, 160, 30);
CreateDumpTruckButton.setBounds(getWidth()-170, 100, 160, 30);
MaskedTextField.setBounds(getWidth()-170,200,150,30);
RemoveButton.setBounds(getWidth()-170, 240, 150, 30);
GoToCheckButton.setBounds(getWidth()-170, 280, 150, 30);
RandomButton.setBounds(getWidth()-170, 320, 150, 30);
RefreshButton.setBounds(getWidth()-170, getHeight()-90, 150, 30);
}
});
}
}