Skip to content

Commit

Permalink
implemented file extension .m2w #13, #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Neop committed Apr 15, 2018
1 parent 38332f9 commit 00470ab
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 53 deletions.
46 changes: 19 additions & 27 deletions src/main/java/mudmap2/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,38 @@ public class Environment {
public static final String GITHUB_URL = "https://github.com/Neop/mudmap2";
public static final String SOURCEFORGE_URL = "http://sf.net/p/mudmap";

public static String getHome(){
String ret = "";
if(System.getProperty("os.name").toLowerCase().contains("win")){
// operating system Windows
ret = System.getenv().get("APPDATA");
} else {
// other operating systems
ret = System.getProperty("user.home");
}
return ret;
}

/**
* Gets the user data path
* @return user data path
*/
public static String getUserDataDir(){
if(userDataDir == null || userDataDir.isEmpty()){
// read the user data path from environment variables
// operating system Windows
if(System.getProperty("os.name").toLowerCase().contains("win"))
if(System.getProperty("os.name").toLowerCase().contains("win")){
// operating system Windows
userDataDir = System.getenv().get("APPDATA") + File.separator + "mudmap" + File.separator;
// other operating systems
else userDataDir = System.getProperty("user.home") + File.separator + ".mudmap" + File.separator;
} else {
// other operating systems
userDataDir = System.getProperty("user.home") + File.separator + ".mudmap" + File.separator;
}
}
return userDataDir;
}

/**
* Changes the user data dir for debugging purposes
* Changes the user data dir for debugging purposes
* @param userDataDir
*/
public static void setUserDataDir(String userDataDir) {
Expand Down Expand Up @@ -95,26 +109,4 @@ public static void openWebsite(String url){
}
}

/**
* checks if path is a directory
* @param path
* @return true, if path is a directory
*/
public static boolean isDirectory(String path){
File f = new File(path);
return f.exists() && f.isDirectory();
}

/**
* Creates a directory
* @param path
*/
public static void createDirectory(String path){
Integer sep = path.lastIndexOf(File.separator);
sep = max(sep, path.lastIndexOf('/'));
if(sep > 0) createDirectory(path.substring(0, sep));

File f = new File(path);
if(!f.exists()) f.mkdir();
}
}
2 changes: 1 addition & 1 deletion src/main/java/mudmap2/backend/WorldFileList.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static void cleanList(){

for(WorldFileEntry entry: worldFileHistory){
if(!entry.getFile().exists()){
worldFileHistory.remove(entry);
toBeRemoved.add(entry);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* MUD Map (v2) - A tool to create and organize maps for text-based games
* Copyright (C) 2018 Neop (email: mneop@web.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*/
package mudmap2.backend.WorldFileReader;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.filechooser.FileFilter;
import mudmap2.backend.WorldFileReader.current.WorldFileDefault;
import mudmap2.frontend.dialog.OpenWorldDialog;

/**
*
* @author neop
*/
public class WorldFileFilterJSON extends 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 2 World Files (all)";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* MUD Map (v2) - A tool to create and organize maps for text-based games
* Copyright (C) 2018 Neop (email: mneop@web.de)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, see <http://www.gnu.org/licenses/>.
*/
package mudmap2.backend.WorldFileReader;

import java.io.File;
import javax.swing.filechooser.FileFilter;

/**
*
* @author neop
*/
public class WorldFileFilterM2W extends FileFilter {

@Override
public boolean accept(File file) {
if(file == null) return false;
if(file.isDirectory()) return true;
return file.getName().endsWith(".m2w");
}

@Override
public String getDescription() {
return "MUD Map 2 World Files (.m2w)";
}

}
28 changes: 7 additions & 21 deletions src/main/java/mudmap2/frontend/dialog/OpenWorldDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import mudmap2.backend.World;
import mudmap2.backend.WorldFileReader.current.WorldFileDefault;
import mudmap2.backend.WorldFileReader.WorldFileFilterJSON;
import mudmap2.backend.WorldFileReader.WorldFileFilterM2W;
import mudmap2.backend.WorldManager;
import mudmap2.frontend.Mainwindow;

Expand All @@ -55,27 +55,13 @@ private void create(){
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("");
}
FileFilter filter;
filechooser.addChoosableFileFilter(filter = new WorldFileFilterM2W());
filechooser.addChoosableFileFilter(new WorldFileFilterJSON());
filechooser.setFileHidingEnabled(false);

@Override
public String getDescription() {
return "MUD Map world files";
}
});
filechooser.setFileFilter(filter);

int ret = filechooser.showOpenDialog(parent);

Expand Down
23 changes: 19 additions & 4 deletions src/main/java/mudmap2/frontend/dialog/SaveWorldDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.filechooser.FileFilter;
import mudmap2.Environment;
import mudmap2.backend.WorldFileReader.WorldFile;
import mudmap2.backend.WorldFileReader.WorldFileFilterJSON;
import mudmap2.backend.WorldFileReader.WorldFileFilterM2W;
import mudmap2.backend.WorldFileReader.current.WorldFileJSON;
import mudmap2.frontend.WorldTab;

Expand All @@ -39,17 +42,29 @@ public class SaveWorldDialog extends JFileChooser {
JRadioButton radioJSON;

public SaveWorldDialog(JFrame parent, WorldTab wt){
super(wt.getWorld().getWorldFile() != null ? wt.getWorld().getWorldFile().getFilename() : Environment.getWorldsDir());
super(wt.getWorld().getWorldFile() != null ? wt.getWorld().getWorldFile().getFilename() : Environment.getHome());

setFileHidingEnabled(false);

FileFilter filter;
addChoosableFileFilter(filter = new WorldFileFilterM2W());
addChoosableFileFilter(new WorldFileFilterJSON());

setFileFilter(filter);

this.wt = wt;
}


public WorldFile getWorldFile(){
String file = getSelectedFile().getAbsolutePath();
WorldFile worldFile;

worldFile = new WorldFileJSON(file);
if(getFileFilter() instanceof WorldFileFilterM2W){
if(!file.endsWith(".m2w")){
file = file + ".m2w";
}
}

WorldFile worldFile = new WorldFileJSON(file);

return worldFile;
}
Expand Down

0 comments on commit 00470ab

Please sign in to comment.