Search in sources :

Example 1 with WorldFileDefault

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");
}
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 WorldFileDefault

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();
    }
}
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 WorldFileDefault

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();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) World(mudmap2.backend.World) WorldFile(mudmap2.backend.WorldFileReader.WorldFile) File(java.io.File) Test(org.junit.Test)

Example 4 with WorldFileDefault

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);
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 5 with WorldFileDefault

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);
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) IOException(java.io.IOException) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

WorldFileDefault (mudmap2.backend.WorldFileReader.current.WorldFileDefault)7 IOException (java.io.IOException)6 File (java.io.File)5 FileNotFoundException (java.io.FileNotFoundException)5 World (mudmap2.backend.World)5 WorldFile (mudmap2.backend.WorldFileReader.WorldFile)5 Test (org.junit.Test)5 JFileChooser (javax.swing.JFileChooser)2 Color (java.awt.Color)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 PrintWriter (java.io.PrintWriter)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 FileFilter (javax.swing.filechooser.FileFilter)1 Layer (mudmap2.backend.Layer)1 Path (mudmap2.backend.Path)1