aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdam NAILI2018-02-03 23:19:39 +0100
committerAdam NAILI2018-02-03 23:19:39 +0100
commitc4fa546ef067317bd5e22cdad40792cbd33035d7 (patch)
tree8963152ad1613a4f745106156af3aaeb2c3e4477
parente86f5e3c3e197aee8899f643d026dcf4468fbd84 (diff)
downloadwallj-c4fa546ef067317bd5e22cdad40792cbd33035d7.tar.gz
Refactoring Main structure + handling JAR/IDE call of the application thru FileSystems
-rw-r--r--src/main/java/fr/umlv/java/wallj/viewer/Main.java27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/main/java/fr/umlv/java/wallj/viewer/Main.java b/src/main/java/fr/umlv/java/wallj/viewer/Main.java
index 01c6211..3055da8 100644
--- a/src/main/java/fr/umlv/java/wallj/viewer/Main.java
+++ b/src/main/java/fr/umlv/java/wallj/viewer/Main.java
@@ -6,29 +6,38 @@ import fr.umlv.java.wallj.board.BoardValidator;
6import fr.umlv.zen5.Application; 6import fr.umlv.zen5.Application;
7 7
8import java.awt.*; 8import java.awt.*;
9import java.io.File;
9import java.io.IOException; 10import java.io.IOException;
10import java.net.URI; 11import java.net.URI;
11import java.net.URISyntaxException; 12import java.net.URISyntaxException;
12import java.nio.file.*; 13import java.nio.file.*;
13import java.util.HashMap; 14import java.util.*;
14import java.util.LinkedList;
15import java.util.List; 15import java.util.List;
16import java.util.Map;
17 16
18public class Main { 17public class Main {
19 18
20 private static final String DEFAULT_MAP_NAME = "/maps/level0.txt"; 19 private static final String DEFAULT_MAP_NAME = "/maps/level0.txt";
21 private static java.util.List<Path> getResourcePaths(String[] args) throws URISyntaxException,IOException { 20
21
22 private static FileSystem fileSystemForContext(URI uri) throws URISyntaxException, IOException {
23 boolean isInJar = Objects.equals(Main.class.getProtectionDomain().getCodeSource().getLocation().getProtocol(), "jar");
24 if (isInJar) {//JAR from command line handling
25 Map<String, String> env = new HashMap<>();
26 env.put("create", "true");
27 return FileSystems.newFileSystem(uri, env);
28 }
29 return FileSystems.getDefault();//IDE Handling
30 }
31
32 private static List<Path> getResourcePaths() throws URISyntaxException, IOException {
33 final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI();
22 LinkedList<Path> paths = new LinkedList<>(); 34 LinkedList<Path> paths = new LinkedList<>();
23 String strFolder = System.getProperty("levels"); 35 String strFolder = System.getProperty("levels");
24 if (strFolder != null) { 36 if (strFolder != null) {
25 Path pathFolder = Paths.get(strFolder); 37 Path pathFolder = Paths.get(strFolder);
26 Files.newDirectoryStream(pathFolder).forEach(paths::add); 38 Files.newDirectoryStream(pathFolder).forEach(paths::add);
27 } else { 39 } else {
28 final URI uri = Main.class.getResource(DEFAULT_MAP_NAME).toURI(); 40 FileSystem fileSystem = fileSystemForContext(uri);
29 Map<String, String> env = new HashMap<>();
30 env.put("create", "true");
31 FileSystem zipfs = FileSystems.newFileSystem(uri, env);
32 paths.add(Paths.get(uri)); 41 paths.add(Paths.get(uri));
33 } 42 }
34 return paths; 43 return paths;
@@ -37,7 +46,7 @@ public class Main {
37 public static void main(String[] args) { 46 public static void main(String[] args) {
38 List<Board> boards = new LinkedList<>(); 47 List<Board> boards = new LinkedList<>();
39 try { 48 try {
40 List<Path> boardPaths = Main.getResourcePaths(args); 49 List<Path> boardPaths = Main.getResourcePaths();
41 for (Path path : boardPaths) { 50 for (Path path : boardPaths) {
42 BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path)); 51 BoardValidator boardValidator = new BoardValidator(BoardParser.parse(path));
43 boards.add( 52 boards.add(