Skip to content

Commit

Permalink
Replace Desktop#browse
Browse files Browse the repository at this point in the history
  • Loading branch information
xxDark committed Jan 5, 2022
1 parent 9fdc407 commit 3212458
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 22 deletions.
7 changes: 7 additions & 0 deletions checkstyle-suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,11 @@
<suppress checks="ClassDataAbstractionCoupling"
files="FernFlowerAccessor.java"
lines="0-9999"/>

<suppress checks="ClassFanOutComplexity"
files="UiUtil.java"
lines="0-9999"/>
<suppress checks="ClassDataAbstractionCoupling"
files="UiUtil.java"
lines="0-9999"/>
</suppressions>
6 changes: 3 additions & 3 deletions src/main/java/me/coley/recaf/command/impl/Initializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import me.coley.recaf.util.LangUtil;
import me.coley.recaf.util.Log;
import me.coley.recaf.util.ThreadUtil;
import me.coley.recaf.util.UiUtil;
import me.coley.recaf.util.self.SelfUpdater;
import me.coley.recaf.workspace.InstrumentationResource;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;

import java.awt.*;
import java.net.URL;
import java.net.URI;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Scanner;
Expand Down Expand Up @@ -105,7 +105,7 @@ private void promptFirstTime() {

private void openDoc() {
try {
Desktop.getDesktop().browse(new URL(Recaf.DOC_URL).toURI());
UiUtil.showDocument(URI.create(Recaf.DOC_URL));
} catch(Exception ex) {
Log.error(ex, "Failed to open documentation url");
}
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/me/coley/recaf/ui/MainMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@
import me.coley.recaf.search.QueryType;
import me.coley.recaf.ui.controls.*;
import me.coley.recaf.ui.controls.pane.*;
import me.coley.recaf.util.ClasspathUtil;
import me.coley.recaf.util.IOUtil;
import me.coley.recaf.util.Log;
import me.coley.recaf.util.OSUtil;
import me.coley.recaf.util.*;
import me.coley.recaf.util.self.SelfUpdater;
import me.coley.recaf.workspace.*;
import org.apache.commons.io.FileUtils;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -457,7 +453,7 @@ private void showContact() {
*/
private void showDocumentation() {
try {
Desktop.getDesktop().browse(new URL(Recaf.DOC_URL).toURI());
UiUtil.showDocument(URI.create(Recaf.DOC_URL));
} catch(Exception ex) {
Log.error(ex, "Failed to open documentation url");
}
Expand Down Expand Up @@ -494,7 +490,7 @@ private void openPluginManager() {
*/
private void openPluginDirectory() {
try {
Desktop.getDesktop().browse(Recaf.getDirectory("plugins").toUri());
UiUtil.showDocument(Recaf.getDirectory("plugins").toUri());
} catch(IOException ex) {
Log.error(ex, "Failed to open plugins directory");
}
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/me/coley/recaf/ui/controls/ExceptionAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import javafx.scene.text.Text;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;
import me.coley.recaf.util.UiUtil;

import java.awt.Desktop;
import java.io.*;
import java.net.URI;
import java.net.URLEncoder;
Expand Down Expand Up @@ -56,7 +56,7 @@ private ExceptionAlert(Throwable t, String msg) {
suffix = suffix + ": " + t.getMessage();
suffix = URLEncoder.encode(suffix, "UTF-8");
// Open link in default browser
Desktop.getDesktop().browse(URI.create(BUG_REPORT_LINK + suffix));
UiUtil.showDocument(URI.create(BUG_REPORT_LINK + suffix));
} catch(IOException exx) {
error(exx, "Failed to open bug report link");
show(exx, "Failed to open bug report link.");
Expand All @@ -69,9 +69,7 @@ private ExceptionAlert(Throwable t, String msg) {
grid.setMaxWidth(Double.MAX_VALUE);
grid.add(lbl, 0, 0);
grid.add(txt, 0, 1);
// If desktop is supported, allow click-able bug report link
if (Desktop.isDesktopSupported())
grid.add(prompt, 0, 2);
grid.add(prompt, 0, 2);
getDialogPane().setExpandableContent(grid);
getDialogPane().setExpanded(true);
// Set icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import javafx.scene.layout.GridPane;
import me.coley.recaf.ui.controls.IconView;
import me.coley.recaf.util.Log;
import java.awt.*;
import me.coley.recaf.util.UiUtil;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

/**
* Panel that shows contact information.
Expand Down Expand Up @@ -39,8 +39,8 @@ private Node link(String url, String iconPath) {
Hyperlink link = new Hyperlink(url, new IconView(iconPath));
link.setOnAction(e -> {
try {
Desktop.getDesktop().browse(new URI(url));
} catch(IOException | URISyntaxException ex) {
UiUtil.showDocument(URI.create(url));
} catch(IOException | IllegalArgumentException ex) {
Log.error(ex, "Failed to open URL");
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import me.coley.recaf.ui.controls.SubLabeled;
import me.coley.recaf.util.ClasspathUtil;
import me.coley.recaf.util.Log;
import me.coley.recaf.util.UiUtil;

import javax.tools.ToolProvider;

import java.awt.*;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -82,7 +82,7 @@ public SysInfoPane() {
clip.setContent(content);
}), new ActionButton(translate("ui.about.opendir"), () -> {
try {
Desktop.getDesktop().open(Recaf.getDirectory().toFile());
UiUtil.showDocument(Recaf.getDirectory().toUri());
} catch(Exception ex) {
Log.error(ex, "Failed to open Recaf directory");
}
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/me/coley/recaf/util/UiUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -310,4 +313,41 @@ private static void animate(Node node, long millis, int r, int g, int b) {
timeline.getKeyFrames().add(kf);
timeline.play();
}

/**
* Attempts to launch a browser to display a {@link URI}.
*
* @param uri
* URI to display.
*
* @throws IOException
* If the browser is not found, or it fails
* to be launched.
*/
public static void showDocument(URI uri) throws IOException {
switch (OSUtil.getOSType()) {
case MAC:
Desktop.getDesktop().browse(uri);
break;
case WINDOWS:
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + uri);
break;
case LINUX:
Runtime rt = Runtime.getRuntime();
String[] browsers = new String[]{"xdg-open", "google-chrome", "firefox", "opera",
"konqueror", "mozilla"};

for (String browser : browsers) {
try (InputStream in = rt.exec(new String[]{"which", browser}).getInputStream()) {
if (in.read() != -1) {
rt.exec(new String[]{browser, uri.toString()});
return;
}
}
}
throw new IOException("No browser found");
default:
throw new IllegalStateException("Unsupported OS");
}
}
}

0 comments on commit 3212458

Please sign in to comment.