Work is done 06
This commit is contained in:
parent
9b23e2f119
commit
d553376508
@ -97,6 +97,43 @@ public class FormMapWithSetArtilleries extends JFrame {
|
||||
});
|
||||
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();
|
||||
|
@ -19,6 +19,10 @@ public class MapWithSetArtilleriesGeneric<T extends IDrawingObject, U extends Ab
|
||||
_map = map;
|
||||
}
|
||||
|
||||
public U getMap() {
|
||||
return _map;
|
||||
}
|
||||
|
||||
public int addArtillery(T artillery) {
|
||||
return _setArtilleries.insert(artillery);
|
||||
}
|
||||
|
@ -59,8 +59,6 @@ public class MapsCollection {
|
||||
for (var storage : _mapsStorage.entrySet()) {
|
||||
writer.println(String.format("%s%c%s", storage.getKey(), separatorDict, storage.getValue().getData(separatorDict, separatorData)));
|
||||
}
|
||||
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -97,4 +95,75 @@ public class MapsCollection {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public boolean saveMap(String mapName, String filename) throws IOException {
|
||||
File file = new File(filename);
|
||||
|
||||
if (file.exists()) {
|
||||
file.delete();
|
||||
}
|
||||
|
||||
file.createNewFile();
|
||||
|
||||
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map = _mapsStorage.getOrDefault(mapName, null);
|
||||
|
||||
if (map == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(file)) {
|
||||
writer.println("Map");
|
||||
writer.println(mapName);
|
||||
writer.println(map.getMap().getClass().getSimpleName());
|
||||
|
||||
for (var artillery : map._setArtilleries.getArtilleries()) {
|
||||
writer.println(artillery.getInfo());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean loadMap(String filename) throws IOException {
|
||||
File file = new File(filename);
|
||||
|
||||
if (!file.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
|
||||
String currentLine = reader.readLine();
|
||||
|
||||
if (currentLine == null || !currentLine.contains("Map")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String mapName = reader.readLine();
|
||||
|
||||
MapWithSetArtilleriesGeneric<IDrawingObject, AbstractMap> map;
|
||||
if (_mapsStorage.containsKey(mapName)) {
|
||||
map = _mapsStorage.get(mapName);
|
||||
if (!map.getMap().getClass().getSimpleName().equals(reader.readLine())) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
map = switch (reader.readLine()) {
|
||||
case "SimpleMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new SimpleMap());
|
||||
case "ForestMap" -> new MapWithSetArtilleriesGeneric<>(_pictureWidth, _pictureHeight, new ForestMap());
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
map._setArtilleries.clear();
|
||||
|
||||
while ((currentLine = reader.readLine()) != null) {
|
||||
map._setArtilleries.insert(DrawingObjectArtillery.create(currentLine));
|
||||
}
|
||||
|
||||
_mapsStorage.put(mapName, map);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -53,4 +53,8 @@ public class SetArtilleriesGeneric<T> {
|
||||
public Iterable<T> getArtilleries() {
|
||||
return () -> _places.stream().filter(Objects::nonNull).iterator();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
_places.clear();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user