Базовый функционал перенесен, изменена форма FormMapWithSetLocomotives

This commit is contained in:
Данила Мочалов 2022-11-05 15:29:55 +04:00
parent ee384c3f2b
commit 8a94c4d44b

View File

@ -5,32 +5,62 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.text.ParseException; import java.text.ParseException;
import java.util.HashMap;
public class FormMapWithSetLocomotives extends JComponent { public class FormMapWithSetLocomotives extends JComponent {
private BufferedImage bufferImg = null; private BufferedImage bufferImg = null;
/// Объект от класса карты с набором объектов /// Объект от класса карты с набором объектов
private MapWithSetLocomotivesGeneric<DrawningObjectLocomotive, AbstractMap> _mapLocomotivesCollectionGeneric; //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
public FormMapWithSetLocomotives() { public FormMapWithSetLocomotives() {
JFrame formFrame = new JFrame("Form Map With SetLocomotives"); formFrame = new JFrame("Form Map With SetLocomotives");
formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); formFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
formFrame.setSize(750, 500); formFrame.setSize(750, 500);
formFrame.setLocationRelativeTo(null); formFrame.setLocationRelativeTo(null);
Panel statusPanel = new Panel(); statusPanel = new Panel();
statusPanel.setBackground(Color.WHITE); statusPanel.setBackground(Color.WHITE);
statusPanel.setLayout(new GridLayout(0, 1, 20, 20)); statusPanel.setLayout(new GridLayout(0, 1, 20, 5));
setLayout(new BorderLayout()); setLayout(new BorderLayout());
add(statusPanel, BorderLayout.EAST); add(statusPanel, BorderLayout.EAST);
// Текстовое поле для ввода названия карты
textFieldNewMapName = new TextField();
statusPanel.add(textFieldNewMapName);
// КомбоБокс с картами // КомбоБокс с картами
String[] maps = { String[] maps = {
"Simple Map", "Simple Map",
"Spike Map", "Spike Map",
"Rail Map" "Rail Map"
}; };
JComboBox mapSelectComboBox = new JComboBox(maps); mapSelectComboBox = new JComboBox(maps);
mapSelectComboBox.setEditable(true); mapSelectComboBox.setEditable(true);
mapSelectComboBox.addActionListener(e -> { /*mapSelectComboBox.addActionListener(e -> {
AbstractMap map = null; AbstractMap map = null;
String item = (String)mapSelectComboBox.getSelectedItem(); String item = (String)mapSelectComboBox.getSelectedItem();
switch (item) { switch (item) {
@ -53,14 +83,74 @@ public class FormMapWithSetLocomotives extends JComponent {
{ {
_mapLocomotivesCollectionGeneric = null; _mapLocomotivesCollectionGeneric = null;
} }
}); });*/
statusPanel.add(mapSelectComboBox); statusPanel.add(mapSelectComboBox);
// 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);
// Кнопка добавления локомотива // Кнопка добавления локомотива
JButton addLocomotiveButton = new JButton("Add Locomotive"); JButton addLocomotiveButton = new JButton("Add Locomotive");
addLocomotiveButton.addActionListener(e -> { addLocomotiveButton.addActionListener(e -> {
// логика добавления // логика добавления
if (_mapLocomotivesCollectionGeneric == null) if (listBoxMaps.getSelectedIndex() == -1)
{ {
return; return;
} }
@ -71,9 +161,10 @@ public class FormMapWithSetLocomotives extends JComponent {
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true); dialog.setVisible(true);
DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive()); DrawningObjectLocomotive locomotive = new DrawningObjectLocomotive(formLocomotive.getSelectedLocomotive());
if (_mapLocomotivesCollectionGeneric.Plus(locomotive) != -1) { //TODO
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Plus(locomotive)!= -1) {
JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION); JOptionPane.showMessageDialog(formFrame, "Object added", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint(); repaint();
} }
else { else {
@ -104,9 +195,10 @@ public class FormMapWithSetLocomotives extends JComponent {
return; return;
} }
int position = Integer.parseInt(finalMaskedTextFieldPosition.getText()); int position = Integer.parseInt(finalMaskedTextFieldPosition.getText());
if (_mapLocomotivesCollectionGeneric.Minus(position) != null) { //TODO
if (_mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).Minus(position) != null) {
JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION); JOptionPane.showMessageDialog(formFrame, "Object removed", "Success", JOptionPane.OK_CANCEL_OPTION);
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint(); repaint();
} }
else{ else{
@ -118,11 +210,12 @@ public class FormMapWithSetLocomotives extends JComponent {
JButton showStorageButton = new JButton("Show Storage"); JButton showStorageButton = new JButton("Show Storage");
showStorageButton.addActionListener(e -> { showStorageButton.addActionListener(e -> {
// логика просмотра // логика просмотра
if (_mapLocomotivesCollectionGeneric == null) //TODO
if (listBoxMaps.getSelectedIndex() == -1)
{ {
return; return;
} }
bufferImg = _mapLocomotivesCollectionGeneric.ShowSet(); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowSet();
repaint(); repaint();
}); });
statusPanel.add(showStorageButton); statusPanel.add(showStorageButton);
@ -130,18 +223,21 @@ public class FormMapWithSetLocomotives extends JComponent {
JButton showOnMapButton = new JButton("Show On Map"); JButton showOnMapButton = new JButton("Show On Map");
showOnMapButton.addActionListener(e -> { showOnMapButton.addActionListener(e -> {
// логика просмотра // логика просмотра
if (_mapLocomotivesCollectionGeneric == null) //TODO
if (listBoxMaps.getSelectedIndex() == -1)
{ {
return; return;
} }
bufferImg = _mapLocomotivesCollectionGeneric.ShowOnMap(); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).ShowOnMap();
repaint();
}); });
statusPanel.add(showOnMapButton); statusPanel.add(showOnMapButton);
ActionListener moveButtonListener = new ActionListener() { ActionListener moveButtonListener = new ActionListener() {
//TODO
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (_mapLocomotivesCollectionGeneric == null) if (listBoxMaps.getSelectedIndex() == -1)
{ {
return; return;
} }
@ -162,7 +258,7 @@ public class FormMapWithSetLocomotives extends JComponent {
dir = Direction.Right; dir = Direction.Right;
break; break;
} }
bufferImg = _mapLocomotivesCollectionGeneric.MoveObject(dir); bufferImg = _mapsCollection.Get(listBoxMaps.getSelectedValue().toString()).MoveObject(dir);
} }
}; };
@ -194,6 +290,24 @@ public class FormMapWithSetLocomotives extends JComponent {
formFrame.setVisible(true); formFrame.setVisible(true);
} }
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();
}
@Override @Override
protected void paintComponent(Graphics g) { protected void paintComponent(Graphics g) {
super.paintComponent(g); super.paintComponent(g);