use of mudmap2.backend.WorldFileReader.current.WorldFileDefault in project mudmap2 by Neop.
the class WorldTab method save.
/**
* Saves the changes in the world
*/
public void save() {
if (!worldPanel.isPassive()) {
WorldFile worldFile = getWorld().getWorldFile();
if (worldFile == null) {
if (filename == null || filename.isEmpty() || (new File(filename)).exists()) {
// get new filename
int ret;
do {
JFileChooser fileChooser = new JFileChooser(Environment.getWorldsDir());
ret = fileChooser.showSaveDialog(this);
if (ret == JFileChooser.APPROVE_OPTION) {
filename = fileChooser.getSelectedFile().getAbsolutePath();
} else {
ret = JOptionPane.showConfirmDialog(this, "No file chosen: " + getWorld().getName() + " will not be saved!", "Saving world", JOptionPane.OK_CANCEL_OPTION);
if (ret == JOptionPane.OK_OPTION)
return;
}
} while (ret != JFileChooser.APPROVE_OPTION || ret != JOptionPane.OK_OPTION);
}
worldFile = new WorldFileDefault(filename);
getWorld().setWorldFile(worldFile);
}
// set meta data writer
if (worldFile.getWorldFileType() == WorldFileType.JSON) {
WorldFileJSON wfj = null;
if (worldFile instanceof WorldFileJSON) {
wfj = (WorldFileJSON) worldFile;
} else if (worldFile instanceof WorldFileDefault) {
wfj = (WorldFileJSON) ((WorldFileDefault) worldFile).getWorldFile();
}
if (wfj != null)
wfj.setMetaGetter(this);
}
// write world file
try {
worldFile.writeFile(getWorld());
WorldManager.putWorld(worldFile.getFilename(), getWorld());
} catch (IOException ex) {
Logger.getLogger(WorldTab.class.getName()).log(Level.SEVERE, null, ex);
JOptionPane.showMessageDialog(getParent(), "Could not save world file " + worldFile.getFilename(), "Saving world file", JOptionPane.ERROR_MESSAGE);
}
}
showMessage("World saved");
}
use of mudmap2.backend.WorldFileReader.current.WorldFileDefault in project mudmap2 by Neop.
the class WorldFileDefaultTest method testReadWorldName.
/**
* Test of readWorldName method, of class WorldFileDefault.
*/
@Test
public void testReadWorldName() throws Exception {
System.out.println("readWorldName");
String worldName = "foobar";
World world = new World(worldName);
String wfjFile = folder.getRoot() + "/wfj";
String wfmm1File = folder.getRoot() + "/wfmm1";
try {
WorldFileJSON wfj = new WorldFileJSON(wfjFile);
wfj.writeFile(world);
WorldFileJSON wfmm1 = new WorldFileJSON(wfmm1File);
wfmm1.writeFile(world);
} catch (IOException ex) {
Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
fail("Could not create files for test");
}
try {
WorldFileDefault instance1 = new WorldFileDefault(wfjFile);
String result1 = instance1.readWorldName();
assertEquals(worldName, result1);
WorldFileDefault instance2 = new WorldFileDefault(wfmm1File);
String result2 = instance2.readWorldName();
assertEquals(worldName, result2);
} catch (Exception ex) {
Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
fail();
}
}
use of mudmap2.backend.WorldFileReader.current.WorldFileDefault in project mudmap2 by Neop.
the class WorldFileDefaultTest method testBackup.
/**
* Test of backup method, of class WorldFileDefault.
*/
@Test
public void testBackup() throws Exception {
System.out.println("backup");
try {
String wfFile = folder.getRoot() + "/wfj";
WorldFileDefault instance = new WorldFileDefault(wfFile);
// don't write world
// instance.writeFile(new World("Foo Bar"));
instance.backup();
File f1 = new File(wfFile);
File f2 = new File(wfFile + ".bak");
assertTrue(FileUtils.contentEquals(f1, f2));
} catch (FileNotFoundException ex) {
Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
fail();
}
try {
String wfFile = folder.getRoot() + "/wfj";
WorldFileDefault instance = new WorldFileDefault(wfFile);
// write world
instance.writeFile(new World("Foo Bar"));
instance.backup();
File f1 = new File(wfFile);
File f2 = new File(wfFile + ".bak");
assertTrue(FileUtils.contentEquals(f1, f2));
} catch (FileNotFoundException ex) {
Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
fail();
}
}
use of mudmap2.backend.WorldFileReader.current.WorldFileDefault in project mudmap2 by Neop.
the class WorldFileList method writeWorldList.
/**
* Saves the available worlds list
* do this after writing the world files or new places won't appear in list
*/
public static void writeWorldList() {
final String file = Environment.getWorldsDir() + "worlds";
try {
// open file
if (!Environment.isDirectory(Environment.getWorldsDir()))
Environment.createDirectory(Environment.getWorldsDir());
try (PrintWriter outstream = new PrintWriter(new BufferedWriter(new FileWriter(file)))) {
outstream.println("# MUD Map (v2) worlds file");
outstream.println("ver " + META_FILE_VER_MAJOR + "." + META_FILE_VER_MINOR);
for (Map.Entry<String, String> w : availableWorlds.entrySet()) {
// check whether the file name in file equals the name in the list
WorldFileDefault worldFile = new WorldFileDefault(w.getKey());
String fw = null;
try {
if (worldFile.canRead()) {
fw = worldFile.readWorldName();
}
} catch (Exception ex) {
Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
}
if (fw != null) {
outstream.println("n " + fw);
String w_file = w.getKey();
outstream.println("f " + w_file);
if (w_file.startsWith(Environment.getWorldsDir())) {
w_file = w_file.substring(Environment.getWorldsDir().length());
outstream.println("g " + w_file);
}
}
}
}
} catch (IOException ex) {
System.out.printf("Couldn't write worlds file " + file);
Logger.getLogger(WorldManager.class.getName()).log(Level.INFO, null, ex);
JOptionPane.showMessageDialog(null, "Could not write worlds list file " + file + ".\nYou might have to open your worlds manually from " + Environment.getWorldsDir(), "WorldManager", JOptionPane.WARNING_MESSAGE);
}
}
use of mudmap2.backend.WorldFileReader.current.WorldFileDefault in project mudmap2 by Neop.
the class WorldFileList method readWorldList.
/**
* Get available worlds from worlds file
*/
public static void readWorldList() {
try {
// read from available worlds file
BufferedReader reader = new BufferedReader(new FileReader(Environment.getAvailableWorldsFile()));
String line;
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.startsWith("f ")) {
// world file entry
String file = line.substring(2).trim();
WorldFileDefault worldFile = new WorldFileDefault(file);
if (worldFile.canRead()) {
// is world file
String name;
try {
// get world name
name = worldFile.readWorldName();
} catch (Exception ex) {
Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
// use file name if world name not found
name = file.substring(file.lastIndexOf('/') + 1);
}
// add found world to list
availableWorlds.put(file, name);
}
}
}
} catch (FileNotFoundException ex) {
} catch (IOException ex) {
Logger.getLogger(WorldManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
Aggregations