Skip to content

Commit 3212458

Browse files
committed
Replace Desktop#browse
1 parent 9fdc407 commit 3212458

File tree

7 files changed

+63
-22
lines changed

7 files changed

+63
-22
lines changed

checkstyle-suppressions.xml

+7
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,11 @@
129129
<suppress checks="ClassDataAbstractionCoupling"
130130
files="FernFlowerAccessor.java"
131131
lines="0-9999"/>
132+
133+
<suppress checks="ClassFanOutComplexity"
134+
files="UiUtil.java"
135+
lines="0-9999"/>
136+
<suppress checks="ClassDataAbstractionCoupling"
137+
files="UiUtil.java"
138+
lines="0-9999"/>
132139
</suppressions>

src/main/java/me/coley/recaf/command/impl/Initializer.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import me.coley.recaf.util.LangUtil;
1111
import me.coley.recaf.util.Log;
1212
import me.coley.recaf.util.ThreadUtil;
13+
import me.coley.recaf.util.UiUtil;
1314
import me.coley.recaf.util.self.SelfUpdater;
1415
import me.coley.recaf.workspace.InstrumentationResource;
1516
import picocli.CommandLine.Command;
1617
import picocli.CommandLine.Option;
1718

18-
import java.awt.*;
19-
import java.net.URL;
19+
import java.net.URI;
2020
import java.nio.file.Path;
2121
import java.util.Optional;
2222
import java.util.Scanner;
@@ -105,7 +105,7 @@ private void promptFirstTime() {
105105

106106
private void openDoc() {
107107
try {
108-
Desktop.getDesktop().browse(new URL(Recaf.DOC_URL).toURI());
108+
UiUtil.showDocument(URI.create(Recaf.DOC_URL));
109109
} catch(Exception ex) {
110110
Log.error(ex, "Failed to open documentation url");
111111
}

src/main/java/me/coley/recaf/ui/MainMenu.java

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,14 @@
1919
import me.coley.recaf.search.QueryType;
2020
import me.coley.recaf.ui.controls.*;
2121
import me.coley.recaf.ui.controls.pane.*;
22-
import me.coley.recaf.util.ClasspathUtil;
23-
import me.coley.recaf.util.IOUtil;
24-
import me.coley.recaf.util.Log;
25-
import me.coley.recaf.util.OSUtil;
22+
import me.coley.recaf.util.*;
2623
import me.coley.recaf.util.self.SelfUpdater;
2724
import me.coley.recaf.workspace.*;
2825
import org.apache.commons.io.FileUtils;
2926

30-
import java.awt.*;
3127
import java.io.File;
3228
import java.io.IOException;
33-
import java.net.URL;
29+
import java.net.URI;
3430
import java.nio.file.Files;
3531
import java.nio.file.Path;
3632
import java.nio.file.Paths;
@@ -457,7 +453,7 @@ private void showContact() {
457453
*/
458454
private void showDocumentation() {
459455
try {
460-
Desktop.getDesktop().browse(new URL(Recaf.DOC_URL).toURI());
456+
UiUtil.showDocument(URI.create(Recaf.DOC_URL));
461457
} catch(Exception ex) {
462458
Log.error(ex, "Failed to open documentation url");
463459
}
@@ -494,7 +490,7 @@ private void openPluginManager() {
494490
*/
495491
private void openPluginDirectory() {
496492
try {
497-
Desktop.getDesktop().browse(Recaf.getDirectory("plugins").toUri());
493+
UiUtil.showDocument(Recaf.getDirectory("plugins").toUri());
498494
} catch(IOException ex) {
499495
Log.error(ex, "Failed to open plugins directory");
500496
}

src/main/java/me/coley/recaf/ui/controls/ExceptionAlert.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import javafx.scene.text.Text;
99
import javafx.scene.text.TextFlow;
1010
import javafx.stage.Stage;
11+
import me.coley.recaf.util.UiUtil;
1112

12-
import java.awt.Desktop;
1313
import java.io.*;
1414
import java.net.URI;
1515
import java.net.URLEncoder;
@@ -56,7 +56,7 @@ private ExceptionAlert(Throwable t, String msg) {
5656
suffix = suffix + ": " + t.getMessage();
5757
suffix = URLEncoder.encode(suffix, "UTF-8");
5858
// Open link in default browser
59-
Desktop.getDesktop().browse(URI.create(BUG_REPORT_LINK + suffix));
59+
UiUtil.showDocument(URI.create(BUG_REPORT_LINK + suffix));
6060
} catch(IOException exx) {
6161
error(exx, "Failed to open bug report link");
6262
show(exx, "Failed to open bug report link.");
@@ -69,9 +69,7 @@ private ExceptionAlert(Throwable t, String msg) {
6969
grid.setMaxWidth(Double.MAX_VALUE);
7070
grid.add(lbl, 0, 0);
7171
grid.add(txt, 0, 1);
72-
// If desktop is supported, allow click-able bug report link
73-
if (Desktop.isDesktopSupported())
74-
grid.add(prompt, 0, 2);
72+
grid.add(prompt, 0, 2);
7573
getDialogPane().setExpandableContent(grid);
7674
getDialogPane().setExpanded(true);
7775
// Set icon

src/main/java/me/coley/recaf/ui/controls/pane/ContactInfoPane.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
import javafx.scene.layout.GridPane;
99
import me.coley.recaf.ui.controls.IconView;
1010
import me.coley.recaf.util.Log;
11-
import java.awt.*;
11+
import me.coley.recaf.util.UiUtil;
12+
1213
import java.io.IOException;
1314
import java.net.URI;
14-
import java.net.URISyntaxException;
1515

1616
/**
1717
* Panel that shows contact information.
@@ -39,8 +39,8 @@ private Node link(String url, String iconPath) {
3939
Hyperlink link = new Hyperlink(url, new IconView(iconPath));
4040
link.setOnAction(e -> {
4141
try {
42-
Desktop.getDesktop().browse(new URI(url));
43-
} catch(IOException | URISyntaxException ex) {
42+
UiUtil.showDocument(URI.create(url));
43+
} catch(IOException | IllegalArgumentException ex) {
4444
Log.error(ex, "Failed to open URL");
4545
}
4646
});

src/main/java/me/coley/recaf/ui/controls/pane/SysInfoPane.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
import me.coley.recaf.ui.controls.SubLabeled;
1414
import me.coley.recaf.util.ClasspathUtil;
1515
import me.coley.recaf.util.Log;
16+
import me.coley.recaf.util.UiUtil;
1617

1718
import javax.tools.ToolProvider;
1819

19-
import java.awt.*;
2020
import java.util.LinkedHashMap;
2121
import java.util.Map;
2222
import java.util.stream.Collectors;
@@ -82,7 +82,7 @@ public SysInfoPane() {
8282
clip.setContent(content);
8383
}), new ActionButton(translate("ui.about.opendir"), () -> {
8484
try {
85-
Desktop.getDesktop().open(Recaf.getDirectory().toFile());
85+
UiUtil.showDocument(Recaf.getDirectory().toUri());
8686
} catch(Exception ex) {
8787
Log.error(ex, "Failed to open Recaf directory");
8888
}

src/main/java/me/coley/recaf/util/UiUtil.java

+40
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import java.awt.*;
2323
import java.awt.image.BufferedImage;
2424
import java.io.ByteArrayInputStream;
25+
import java.io.IOException;
26+
import java.io.InputStream;
2527
import java.lang.reflect.Method;
28+
import java.net.URI;
2629
import java.util.Arrays;
2730
import java.util.Set;
2831
import java.util.stream.Collectors;
@@ -310,4 +313,41 @@ private static void animate(Node node, long millis, int r, int g, int b) {
310313
timeline.getKeyFrames().add(kf);
311314
timeline.play();
312315
}
316+
317+
/**
318+
* Attempts to launch a browser to display a {@link URI}.
319+
*
320+
* @param uri
321+
* URI to display.
322+
*
323+
* @throws IOException
324+
* If the browser is not found, or it fails
325+
* to be launched.
326+
*/
327+
public static void showDocument(URI uri) throws IOException {
328+
switch (OSUtil.getOSType()) {
329+
case MAC:
330+
Desktop.getDesktop().browse(uri);
331+
break;
332+
case WINDOWS:
333+
Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + uri);
334+
break;
335+
case LINUX:
336+
Runtime rt = Runtime.getRuntime();
337+
String[] browsers = new String[]{"xdg-open", "google-chrome", "firefox", "opera",
338+
"konqueror", "mozilla"};
339+
340+
for (String browser : browsers) {
341+
try (InputStream in = rt.exec(new String[]{"which", browser}).getInputStream()) {
342+
if (in.read() != -1) {
343+
rt.exec(new String[]{browser, uri.toString()});
344+
return;
345+
}
346+
}
347+
}
348+
throw new IOException("No browser found");
349+
default:
350+
throw new IllegalStateException("Unsupported OS");
351+
}
352+
}
313353
}

0 commit comments

Comments
 (0)