407 lines
16 KiB
Java
407 lines
16 KiB
Java
import javax.swing.*;
|
||
import javax.swing.event.ListSelectionEvent;
|
||
import javax.swing.event.ListSelectionListener;
|
||
import javax.swing.filechooser.FileNameExtensionFilter;
|
||
import java.awt.*;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.awt.image.CropImageFilter;
|
||
import java.io.File;
|
||
import java.util.LinkedList;
|
||
import java.util.PriorityQueue;
|
||
import java.util.Queue;
|
||
|
||
class CollectionFrame extends JComponent {
|
||
private final CarsGenericStorage _tanksStorage;
|
||
private final GarageFrame frame;
|
||
private Queue<DrawTanker> removedTankers;
|
||
protected final int Width;
|
||
protected final int Height;
|
||
public CollectionFrame(int width, int height, GarageFrame fr)
|
||
{
|
||
Width = width;
|
||
Height = height;
|
||
_tanksStorage = new CarsGenericStorage(Width, Height);
|
||
frame = fr;
|
||
removedTankers = new LinkedList<>();
|
||
}
|
||
|
||
@Override
|
||
public void paintComponent(Graphics g) {
|
||
super.paintComponent(g);
|
||
if (frame.listStorage.getSelectedIndex() == -1)
|
||
return;
|
||
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||
if (obj == null)
|
||
return;
|
||
if (obj.ShowCars() == null)
|
||
return;
|
||
g.drawImage(obj.ShowCars(), 0, 0, this);
|
||
super.repaint();
|
||
}
|
||
|
||
protected void ReloadObjects()
|
||
{
|
||
int index = frame.listStorage.getSelectedIndex();
|
||
frame.listModel.clear();
|
||
for (String key : _tanksStorage.Keys())
|
||
{
|
||
frame.listModel.addElement(key);
|
||
}
|
||
if (!frame.listModel.isEmpty() && (index == -1 || index >= frame.listModel.size()))
|
||
{
|
||
frame.listStorage.setSelectedIndex(0);
|
||
}
|
||
else if (!frame.listModel.isEmpty() && index > -1 && index < frame.listModel.size())
|
||
{
|
||
frame.listStorage.setSelectedIndex(index);
|
||
}
|
||
|
||
}
|
||
protected void ButtonAddObject_Click()
|
||
{
|
||
String name = frame.nameStorageField.getText();
|
||
if (name.length() == 0)
|
||
return;
|
||
_tanksStorage.AddSet(name);
|
||
ReloadObjects();
|
||
}
|
||
|
||
protected void ButtonRemoveObject_Click()
|
||
{
|
||
if (frame.listStorage.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
if (JOptionPane.showConfirmDialog(null, "Delete object " + frame.listStorage.getSelectedValue() + "?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
|
||
{
|
||
return;
|
||
}
|
||
_tanksStorage.DelSet(frame.listStorage.getSelectedValue());
|
||
ReloadObjects();
|
||
}
|
||
|
||
protected void ButtonAddTank_Click()
|
||
{
|
||
if (frame.listStorage.getSelectedIndex() == -1)
|
||
return;
|
||
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||
if (obj == null)
|
||
return;
|
||
ConfigFrame form = new ConfigFrame();
|
||
form.setVisible(true);
|
||
form.buttonAdd.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
DrawTanker dT = form.Gasoline._tanker;
|
||
if (dT != null && obj.plus(dT) != -1)
|
||
{
|
||
form.dispose();
|
||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Объект добавлен");
|
||
|
||
}
|
||
else
|
||
{
|
||
form.dispose();
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Не удалось добавить объект");
|
||
}
|
||
}
|
||
}
|
||
);
|
||
}
|
||
|
||
protected void ButtonRemoveTank_Click(String text)
|
||
{
|
||
if (frame.listStorage.getSelectedIndex() == -1)
|
||
return;
|
||
var obj = _tanksStorage.get(frame.listStorage.getSelectedValue());
|
||
if (obj == null)
|
||
return;
|
||
if (JOptionPane.showConfirmDialog(null, "Delete Tanker?", "Delete", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
|
||
{
|
||
int index = -1;
|
||
DrawTanker deleted = null;
|
||
try {
|
||
deleted = obj.minus(Integer.parseInt(text));
|
||
} catch(NumberFormatException e) {
|
||
}
|
||
if (deleted != null)
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
removedTankers.add(deleted);
|
||
super.repaint();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
}
|
||
}
|
||
super.repaint();
|
||
}
|
||
|
||
protected void ButtonUpdate_Click() {super.repaint();}
|
||
|
||
protected void ButtonCreateDeleted_Click()
|
||
{
|
||
if (removedTankers.isEmpty())
|
||
return;
|
||
var tank = removedTankers.poll();
|
||
BaseFrame f = new BaseFrame(tank);
|
||
f.setVisible(true);
|
||
}
|
||
|
||
protected void OpenFile_Click()
|
||
{
|
||
JFileChooser fileChooser = new JFileChooser();
|
||
|
||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||
|
||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||
|
||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||
File selectedFile = fileChooser.getSelectedFile();
|
||
if (_tanksStorage.LoadData(selectedFile.getAbsolutePath())) {
|
||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||
} else {
|
||
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
ReloadObjects();
|
||
}
|
||
protected void SaveFile_Click()
|
||
{
|
||
JFileChooser fileChooser = new JFileChooser();
|
||
fileChooser.setDialogTitle("Выберите файл для сохранения данных");
|
||
|
||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||
|
||
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||
File selectedFile = fileChooser.getSelectedFile();
|
||
if (_tanksStorage.SaveData(selectedFile.getAbsolutePath()))
|
||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||
else
|
||
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
protected void LoadFileSingle_Click()
|
||
{
|
||
JFileChooser fileChooser = new JFileChooser();
|
||
|
||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||
|
||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||
|
||
fileChooser.setDialogTitle("Выберите файл для загрузки данных");
|
||
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||
File selectedFile = fileChooser.getSelectedFile();
|
||
if (_tanksStorage.LoadDataSingle(selectedFile.getAbsolutePath())) {
|
||
JOptionPane.showMessageDialog(null, "Загрузка прошла успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||
} else {
|
||
JOptionPane.showMessageDialog(null, "Не загрузилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
ReloadObjects();
|
||
}
|
||
protected void SaveFileSingle_Click(JList<String> jListStorage)
|
||
{
|
||
if (jListStorage.getSelectedValue() == null){
|
||
JOptionPane.showMessageDialog(null, "Не выбран гараж", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||
return;
|
||
}
|
||
|
||
JFileChooser fileChooser = new JFileChooser();
|
||
fileChooser.setDialogTitle("Выберите файл для сохранения данных");
|
||
|
||
// Установка фильтра для файлов с определенным расширением (например, .txt)
|
||
fileChooser.setFileFilter(new FileNameExtensionFilter("Текстовые файлы (*.txt)", "txt"));
|
||
|
||
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
|
||
File selectedFile = fileChooser.getSelectedFile();
|
||
if (_tanksStorage.SaveDataSingle(selectedFile.getAbsolutePath(), jListStorage.getSelectedValue()))
|
||
JOptionPane.showMessageDialog(null, "Сохранение прошло успешно", "Результат", JOptionPane.INFORMATION_MESSAGE);
|
||
else
|
||
JOptionPane.showMessageDialog(null, "Не сохранилось", "Результат", JOptionPane.ERROR_MESSAGE);
|
||
}
|
||
}
|
||
}
|
||
|
||
class GarageFrame extends JFrame {
|
||
public GarageFrame()
|
||
{
|
||
initUI();
|
||
}
|
||
|
||
protected static final int Width = 1000;
|
||
protected static final int Height = 600;
|
||
CollectionFrame Collection;
|
||
|
||
JButton addTankerButton;
|
||
JTextField indexTankerField;
|
||
JButton deleteTankerButton;
|
||
JButton updateCollectionButton;
|
||
JList<String> listStorage;
|
||
DefaultListModel<String> listModel;
|
||
JButton addCollectionButton;
|
||
JTextField nameStorageField;
|
||
JButton deleteCollectionbutton;
|
||
JButton createDeletedButton;
|
||
|
||
private void initUI()
|
||
{
|
||
setSize(Width, Height);
|
||
setTitle("TankGasoline");
|
||
setLocationRelativeTo(null);
|
||
setLayout(null);
|
||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||
setResizable(false);
|
||
|
||
|
||
addTankerButton = new JButton("Add tanker");
|
||
addTankerButton.setLayout(null);
|
||
addTankerButton.setBounds(Width-200, Height-550, 200, 30);
|
||
add(addTankerButton);
|
||
addTankerButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.ButtonAddTank_Click();
|
||
}
|
||
});
|
||
|
||
indexTankerField = new JTextField();
|
||
indexTankerField.setBounds(Width- 200, Height-500, 200, 30);
|
||
indexTankerField.setLayout(null);
|
||
add(indexTankerField);
|
||
|
||
deleteTankerButton = new JButton("Delete tanker");
|
||
deleteTankerButton.setLayout(null);
|
||
deleteTankerButton.setBounds(Width-200, Height-450, 200, 30);
|
||
add(deleteTankerButton);
|
||
deleteTankerButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.ButtonRemoveTank_Click(indexTankerField.getText());
|
||
}
|
||
});
|
||
|
||
updateCollectionButton = new JButton("Update collection");
|
||
updateCollectionButton.setLayout(null);
|
||
updateCollectionButton.setBounds(Width-200, Height-400, 200, 30);
|
||
add(updateCollectionButton);
|
||
updateCollectionButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.ButtonUpdate_Click();
|
||
}
|
||
});
|
||
|
||
listModel = new DefaultListModel<>();
|
||
listStorage = new JList<String>(listModel);
|
||
listStorage.setLayout(null);
|
||
listStorage.setBounds(Width - 190, Height - 250, 180, 130);
|
||
add(listStorage);
|
||
listStorage.addListSelectionListener(new ListSelectionListener() {
|
||
@Override
|
||
public void valueChanged(ListSelectionEvent e) {
|
||
Collection.repaint();
|
||
}
|
||
});
|
||
|
||
|
||
addCollectionButton = new JButton("Add Collection");
|
||
addCollectionButton.setLayout(null);
|
||
addCollectionButton.setBounds(Width-190, Height - 350, 180, 30);
|
||
add(addCollectionButton);
|
||
addCollectionButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.ButtonAddObject_Click();
|
||
}
|
||
});
|
||
|
||
nameStorageField = new JTextField();
|
||
nameStorageField.setBounds(Width- 190, Height- 300, 180, 30);
|
||
nameStorageField.setLayout(null);
|
||
add(nameStorageField);
|
||
|
||
|
||
deleteCollectionbutton = new JButton("Remove Collection");
|
||
deleteCollectionbutton.setBounds(Width-190, Height - 100, 180, 30);
|
||
deleteCollectionbutton.setLayout(null);
|
||
add(deleteCollectionbutton);
|
||
deleteCollectionbutton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.ButtonRemoveObject_Click();
|
||
}
|
||
});
|
||
|
||
createDeletedButton = new JButton("Create deleted");
|
||
createDeletedButton.setLayout(null);
|
||
createDeletedButton.setBounds(Width - 200, 10, 200, 30);
|
||
add(createDeletedButton);
|
||
createDeletedButton.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.ButtonCreateDeleted_Click();
|
||
}
|
||
});
|
||
|
||
Collection = new CollectionFrame(Width-200, Height, this);
|
||
Collection.setLayout(null);
|
||
Collection.setBounds(0, 0, Width-200, Height);
|
||
add(Collection);
|
||
|
||
JMenuBar menuBar = new JMenuBar();
|
||
JMenu fileMenu = new JMenu("File");
|
||
JMenuItem openItem = new JMenuItem("Open");
|
||
openItem.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.OpenFile_Click();
|
||
}
|
||
}
|
||
);
|
||
|
||
JMenuItem saveItem = new JMenuItem("Save");
|
||
saveItem.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.SaveFile_Click();
|
||
}}
|
||
);
|
||
|
||
JMenuItem openItemSingle = new JMenuItem("Open single");
|
||
openItemSingle.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.LoadFileSingle_Click();
|
||
}
|
||
}
|
||
);
|
||
|
||
JMenuItem saveItemSingle = new JMenuItem("Save single");
|
||
saveItemSingle.addActionListener(new ActionListener() {
|
||
@Override
|
||
public void actionPerformed(ActionEvent e) {
|
||
Collection.SaveFileSingle_Click(listStorage);
|
||
}
|
||
}
|
||
);
|
||
|
||
fileMenu.add(openItem);
|
||
fileMenu.add(saveItem);
|
||
fileMenu.add(openItemSingle);
|
||
fileMenu.add(saveItemSingle);
|
||
|
||
menuBar.add(fileMenu);
|
||
menuBar.setBounds(Width - 50, Height - 70, 50, 20);
|
||
add(menuBar);
|
||
}
|
||
|
||
} |