Search in sources :

Example 6 with WorldFileDefault

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

the class OpenWorldDialog method create.

private void create() {
    filechooser = new JFileChooser();
    filechooser.setAcceptAllFileFilterUsed(false);
    filechooser.setDialogTitle("Open world");
    filechooser.setMultiSelectionEnabled(false);
    filechooser.addChoosableFileFilter(new FileFilter() {

        @Override
        public boolean accept(File file) {
            if (file == null)
                return false;
            if (file.isDirectory())
                return true;
            String worldname = null;
            try {
                worldname = (new WorldFileDefault(file.getPath()).readWorldName());
            } catch (Exception ex) {
                Logger.getLogger(OpenWorldDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
            return worldname != null && !worldname.equals("");
        }

        @Override
        public String getDescription() {
            return "MUD Map world files";
        }
    });
    int ret = filechooser.showOpenDialog(parent);
    if (ret == JFileChooser.APPROVE_OPTION) {
        String file = filechooser.getSelectedFile().toString();
        try {
            World world = WorldManager.getWorld(file);
            if (null != world) {
                // create world tab
                parent.createTab(world, file);
            }
        } catch (Exception ex) {
            Logger.getLogger(OpenWorldDialog.class.getName()).log(Level.WARNING, null, ex);
            JOptionPane.showMessageDialog(parent, "Could not open world file '" + file + "'", "Open world", JOptionPane.ERROR_MESSAGE);
        }
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) FileFilter(javax.swing.filechooser.FileFilter) World(mudmap2.backend.World) File(java.io.File) WorldFileDefault(mudmap2.backend.WorldFileReader.current.WorldFileDefault)

Example 7 with WorldFileDefault

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

the class WorldFileDefaultTest method testSetWorldFile.

/**
 * Test of setWorldFile method, of class WorldFileDefault.
 */
@Test
public void testSetWorldFile() {
    System.out.println("setWorldFile");
    try {
        // getPlace access to private member
        Field field = WorldFileDefault.class.getDeclaredField("worldFile");
        field.setAccessible(true);
        WorldFileDefault instance = new WorldFileDefault(null);
        assertNotNull(field.get(instance));
        WorldFile worldFile = new WorldFileJSON(null);
        instance = new WorldFileDefault(null);
        instance.setWorldFile(worldFile);
        assertEquals(worldFile, field.get(instance));
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
        Logger.getLogger(WorldFileDefaultTest.class.getName()).log(Level.SEVERE, null, ex);
        fail();
    }
}
Also used : Field(java.lang.reflect.Field) WorldFile(mudmap2.backend.WorldFileReader.WorldFile) Test(org.junit.Test)

Example 8 with WorldFileDefault

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

the class WorldFileDefaultTest method testCanRead.

/**
 * Test of canRead method, of class WorldFileDefault.
 */
@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");
    }
    WorldFileDefault instancewfj = new WorldFileDefault(wfjFile);
    Boolean result = instancewfj.canRead();
    assertTrue(result);
}
Also used : IOException(java.io.IOException) World(mudmap2.backend.World) Test(org.junit.Test)

Example 9 with WorldFileDefault

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

the class WorldFileDefaultTest method testReadWriteFile.

/**
 * Test of readFile an writeFile methods, of class WorldFileDefault.
 * @throws java.lang.Exception
 */
@Test
public void testReadWriteFile() throws Exception {
    System.out.println("writeFile");
    String worldName = "FooBar";
    World world = new World(worldName);
    Layer layer1 = world.getNewLayer();
    Layer layer2 = world.getNewLayer();
    String[] placeNames = { "Foo", "Foo Bar", "Baz" };
    Integer[] placeX = { 0, 1, 6 };
    Integer[] placeY = { 0, 0, 4 };
    Place pl0 = new Place(placeNames[0], placeX[0], placeY[0], layer1);
    Place pl1 = new Place(placeNames[1], placeX[1], placeY[1], layer1);
    Place pl2 = new Place(placeNames[2], placeX[2], placeY[2], layer2);
    layer1.put(pl0);
    layer1.put(pl1);
    layer2.put(pl2);
    Path path0 = new Path(pl0, "n", pl1, "s");
    Path path1 = new Path(pl1, "e", pl0, "w");
    pl0.connectPath(path0);
    pl1.connectPath(path1);
    pl0.connectChild(pl2);
    String pgName0 = "myGroup";
    String pgName1 = "my second area";
    Color pgCol0 = Color.orange;
    Color pgCol1 = Color.blue;
    PlaceGroup pg0 = new PlaceGroup(pgName0, pgCol0);
    PlaceGroup pg1 = new PlaceGroup(pgName1, pgCol1);
    pl0.setPlaceGroup(pg0);
    pl1.setPlaceGroup(pg1);
    String flag0 = "a";
    String flag1 = "cd e";
    String flag2 = "fgh";
    pl0.setFlag(flag0, true);
    pl0.setFlag(flag1, true);
    pl0.setFlag(flag2, false);
    String comment0 = "This is a test comment";
    String comment1 = "and a second comment line";
    String comment2 = "and another one";
    pl0.addComment(comment0);
    pl0.addComment(comment1);
    pl0.addComment(comment2);
    String wfFile = folder.getRoot() + "/wfj";
    WorldFileDefault instanceWriter = new WorldFileDefault(wfFile);
    instanceWriter.writeFile(world);
    WorldFileDefault instanceReader = new WorldFileDefault(wfFile);
    World result = instanceReader.readFile();
    assertEquals(worldName, result.getName());
    assertEquals(2, result.getLayers().size());
    int placeNum = 0;
    Layer layer1new = null;
    Layer layer2new = null;
    for (Layer layer : result.getLayers()) {
        placeNum += layer.getPlaces().size();
        // find corresponding layers
        if (layer.getPlaces().size() == 1)
            layer2new = layer;
        else if (layer.getPlaces().size() == 2)
            layer1new = layer;
    }
    assertEquals(3, placeNum);
    assertNotNull(layer1new);
    assertNotNull(layer2new);
    Place pl0r = layer1new.get(placeX[0], placeY[0]);
    Place pl1r = layer1new.get(placeX[1], placeY[1]);
    Place pl2r = layer2new.get(placeX[2], placeY[2]);
    assertNotNull(pl0r);
    assertNotNull(pl1r);
    assertNotNull(pl2r);
    assertEquals(pl0.getName(), pl0r.getName());
    assertEquals(pl1.getName(), pl1r.getName());
    assertEquals(pl2.getName(), pl2r.getName());
    assertEquals(pl0.getX(), pl0r.getX());
    assertEquals(pl0.getY(), pl0r.getY());
    assertEquals(pl1.getX(), pl1r.getX());
    assertEquals(pl1.getY(), pl1r.getY());
    assertEquals(pl2.getX(), pl2r.getX());
    assertEquals(pl2.getY(), pl2r.getY());
    assertEquals(pl0r.getLayer(), pl1r.getLayer());
    assertNotSame(pl0r.getLayer().getId(), pl2r.getLayer().getId());
    assertEquals(2, pl0r.getPaths().size());
    assertEquals(2, pl1r.getPaths().size());
    assertEquals(0, pl2r.getPaths().size());
    Path path0r = pl0r.getPaths().toArray(new Path[2])[0];
    Path path1r = pl0r.getPaths().toArray(new Path[2])[1];
    Path path2r = pl1r.getPaths().toArray(new Path[2])[0];
    Path path3r = pl1r.getPaths().toArray(new Path[2])[1];
    assertTrue(path0r != path1r);
    assertTrue(path0r == path2r || path0r == path3r);
    assertTrue(path1r == path2r || path1r == path3r);
    assertEquals(pl0r.getPathTo("n"), pl1r.getPathTo("s"));
    assertEquals(pl0r.getPathTo("w"), pl1r.getPathTo("e"));
    assertEquals(1, pl0r.getChildren().size());
    assertEquals(0, pl1r.getChildren().size());
    assertEquals(0, pl2r.getChildren().size());
    assertEquals(0, pl0r.getParents().size());
    assertEquals(0, pl1r.getParents().size());
    assertEquals(1, pl2r.getParents().size());
    assertEquals(pl0r.getChildren().toArray(new Place[1])[0], pl2r);
    assertEquals(pl2r.getParents().toArray(new Place[1])[0], pl0r);
    assertNotNull(pl0r.getPlaceGroup());
    assertNotNull(pl1r.getPlaceGroup());
    assertNull(pl2r.getPlaceGroup());
    assertEquals(pl0.getPlaceGroup().getName(), pl0r.getPlaceGroup().getName());
    assertEquals(pl1.getPlaceGroup().getName(), pl1r.getPlaceGroup().getName());
    assertEquals(2, pl0r.getFlags().size());
    assertEquals(0, pl1r.getFlags().size());
    assertEquals(0, pl2r.getFlags().size());
    assertTrue(pl0r.getFlag(flag0));
    assertTrue(pl0r.getFlag(flag1));
    assertFalse(pl0r.getFlag(flag2));
    assertEquals(3, pl0r.getComments().size());
    assertEquals(0, pl1r.getComments().size());
    assertEquals(0, pl2r.getComments().size());
    assertTrue(pl0r.getComments().contains(comment0));
    assertTrue(pl0r.getComments().contains(comment1));
    assertTrue(pl0r.getComments().contains(comment2));
}
Also used : Path(mudmap2.backend.Path) Color(java.awt.Color) World(mudmap2.backend.World) PlaceGroup(mudmap2.backend.PlaceGroup) Layer(mudmap2.backend.Layer) Place(mudmap2.backend.Place) Test(org.junit.Test)

Example 10 with WorldFileDefault

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

the class WorldFileList method readDirectory.

/**
 * Get available worlds from filesystem
 */
public static void readDirectory() {
    // get file list
    File dir = new File(Environment.getWorldsDir());
    File[] fileList = dir.listFiles();
    if (fileList != null) {
        // find world files in file list
        for (File file : fileList) {
            // exclude meta and backup files
            if (!file.getName().equals("worlds") && !file.getName().endsWith("_meta") && !file.getName().endsWith(".backup") && !file.getName().endsWith(".bak") && file.isFile() && file.canRead()) {
                // check if file is world file
                WorldFileDefault worldFile = new WorldFileDefault(file.getPath());
                try {
                    if (worldFile.canRead()) {
                        // is world file
                        String name = worldFile.readWorldName();
                        availableWorlds.put(file.getPath(), name);
                    }
                } catch (FileNotFoundException ex) {
                } catch (Exception ex) {
                    Logger.getLogger(WorldFileList.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) 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