PIbd21.LyovushkinaA.A.Conta.../FormShipCollection.java
2023-11-04 20:38:51 +04:00

231 lines
9.3 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 java.awt.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.*;
import java.util.Stack;
public class FormShipCollection {
class Canvas extends JComponent{
public Canvas(){}
public void paintComponent(Graphics g){
super.paintComponent(g);
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if(obj.ShowShips() != null){
g.drawImage(obj.ShowShips(), 0, 0, this);
}
super.repaint();
}
}
Canvas canv;
static int pictureBoxWidth = 820;
static int pictureBoxHeight = 580;
private Stack<DrawingShip> removedShips;
private ShipGenericStorage _storage;
private JList<String> jListStorage;
private DefaultListModel<String> listModel;
private void ReloadObjects()
{
int index = jListStorage.getSelectedIndex();
listModel.clear();
for (String key : _storage.Keys()) {
listModel.addElement(key);
}
if (listModel.size() > 0 && (index == -1 || index >= listModel.size()))
{
jListStorage.setSelectedIndex(0);
}
else if (listModel.size() > 0 && index > -1 && index < listModel.size())
{
jListStorage.setSelectedIndex(index);
}
}
public void Draw(){
canv.repaint();
}
FormShipCollection(){
canv = new Canvas();
JFrame w = new JFrame("FormShipCollection");
_storage = new ShipGenericStorage(pictureBoxWidth, pictureBoxHeight);
listModel = new DefaultListModel<String>();
jListStorage = new JList<String>(listModel);
JButton ButtonAddShip = new JButton("Добавить кораблик");
JButton ButtonDeleteShip = new JButton("Удалить кораблик");
JButton ButtonRefreshCollection = new JButton("Обновить коллекцию");
JButton forNewForm = new JButton("новая форма");
JButton buttonGetRemoved = new JButton("Получить удалённое");
JButton buttonAddSet = new JButton("Добавить коллекцию");
JButton buttonRemoveSet = new JButton("Удалить коллекцию");
JTextField textBoxSetName = new JTextField();
JTextField TextBoxNumber = new JTextField();
ButtonAddShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
FormContainerShip form = new FormContainerShip();
form.buttonSelectedShip.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
if(obj.Add(form._drawingShip) != -1){
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
else{
JOptionPane.showMessageDialog(null, "Объект не добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
form.w.dispose();
}
}
);
}
}
);
forNewForm.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
NewFormRand newFormRand = new NewFormRand();
}
}
);
removedShips = new Stack<DrawingShip>();
ButtonDeleteShip.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
var obj = _storage.get(jListStorage.getSelectedValue());
if (obj == null)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
int pos = Integer.parseInt(TextBoxNumber.getText());
var rem = obj.remove(pos);
if (rem != null)
{
removedShips.push(rem);
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
Draw();
}
else
{
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
}
}
}
);
buttonGetRemoved.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (removedShips.empty()){
JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
FormContainerShip form = new FormContainerShip();
form._drawingShip = removedShips.peek();
form._drawingShip.SetPosition(100, 100);
removedShips.pop();
form.Draw();
}
}
);
ButtonRefreshCollection.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
Draw();
}
}
);
buttonAddSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (textBoxSetName.getText().length() == 0)
{
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
return;
}
_storage.AddSet(textBoxSetName.getText());
ReloadObjects();
}
}
);
jListStorage.addListSelectionListener(
new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e){
Draw();
}
}
);
buttonRemoveSet.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
if (jListStorage.getSelectedIndex() == -1)
{
return;
}
if (JOptionPane.showConfirmDialog(null, "Удалить объект " + jListStorage.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
{
return;
}
_storage.DelSet(jListStorage.getSelectedValue());
ReloadObjects();
}
}
);
w.setSize (1000, 600);
w.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
w.setLayout(null);
canv.setBounds(0, 0, pictureBoxWidth, pictureBoxHeight);
ButtonAddShip.setBounds(pictureBoxWidth, 0, 120, 20);
TextBoxNumber.setBounds(pictureBoxWidth, 50, 120, 20);
ButtonDeleteShip.setBounds(pictureBoxWidth, 80, 120, 20);
ButtonRefreshCollection.setBounds(pictureBoxWidth, 120, 120, 20);
forNewForm.setBounds(pictureBoxWidth, 150, 120, 20);
buttonAddSet.setBounds(pictureBoxWidth, 180, 120, 20);
textBoxSetName.setBounds(pictureBoxWidth, 210, 120, 20);
jListStorage.setBounds(pictureBoxWidth, 240, 120, 60);
buttonRemoveSet.setBounds(pictureBoxWidth, 310, 120, 20);
buttonGetRemoved.setBounds(pictureBoxWidth, 340, 120, 20);
w.add(canv);
w.add(ButtonAddShip);
w.add(ButtonDeleteShip);
w.add(ButtonRefreshCollection);
w.add(forNewForm);
w.add(TextBoxNumber);
w.add(buttonAddSet);
w.add(textBoxSetName);
w.add(jListStorage);
w.add(buttonRemoveSet);
w.add(buttonGetRemoved);
w.setVisible(true);
}
}