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-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 MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> _mapLocomotivesCollectionGeneric;
|
|
|
|
|
|
|
|
|
|
/// Словарь для выпадающего списка
|
|
|
|
|
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;
|
|
|
|
|
//TODO
|
|
|
|
|
|
|
|
|
|
|
2022-10-21 21:27:07 +04:00
|
|
|
|
public FormMapWithSetLocomotives() {
|
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);
|
2022-11-05 15:29:55 +04:00
|
|
|
|
/*mapSelectComboBox.addActionListener(e -> {
|
2022-10-21 21:27:07 +04:00
|
|
|
|
AbstractMap map = null;
|
|
|
|
|
String item = (String)mapSelectComboBox.getSelectedItem();
|
|
|
|
|
switch (item) {
|
|
|
|
|
case "Simple Map":
|
|
|
|
|
map = new SimpleMap();
|
|
|
|
|
break;
|
|
|
|
|
case "Spike Map":
|
|
|
|
|
map = new SpikeMap();
|
|
|
|
|
break;
|
|
|
|
|
case "Rail Map":
|
|
|
|
|
map = new RailMap();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (map != null)
|
|
|
|
|
{
|
|
|
|
|
_mapLocomotivesCollectionGeneric = new MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap>
|
|
|
|
|
(600, 500, map);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_mapLocomotivesCollectionGeneric = null;
|
|
|
|
|
}
|
2022-11-05 15:29:55 +04:00
|
|
|
|
});*/
|
2022-10-21 21:27:07 +04:00
|
|
|
|
statusPanel.add(mapSelectComboBox);
|
|
|
|
|
|
2022-11-05 15:29:55 +04:00
|
|
|
|
// Initialization
|
|
|
|
|
// TODO
|
|
|
|
|
_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)
|
|
|
|
|
{
|
|
|
|
|
//MessageBox.Show("Не все данные заполнены", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
|
JOptionPane.showMessageDialog(null, "Not all data is complete!");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//TODO фиг знает как этот комбобокс работает, может не найдет карту
|
|
|
|
|
if (!_mapsDict.containsKey(mapSelectComboBox.getSelectedItem().toString()))
|
|
|
|
|
{
|
|
|
|
|
JOptionPane.showMessageDialog(null, "No such map");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_mapsCollection.AddMap(textFieldNewMapName.getText(), _mapsDict.get(mapSelectComboBox.getSelectedItem().toString()));
|
|
|
|
|
ReloadMaps();
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(addMapButton);
|
|
|
|
|
|
|
|
|
|
// ListBox для созданных карт
|
|
|
|
|
listBoxMaps = new JList(mapsListModel);
|
|
|
|
|
listBoxMaps.addListSelectionListener(e -> {
|
|
|
|
|
//TODO фиг знает как этот лист работает, может не вернет ничего
|
|
|
|
|
if(listBoxMaps.getSelectedValue() == null) return;
|
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
//TODO фиг знает как этот лист работает, может не вернет ничего
|
|
|
|
|
if(listBoxMaps.getSelectedValue().toString() == null) return;
|
|
|
|
|
_mapsCollection.DelMap(listBoxMaps.getSelectedValue().toString());
|
|
|
|
|
ReloadMaps();
|
|
|
|
|
repaint();
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(deleteMapButton);
|
|
|
|
|
|
2022-10-21 21:27:07 +04:00
|
|
|
|
// Кнопка добавления локомотива
|
|
|
|
|
JButton addLocomotiveButton = new JButton("Add Locomotive");
|
|
|
|
|
addLocomotiveButton.addActionListener(e -> {
|
|
|
|
|
// логика добавления
|
2022-11-05 15:29:55 +04:00
|
|
|
|
if (listBoxMaps.getSelectedIndex() == -1)
|
2022-10-21 21:27:07 +04:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
JDialog dialog = new JDialog(formFrame, "Dialog", true);
|
|
|
|
|
FormLocomotive formLocomotive = new FormLocomotive(dialog);
|
|
|
|
|
dialog.setSize(800, 500);
|
|
|
|
|
dialog.setContentPane(formLocomotive);
|
|
|
|
|
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
|
|
|
|
dialog.setVisible(true);
|
|
|
|
|
DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive());
|
2022-11-05 15:29:55 +04:00
|
|
|
|
//TODO
|
|
|
|
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(locomotive)!= -1) {
|
2022-10-21 21:27:07 +04:00
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
|
2022-11-05 15:29:55 +04:00
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
2022-10-21 21:27:07 +04:00
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be added", "Error", JOptionPane.OK_CANCEL_OPTION);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
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 -> {
|
|
|
|
|
// логика удаления
|
|
|
|
|
if ((String)mapSelectComboBox.getSelectedItem() == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (finalMaskedTextFieldPosition == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
|
2022-11-05 15:29:55 +04:00
|
|
|
|
//TODO
|
|
|
|
|
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
|
2022-10-21 21:27:07 +04:00
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
|
2022-11-05 15:29:55 +04:00
|
|
|
|
bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
|
2022-10-21 21:27:07 +04:00
|
|
|
|
repaint();
|
|
|
|
|
}
|
|
|
|
|
else{
|
|
|
|
|
JOptionPane.showMessageDialog(formFrame, "Object cannot be removed", "Error", JOptionPane.OK_CANCEL_OPTION);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(deleteLocomotiveButton);
|
|
|
|
|
//Кнопка просмотра хранилища
|
|
|
|
|
JButton showStorageButton = new JButton("Show Storage");
|
|
|
|
|
showStorageButton.addActionListener(e -> {
|
|
|
|
|
// логика просмотра
|
2022-11-05 15:29:55 +04:00
|
|
|
|
//TODO
|
|
|
|
|
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
|
|
|
|
//TODO
|
|
|
|
|
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()).ShowOnMap();
|
|
|
|
|
repaint();
|
2022-10-21 21:27:07 +04:00
|
|
|
|
});
|
|
|
|
|
statusPanel.add(showOnMapButton);
|
|
|
|
|
|
|
|
|
|
ActionListener moveButtonListener = new ActionListener() {
|
2022-11-05 15:29:55 +04:00
|
|
|
|
//TODO
|
2022-10-21 21:27:07 +04:00
|
|
|
|
@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);
|
|
|
|
|
// TODO чтобы передать локомотив в форму добавляем метод в formlocomotive
|
|
|
|
|
formLocomotive.SetLocomotive(locomotive);
|
|
|
|
|
dialog.setSize(800, 500);
|
|
|
|
|
dialog.setContentPane(formLocomotive);
|
|
|
|
|
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
|
|
|
|
|
dialog.setVisible(true);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
statusPanel.add(showDeletedButton);
|
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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|