From 5aefd9bc8c59c385727637a9a2d4c17ecd82539d Mon Sep 17 00:00:00 2001 From: tduva Date: Tue, 16 Aug 2016 18:17:43 +0200 Subject: [PATCH] Change global hotkeys handling - Change to be always enabled on Windows - Only output warning about missing jintellitype library when a global hotkey is added --- src/chatty/Chatty.java | 2 +- .../util/hotkeys/GlobalHotkeySetter.java | 28 +++++++++++++--- src/chatty/util/hotkeys/HotkeyManager.java | 33 +++++++++++++++++-- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/chatty/Chatty.java b/src/chatty/Chatty.java index 7c20adcb3..1b4047444 100644 --- a/src/chatty/Chatty.java +++ b/src/chatty/Chatty.java @@ -28,7 +28,7 @@ public class Chatty { /** * Enables the hotkey feature for running commercials (windows only). */ - public static final boolean HOTKEY = false; + public static final boolean HOTKEY = MiscUtil.OS_WINDOWS; /** * The Chatty website as it can be opened in the menu. diff --git a/src/chatty/util/hotkeys/GlobalHotkeySetter.java b/src/chatty/util/hotkeys/GlobalHotkeySetter.java index 840c7d3ab..16d663448 100644 --- a/src/chatty/util/hotkeys/GlobalHotkeySetter.java +++ b/src/chatty/util/hotkeys/GlobalHotkeySetter.java @@ -20,6 +20,12 @@ public class GlobalHotkeySetter implements HotkeyListener { private static final Logger LOGGER = Logger.getLogger(GlobalHotkeySetter.class.getName()); private boolean initialized = false; + + /** + * Error that occured when initializing (usually the correct dll not found). + */ + private String error; + private final GlobalHotkeyListener listener; /** @@ -46,17 +52,31 @@ public final void initialize() { JIntellitype.getInstance().addHotKeyListener(this); initialized = true; } catch (JIntellitypeException ex) { - String info = "Failed adding HotKeyListener: "+ex.getLocalizedMessage(); - LOGGER.log(Logging.USERINFO, info+" (if you don't use global " - + "hotkeys you can just ignore this)"); - LOGGER.warning(info); + error = "Failed adding global hotkeys listener: "+ex.getLocalizedMessage(); + LOGGER.warning(error); } } + /** + * Whether the listener has been added without an error. If false, then an + * error occured which can be retrieved with the + * {@link #getError() getError} method. + * + * @return true if global hotkeys can be added, false if an error occured + */ public boolean isInitalized() { return initialized; } + /** + * Returns an error message if an error occured while initializing. + * + * @return The error message, or null if no error occured + */ + public String getError() { + return error; + } + /** * Called when global hotkey is pressed. This is run in the EDT. * diff --git a/src/chatty/util/hotkeys/HotkeyManager.java b/src/chatty/util/hotkeys/HotkeyManager.java index 8606833a9..9883a56d9 100644 --- a/src/chatty/util/hotkeys/HotkeyManager.java +++ b/src/chatty/util/hotkeys/HotkeyManager.java @@ -72,6 +72,13 @@ public class HotkeyManager { private GlobalHotkeySetter globalHotkeys; + /** + * Warning text intended to be output to the user, about an error of the + * global hotkey feature. Should be set to null if warning was output, so + * it's only shown once. + */ + private String globalHotkeyErrorWarning; + public HotkeyManager(MainGui main) { this.main = main; @@ -87,13 +94,12 @@ public void onHotkey(Object hotkeyId) { // If an error occured during initialization, then set to null // which means it's not going to be used. if (!globalHotkeys.isInitalized()) { + globalHotkeyErrorWarning = globalHotkeys.getError(); globalHotkeys = null; } } catch (NoClassDefFoundError ex) { LOGGER.warning("Failed to initialize hotkey setter [" + ex + "]"); - LOGGER.log(Logging.USERINFO, "Failed to initialize global " - + "hotkeys (if you don't use global hotkeys you can " - + "just ignore this)."); + globalHotkeyErrorWarning = "Failed to initialize global hotkeys (jintellitype-xx.jar not found)."; globalHotkeys = null; } } @@ -400,6 +406,7 @@ public synchronized void loadFromSettings(Settings settings) { } } updateHotkeys(); + checkGlobalHotkeyWarning(); } /** @@ -453,6 +460,26 @@ private Hotkey listToHotkey(List list) { } } + /** + * Output warning of error when initializing global hotkey feature. Only + * output once and only when a global hotkey is currently configured. + */ + private void checkGlobalHotkeyWarning() { + if (globalHotkeyErrorWarning == null) { + return; + } + for (Hotkey hotkey : hotkeys) { + if (doesHotkeyHaveAction(hotkey) && hotkey.type == Type.GLOBAL) { + LOGGER.log(Logging.USERINFO, globalHotkeyErrorWarning+" " + + "[You are getting this message because you have a " + + "global hotkey configured. If you don't use it you " + + "can ignore this warning.]"); + globalHotkeyErrorWarning = null; + return; + } + } + } + /** * Creates a bridge Action which calls the actual registered action for this * hotkey, adding some more information.