From de96d7b661e3f37cf3ef6d4c8ee943f043064ecb Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 21 Jan 2020 11:21:34 +0100 Subject: [PATCH] Add functionnality openning in finder --- src/clientP2P/ClientP2P.java | 16 ++++++++++++++-- src/tools/Directories.java | 18 +++++++++++++++++- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/clientP2P/ClientP2P.java b/src/clientP2P/ClientP2P.java index 8d7e678..e409d48 100644 --- a/src/clientP2P/ClientP2P.java +++ b/src/clientP2P/ClientP2P.java @@ -1,16 +1,28 @@ package clientP2P; import clientP2P.ClientManagementUDP; import tools.Directories; +import java.util.Scanner; public class ClientP2P { private String host; private int port; private String dataHomeDirectory; public ClientP2P() { + Directories d = new Directories("P2P_JAVA_PROJECT_CLIENT"); + String os = System.getProperty("os.name"); + host = "localhost"; port = 40000; - dataHomeDirectory = new Directories("P2P_JAVA_PROJECT_CLIENT").getDataHomeDirectory(); - System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); + dataHomeDirectory = d.getDataHomeDirectory(); + System.out.println("Client will try to contact server at " + host + " on port " + port + ". It will save files in " + dataHomeDirectory); + if (os.equals("Linux")||os.equals("Mac")||os.equals("Mac OS X")) { + System.out.println("Do you want to open this directory? (y/N)"); + Scanner scanner = new Scanner(System.in); + String resp = scanner.nextLine(); + if (resp == "y" || resp == "Y") { + d.openDataHomeDirectory(); + } + } } public static void main(String [] args) { diff --git a/src/tools/Directories.java b/src/tools/Directories.java index 959bad9..42543a8 100644 --- a/src/tools/Directories.java +++ b/src/tools/Directories.java @@ -1,5 +1,8 @@ package tools; import java.io.File; +import java.lang.Runtime; +import java.io.IOException; + /** Helper to get application directories. * @author Louis Royer @@ -51,7 +54,20 @@ public class Directories { public String getDataHomeDirectory() { return dataHomeDirectory; } - + + public void openDataHomeDirectory() { + try { + String os = System.getProperty("os.name"); + + if (os.equals("Linux")) { + Runtime runtime = Runtime.getRuntime(); + runtime.exec(new String[] { "xdg-open", dataHomeDirectory }); + } else if (os.equals("Mac")||os.equals("Mac OS X")) { + Runtime runtime = Runtime.getRuntime(); + runtime.exec(new String[] { "open", dataHomeDirectory }); + } + } catch (IOException e) {} + } }