Browse Source

Ensure event helper is installed when using a JPopupMenu.

pull/214/head
weisj 4 years ago
parent
commit
2e958de5c4
  1. 4
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java
  2. 66
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/EventHelperUtil.java
  3. 8
      core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java

4
core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/DarkPopupMenuUI.java

@ -56,6 +56,10 @@ public class DarkPopupMenuUI extends BasicPopupMenuUI {
return new DarkPopupMenuUI(); return new DarkPopupMenuUI();
} }
public DarkPopupMenuUI() {
EventHelperUtil.installEventHelper();
}
@Override @Override
public void paint(final Graphics g, final JComponent c) { public void paint(final Graphics g, final JComponent c) {
Window window = SwingUtilities.getWindowAncestor(c); Window window = SwingUtilities.getWindowAncestor(c);

66
core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/EventHelperUtil.java

@ -0,0 +1,66 @@
/*
* MIT License
*
* Copyright (c) 2020 Jannis Weis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
package com.github.weisj.darklaf.ui.popupmenu;
import javax.swing.plaf.basic.BasicLookAndFeel;
public class EventHelperUtil {
private static boolean eventHelperInstallerFlagSet;
public static void installEventHelper() {
if (eventHelperInstallerFlagSet) return;
eventHelperInstallerFlagSet = true;
new DummyBasicLookAndFeel().initialize();
}
private static class DummyBasicLookAndFeel extends BasicLookAndFeel {
@Override
public String getName() {
return null;
}
@Override
public String getID() {
return null;
}
@Override
public String getDescription() {
return null;
}
@Override
public boolean isNativeLookAndFeel() {
return false;
}
@Override
public boolean isSupportedLookAndFeel() {
return false;
}
}
}

8
core/src/main/java/com/github/weisj/darklaf/ui/popupmenu/MouseGrabberUtil.java

@ -37,9 +37,7 @@ import com.github.weisj.darklaf.util.DarkUIUtil;
public class MouseGrabberUtil { public class MouseGrabberUtil {
private MouseGrabberUtil() { private MouseGrabberUtil() {}
}
private static MouseGrabber mouseGrabber; private static MouseGrabber mouseGrabber;
@ -58,7 +56,7 @@ public class MouseGrabberUtil {
} }
} }
public static ChangeListener getOldMouseGrabber() { private static ChangeListener getOldMouseGrabber() {
MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager(); MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
for (ChangeListener listener : menuSelectionManager.getChangeListeners()) { for (ChangeListener listener : menuSelectionManager.getChangeListeners()) {
if (listener == null) continue; if (listener == null) continue;
@ -78,7 +76,7 @@ public class MouseGrabberUtil {
* This Method is responsible for removing the old MouseGrabber from the AppContext, to be able to add our own * This Method is responsible for removing the old MouseGrabber from the AppContext, to be able to add our own
* implementation for it that is a bit more generous with closing the popup. * implementation for it that is a bit more generous with closing the popup.
*/ */
public static void uninstallOldMouseGrabber(final ChangeListener oldMouseGrabber) { private static void uninstallOldMouseGrabber(final ChangeListener oldMouseGrabber) {
if (oldMouseGrabber == null) return; if (oldMouseGrabber == null) return;
MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager(); MenuSelectionManager menuSelectionManager = MenuSelectionManager.defaultManager();
menuSelectionManager.removeChangeListener(oldMouseGrabber); menuSelectionManager.removeChangeListener(oldMouseGrabber);

Loading…
Cancel
Save