280 lines
11 KiB
Java
280 lines
11 KiB
Java
package laba1Loco;
|
||
|
||
import java.awt.Graphics;
|
||
import java.awt.Graphics2D;
|
||
import java.awt.event.ActionEvent;
|
||
import java.awt.event.ActionListener;
|
||
import java.util.LinkedList;
|
||
|
||
import javax.swing.DefaultListModel;
|
||
import javax.swing.JButton;
|
||
import javax.swing.JComboBox;
|
||
import javax.swing.JComponent;
|
||
import javax.swing.JFrame;
|
||
import javax.swing.JList;
|
||
import javax.swing.JOptionPane;
|
||
import javax.swing.JTextField;
|
||
import javax.swing.event.ListSelectionEvent;
|
||
import javax.swing.event.ListSelectionListener;
|
||
|
||
public class FormTrainCollecltion {
|
||
private 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.ShowTrains() != null) {
|
||
g.drawImage(obj.ShowTrains(), 0, 0, this);
|
||
}
|
||
super.repaint();
|
||
}
|
||
}
|
||
Canvas canv;
|
||
static int pictureBoxWidth = 820;
|
||
static int pictureBoxHeight = 580;
|
||
private TrainsGenericStorage _storage;
|
||
public void Draw(){
|
||
canv.repaint();
|
||
}
|
||
|
||
private LinkedList<DrawingTrain> linkedListRemoved;
|
||
private JList<String> jListStorage;
|
||
private DefaultListModel<String> listModel;
|
||
/// <summary>
|
||
/// Заполнение listBoxObjects
|
||
/// </summary>
|
||
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);
|
||
}
|
||
}
|
||
|
||
FormTrainCollecltion(){
|
||
listModel = new DefaultListModel<String>();
|
||
jListStorage = new JList<String>(listModel);
|
||
canv = new Canvas();
|
||
JFrame w = new JFrame ("TrainCollecltion");
|
||
_storage = new TrainsGenericStorage(pictureBoxWidth, pictureBoxHeight);
|
||
|
||
JButton ButtonAddTrain = new JButton("Add Train");
|
||
ButtonAddTrain.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
if (jListStorage.getSelectedIndex() == -1)
|
||
{
|
||
return;
|
||
}
|
||
var obj = _storage.get(jListStorage.getSelectedValue());
|
||
if (obj == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
FormTrainConfig form = new FormTrainConfig();
|
||
form.buttonAdd.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
if (obj != null && obj.Add(form._train) != -1)
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Объект добавлен", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Объект добавлен");
|
||
Draw();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось добавить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Не удалось добавить объект");
|
||
}
|
||
form.w.dispose();
|
||
}
|
||
}
|
||
);
|
||
}
|
||
}
|
||
);
|
||
|
||
linkedListRemoved = new LinkedList<DrawingTrain>();
|
||
JTextField TextBoxNumber = new JTextField();
|
||
JButton ButtonRemoveTrain = new JButton("Remove Train");
|
||
ButtonRemoveTrain.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;
|
||
}
|
||
for (char it : TextBoxNumber.getText().toCharArray())
|
||
if (it < '0' || it > '9')
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Не удалось удалить объект");
|
||
return;
|
||
}
|
||
if (TextBoxNumber.getText().length() == 0)
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Не удалось удалить объект");
|
||
return;
|
||
}
|
||
|
||
int pos = Integer.parseInt(TextBoxNumber.getText());
|
||
var removed = obj.remove(pos);
|
||
if (removed != null)
|
||
{
|
||
linkedListRemoved.add(removed);
|
||
JOptionPane.showMessageDialog(null, "Объект удален", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Объект удален");
|
||
Draw();
|
||
}
|
||
else
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не удалось удалить объект", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Не удалось удалить объект");
|
||
}
|
||
}
|
||
}
|
||
);
|
||
|
||
JButton buttonGetRemoved = new JButton("buttonGetRemoved");
|
||
buttonGetRemoved.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
if (linkedListRemoved.size()==0){
|
||
JOptionPane.showMessageDialog(null, "Нет удалённых", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Нет удалённых");
|
||
return;
|
||
}
|
||
FormTrain form = new FormTrain();
|
||
form._drawingTrain = linkedListRemoved.getLast();
|
||
linkedListRemoved.removeLast();
|
||
form.Draw();
|
||
}
|
||
}
|
||
);
|
||
|
||
JButton ButtonRefreshCollection = new JButton("Refresh Collection");
|
||
ButtonRefreshCollection.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
Draw();
|
||
}
|
||
}
|
||
);
|
||
|
||
JButton toForm4GenericDopClass = new JButton("ToForm4GenericDopClass");
|
||
toForm4GenericDopClass.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass();
|
||
}
|
||
}
|
||
);
|
||
|
||
JTextField textBoxSetName = new JTextField();
|
||
JButton buttonAddSet = new JButton("Add Set");
|
||
buttonAddSet.addActionListener(
|
||
new ActionListener() {
|
||
public void actionPerformed(ActionEvent e){
|
||
if (textBoxSetName.getText().length() == 0)
|
||
{
|
||
JOptionPane.showMessageDialog(null, "Не все данные заполнены", "Информация", JOptionPane.INFORMATION_MESSAGE);
|
||
System.out.println("Не все данные заполнены");
|
||
return;
|
||
}
|
||
_storage.AddSet(textBoxSetName.getText());
|
||
ReloadObjects();
|
||
}
|
||
}
|
||
);
|
||
|
||
jListStorage.addListSelectionListener(
|
||
new ListSelectionListener() {
|
||
public void valueChanged(ListSelectionEvent e){
|
||
Draw();
|
||
}
|
||
}
|
||
);
|
||
|
||
JButton buttonRemoveSet = new JButton("Remove Set");
|
||
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);
|
||
ButtonAddTrain.setBounds(pictureBoxWidth, 0, 160, 20);
|
||
TextBoxNumber.setBounds(pictureBoxWidth, 30, 160, 20);
|
||
ButtonRemoveTrain.setBounds(pictureBoxWidth, 60, 160, 20);
|
||
ButtonRefreshCollection.setBounds(pictureBoxWidth, 90, 160, 20);
|
||
toForm4GenericDopClass.setBounds(pictureBoxWidth, 120, 160, 20);
|
||
|
||
buttonAddSet.setBounds(pictureBoxWidth, 150, 160, 20);
|
||
textBoxSetName.setBounds(pictureBoxWidth, 180, 160, 20);
|
||
jListStorage.setBounds(pictureBoxWidth, 210, 160, 80);
|
||
buttonRemoveSet.setBounds(pictureBoxWidth, 300, 160, 20);
|
||
|
||
buttonGetRemoved.setBounds(pictureBoxWidth, 330, 160, 20);
|
||
|
||
w.add(canv);
|
||
w.add(ButtonAddTrain);
|
||
w.add(ButtonRemoveTrain);
|
||
w.add(ButtonRefreshCollection);
|
||
w.add(TextBoxNumber);
|
||
w.add(toForm4GenericDopClass);
|
||
|
||
w.add(buttonAddSet);
|
||
w.add(textBoxSetName);
|
||
w.add(jListStorage);
|
||
w.add(buttonRemoveSet);
|
||
|
||
w.add(buttonGetRemoved);
|
||
|
||
w.setVisible(true);
|
||
}
|
||
}
|