From 751d06e97e7db9cfe43db8bb5308045d95b6f7f6 Mon Sep 17 00:00:00 2001 From: Ash Date: Fri, 31 Mar 2017 22:00:33 +1100 Subject: [PATCH] Refactor messageboxes for cross-thread wonders. My god, @Maschell. --- .../gui/GuiControllerListItem.java | 4 +- .../HIDToVPADNetworkClient/gui/GuiMain.java | 17 ++++++++ .../manager/ControllerManager.java | 2 + .../util/MessageBox.java | 39 +++++++++++++++++++ .../util/Utilities.java | 7 ---- 5 files changed, 60 insertions(+), 9 deletions(-) create mode 100644 src/net/ash/HIDToVPADNetworkClient/util/MessageBox.java diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiControllerListItem.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiControllerListItem.java index a7464ee..6130b5a 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiControllerListItem.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiControllerListItem.java @@ -33,7 +33,7 @@ import javax.swing.Timer; import lombok.Getter; import net.ash.HIDToVPADNetworkClient.controller.Controller; import net.ash.HIDToVPADNetworkClient.network.NetworkManager; -import net.ash.HIDToVPADNetworkClient.util.Utilities; +import net.ash.HIDToVPADNetworkClient.util.MessageBox; public class GuiControllerListItem extends JPanel implements ActionListener { private static final long serialVersionUID = 1L; @@ -78,7 +78,7 @@ public class GuiControllerListItem extends JPanel implements ActionListener { private void checkIfDisplayNoConfigMessage() { if (hasConfigCache == false) { - Utilities.messageBox("No configuration for this controller found on the console."); + MessageBox.show(new MessageBox("No configuration for this controller found on the console.", MessageBox.MESSAGE_ERROR)); } } diff --git a/src/net/ash/HIDToVPADNetworkClient/gui/GuiMain.java b/src/net/ash/HIDToVPADNetworkClient/gui/GuiMain.java index 8f9583e..2709799 100644 --- a/src/net/ash/HIDToVPADNetworkClient/gui/GuiMain.java +++ b/src/net/ash/HIDToVPADNetworkClient/gui/GuiMain.java @@ -23,12 +23,17 @@ package net.ash.HIDToVPADNetworkClient.gui; import java.awt.BorderLayout; import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import javax.swing.JComponent; import javax.swing.JFrame; +import javax.swing.JOptionPane; import javax.swing.JPanel; +import javax.swing.Timer; import net.ash.HIDToVPADNetworkClient.Main; +import net.ash.HIDToVPADNetworkClient.util.MessageBox; public class GuiMain extends JPanel { private static final long serialVersionUID = 1L; @@ -65,6 +70,18 @@ public class GuiMain extends JPanel { Main.fatal(); } add(rightSideControls, BorderLayout.LINE_END); + + int delay = 100; + ActionListener messageBoxPerformer = new ActionListener() { + public void actionPerformed(ActionEvent evt) { + MessageBox msg = MessageBox.getNextMessage(); + if (msg != null) { + JOptionPane.showMessageDialog(GuiMain.instance(), msg.getMessage(), "HID To VPAD Network Client", msg.getType()); + MessageBox.bumpQueue(); + } + } + }; + new Timer(delay, messageBoxPerformer).start(); } public static GuiMain instance() { diff --git a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java index 0e565fc..540f260 100644 --- a/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java +++ b/src/net/ash/HIDToVPADNetworkClient/manager/ControllerManager.java @@ -44,6 +44,7 @@ import net.ash.HIDToVPADNetworkClient.controller.XInput13Controller; import net.ash.HIDToVPADNetworkClient.controller.XInput14Controller; import net.ash.HIDToVPADNetworkClient.controller.XInputController; import net.ash.HIDToVPADNetworkClient.exeption.ControllerInitializationFailedException; +import net.ash.HIDToVPADNetworkClient.util.MessageBox; import net.ash.HIDToVPADNetworkClient.util.PureJavaHidApiManager; import net.ash.HIDToVPADNetworkClient.util.Settings; import purejavahidapi.HidDeviceInfo; @@ -161,6 +162,7 @@ public class ControllerManager { log.info("This error can be fixed! Please install the Visual C++ Redistributables:"); log.info("https://www.microsoft.com/en-us/download/details.aspx?id=48145"); log.info("If that doesn't help, create an issue on GitHub."); + MessageBox.show(new MessageBox("There was a problem setting up XInput.\nTo fix this, try installing the Visual C++\nredistributables: https://tinyurl.com/vcredist2015.\n\nOther controller types should still work.", MessageBox.MESSAGE_ERROR)); threwUnsatisfiedLinkError = true; } } diff --git a/src/net/ash/HIDToVPADNetworkClient/util/MessageBox.java b/src/net/ash/HIDToVPADNetworkClient/util/MessageBox.java new file mode 100644 index 0000000..fdf7e23 --- /dev/null +++ b/src/net/ash/HIDToVPADNetworkClient/util/MessageBox.java @@ -0,0 +1,39 @@ +package net.ash.HIDToVPADNetworkClient.util; + +import java.util.ArrayList; +import java.util.List; + +import javax.swing.JOptionPane; + +import lombok.Getter; + +public class MessageBox { + public static final int MESSAGE_INFO = JOptionPane.INFORMATION_MESSAGE; + public static final int MESSAGE_WARNING = JOptionPane.WARNING_MESSAGE; + public static final int MESSAGE_ERROR = JOptionPane.ERROR_MESSAGE; + + @Getter + private String message; + @Getter + private int type; + + public MessageBox(String message, int type) { + this.message = message; + this.type = type; + } + + private static List messageBoxQueue = new ArrayList(); + public static void show(MessageBox box) { + messageBoxQueue.add(box); + } + public static MessageBox getNextMessage() { + if (messageBoxQueue.size() > 0) { + return messageBoxQueue.get(0); + } else return null; + } + public static void bumpQueue() { + if (messageBoxQueue.size() > 0) { + messageBoxQueue.remove(0); + } + } +} diff --git a/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java b/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java index 8ede2c4..3dd1a39 100644 --- a/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java +++ b/src/net/ash/HIDToVPADNetworkClient/util/Utilities.java @@ -21,9 +21,6 @@ *******************************************************************************/ package net.ash.HIDToVPADNetworkClient.util; -import javax.swing.JFrame; -import javax.swing.JOptionPane; - public class Utilities { private Utilities() { @@ -79,8 +76,4 @@ public class Utilities { public static short signedShortToByte(short value) { return signedShortToByte((int) value); } - - public static void messageBox(String string) { - JOptionPane.showMessageDialog(new JFrame(), string); - } }