ShichimiFX 1.0.4 released
I have released ShichimiFX 1.0.4.
Get the code from my ShichimiFX Bitbucket Repo
Maven
<dependency> <groupId>de.jensd</groupId> <artifactId>shichimifx</artifactId> <version>1.0.4</version> </dependency>
This release contains 2 new utility classes I needed for MQTT.fx‘s script editing function Desktop
and Shell
.
By now I used to open a script for editing with the system default editor via java.awt.Desktop
. So far this works well on my Mac but there were issues on Windows and Linux. To get rid of these issues I create my own Desktop
class to open the script directly with the system shell:
Desktop
public class Desktop { public static boolean edit(String fileToEdit) { return openWithSystem(fileToEdit); } private static boolean openWithSystem(String fileToOpen) { if (OS.isLinux()) { if (Shell.canOpen("gnome-open", fileToOpen)) { return true; } else if (Shell.canOpen("kde-open", fileToOpen)) { return true; } else if (Shell.canOpen("xdg-open", fileToOpen)) { return true; } } else if (OS.isWindows()) { if (Shell.canOpen("explorer", fileToOpen)) { return true; } } else if (OS.isMac()) { if (Shell.canOpen("open", fileToOpen)) { return true; } } return false; } }
Shell
public class Shell { public static boolean canOpen(String... command) { try { ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.directory(new File(System.getProperty("user.home"))).start(); if (process == null) { return false; } try { process.exitValue(); // if this does not throw an IllegalThreadStateException the process is not running/has exited immediately return false; } catch (IllegalThreadStateException e) { // the process seems to be in a running state return true; } } catch (IOException ex) { return false; } } public static List<String> prepareOpenFileLine(String command, String fileToOpen) { List<String> commandList = new ArrayList<>(); for (String s : command.split(" ")) { commandList.add(s.trim()); } commandList.add(fileToOpen); return commandList; } public static Optional<Process> execute(List<String> command) throws IOException { return execute(new File(System.getProperty("user.home")), command); } public static Optional<Process> execute(File directory, String... command) throws IOException { return execute(directory, Arrays.asList(command)); } public static Optional<Process> execute(String... command) throws IOException { return Shell.execute(new File(System.getProperty("user.home")), command); } public static Optional<Process> execute(File directory, List<String> command) throws IOException { ProcessBuilder builder = new ProcessBuilder(command); Process process = builder.directory(directory).start(); return Optional.of(process); } }
Leave a Reply