PIbd-23_Panina_A.D.Cruiser..../CruiserCollectionFrame.java
2023-12-29 17:26:50 +04:00

182 lines
8.5 KiB
Java
Raw Permalink 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 javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class CruiserCollectionFrame extends JFrame {
private CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers;
public CruiserCollectionFrame() {
this.setSize(800, 600);
this.setTitle("Collection");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setLocationRelativeTo(null);
CollectionPanel panel = new CollectionPanel();
this.add(panel);
this.setVisible(true);
}
public class CollectionPanel extends JPanel {
private CruiserGenericStorage _storage;
TrashCollection<DrawingCruiser> _trash = new TrashCollection<>();
private JList<String> listBoxStoragesJList;
private DefaultListModel<String> listBoxStorages;
static final int SCREEN_W = 800;
static final int SCREEN_H = 600;
CollectionPanel() {
_storage = new CruiserGenericStorage(SCREEN_W,
SCREEN_H);
this.setLayout(null);
this.setPreferredSize(new Dimension(SCREEN_W, SCREEN_H));
JTextField textFieldRemove = new JTextField();
textFieldRemove.setBounds(600, 200, 150, 30);
this.add(textFieldRemove);
JButton ButtonAddCruiser = new JButton("Добавить объект");
ButtonAddCruiser.setBounds(600, 150, 150, 30);
ButtonAddCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
FormCruiserConfig form = new FormCruiserConfig();
if (form.getSelectedCruiser() != null) {
if (_cruisers.plus(_cruisers, form.SelectedCruiser)) {
JOptionPane.showMessageDialog(null, "Объект добавлен");
} else {
JOptionPane.showMessageDialog(null, "Не удалось добавить объект");
}
}
repaint();
}
});
this.add(ButtonAddCruiser);
JButton ButtomRemoveCruiser = new JButton("Удалить объект");
ButtomRemoveCruiser.setBounds(600, 250, 150, 30);
ButtomRemoveCruiser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser> _cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
if (_cruisers == null) {
return;
}
String tmp = textFieldRemove.getText();
int num;
try {
num = Integer.parseInt(tmp);
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Введите число", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
DrawingCruiser deletedCruiser = _cruisers.Get(num);
_trash.Add(deletedCruiser);
_cruisers.minus(_cruisers, num);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
repaint();
}
});
this.add(ButtomRemoveCruiser);
JButton ButtonRefresh = new JButton("Обновить");
ButtonRefresh.setBounds(600, 300, 150, 30);
ButtonRefresh.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
repaint();
}
});
this.add(ButtonRefresh);
JButton ButtonDelStorage = new JButton("Удалить набор");
ButtonDelStorage.setBounds(600, 450, 170, 30);
ButtonDelStorage.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_storage.DelSet(listBoxStoragesJList.getSelectedValue());
ReloadObjects();
}
});
this.add(ButtonDelStorage);
JButton ButtonTrash = new JButton("Удаленные объекты");
ButtonTrash.setBounds(600, 500, 170, 30);
ButtonTrash.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(_trash.GetSize() == 0)
return;
GameFrame form = new GameFrame(_trash.GetLast());
_trash.PopHead();
}
});
this.add(ButtonTrash);
JTextField textBoxStorageName = new JTextField();
textBoxStorageName.setBounds(600, 10, 150, 30);
this.add(textBoxStorageName);
JButton ButtonAddObject = new JButton("Добавить набор");
ButtonAddObject.setBounds(600, 40, 150, 30);
ButtonAddObject.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (textBoxStorageName.getText().equals( "") || textBoxStorageName.getText().equals(null))
{
return;
}
_storage.AddSet(textBoxStorageName.getText());
ReloadObjects();
repaint();
}});
this.add(ButtonAddObject);
listBoxStorages = new DefaultListModel<>();
listBoxStoragesJList = new JList<>(listBoxStorages);
listBoxStoragesJList.setBounds(600, 350, 150, 70);
listBoxStoragesJList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {
if(listBoxStoragesJList.getSelectedIndex() == -1) {
return;
}
_cruisers = _storage.Get(listBoxStoragesJList.getSelectedValue());
repaint();
}
}
});
this.add(listBoxStoragesJList);
_cruisers = new CruiserGenericCollection<DrawingCruiser, DrawingObjectCruiser>(SCREEN_W, SCREEN_H);
}
private void ReloadObjects()
{
int index = listBoxStoragesJList.getSelectedIndex();
listBoxStorages.clear();
List<String> keys = _storage.Keys();
for(int i = 0; i < keys.size(); i++){
listBoxStorages.addElement(keys.get(i));
}
if(listBoxStorages.size() > 0 && (index == -1 || index >= listBoxStorages.size()))
listBoxStoragesJList.setSelectedIndex(0);
else if(listBoxStorages.size() > 0)
listBoxStoragesJList.setSelectedIndex(index);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
draw(g);
}
public void draw(Graphics g) {
if (listBoxStoragesJList.getSelectedIndex() != -1) {
_storage.getByIndex(listBoxStorages.get(listBoxStoragesJList.getSelectedIndex())).Showcruisers(g);
_cruisers.Showcruisers(g);
}
}
}
}