PIbd-23_Minhasapov_R.H._Exc.../FormMapWithSetTracktor.java

303 lines
13 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 javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import java.awt.*;
import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Optional;
public class FormMapWithSetTracktor extends JFrame {
private JMenuBar menuBar;
private JPanel ContentPanel;
private JPanel pictureBox;
private JPanel toolsGroup;
private JLabel toolsLabel;
private JComboBox<String> comboBoxMapSelector;
private JButton buttonAddTracktor;
private JFormattedTextField textBoxPosition;
private JButton buttonRemoveTracktor;
private JButton buttonShowStorage;
private JButton buttonShowOnMap;
private JButton buttonUp;
private JButton buttonLeft;
private JButton buttonRight;
private JButton buttonDown;
private JLabel mapsLabel;
private JTextField textFieldMapName;
private JButton buttonAddMap;
private JList<String> listBoxMaps;
private JPanel mapsGroup;
private JButton buttonDeleteMap;
private JButton buttonShowDeleted;
private Image bufferedImage;
private final HashMap<String, AbstractMap> _mapsDict = new HashMap<>() {{
put("Простая карта", new SimpleMap());
put("Свалка карта", new DumpMap());
}};
private final MapsCollection _mapsCollection;
private final LinkedList<IDrawningObject> deletedObjects = new LinkedList<>();
public FormMapWithSetTracktor() {
this.setTitle("Трактор");
this.setContentPane(ContentPanel);
this.setSize(800,600);
this.setVisible(true);
try {
textBoxPosition.setFormatterFactory(new DefaultFormatterFactory(new MaskFormatter("##")));
} catch (ParseException e) {
e.printStackTrace();
}
_mapsCollection = new MapsCollection(pictureBox.getWidth(), pictureBox.getHeight());
menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("Файл");
menuBar.add(fileMenu);
JMenuItem saveMenuItem = new JMenuItem("Сохранить");
saveMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showSaveDialog(this);
try {
if (_mapsCollection.saveData(dialog.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(this, "Сохранение прошло успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не сохранилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(saveMenuItem);
JMenuItem loadMenuItem = new JMenuItem("Загрузить");
loadMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showOpenDialog(this);
try {
if (_mapsCollection.loadData(dialog.getSelectedFile().getAbsolutePath())) {
reloadMaps();
JOptionPane.showMessageDialog(this, "Загрузка прошла успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не загрузилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(loadMenuItem);
JMenuItem saveMapMenuItem = new JMenuItem("Сохранить карту");
saveMapMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showSaveDialog(this);
try {
if (_mapsCollection.saveMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse(""), dialog.getSelectedFile().getAbsolutePath())) {
JOptionPane.showMessageDialog(this, "Сохранение прошло успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не сохранилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(saveMapMenuItem);
JMenuItem loadMapMenuItem = new JMenuItem("Загрузить карту");
loadMapMenuItem.addActionListener(e -> {
JFileChooser dialog = new JFileChooser();
dialog.setFileFilter(new FileNameExtensionFilter("TXT file", "txt"));
dialog.showOpenDialog(this);
try {
if (_mapsCollection.loadMap(dialog.getSelectedFile().getAbsolutePath())) {
reloadMaps();
JOptionPane.showMessageDialog(this, "Загрузка прошла успешно", "Успех", JOptionPane.INFORMATION_MESSAGE);
} else {
JOptionPane.showMessageDialog(this, "Не загрузилось", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
} catch (IOException ex) {
ex.printStackTrace();
}
});
fileMenu.add(loadMapMenuItem);
setJMenuBar(menuBar);
comboBoxMapSelector.removeAllItems();
for (var key : _mapsDict.keySet()) {
comboBoxMapSelector.addItem(key);
}
buttonAddMap.addActionListener(e -> {
if (comboBoxMapSelector.getSelectedIndex() == -1 || textFieldMapName.getText() == null || textFieldMapName.getText().isEmpty()) {
JOptionPane.showMessageDialog(this, "Не все данные заполнены", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
if (!_mapsDict.containsKey((String)comboBoxMapSelector.getSelectedItem())) {
JOptionPane.showMessageDialog(this, "Нет такой карты", "Ошибка", JOptionPane.ERROR_MESSAGE);
return;
}
_mapsCollection.addMap(textFieldMapName.getText(), _mapsDict.get((String)comboBoxMapSelector.getSelectedItem()));
reloadMaps();
});
buttonDeleteMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
if (JOptionPane.showConfirmDialog(this, "Удалить карту " + listBoxMaps.getSelectedValue() + "?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
_mapsCollection.deleteMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse(""));
reloadMaps();
}
});
listBoxMaps.addListSelectionListener(e -> {
if (listBoxMaps.getSelectedValue() != null) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
});
buttonAddTracktor.addActionListener(e -> {
FormTracktorConfig form = new FormTracktorConfig();
form.addListener(tracktor -> {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
if (tracktor != null) {
DrawningObjectExcavator objectTracktor = new DrawningObjectExcavator(tracktor);
if (_mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).addTracktor(objectTracktor) != -1)
{
JOptionPane.showMessageDialog(this, "Объект добавлен", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
}
else
{
JOptionPane.showMessageDialog(this, "Не удалось добавить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
}
});
form.setVisible(true);
});
buttonRemoveTracktor.addActionListener(e -> {
String text = textBoxPosition.getText();
if (text == null || text.isEmpty()) {
return;
}
if (JOptionPane.showConfirmDialog(this, "Удалить объект?", "Удаление", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
return;
}
int position = Integer.parseInt(text);
IDrawningObject deleted = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).removeTracktorAt(position);
if (deleted!=null) {
deletedObjects.add(deleted);
JOptionPane.showMessageDialog(this, "Объект удалён", "Успех", JOptionPane.INFORMATION_MESSAGE);
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
} else {
JOptionPane.showMessageDialog(this, "Не удалось удалить объект", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonShowStorage.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showSet();
repaint();
});
buttonShowOnMap.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() == -1) {
return;
}
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).showOnMap();
repaint();
});
buttonShowDeleted.addActionListener(e -> {
if (!deletedObjects.isEmpty()) {
DrawningObjectExcavator deleted = (DrawningObjectExcavator) deletedObjects.pop();
FormTracktor dialog = new FormTracktor(deleted.getTracktor());
dialog.setSize(800, 500);
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} else {
JOptionPane.showMessageDialog(this, "Связанный список удалённых объектов пуст", "Провал", JOptionPane.INFORMATION_MESSAGE);
}
});
buttonLeft.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Left);
repaint();
}
});
buttonRight.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Right);
repaint();
}
});
buttonUp.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Up);
repaint();
}
});
buttonDown.addActionListener(e -> {
if (listBoxMaps.getSelectedIndex() != -1) {
bufferedImage = _mapsCollection.getMap(Optional.ofNullable(listBoxMaps.getSelectedValue()).orElse("")).moveObject(Direction.Down);
repaint();
}
});
}
private void reloadMaps() {
int index = listBoxMaps.getSelectedIndex();
listBoxMaps.setListData(_mapsCollection.getKeys().toArray(new String[0]));
int size = listBoxMaps.getModel().getSize();
if (size > 0 && (index == -1 || index >= size)) {
listBoxMaps.setSelectedIndex(0);
} else if (index > -1 && index < size) {
listBoxMaps.setSelectedIndex(index);
}
}
@Override
public void paint(Graphics g) {
super.paint(g);
if (bufferedImage != null) {
pictureBox.paintComponents(bufferedImage.getGraphics());
pictureBox.getGraphics().drawImage(bufferedImage, 0, 0, null);
}
}
}