From 2cd6ba59ff5410299ae47b42b1b94bf1f30f8b93 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sat, 14 Nov 2015 12:09:46 -0800 Subject: [PATCH] Load UI assets in the background. --- .../launcher/creator/dialog/AboutDialog.java | 2 +- .../creator/dialog/GenerateListingDialog.java | 2 +- .../creator/dialog/PackManagerFrame.java | 16 ++--- .../creator/dialog/WelcomeDialog.java | 8 +-- .../model/swing/ModFileTableModel.java | 6 +- .../creator/model/swing/PackTableModel.java | 10 +-- .../model/swing/ProblemTableModel.java | 6 +- .../skcraft/launcher/dialog/ConsoleFrame.java | 4 +- .../launcher/dialog/LauncherFrame.java | 2 +- .../com/skcraft/launcher/swing/EmptyIcon.java | 38 ++++++++++++ .../launcher/swing/InstanceTableModel.java | 16 ++--- .../skcraft/launcher/swing/SwingHelper.java | 61 ++++++++++--------- 12 files changed, 103 insertions(+), 68 deletions(-) create mode 100644 launcher/src/main/java/com/skcraft/launcher/swing/EmptyIcon.java diff --git a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/AboutDialog.java b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/AboutDialog.java index 8997a71..2885959 100644 --- a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/AboutDialog.java +++ b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/AboutDialog.java @@ -46,7 +46,7 @@ public class AboutDialog extends JDialog { JPanel container = new JPanel(); container.setLayout(new MigLayout("insets dialog")); - container.add(new JLabel(SwingHelper.readImageIcon(Creator.class, "about_header.png")), "dock north"); + container.add(new JLabel(SwingHelper.createIcon(Creator.class, "about_header.png")), "dock north"); container.add(new JLabel("Version " + version), "wrap"); container.add(new JLabel("Licensed under GNU Lesser General Public License, version 3."), "wrap, gapbottom unrel"); container.add(new JLabel("Created by the SKCraft team. Visit our website!"), "wrap, gapbottom unrel"); diff --git a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/GenerateListingDialog.java b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/GenerateListingDialog.java index 9bd7c97..6a4a42c 100644 --- a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/GenerateListingDialog.java +++ b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/GenerateListingDialog.java @@ -27,7 +27,7 @@ public class GenerateListingDialog extends JDialog { @Getter private final DirectoryField destDirField = new DirectoryField(); @Getter private final JComboBox listingTypeCombo = new JComboBox<>(new ListingTypeComboBoxModel()); @Getter private final JTable manifestsTable = new DefaultTable(); - @Getter private final JLabel gameKeyWarning = new JLabel("Selected listing type won't support adding modpacks using 'game keys'.", SwingHelper.readImageIcon(Creator.class, "warning_icon.png"), SwingConstants.LEFT); + @Getter private final JLabel gameKeyWarning = new JLabel("Selected listing type won't support adding modpacks using 'game keys'.", SwingHelper.createIcon(Creator.class, "warning_icon.png"), SwingConstants.LEFT); @Getter private final JButton editManifestButton = new JButton("Modify..."); diff --git a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/PackManagerFrame.java b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/PackManagerFrame.java index 6b0a30c..e8589c7 100644 --- a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/PackManagerFrame.java +++ b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/PackManagerFrame.java @@ -21,13 +21,13 @@ import java.awt.event.KeyEvent; public class PackManagerFrame extends JFrame { - @Getter private final JButton newPackButton = new JButton("New Pack", SwingHelper.readImageIcon(Creator.class, "new.png")); - @Getter private final JButton importButton = new JButton("Add Existing", SwingHelper.readImageIcon(Creator.class, "import.png")); - @Getter private final JButton editConfigButton = new JButton("Modify", SwingHelper.readImageIcon(Creator.class, "edit.png")); - @Getter private final JButton openFolderButton = new JButton("Open", SwingHelper.readImageIcon(Creator.class, "open_folder.png")); - @Getter private final JButton checkProblemsButton = new JButton("Check", SwingHelper.readImageIcon(Creator.class, "check.png")); - @Getter private final JButton testButton = new JButton("Test", SwingHelper.readImageIcon(Creator.class, "test.png")); - @Getter private final JButton buildButton = new JButton("Build", SwingHelper.readImageIcon(Creator.class, "build.png")); + @Getter private final JButton newPackButton = new JButton("New Pack", SwingHelper.createIcon(Creator.class, "new.png")); + @Getter private final JButton importButton = new JButton("Add Existing", SwingHelper.createIcon(Creator.class, "import.png")); + @Getter private final JButton editConfigButton = new JButton("Modify", SwingHelper.createIcon(Creator.class, "edit.png")); + @Getter private final JButton openFolderButton = new JButton("Open", SwingHelper.createIcon(Creator.class, "open_folder.png")); + @Getter private final JButton checkProblemsButton = new JButton("Check", SwingHelper.createIcon(Creator.class, "check.png")); + @Getter private final JButton testButton = new JButton("Test", SwingHelper.createIcon(Creator.class, "test.png")); + @Getter private final JButton buildButton = new JButton("Build", SwingHelper.createIcon(Creator.class, "build.png")); @Getter private final JMenuItem newPackMenuItem = new JMenuItem("New Pack..."); @Getter private final JMenuItem newPackAtLocationMenuItem = new JMenuItem("New Pack at Location..."); @@ -68,7 +68,7 @@ public class PackManagerFrame extends JFrame { pack(); setLocationRelativeTo(null); - SwingHelper.setIconImage(this, Creator.class, "icon.png"); + SwingHelper.setFrameIcon(this, Creator.class, "icon.png"); } private void initComponents() { diff --git a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/WelcomeDialog.java b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/WelcomeDialog.java index f1f2099..0e0b0f3 100644 --- a/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/WelcomeDialog.java +++ b/creator-tools/src/main/java/com/skcraft/launcher/creator/dialog/WelcomeDialog.java @@ -19,8 +19,8 @@ import java.awt.event.KeyEvent; public class WelcomeDialog extends JFrame { - @Getter private final JButton newButton = new JButton("New Workspace...", SwingHelper.readImageIcon(Creator.class, "new.png")); - @Getter private final JButton openButton = new JButton("Open Workspace...", SwingHelper.readImageIcon(Creator.class, "open_folder.png")); + @Getter private final JButton newButton = new JButton("New Workspace...", SwingHelper.createIcon(Creator.class, "new.png")); + @Getter private final JButton openButton = new JButton("Open Workspace...", SwingHelper.createIcon(Creator.class, "open_folder.png")); @Getter private final JButton helpButton = new JButton("Help"); @Getter private final JButton aboutButton = new JButton("About"); @Getter private final JButton quitButton = new JButton("Quit"); @@ -35,7 +35,7 @@ public class WelcomeDialog extends JFrame { pack(); setLocationRelativeTo(null); - SwingHelper.setIconImage(this, Creator.class, "icon.png"); + SwingHelper.setFrameIcon(this, Creator.class, "icon.png"); } private void initComponents() { @@ -44,7 +44,7 @@ public class WelcomeDialog extends JFrame { JPanel container = new JPanel(); container.setLayout(new MigLayout("insets 50 20")); - container.add(new JLabel(SwingHelper.readImageIcon(Creator.class, "welcome_logo.png")), "wrap, gap 20 20, gapbottom 30"); + container.add(new JLabel(SwingHelper.createIcon(Creator.class, "welcome_logo.png")), "wrap, gap 20 20, gapbottom 30"); container.add(newButton, "grow, gap 50 50, wrap"); container.add(openButton, "grow, gap 50 50, wrap"); diff --git a/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ModFileTableModel.java b/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ModFileTableModel.java index 497ea16..1130c52 100644 --- a/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ModFileTableModel.java +++ b/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ModFileTableModel.java @@ -19,11 +19,11 @@ import static com.google.common.base.Preconditions.checkNotNull; public class ModFileTableModel extends AbstractTableModel { - private static final ImageIcon WWW_ICON; + private static final Icon WWW_ICON; private final List mods; static { - WWW_ICON = SwingHelper.readImageIcon(Creator.class, "www_icon.png"); + WWW_ICON = SwingHelper.createIcon(Creator.class, "www_icon.png"); } public ModFileTableModel(List mods) { @@ -49,7 +49,7 @@ public class ModFileTableModel extends AbstractTableModel { public Class getColumnClass(int columnIndex) { switch (columnIndex) { case 0: - return ImageIcon.class; + return Icon.class; default: return String.class; } diff --git a/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/PackTableModel.java b/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/PackTableModel.java index c44b6b9..528e515 100644 --- a/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/PackTableModel.java +++ b/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/PackTableModel.java @@ -17,15 +17,15 @@ import java.util.List; public class PackTableModel extends AbstractTableModel { - private final ImageIcon instanceIcon; - private final ImageIcon warningIcon; + private final Icon instanceIcon; + private final Icon warningIcon; private final List packs; public PackTableModel(List packs) { this.packs = packs; - instanceIcon = SwingHelper.readImageIcon(Creator.class, "pack_icon.png"); - warningIcon = SwingHelper.readImageIcon(Creator.class, "warning_icon.png"); + instanceIcon = SwingHelper.createIcon(Creator.class, "pack_icon.png"); + warningIcon = SwingHelper.createIcon(Creator.class, "warning_icon.png"); } @Override @@ -50,7 +50,7 @@ public class PackTableModel extends AbstractTableModel { public Class getColumnClass(int columnIndex) { switch (columnIndex) { case 0: - return ImageIcon.class; + return Icon.class; default: return String.class; } diff --git a/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ProblemTableModel.java b/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ProblemTableModel.java index 28d01df..5758886 100644 --- a/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ProblemTableModel.java +++ b/creator-tools/src/main/java/com/skcraft/launcher/creator/model/swing/ProblemTableModel.java @@ -16,10 +16,10 @@ import java.util.List; public class ProblemTableModel extends AbstractTableModel { - private static final ImageIcon WARNING_ICON; + private static final Icon WARNING_ICON; static { - WARNING_ICON = SwingHelper.readImageIcon(Creator.class, "warning_icon.png"); + WARNING_ICON = SwingHelper.createIcon(Creator.class, "warning_icon.png"); } private final List problems; @@ -44,7 +44,7 @@ public class ProblemTableModel extends AbstractTableModel { public Class getColumnClass(int columnIndex) { switch (columnIndex) { case 0: - return ImageIcon.class; + return Icon.class; case 1: return Problem.class; default: diff --git a/launcher/src/main/java/com/skcraft/launcher/dialog/ConsoleFrame.java b/launcher/src/main/java/com/skcraft/launcher/dialog/ConsoleFrame.java index 980f4a8..7903ac3 100644 --- a/launcher/src/main/java/com/skcraft/launcher/dialog/ConsoleFrame.java +++ b/launcher/src/main/java/com/skcraft/launcher/dialog/ConsoleFrame.java @@ -58,8 +58,8 @@ public class ConsoleFrame extends JFrame { */ public ConsoleFrame(@NonNull String title, int numLines, boolean colorEnabled) { messageLog = new MessageLog(numLines, colorEnabled); - trayRunningIcon = SwingHelper.readIconImage(Launcher.class, "tray_ok.png"); - trayClosedIcon = SwingHelper.readIconImage(Launcher.class, "tray_closed.png"); + trayRunningIcon = SwingHelper.createImage(Launcher.class, "tray_ok.png"); + trayClosedIcon = SwingHelper.createImage(Launcher.class, "tray_closed.png"); setTitle(title); setIconImage(trayRunningIcon); diff --git a/launcher/src/main/java/com/skcraft/launcher/dialog/LauncherFrame.java b/launcher/src/main/java/com/skcraft/launcher/dialog/LauncherFrame.java index e7dc7af..1aa47b0 100644 --- a/launcher/src/main/java/com/skcraft/launcher/dialog/LauncherFrame.java +++ b/launcher/src/main/java/com/skcraft/launcher/dialog/LauncherFrame.java @@ -73,7 +73,7 @@ public class LauncherFrame extends JFrame { pack(); setLocationRelativeTo(null); - SwingHelper.setIconImage(this, Launcher.class, "icon.png"); + SwingHelper.setFrameIcon(this, Launcher.class, "icon.png"); SwingUtilities.invokeLater(new Runnable() { @Override diff --git a/launcher/src/main/java/com/skcraft/launcher/swing/EmptyIcon.java b/launcher/src/main/java/com/skcraft/launcher/swing/EmptyIcon.java new file mode 100644 index 0000000..8ab1b26 --- /dev/null +++ b/launcher/src/main/java/com/skcraft/launcher/swing/EmptyIcon.java @@ -0,0 +1,38 @@ +/* + * SK's Minecraft Launcher + * Copyright (C) 2010-2014 Albert Pham and contributors + * Please see LICENSE.txt for license information. + */ + +package com.skcraft.launcher.swing; + +import javax.swing.*; +import java.awt.*; + +public final class EmptyIcon implements Icon { + + private int width; + private int height; + + public EmptyIcon() { + this(0, 0); + } + + public EmptyIcon(int width, int height) { + this.width = width; + this.height = height; + } + + public int getIconHeight() { + return height; + } + + public int getIconWidth() { + return width; + } + + @Override + public void paintIcon(Component c, Graphics g, int x, int y) { + } + +} diff --git a/launcher/src/main/java/com/skcraft/launcher/swing/InstanceTableModel.java b/launcher/src/main/java/com/skcraft/launcher/swing/InstanceTableModel.java index eaa79c4..583011d 100644 --- a/launcher/src/main/java/com/skcraft/launcher/swing/InstanceTableModel.java +++ b/launcher/src/main/java/com/skcraft/launcher/swing/InstanceTableModel.java @@ -13,23 +13,19 @@ import com.skcraft.launcher.util.SharedLocale; import javax.swing.*; import javax.swing.table.AbstractTableModel; -import java.awt.*; public class InstanceTableModel extends AbstractTableModel { private final InstanceList instances; - private final ImageIcon instanceIcon; - private final ImageIcon customInstanceIcon; - private final ImageIcon downloadIcon; + private final Icon instanceIcon; + private final Icon customInstanceIcon; + private final Icon downloadIcon; public InstanceTableModel(InstanceList instances) { this.instances = instances; - instanceIcon = new ImageIcon(SwingHelper.readIconImage(Launcher.class, "instance_icon.png") - .getScaledInstance(16, 16, Image.SCALE_SMOOTH)); - customInstanceIcon = new ImageIcon(SwingHelper.readIconImage(Launcher.class, "custom_instance_icon.png") - .getScaledInstance(16, 16, Image.SCALE_SMOOTH)); - downloadIcon = new ImageIcon(SwingHelper.readIconImage(Launcher.class, "download_icon.png") - .getScaledInstance(14, 14, Image.SCALE_SMOOTH)); + instanceIcon = SwingHelper.createIcon(Launcher.class, "instance_icon.png", 16, 16); + customInstanceIcon = SwingHelper.createIcon(Launcher.class, "custom_instance_icon.png", 16, 16); + downloadIcon = SwingHelper.createIcon(Launcher.class, "download_icon.png", 14, 14); } public void update() { diff --git a/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java b/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java index bee13ba..c06b6c0 100644 --- a/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java +++ b/launcher/src/main/java/com/skcraft/launcher/swing/SwingHelper.java @@ -290,7 +290,35 @@ public final class SwingHelper { } } - public static BufferedImage readIconImage(Class clazz, String path) { + public static Image createImage(Class clazz, String name) { + URL resource = clazz.getResource(name); + if (resource != null) { + return Toolkit.getDefaultToolkit().createImage(resource); + } else { + log.log(Level.WARNING, "Failed to read image from resource: " + name); + return null; + } + } + + public static Icon createIcon(Class clazz, String name) { + Image image = createImage(clazz, name); + if (image != null) { + return new ImageIcon(image); + } else { + return new EmptyIcon(16, 16); + } + } + + public static Icon createIcon(Class clazz, String name, int width, int height) { + Image image = createImage(clazz, name); + if (image != null) { + return new ImageIcon(image.getScaledInstance(width, height, Image.SCALE_SMOOTH)); + } else { + return new EmptyIcon(width, height); + } + } + + public static BufferedImage readBufferedImage(Class clazz, String path) { InputStream in = null; try { in = clazz.getResourceAsStream(path); @@ -304,40 +332,13 @@ public final class SwingHelper { return null; } - public static Image readIconImageScaled(Class clazz, String path, int w, int h) { - BufferedImage image = readIconImage(clazz, path); - if (image != null) { - return image.getScaledInstance(w, h, Image.SCALE_SMOOTH); - } else { - return null; - } - } - - public static void setIconImage(JFrame frame, Class clazz, String path) { - BufferedImage image = readIconImage(clazz, path); + public static void setFrameIcon(JFrame frame, Class clazz, String path) { + BufferedImage image = readBufferedImage(clazz, path); if (image != null) { frame.setIconImage(image); } } - public static ImageIcon readImageIcon(Class clazz, String path) { - BufferedImage image = readIconImage(clazz, path); - if (image != null) { - return new ImageIcon(image); - } else { - return null; - } - } - - public static ImageIcon readImageIconScaled(Class clazz, String path, int w, int h) { - Image image = readIconImageScaled(clazz, path, w, h); - if (image != null) { - return new ImageIcon(image); - } else { - return null; - } - } - /** * Focus a component. *