From 234f8990208bce91e424ce710ee2ba59b31cec9a Mon Sep 17 00:00:00 2001 From: gered Date: Sun, 3 Jan 2016 13:26:49 -0500 Subject: [PATCH] immediately close app if registration fails. resolves #10 --- .../java/org/fenix/llanfair/Language.java | 3 +- .../java/org/fenix/llanfair/Llanfair.java | 30 +++++++++++++------ .../org/fenix/llanfair/dialog/TabHotkeys.java | 8 ----- src/main/resources/language.properties | 3 +- 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/main/java/org/fenix/llanfair/Language.java b/src/main/java/org/fenix/llanfair/Language.java index 8429479..669d013 100644 --- a/src/main/java/org/fenix/llanfair/Language.java +++ b/src/main/java/org/fenix/llanfair/Language.java @@ -50,8 +50,7 @@ public enum Language { setting_hotkey_stop, setting_hotkey_pause, setting_hotkey_lock, - GLOBAL_HOTKEYS_WARNING, - GLOBAL_HOTKEYS_STARTUP_WARNING, + GLOBAL_HOTKEYS_STARTUP_ERROR, // Settings > Header setting_header_goal, diff --git a/src/main/java/org/fenix/llanfair/Llanfair.java b/src/main/java/org/fenix/llanfair/Llanfair.java index 11eb256..78a0433 100644 --- a/src/main/java/org/fenix/llanfair/Llanfair.java +++ b/src/main/java/org/fenix/llanfair/Llanfair.java @@ -82,7 +82,11 @@ public class Llanfair extends BorderlessFrame implements TableModelListener, actions = new Actions( this ); setMenu(); - setBehavior(); + + boolean behaviourSucceed = setBehavior(); + if (!behaviourSucceed) + return; + setRun( run ); setVisible( true ); @@ -450,13 +454,27 @@ public class Llanfair extends BorderlessFrame implements TableModelListener, * * @throws IllegalStateException if JNativeHook cannot be registered. */ - private void setBehavior() { - registerNativeKeyHook(); + private boolean setBehavior() { + boolean registered = registerNativeKeyHook(); + + if (!registered) { + // NOTE: in the event of a failure, JNativeHook now has some ability (on some OS's at least) + // to pop up an OS-specific dialog or other action that allows the user to rectify the + // problem. e.g. on OS X, if an exception is thrown a dialog telling the user that the + // application has requested some accessibility-related access shows up. + + JOptionPane.showMessageDialog(this, Language.GLOBAL_HOTKEYS_STARTUP_ERROR, Language.ERROR.get(), JOptionPane.ERROR_MESSAGE); + this.dispose(); + return false; + } + setAlwaysOnTop(Settings.alwaysOnTop.get()); addWindowListener(this); addMouseWheelListener(this); Settings.addPropertyChangeListener(this); GlobalScreen.addNativeKeyListener(this); + + return true; } /** @@ -477,12 +495,6 @@ public class Llanfair extends BorderlessFrame implements TableModelListener, GlobalScreen.registerNativeHook(); return true; } catch (NativeHookException e) { - // NOTE: in the event of a failure, JNativeHook now has some ability (on some OS's at least) - // to pop up an OS-specific dialog or other action that allows the user to rectify the - // problem. e.g. on OS X, if an exception is thrown a dialog telling the user that the - // application has requested some accessibility-related access shows up. - - JOptionPane.showMessageDialog(this, Language.GLOBAL_HOTKEYS_WARNING, Language.ERROR.get(), JOptionPane.ERROR_MESSAGE); return false; } } diff --git a/src/main/java/org/fenix/llanfair/dialog/TabHotkeys.java b/src/main/java/org/fenix/llanfair/dialog/TabHotkeys.java index fa70d2b..58872ff 100644 --- a/src/main/java/org/fenix/llanfair/dialog/TabHotkeys.java +++ b/src/main/java/org/fenix/llanfair/dialog/TabHotkeys.java @@ -23,8 +23,6 @@ class TabHotkeys extends SettingsTab { private JCheckBox globalHotKeys; - private JLabel globalHotKeysHookWarning; - /** * List of all key fields customizable by the user. */ @@ -50,9 +48,6 @@ class TabHotkeys extends SettingsTab { public void actionPerformed(ActionEvent e) { Settings.useGlobalHotkeys.set(globalHotKeys.isSelected()); } }); - globalHotKeysHookWarning = new JLabel("" + Language.GLOBAL_HOTKEYS_WARNING); - globalHotKeysHookWarning.setForeground(Color.RED); - keyFields = new ArrayList(); keyLabels = new ArrayList(); @@ -97,9 +92,6 @@ class TabHotkeys extends SettingsTab { } add(globalHotKeys, GBC.grid(2, 3).insets(0, 50, 0, 0).anchor(GBC.LS)); - - if (GlobalScreen.isNativeHookRegistered()) - add(globalHotKeysHookWarning, GBC.grid(0, row + 1, 3, 1).insets(10, 0, 10, 0)); } // --------------------------------------------------------- INTERNAL TYPES diff --git a/src/main/resources/language.properties b/src/main/resources/language.properties index d633e8f..d8564b5 100644 --- a/src/main/resources/language.properties +++ b/src/main/resources/language.properties @@ -31,8 +31,7 @@ setting_hotkey_reset = Reset setting_hotkey_stop = Stop setting_hotkey_pause = Pause setting_hotkey_lock = Lock / Unlock -GLOBAL_HOTKEYS_WARNING =
Key event hook registration failed. You will not be able to set or use any of your hotkeys until this is fixed! To fix this, you will probably need to grant Llanfair some extended accessibility permissions from your operating system and then restart Llanfair.
-GLOBAL_HOTKEYS_STARTUP_WARNING = Key event hook registration failed. You will not be able to set or use any of your hotkeys until this is fixed! To fix this, you will probably need to grant Llanfair some extended accessibility permissions from your operating system and then restart Llanfair. +GLOBAL_HOTKEYS_STARTUP_ERROR =
Key event hook registration failed.

Llanfair requires global access to key events which (depending on your OS) might require some extra security or accessibility permissions. Click the "OK" button to close Llanfair. You will need to grant the required permissions before you will be able to use Llanfair.

Your OS might have just now popped up some sort of notification which will allow you to quickly grant Llanfair the required permissions.
# Settings > Header setting_header_goal = Display Goal