Skip to content

Commit db0f4ce

Browse files
committed
move GUI code to separate class
1 parent 3aef35e commit db0f4ce

File tree

2 files changed

+77
-70
lines changed

2 files changed

+77
-70
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.quiltmc.installer;
2+
3+
import javax.swing.*;
4+
import javax.swing.event.HyperlinkEvent;
5+
import java.awt.*;
6+
7+
// Gui code MUST be in a separate class because not every JRE supports Swing
8+
public class GuiMain {
9+
static void tryShowGuiError() {
10+
try {
11+
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
12+
} catch (ClassNotFoundException | UnsupportedLookAndFeelException | IllegalAccessException | InstantiationException e) {
13+
// oh well
14+
e.printStackTrace();
15+
}
16+
17+
String javaDownloadUrl = "https://adoptium.net/temurin/releases/?package=jdk&version=lts";
18+
19+
// best-effort attempt to make the download URL more user friendly
20+
String osName = System.getProperty("os.name", "unknown");
21+
if(osName.contains("Windows")) {
22+
javaDownloadUrl += "&os=windows";
23+
}
24+
else if (osName.contains("Linux")) {
25+
javaDownloadUrl += "&os=linux";
26+
}
27+
else if (osName.contains("OS X")) {
28+
javaDownloadUrl += "&os=mac";
29+
}
30+
31+
String osArch = System.getProperty("os.arch", "unknown");
32+
if (osArch.equals("x86_64") || osArch.equals("amd64") || osArch.equals("x64")) {
33+
javaDownloadUrl += "&arch=x64";
34+
}
35+
else if (osArch.equals("aarch64") || osArch.equals("arm64")) {
36+
javaDownloadUrl += "&arch=aarch64";
37+
}
38+
39+
showPopup("Quilt Installer crashed!", String.format("Quilt Installer needs Java %s to run." +
40+
"<br><br>Install the latest LTS release of Java from <a href=\"%s\">Adoptium</a> and try again." +
41+
"<br><br>If you need help, ask in the <a href=\"discord.quiltmc.org\">Quilt Discord server</a>.", BuildConstants8.MIN_JAVA_VERSION, javaDownloadUrl), JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
42+
}
43+
44+
// Copied from AbstractPanel
45+
protected static boolean showPopup(String title, String description, int optionType, int messageType) {
46+
JEditorPane pane = new JEditorPane("text/html",
47+
"<html><body style=\"" + buildEditorPaneStyle() + "\">" + description + "</body></html>");
48+
pane.setEditable(false);
49+
pane.addHyperlinkListener(e -> {
50+
try {
51+
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
52+
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
53+
Desktop.getDesktop().browse(e.getURL().toURI());
54+
} else {
55+
throw new UnsupportedOperationException("Failed to open " + e.getURL().toString());
56+
}
57+
}
58+
} catch (Throwable throwable) {
59+
// Oh well
60+
throwable.printStackTrace();
61+
}
62+
});
63+
return JOptionPane.showOptionDialog(null, pane, title, optionType, messageType, null, null, null) == 0;
64+
}
65+
66+
private static String buildEditorPaneStyle() {
67+
JLabel label = new JLabel();
68+
Font font = label.getFont();
69+
Color color = label.getBackground();
70+
71+
return String.format(
72+
"font-family:%s;font-weight:%s;font-size:%dpt;background-color: rgb(%d,%d,%d);",
73+
font.getFamily(), (font.isBold() ? "bold" : "normal"), font.getSize(), color.getRed(), color.getGreen(), color.getBlue()
74+
);
75+
}
76+
}

src/java8/java/org/quiltmc/installer/Main.java

Lines changed: 1 addition & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
package org.quiltmc.installer;
1818

19-
import javax.swing.*;
20-
import javax.swing.event.HyperlinkEvent;
2119
import java.awt.*;
2220
import java.lang.invoke.MethodHandles;
2321
import java.lang.invoke.MethodType;
@@ -52,7 +50,7 @@ public static void main(String[] args) {
5250
} catch (UnsupportedClassVersionError error) {
5351
System.err.printf("Quilt Installer requires Java %s or greater to run.%n", BuildConstants8.MIN_JAVA_VERSION);
5452
if (!cliMode) {
55-
tryShowGuiError();
53+
GuiMain.tryShowGuiError();
5654
}
5755

5856
System.exit(1);
@@ -61,71 +59,4 @@ public static void main(String[] args) {
6159
}
6260
}
6361

64-
private static void tryShowGuiError() {
65-
try {
66-
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
67-
} catch (ClassNotFoundException | UnsupportedLookAndFeelException | IllegalAccessException | InstantiationException e) {
68-
// oh well
69-
e.printStackTrace();
70-
}
71-
72-
String javaDownloadUrl = "https://adoptium.net/temurin/releases/?package=jdk&version=lts";
73-
74-
// best-effort attempt to make the download URL more user friendly
75-
String osName = System.getProperty("os.name", "unknown");
76-
if(osName.contains("Windows")) {
77-
javaDownloadUrl += "&os=windows";
78-
}
79-
else if (osName.contains("Linux")) {
80-
javaDownloadUrl += "&os=linux";
81-
}
82-
else if (osName.contains("OS X")) {
83-
javaDownloadUrl += "&os=mac";
84-
}
85-
86-
String osArch = System.getProperty("os.arch", "unknown");
87-
if (osArch.equals("x86_64") || osArch.equals("amd64") || osArch.equals("x64")) {
88-
javaDownloadUrl += "&arch=x64";
89-
}
90-
else if (osArch.equals("aarch64") || osArch.equals("arm64")) {
91-
javaDownloadUrl += "&arch=aarch64";
92-
}
93-
94-
showPopup("Quilt Installer crashed!", String.format("Quilt Installer needs Java %s to run." +
95-
"<br><br>Install the latest LTS release of Java from <a href=\"%s\">Adoptium</a> and try again." +
96-
"<br><br>If you need help, ask in the <a href=\"discord.quiltmc.org\">Quilt Discord server</a>.", BuildConstants8.MIN_JAVA_VERSION, javaDownloadUrl), JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE);
97-
}
98-
99-
// Copied from AbstractPanel
100-
protected static boolean showPopup(String title, String description, int optionType, int messageType) {
101-
JEditorPane pane = new JEditorPane("text/html",
102-
"<html><body style=\"" + buildEditorPaneStyle() + "\">" + description + "</body></html>");
103-
pane.setEditable(false);
104-
pane.addHyperlinkListener(e -> {
105-
try {
106-
if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
107-
if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.BROWSE)) {
108-
Desktop.getDesktop().browse(e.getURL().toURI());
109-
} else {
110-
throw new UnsupportedOperationException("Failed to open " + e.getURL().toString());
111-
}
112-
}
113-
} catch (Throwable throwable) {
114-
// Oh well
115-
throwable.printStackTrace();
116-
}
117-
});
118-
return JOptionPane.showOptionDialog(null, pane, title, optionType, messageType, null, null, null) == 0;
119-
}
120-
121-
private static String buildEditorPaneStyle() {
122-
JLabel label = new JLabel();
123-
Font font = label.getFont();
124-
Color color = label.getBackground();
125-
126-
return String.format(
127-
"font-family:%s;font-weight:%s;font-size:%dpt;background-color: rgb(%d,%d,%d);",
128-
font.getFamily(), (font.isBold() ? "bold" : "normal"), font.getSize(), color.getRed(), color.getGreen(), color.getBlue()
129-
);
130-
}
13162
}

0 commit comments

Comments
 (0)