Search in sources :

Example 1 with WorldFileJSON

use of mudmap2.backend.WorldFileReader.current.WorldFileJSON 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");
}
Also used : JFileChooser(javax.swing.JFileChooser) WorldFile(mudmap2.backend.WorldFileReader.WorldFile) IOException(java.io.IOException) WorldFileJSON(mudmap2.backend.WorldFileReader.current.WorldFileJSON) WorldFile(mudmap2.backend.WorldFileReader.WorldFile) File(java.io.File) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault)

Example 2 with WorldFileJSON

use of mudmap2.backend.WorldFileReader.current.WorldFileJSON 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();
    }
}
Also used : IOException(java.io.IOException) World(mudmap2.backend.World) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) Test(org.junit.Test)

Example 3 with WorldFileJSON

use of mudmap2.backend.WorldFileReader.current.WorldFileJSON in project mudmap2 by Neop.

the class WorldFileJSONTest method testCanRead.

/**
 * Test of canRead method, of class WorldFileJSON.
 */
@Test
public void testCanRead() {
    System.out.println("canRead");
    World world = new World("foobar");
    String wfjFile = folder.getRoot() + "/wfj";
    try {
        WorldFileJSON wfj = new WorldFileJSON(wfjFile);
        wfj.writeFile(world);
    } catch (IOException ex) {
        Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
        fail("Could not create files for test");
    }
    WorldFileJSON instancewfj = new WorldFileJSON(wfjFile);
    Boolean result = instancewfj.canRead();
    assertTrue(result);
}
Also used : IOException(java.io.IOException) World(mudmap2.backend.World) Test(org.junit.Test)

Example 4 with WorldFileJSON

use of mudmap2.backend.WorldFileReader.current.WorldFileJSON in project mudmap2 by Neop.

the class WorldFileJSONTest method testBackup.

/**
 * Test of backup method, of class WorldFileJSON.
 * @throws java.io.IOException
 */
@Test
public void testBackup() throws IOException {
    System.out.println("backup");
    try {
        String wfjFile = folder.getRoot() + "/wfj";
        WorldFileJSON instance = new WorldFileJSON(wfjFile);
        // don't write world
        // instance.writeFile(new World("Foo Bar"));
        instance.backup();
        File f1 = new File(wfjFile);
        File f2 = new File(wfjFile + ".bak");
        assertTrue(FileUtils.contentEquals(f1, f2));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
        fail();
    }
    try {
        String wfjFile = folder.getRoot() + "/wfj";
        WorldFileJSON instance = new WorldFileJSON(wfjFile);
        // write world
        instance.writeFile(new World("Foo Bar"));
        instance.backup();
        File f1 = new File(wfjFile);
        File f2 = new File(wfjFile + ".bak");
        assertTrue(FileUtils.contentEquals(f1, f2));
    } catch (FileNotFoundException ex) {
        Logger.getLogger(WorldFileJSONTest.class.getName()).log(Level.SEVERE, null, ex);
        fail();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) World(mudmap2.backend.World) File(java.io.File) Test(org.junit.Test)

Example 5 with WorldFileJSON

use of mudmap2.backend.WorldFileReader.current.WorldFileJSON in project mudmap2 by Neop.

the class WorldFileJSONTest method testGetWorldFileType.

/**
 * Test of getWorldFileType method, of class WorldFileJSON.
 */
@Test
public void testGetWorldFileType() {
    System.out.println("getWorldFileType");
    WorldFileJSON instance = new WorldFileJSON(null);
    WorldFileType result = instance.getWorldFileType();
    assertEquals(WorldFileType.JSON, result);
}
Also used : WorldFileType(mudmap2.backend.WorldFileReader.WorldFileType) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)8 World (mudmap2.backend.World)6 IOException (java.io.IOException)5 FileNotFoundException (java.io.FileNotFoundException)3 WorldFile (mudmap2.backend.WorldFileReader.WorldFile)3 File (java.io.File)2 WorldFileJSON (mudmap2.backend.WorldFileReader.current.WorldFileJSON)2 Color (java.awt.Color)1 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 JFileChooser (javax.swing.JFileChooser)1 Layer (mudmap2.backend.Layer)1 Path (mudmap2.backend.Path)1 Place (mudmap2.backend.Place)1 PlaceGroup (mudmap2.backend.PlaceGroup)1 WorldFileType (mudmap2.backend.WorldFileReader.WorldFileType)1 WorldFileDefault (mudmap2.backend.WorldFileReader.current.WorldFileDefault)1