2022-10-21 21:27:07 +04:00
|
|
|
|
import javax.swing.*;
|
|
|
|
|
import javax.swing.text.MaskFormatter;
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.text.ParseException;
|
2022-11-05 15:29:55 +04:00
|
|
|
|
import java.util.HashMap;
|
2022-12-04 17:50:10 +04:00
|
|
|
|
import org.apache.logging.log4j.Level;
|
|
|
|
|
import org.apache.logging.log4j.Logger;
|
2022-10-21 21:27:07 +04:00
|
|
|
|
|
|
|
|
|
public class FormMapWithSetLocomotives extends JComponent {
|
|
|
|
|
private BufferedImage bufferImg = null;
|
2022-11-05 15:29:55 +04:00
|
|
|
|
/// Словарь для выпадающего списка
|
|
|
|
|
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {
|
|
|
|
|
{
|
|
|
|
|
put("Simple Map", new SimpleMap());
|
|
|
|
|
put("Spike Map", new SpikeMap());
|
|
|
|
|
put("Rail Map", new RailMap());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
/// Объект от коллекции карт
|
|
|
|
|
private final MapsCollection _mapsCollection;
|
|
|
|
|
|
|
|
|
|
// Элементы на форме
|
|
|
|
|
JFrame formFrame;
|
|
|
|
|
Panel statusPanel;
|
|
|
|
|
TextField textFieldNewMapName;
|
|
|
|
|
JComboBox mapSelectComboBox;
|
|
|
|
|
DefaultListModel<String> mapsListModel = new DefaultListModel<>();
|
|
|
|
|
JScrollPane listScroller = new JScrollPane();
|
|
|
|
|
JList listBoxMaps;
|
|
|
|
|
|
2022-12-04 17:50:10 +04:00
|
|
|
|
private final Logger logger;
|
|
|
|
|
|
|
|
|
|
public FormMapWithSetLocomotives(Logger logger) {
|
|
|
|
|
|
|
|
|
|
this.logger = logger;
|
|
|
|
|
|
2022-11-05 15:29:55 +04:00
|
|
|
|
formFrame = new JFrame("Form Map With SetLocomotives");
|
2022-10-21 21:27:07 +04:00
|
|
|
|
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
|
|
|
formFrame.setSize(750, 500);
|
|
|
|
|
formFrame.setLocationRelativeTo(null);
|
2022-10-22 16:12:48 +04:00
|
|
|
|
|
2022-11-05 15:29:55 +04:00
|
|
|
|
statusPanel = new Panel();
|
2022-10-21 21:27:07 +04:00
|
|
|
|
statusPanel.setBackground(Color.WHITE);
|
2022-11-05 15:29:55 +04:00
|
|
|
|
statusPanel.setLayout(new GridLayout(0, 1, 20, 5));
|
2022-10-21 21:27:07 +04:00
|
|
|
|
setLayout(new BorderLayout());
|
|
|
|
|
add(statusPanel, BorderLayout.EAST);
|
|
|
|
|
|
2022-11-05 15:29:55 +04:00
|
|
|
|
// Текстовое поле для ввода названия карты
|
|
|
|
|
textFieldNewMapName = new TextField();
|
|
|
|
|
statusPanel.add(textFieldNewMapName);
|
|
|
|
|
|
2022-10-21 21:27:07 +04:00
|
|
|
|
// КомбоБокс с картами
|
|
|
|
|
String[] maps = {
|
|
|
|
|
"Simple Map",
|
|
|
|
|
"Spike Map",
|
|
|
|
|
"Rail Map"
|
|
|
|
|
};
|
2022-11-05 15:29:55 +04:00
|
|
|
|
mapSelectComboBox = new JComboBox(maps);
|
2022-10-21 21:27:07 +04:00
|
|
|
|
mapSelectComboBox.setEditable(true);
|
|
|
|
|
statusPanel.add(mapSelectComboBox);
|
|
|
|
|
|
2022-11-05 15:29:55 +04:00
|
|
|
|
// Initialization
|
|
|
|
|
_mapsCollection = new MapsCollection(600, 500);
|
|
|
|
|
mapSelectComboBox.removeAllItems();
|
|
|
|
|
for (var elem : _mapsDict.keySet())
|
|
|
|
|
{
|
|
|
|
|
mapSelectComboBox.addItem(elem);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Кнопка добавления карты
|
|
|
|
|
JButton addMapButton = new JButton("Add Map");
|
|
|
|
|
addMapButton.addActionListener(e -> {
|
|
|
|
|
// логика добавления
|
|
|
|
|
if (mapSelectComboBox.getSelectedIndex() == -1 || textFieldNewMapName.getText() == null)
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Not all data is complete!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString()))
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "No such map");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO, "Map " + textFieldNewMapName.getText() + " added");
|
2022-11-05 15:29:55 +04:00
|
|
|
|
ReloadMaps();
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(addMapButton);
|
|
|
|
|
|
|
|
|
|
// ListBox для созданных карт
|
|
|
|
|
listBoxMaps = new JList(mapsListModel);
|
|
|
|
|
listBoxMaps.addListSelectionListener(e -> {
|
|
|
|
|
if(listBoxMaps.getSelectedValue() == null) return;
|
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO,"Map switched to " + listBoxMaps.getSelectedValue().toString());
|
2022-11-05 15:29:55 +04:00
|
|
|
|
repaint();
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(listBoxMaps);
|
|
|
|
|
|
|
|
|
|
listScroller.setViewportView(listBoxMaps);
|
|
|
|
|
listBoxMaps.setLayoutOrientation(JList.VERTICAL);
|
|
|
|
|
statusPanel.add(listScroller);
|
|
|
|
|
|
|
|
|
|
// Кнопка для удаления карты
|
|
|
|
|
JButton deleteMapButton = new JButton("Delete Map");
|
|
|
|
|
deleteMapButton.addActionListener(e -> {
|
|
|
|
|
// логика удаления
|
|
|
|
|
if (listBoxMaps.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
|
|
|
|
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO,"Map " +listBoxMaps.getSelectedValue().toString() +" deleted");
|
2022-11-05 15:29:55 +04:00
|
|
|
|
ReloadMaps();
|
|
|
|
|
repaint();
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(deleteMapButton);
|
|
|
|
|
|
2022-10-21 21:27:07 +04:00
|
|
|
|
// Кнопка добавления локомотива
|
|
|
|
|
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
|
|
|
|
addLocomotiveButton.addActionListener(e -> {
|
2022-12-04 17:50:10 +04:00
|
|
|
|
FormLocomotiveConfig formLocomotiveConfig = new FormLocomotiveConfig();
|
|
|
|
|
formLocomotiveConfig.setVisible(true);
|
|
|
|
|
formLocomotiveConfig.AddListener(locomotive -> {
|
|
|
|
|
if (listBoxMaps.getSelectedIndex() == -1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (locomotive!=null) {
|
|
|
|
|
try {
|
|
|
|
|
DrawningObjectLocomotive objectLocomotive = new DrawningObjectLocomotive(locomotive);
|
|
|
|
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(objectLocomotive)!= -1){
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
|
|
|
|
logger.log(Level.INFO,"Object " + locomotive + " added");
|
|
|
|
|
repaint();
|
2022-12-02 18:34:48 +04:00
|
|
|
|
}
|
2022-12-04 17:50:10 +04:00
|
|
|
|
else {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
|
2022-12-02 18:34:48 +04:00
|
|
|
|
}
|
2022-11-15 20:22:49 +04:00
|
|
|
|
}
|
2022-12-04 17:50:10 +04:00
|
|
|
|
catch (StorageOverflowException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
|
|
|
|
|
logger.log(Level.WARN,"Error " + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
|
|
|
|
|
logger.log(Level.FATAL,"Error " + ex.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-10-21 21:27:07 +04:00
|
|
|
|
});
|
|
|
|
|
statusPanel.add(addLocomotiveButton);
|
|
|
|
|
|
|
|
|
|
// Текстовое поле для ввода позиции с маской
|
|
|
|
|
JFormattedTextField maskedTextFieldPosition = null;
|
|
|
|
|
try {
|
|
|
|
|
MaskFormatter positionMask = new MaskFormatter("##");
|
|
|
|
|
maskedTextFieldPosition = new JFormattedTextField(positionMask);
|
|
|
|
|
statusPanel.add(maskedTextFieldPosition);
|
|
|
|
|
}
|
|
|
|
|
catch (ParseException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
//Кнопка удаления локомотива
|
|
|
|
|
JButton deleteLocomotiveButton = new JButton("Delete Locomotive");
|
|
|
|
|
JFormattedTextField finalMaskedTextFieldPosition = maskedTextFieldPosition;
|
|
|
|
|
deleteLocomotiveButton.addActionListener(e -> {
|
|
|
|
|
// логика удаления
|
2022-12-02 18:34:48 +04:00
|
|
|
|
try{
|
|
|
|
|
if ((String)mapSelectComboBox.getSelectedItem() == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (finalMaskedTextFieldPosition == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
|
|
|
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO,"Locomotive deleted at position " + position);
|
2022-12-02 18:34:48 +04:00
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION);
|
|
|
|
|
}}
|
|
|
|
|
catch (LocomotiveNotFoundException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Locomotive not found error: " + ex.getMessage());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.WARN,"Error " + ex.getMessage());
|
2022-10-21 21:27:07 +04:00
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Unknown error: "+ ex.getMessage());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.FATAL,"Error " + ex.getMessage());
|
2022-10-21 21:27:07 +04:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(deleteLocomotiveButton);
|
|
|
|
|
//Кнопка просмотра хранилища
|
|
|
|
|
JButton showStorageButton = new JButton("Show Storage");
|
|
|
|
|
showStorageButton.addActionListener(e -> {
|
|
|
|
|
// логика просмотра
|
2022-11-05 15:29:55 +04:00
|
|
|
|
if (listBoxMaps.getSelectedIndex() == -1)
|
2022-10-21 21:27:07 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-11-05 15:29:55 +04:00
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
2022-10-21 21:27:07 +04:00
|
|
|
|
repaint();
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(showStorageButton);
|
|
|
|
|
//Кнопка просмотра карты
|
|
|
|
|
JButton showOnMapButton = new JButton("Show On Map");
|
|
|
|
|
showOnMapButton.addActionListener(e -> {
|
|
|
|
|
// логика просмотра
|
2022-11-05 15:29:55 +04:00
|
|
|
|
if (listBoxMaps.getSelectedIndex() == -1)
|
2022-10-21 21:27:07 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
|
try {
|
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
2022-12-04 17:50:10 +04:00
|
|
|
|
catch (StorageOverflowException ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Storage overflow error: "+ ex.getMessage());
|
|
|
|
|
logger.log(Level.WARN,"Error " + ex.getMessage());
|
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Unknown error: "+ ex.getMessage());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.FATAL,"Error " + ex.getMessage());
|
2022-12-02 18:34:48 +04:00
|
|
|
|
}
|
2022-10-21 21:27:07 +04:00
|
|
|
|
});
|
|
|
|
|
statusPanel.add(showOnMapButton);
|
|
|
|
|
|
|
|
|
|
ActionListener moveButtonListener = new ActionListener() {
|
|
|
|
|
@Override
|
|
|
|
|
public void actionPerformed(ActionEvent e) {
|
2022-11-05 15:29:55 +04:00
|
|
|
|
if (listBoxMaps.getSelectedIndex() == -1)
|
2022-10-21 21:27:07 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
String name = e.getActionCommand();
|
|
|
|
|
Direction dir = Direction.None;
|
|
|
|
|
switch (name)
|
|
|
|
|
{
|
|
|
|
|
case "Up":
|
|
|
|
|
dir = Direction.Up;
|
|
|
|
|
break;
|
|
|
|
|
case "Down":
|
|
|
|
|
dir = Direction.Down;
|
|
|
|
|
break;
|
|
|
|
|
case "Left":
|
|
|
|
|
dir = Direction.Left;
|
|
|
|
|
break;
|
|
|
|
|
case "Right":
|
|
|
|
|
dir = Direction.Right;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-11-05 15:29:55 +04:00
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).MoveObject(dir);
|
2022-10-21 21:27:07 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//Кнопки управления
|
|
|
|
|
JButton moveDownButton = new JButton("Down");
|
|
|
|
|
moveDownButton.addActionListener(moveButtonListener);
|
|
|
|
|
|
|
|
|
|
JButton moveUpButton = new JButton("Up");
|
|
|
|
|
moveUpButton.addActionListener(moveButtonListener);
|
|
|
|
|
|
|
|
|
|
JButton moveLeftButton = new JButton("Left");
|
|
|
|
|
moveLeftButton.addActionListener(moveButtonListener);
|
|
|
|
|
|
|
|
|
|
JButton moveRightButton = new JButton("Right");
|
|
|
|
|
moveRightButton.addActionListener(moveButtonListener);
|
|
|
|
|
|
|
|
|
|
statusPanel.add(moveUpButton);
|
|
|
|
|
statusPanel.add(moveDownButton);
|
|
|
|
|
statusPanel.add(moveLeftButton);
|
|
|
|
|
statusPanel.add(moveRightButton);
|
|
|
|
|
|
2022-10-22 16:12:48 +04:00
|
|
|
|
JButton showGalleryButton = new JButton("Show Gallery");
|
|
|
|
|
showGalleryButton.addActionListener(e -> {
|
|
|
|
|
new FormEntityWithExtraGallery();
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(showGalleryButton);
|
2022-11-05 18:49:21 +04:00
|
|
|
|
|
|
|
|
|
// Кнопка показа удаленных объектов
|
|
|
|
|
JButton showDeletedButton = new JButton("Show Deleted");
|
|
|
|
|
showDeletedButton.addActionListener(e -> {
|
|
|
|
|
// По отдельной кнопке вызывать форму работы с объектом (из
|
|
|
|
|
//первой лабораторной), передавая туда элемент из коллекции
|
|
|
|
|
//удаленных, если там есть
|
|
|
|
|
DrawningObjectLocomotive locomotive = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).getDeleted();
|
2022-11-08 16:50:09 +04:00
|
|
|
|
if (locomotive == null) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "No deleted objects");
|
|
|
|
|
}
|
2022-11-05 18:49:21 +04:00
|
|
|
|
if (locomotive != null) {
|
|
|
|
|
JDialog dialog = new JDialog(formFrame, "Deleted", true);
|
|
|
|
|
FormLocomotive formLocomotive = new FormLocomotive(dialog);
|
|
|
|
|
formLocomotive.SetLocomotive(locomotive);
|
|
|
|
|
dialog.setSize(800, 500);
|
|
|
|
|
dialog.setContentPane(formLocomotive);
|
|
|
|
|
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
|
|
|
|
dialog.setVisible(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
2022-11-19 21:53:44 +04:00
|
|
|
|
// Сохранения и загрузки
|
2022-11-19 19:32:56 +04:00
|
|
|
|
JFileChooser fileChooser = new JFileChooser();
|
2022-11-19 21:53:44 +04:00
|
|
|
|
// Сохранение всех хранилищ
|
2022-11-19 19:32:56 +04:00
|
|
|
|
JButton saveButton = new JButton("Save");
|
|
|
|
|
saveButton.addActionListener(e -> {
|
|
|
|
|
fileChooser.setDialogTitle("Saving");
|
|
|
|
|
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
|
|
|
|
if (result == JFileChooser.APPROVE_OPTION ) {
|
2022-12-02 18:34:48 +04:00
|
|
|
|
try {
|
|
|
|
|
_mapsCollection.SaveData(fileChooser.getSelectedFile().getAbsolutePath());
|
2022-11-19 19:32:56 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Save success");
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO,"Saved all to " + fileChooser.getSelectedFile().getAbsolutePath());
|
2022-11-19 19:32:56 +04:00
|
|
|
|
}
|
2022-12-02 18:34:48 +04:00
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Save error: " + ex.getMessage());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
2022-12-02 18:34:48 +04:00
|
|
|
|
}
|
2022-11-19 19:32:56 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(saveButton);
|
2022-11-19 21:53:44 +04:00
|
|
|
|
// Загрузка всех хранилищ
|
2022-11-19 19:32:56 +04:00
|
|
|
|
JButton loadButton = new JButton("Load");
|
|
|
|
|
loadButton.addActionListener(e -> {
|
|
|
|
|
fileChooser.setDialogTitle("Loading");
|
|
|
|
|
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
|
|
|
|
if (result == JFileChooser.APPROVE_OPTION ) {
|
2022-12-02 18:34:48 +04:00
|
|
|
|
try {
|
|
|
|
|
_mapsCollection.LoadData(fileChooser.getSelectedFile().getAbsolutePath());
|
2022-11-19 19:32:56 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Load success");
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO,"Loaded all from " + fileChooser.getSelectedFile().getAbsolutePath());
|
2022-12-02 18:34:48 +04:00
|
|
|
|
ReloadMaps();
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Load error: " + ex.getMessage());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
2022-11-19 19:32:56 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(loadButton);
|
2022-11-19 21:53:44 +04:00
|
|
|
|
|
|
|
|
|
// Сохранение выбранной карты
|
|
|
|
|
JButton saveMapButton = new JButton("Save Map");
|
|
|
|
|
saveMapButton.addActionListener(e -> {
|
|
|
|
|
fileChooser.setDialogTitle("Saving Map");
|
|
|
|
|
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
|
|
|
|
if (result == JFileChooser.APPROVE_OPTION ) {
|
|
|
|
|
if (listBoxMaps.getSelectedIndex() == -1)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
2022-12-02 18:34:48 +04:00
|
|
|
|
try {
|
|
|
|
|
_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath());
|
2022-11-19 21:53:44 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Map saving success");
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO,"Saved map to " + fileChooser.getSelectedFile().getAbsolutePath());
|
2022-12-02 18:34:48 +04:00
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Map saving error: " + ex.getMessage());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
2022-11-19 21:53:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(saveMapButton);
|
|
|
|
|
|
|
|
|
|
// Загрузка одной карты
|
|
|
|
|
JButton loadMapButton = new JButton("Load Map");
|
|
|
|
|
loadMapButton.addActionListener(e -> {
|
|
|
|
|
fileChooser.setDialogTitle("Loading");
|
|
|
|
|
int result = fileChooser.showSaveDialog(FormMapWithSetLocomotives.this);
|
|
|
|
|
if (result == JFileChooser.APPROVE_OPTION ) {
|
2022-12-02 18:34:48 +04:00
|
|
|
|
try {
|
|
|
|
|
_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath());
|
2022-11-19 21:53:44 +04:00
|
|
|
|
JOptionPane.showMessageDialog(null, "Load Map success");
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.INFO,"Loaded map from " + fileChooser.getSelectedFile().getAbsolutePath());
|
2022-12-02 18:34:48 +04:00
|
|
|
|
ReloadMaps();
|
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) {
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Load Map error: " + ex.getMessage());
|
2022-12-04 17:50:10 +04:00
|
|
|
|
logger.log(Level.ERROR,"Error " + ex.getMessage());
|
2022-11-19 21:53:44 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(loadMapButton);
|
2022-10-22 16:12:48 +04:00
|
|
|
|
|
2022-10-21 21:27:07 +04:00
|
|
|
|
formFrame.getContentPane().add(this);
|
2022-10-22 16:12:48 +04:00
|
|
|
|
formFrame.setVisible(true);
|
2022-10-21 21:27:07 +04:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-05 15:29:55 +04:00
|
|
|
|
private void ReloadMaps(){
|
|
|
|
|
int index = listBoxMaps.getSelectedIndex();
|
|
|
|
|
listBoxMaps.removeAll();
|
|
|
|
|
mapsListModel.removeAllElements();
|
|
|
|
|
for (int i = 0; i < _mapsCollection.keys().size(); i++){
|
|
|
|
|
mapsListModel.addElement(_mapsCollection.keys().get(i));
|
|
|
|
|
}
|
|
|
|
|
if (mapsListModel.size() > 0 && (index == -1 || index >= mapsListModel.size())){
|
|
|
|
|
listBoxMaps.setSelectedIndex(0);
|
|
|
|
|
}
|
|
|
|
|
else if (mapsListModel.size() > 0 && index > -1 && index < mapsListModel.size())
|
|
|
|
|
{
|
|
|
|
|
listBoxMaps.setSelectedIndex(index);
|
|
|
|
|
}
|
|
|
|
|
listBoxMaps.setModel(mapsListModel);
|
|
|
|
|
statusPanel.repaint();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 21:27:07 +04:00
|
|
|
|
@Override
|
|
|
|
|
protected void paintComponent(Graphics g) {
|
|
|
|
|
super.paintComponent(g);
|
|
|
|
|
Graphics2D g2 = (Graphics2D)g;
|
|
|
|
|
if (bufferImg != null) g2.drawImage(bufferImg, 0,0,600,500,null);
|
|
|
|
|
super.repaint();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|