commenting

This commit is contained in:
Greg DiCristofaro 2023-08-08 13:04:30 -04:00
parent 2d32df628c
commit 5662187b2d

View File

@ -49,12 +49,19 @@ public class Desktop {
return xdgSupported; return xdgSupported;
} }
/**
* @return True if this class's external calls can be used on this operating
* system.
*/
public static boolean isDesktopSupported() { public static boolean isDesktopSupported() {
return java.awt.Desktop.isDesktopSupported() || isXdgSupported(); return java.awt.Desktop.isDesktopSupported() || isXdgSupported();
} }
private static Desktop instance = null; private static Desktop instance = null;
/**
* @return A singleton instance of this class.
*/
public static Desktop getDesktop() { public static Desktop getDesktop() {
if (instance == null) { if (instance == null) {
instance = new Desktop(java.awt.Desktop.getDesktop()); instance = new Desktop(java.awt.Desktop.getDesktop());
@ -65,14 +72,28 @@ public class Desktop {
private final java.awt.Desktop awtDesktop; private final java.awt.Desktop awtDesktop;
/**
* Private constructor for this wrapper.
* @param awtDesktop The delegate java.awt.Desktop.
*/
private Desktop(java.awt.Desktop awtDesktop) { private Desktop(java.awt.Desktop awtDesktop) {
this.awtDesktop = awtDesktop; this.awtDesktop = awtDesktop;
} }
/**
* Opens a given path using `xdg-open` on linux.
* @param path The path.
* @throws IOException
*/
private void xdgOpen(String path) throws IOException { private void xdgOpen(String path) throws IOException {
Runtime.getRuntime().exec(new String[]{"xdg-open", path}); Runtime.getRuntime().exec(new String[]{"xdg-open", path});
} }
/**
* Triggers the OS to navigate to the given uri.
* @param uri The uri.
* @throws IOException
*/
public void browse(URI uri) throws IOException { public void browse(URI uri) throws IOException {
if (!awtDesktop.isSupported(java.awt.Desktop.Action.BROWSE) && isXdgSupported()) { if (!awtDesktop.isSupported(java.awt.Desktop.Action.BROWSE) && isXdgSupported()) {
xdgOpen(uri.toString()); xdgOpen(uri.toString());
@ -81,6 +102,11 @@ public class Desktop {
} }
} }
/**
* Triggers the OS to open the given file.
* @param file The file.
* @throws IOException
*/
public void open(File file) throws IOException { public void open(File file) throws IOException {
if (!awtDesktop.isSupported(java.awt.Desktop.Action.OPEN) && isXdgSupported()) { if (!awtDesktop.isSupported(java.awt.Desktop.Action.OPEN) && isXdgSupported()) {
xdgOpen(file.getAbsolutePath()); xdgOpen(file.getAbsolutePath());
@ -89,6 +115,11 @@ public class Desktop {
} }
} }
/**
* Triggers the OS to edit the given file.
* @param file The file.
* @throws IOException
*/
public void edit(File file) throws IOException { public void edit(File file) throws IOException {
if (!awtDesktop.isSupported(java.awt.Desktop.Action.EDIT) && isXdgSupported()) { if (!awtDesktop.isSupported(java.awt.Desktop.Action.EDIT) && isXdgSupported()) {
xdgOpen(file.getAbsolutePath()); xdgOpen(file.getAbsolutePath());