2023-10-23 18:05:55 +04:00
package laba1Loco ;
import java.awt.Graphics ;
import java.awt.Graphics2D ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
2023-11-05 01:47:52 +04:00
import java.util.LinkedList ;
2023-10-23 18:05:55 +04:00
2023-10-24 02:19:14 +04:00
import javax.swing.DefaultListModel ;
2023-10-23 18:05:55 +04:00
import javax.swing.JButton ;
2023-10-24 02:19:14 +04:00
import javax.swing.JComboBox ;
2023-10-23 18:05:55 +04:00
import javax.swing.JComponent ;
import javax.swing.JFrame ;
2023-10-24 02:19:14 +04:00
import javax.swing.JList ;
2023-10-23 18:05:55 +04:00
import javax.swing.JOptionPane ;
import javax.swing.JTextField ;
2023-10-24 02:19:14 +04:00
import javax.swing.event.ListSelectionEvent ;
import javax.swing.event.ListSelectionListener ;
2023-10-23 18:05:55 +04:00
public class FormTrainCollecltion {
2023-10-23 21:52:50 +04:00
private class Canvas extends JComponent {
2023-10-23 18:05:55 +04:00
public Canvas ( ) {
}
public void paintComponent ( Graphics g ) {
super . paintComponent ( g ) ;
2023-10-24 02:19:14 +04:00
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 ) ;
2023-10-23 18:05:55 +04:00
}
super . repaint ( ) ;
}
}
Canvas canv ;
2023-10-24 02:19:14 +04:00
static int pictureBoxWidth = 820 ;
2023-10-23 18:05:55 +04:00
static int pictureBoxHeight = 580 ;
2023-10-24 02:19:14 +04:00
private TrainsGenericStorage _storage ;
2023-10-23 18:05:55 +04:00
public void Draw ( ) {
canv . repaint ( ) ;
}
2023-10-24 02:19:14 +04:00
2023-11-05 01:47:52 +04:00
private LinkedList < DrawingTrain > linkedListRemoved ;
2023-10-24 02:19:14 +04:00
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 ) ;
}
}
2023-10-23 18:05:55 +04:00
FormTrainCollecltion ( ) {
2023-10-24 02:19:14 +04:00
listModel = new DefaultListModel < String > ( ) ;
jListStorage = new JList < String > ( listModel ) ;
2023-10-23 18:05:55 +04:00
canv = new Canvas ( ) ;
JFrame w = new JFrame ( " TrainCollecltion " ) ;
2023-10-24 02:19:14 +04:00
_storage = new TrainsGenericStorage ( pictureBoxWidth , pictureBoxHeight ) ;
2023-10-23 18:05:55 +04:00
2023-10-24 02:19:14 +04:00
JButton ButtonAddTrain = new JButton ( " Add Train " ) ;
2023-10-23 18:05:55 +04:00
ButtonAddTrain . addActionListener (
new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
2023-10-24 02:19:14 +04:00
if ( jListStorage . getSelectedIndex ( ) = = - 1 )
{
return ;
}
var obj = _storage . get ( jListStorage . getSelectedValue ( ) ) ;
if ( obj = = null )
{
return ;
}
2023-10-23 18:05:55 +04:00
FormTrain form = new FormTrain ( ) ;
form . buttonSelectTrain . addActionListener (
new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
2023-10-24 02:19:14 +04:00
if ( obj . Add ( form . _drawingTrain ) ! = - 1 )
2023-10-23 18:05:55 +04:00
{
JOptionPane . showMessageDialog ( null , " Объект добавлен " , " Информация " , JOptionPane . INFORMATION_MESSAGE ) ;
System . out . println ( " Объект добавлен " ) ;
Draw ( ) ;
}
else
{
JOptionPane . showMessageDialog ( null , " Н е удалось добавить объект" , " Информация " , JOptionPane . INFORMATION_MESSAGE ) ;
System . out . println ( " Н е удалось добавить объект" ) ;
}
form . w . dispose ( ) ;
}
}
) ;
}
}
) ;
2023-11-05 01:47:52 +04:00
linkedListRemoved = new LinkedList < DrawingTrain > ( ) ;
2023-10-23 18:05:55 +04:00
JTextField TextBoxNumber = new JTextField ( ) ;
2023-10-24 02:19:14 +04:00
JButton ButtonRemoveTrain = new JButton ( " Remove Train " ) ;
2023-10-23 18:05:55 +04:00
ButtonRemoveTrain . addActionListener (
new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
2023-10-24 02:19:14 +04:00
if ( jListStorage . getSelectedIndex ( ) = = - 1 )
{
return ;
}
var obj = _storage . get ( jListStorage . getSelectedValue ( ) ) ;
if ( obj = = null )
{
return ;
}
2023-10-23 18:05:55 +04:00
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 ( ) ) ;
2023-11-05 01:47:52 +04:00
var removed = obj . remove ( pos ) ;
if ( removed ! = null )
2023-10-23 18:05:55 +04:00
{
2023-11-05 01:47:52 +04:00
linkedListRemoved . add ( removed ) ;
2023-10-23 18:05:55 +04:00
JOptionPane . showMessageDialog ( null , " Объект удален " , " Информация " , JOptionPane . INFORMATION_MESSAGE ) ;
System . out . println ( " Объект удален " ) ;
Draw ( ) ;
}
else
{
JOptionPane . showMessageDialog ( null , " Н е удалось удалить объект" , " Информация " , JOptionPane . INFORMATION_MESSAGE ) ;
System . out . println ( " Н е удалось удалить объект" ) ;
}
}
}
) ;
2023-11-05 01:47:52 +04:00
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 ( ) ;
}
}
) ;
2023-10-24 02:19:14 +04:00
JButton ButtonRefreshCollection = new JButton ( " Refresh Collection " ) ;
2023-10-23 18:05:55 +04:00
ButtonRefreshCollection . addActionListener (
new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
Draw ( ) ;
}
}
) ;
2023-10-23 21:52:50 +04:00
JButton toForm4GenericDopClass = new JButton ( " ToForm4GenericDopClass " ) ;
toForm4GenericDopClass . addActionListener (
new ActionListener ( ) {
public void actionPerformed ( ActionEvent e ) {
Form4GenericDopClass form4GenericDopClass = new Form4GenericDopClass ( ) ;
}
}
) ;
2023-10-24 02:19:14 +04:00
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 ( ) ;
}
}
) ;
2023-10-23 18:05:55 +04:00
w . setSize ( 1000 , 600 ) ;
w . setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE ) ;
w . setLayout ( null ) ;
canv . setBounds ( 0 , 0 , pictureBoxWidth , pictureBoxHeight ) ;
2023-10-24 02:19:14 +04:00
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 ) ;
2023-11-05 01:47:52 +04:00
buttonGetRemoved . setBounds ( pictureBoxWidth , 330 , 160 , 20 ) ;
2023-10-23 18:05:55 +04:00
w . add ( canv ) ;
w . add ( ButtonAddTrain ) ;
w . add ( ButtonRemoveTrain ) ;
w . add ( ButtonRefreshCollection ) ;
w . add ( TextBoxNumber ) ;
2023-10-23 21:52:50 +04:00
w . add ( toForm4GenericDopClass ) ;
2023-10-24 02:19:14 +04:00
w . add ( buttonAddSet ) ;
w . add ( textBoxSetName ) ;
w . add ( jListStorage ) ;
w . add ( buttonRemoveSet ) ;
2023-11-05 01:47:52 +04:00
w . add ( buttonGetRemoved ) ;
2023-10-23 18:05:55 +04:00
w . setVisible ( true ) ;
}
}