Mochalov D.V. Hard LabWork06 #6
@ -263,8 +263,9 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
dialog.setVisible(true);
|
||||
}
|
||||
});
|
||||
|
||||
// Сохранения и загрузки
|
||||
JFileChooser fileChooser = new JFileChooser();
|
||||
// Сохранение всех хранилищ
|
||||
JButton saveButton = new JButton("Save");
|
||||
saveButton.addActionListener(e -> {
|
||||
fileChooser.setDialogTitle("Saving");
|
||||
@ -278,6 +279,7 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
|
||||
});
|
||||
statusPanel.add(saveButton);
|
||||
// Загрузка всех хранилищ
|
||||
JButton loadButton = new JButton("Load");
|
||||
loadButton.addActionListener(e -> {
|
||||
fileChooser.setDialogTitle("Loading");
|
||||
@ -293,6 +295,42 @@ public class FormMapWithSetLocomotives extends JComponent {
|
||||
});
|
||||
statusPanel.add(loadButton);
|
||||
|
||||
// Сохранение выбранной карты
|
||||
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;
|
||||
if (_mapsCollection.SaveMap(listBoxMaps.getSelectedValue().toString(), fileChooser.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Map saving success");
|
||||
}
|
||||
else JOptionPane.showMessageDialog(null, "Map saving fail");
|
||||
repaint();
|
||||
}
|
||||
});
|
||||
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 ) {
|
||||
if (_mapsCollection.LoadMap(fileChooser.getSelectedFile().getAbsolutePath())) {
|
||||
JOptionPane.showMessageDialog(null, "Load Map success");
|
||||
}
|
||||
else JOptionPane.showMessageDialog(null, "Load Map failed");
|
||||
}
|
||||
ReloadMaps();
|
||||
repaint();
|
||||
});
|
||||
statusPanel.add(loadMapButton);
|
||||
|
||||
formFrame.getContentPane().add(this);
|
||||
formFrame.setVisible(true);
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import java.io.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class MapsCollection {
|
||||
@ -69,6 +70,67 @@ public class MapsCollection {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Сохранение одной выбранной карты
|
||||
public boolean SaveMap(String map_name, String filename) {
|
||||
File file = new File(filename);
|
||||
MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap> map = Get(map_name);
|
||||
|
||||
if (file.exists())
|
||||
{
|
||||
file.delete();
|
||||
}
|
||||
try(BufferedWriter bw = new BufferedWriter(new FileWriter(filename))) {
|
||||
bw.write("SingleMap\n");
|
||||
bw.write("" + map_name + separatorDict +map.GetData(separatorDict.charAt(0), separatorData) + "\n");
|
||||
}
|
||||
catch (IOException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Загрузка одной карты
|
||||
public boolean LoadMap(String filename){
|
||||
File file = new File(filename);
|
||||
if (!file.exists())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
BufferedReader br = new BufferedReader(new FileReader(filename));
|
||||
String curLine = br.readLine();
|
||||
if (!curLine.contains("SingleMap")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
curLine = br.readLine();
|
||||
var elems = curLine.split(separatorDict);
|
||||
if (_mapStorages.containsKey(elems[0])) {
|
||||
_mapStorages.get(elems[0])._setLocomotives.Clear();
|
||||
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
|
||||
return true;
|
||||
}
|
||||
AbstractMap map = null;
|
||||
switch (elems[1])
|
||||
{
|
||||
case "class SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
case "class SpikeMap":
|
||||
map = new SpikeMap();
|
||||
break;
|
||||
case "class RailMap":
|
||||
map = new RailMap();
|
||||
break;
|
||||
}
|
||||
_mapStorages.put(elems[0], new MapWithSetLocomotivesGeneric<IDrawningObject, AbstractMap>(_pictureWidth, _pictureHeight, map));
|
||||
_mapStorages.get(elems[0]).LoadData(elems[2].split(Character.toString(separatorData)));
|
||||
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Загрузка информации по локомотивам в депо из файла
|
||||
public boolean LoadData(String filename)
|
||||
{
|
||||
@ -90,7 +152,7 @@ public class MapsCollection {
|
||||
AbstractMap map = null;
|
||||
|
||||
switch (elems[1])
|
||||
{ //TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
||||
{
|
||||
case "class SimpleMap":
|
||||
map = new SimpleMap();
|
||||
break;
|
||||
|
@ -34,6 +34,10 @@ public class SetLocomotivesGeneric <T>
|
||||
return result;
|
||||
}
|
||||
|
||||
public void Clear() {
|
||||
_places.clear();
|
||||
}
|
||||
|
||||
public T Get(int position)
|
||||
{
|
||||
if (position >= _maxCount || position < 0)
|
||||
|
Loading…
Reference in New Issue
Block a user
Нет проверки, что get вернул не null значение