Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent hide on popup window losing focus, fixed escape key listener to close popup #133

Merged
merged 3 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ class EventHandler(val name: String) : Runnable {
* Check if a class loader is banned
*/
fun isBanned(classLoader: ClassLoader): Boolean {
for(bannedClassLoader in bannedClassLoaders) {
if(bannedClassLoader.get() == classLoader) return true
for (bannedClassLoader in bannedClassLoaders) {
if (bannedClassLoader.get() == classLoader) return true
}

return false
Expand All @@ -63,7 +63,7 @@ class EventHandler(val name: String) : Runnable {
* thread-safe operation (synchronized)
*/
val listeners: Array<EventListener>
get() {
get() {
synchronized(lock) {
return internalListeners.toTypedArray()
}
Expand All @@ -82,18 +82,18 @@ class EventHandler(val name: String) : Runnable {

var callRightAway = false

private val internalListeners = ArrayList<EventListener>()
private val internalListeners = ArrayList<EventListener>()
private val internalOnceListeners = ArrayList<EventListener>()

/**
* Run all the listeners in this event handler
*/
override fun run() {
for(listener in listeners) {
for (listener in listeners) {
try {
runListener(listener, false)
} catch (ex: Exception) {
if(ex is InterruptedException) {
if (ex is InterruptedException) {
logger.warn("Rethrowing InterruptedException...")
throw ex
} else {
Expand All @@ -105,11 +105,11 @@ class EventHandler(val name: String) : Runnable {
val toRemoveOnceListeners = mutableListOf<EventListener>()

//executing "doOnce" listeners
for(listener in onceListeners) {
for (listener in onceListeners) {
try {
runListener(listener, true)
} catch (ex: Exception) {
if(ex is InterruptedException) {
if (ex is InterruptedException) {
logger.warn("Rethrowing InterruptedException...")
throw ex
} else {
Expand All @@ -122,7 +122,7 @@ class EventHandler(val name: String) : Runnable {
}

synchronized(onceLock) {
for(listener in toRemoveOnceListeners) {
for (listener in toRemoveOnceListeners) {
internalOnceListeners.remove(listener)
}
}
Expand All @@ -133,7 +133,7 @@ class EventHandler(val name: String) : Runnable {
* @param listener the listener to add
*/
fun doOnce(listener: EventListener) {
if(callRightAway)
if (callRightAway)
runListener(listener, true)
else synchronized(onceLock) {
internalOnceListeners.add(listener)
Expand All @@ -155,7 +155,7 @@ class EventHandler(val name: String) : Runnable {
internalListeners.add(listener)
}

if(callRightAway) runListener(listener, false)
if (callRightAway) runListener(listener, false)

return EventListenerRemover(this, listener, false)
}
Expand All @@ -171,7 +171,7 @@ class EventHandler(val name: String) : Runnable {
* @param listener the listener to remove
*/
fun removePersistentListener(listener: EventListener) {
if(internalListeners.contains(listener)) {
if (internalListeners.contains(listener)) {
synchronized(lock) { internalListeners.remove(listener) }
}
}
Expand All @@ -181,7 +181,7 @@ class EventHandler(val name: String) : Runnable {
* @param listener the listener to remove
*/
fun removeOnceListener(listener: EventListener) {
if(internalOnceListeners.contains(listener)) {
if (internalOnceListeners.contains(listener)) {
synchronized(onceLock) { internalOnceListeners.remove(listener) }
}
}
Expand Down Expand Up @@ -215,7 +215,7 @@ class EventHandler(val name: String) : Runnable {
operator fun invoke(listener: EventListener) = doPersistent(listener)

private fun runListener(listener: EventListener, isOnce: Boolean) {
if(isBanned(listener.javaClass.classLoader)) {
if (isBanned(listener.javaClass.classLoader)) {
removeBannedListeners()
return
}
Expand All @@ -224,15 +224,15 @@ class EventHandler(val name: String) : Runnable {
}

private fun removeBannedListeners() {
for(listener in internalListeners.toArray()) {
if(isBanned(listener.javaClass.classLoader)) {
for (listener in internalListeners.toArray()) {
if (isBanned(listener.javaClass.classLoader)) {
internalListeners.remove(listener)
logger.warn("Removed banned listener from ${listener.javaClass.classLoader}")
}
}

for(listener in internalOnceListeners.toArray()) {
if(isBanned(listener.javaClass.classLoader)) internalOnceListeners.remove(listener)
for (listener in internalOnceListeners.toArray()) {
if (isBanned(listener.javaClass.classLoader)) internalOnceListeners.remove(listener)
logger.warn("Removed banned listener from ${listener.javaClass.classLoader}")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import com.github.serivesmejia.eocvsim.util.exception.handling.CrashReport;
import io.github.deltacv.eocvsim.plugin.loader.PluginManager;



import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
Expand All @@ -63,6 +65,7 @@ public static void createYesOrNo(Component parent, String message, String submes
}

invokeLater(() -> result.accept(

JOptionPane.showConfirmDialog(parent, panel, "Confirm",
JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE
Expand Down Expand Up @@ -97,6 +100,7 @@ public static FileChooser createFileChooser(Component parent) {
public static void createSourceDialog(EOCVSim eocvSim,
SourceType type,
File initialFile) {

invokeLater(() -> {
switch (type) {
case IMAGE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class PopupX @JvmOverloads constructor(windowAncestor: Window,

window.size = panel.preferredSize

windowAncestor.requestFocusInWindow()
windowAncestor.addKeyListener(object: KeyAdapter() {
override fun keyPressed(e: KeyEvent?) {
if(e?.keyCode == KeyEvent.VK_ESCAPE) {
Expand Down Expand Up @@ -88,11 +89,7 @@ class PopupX @JvmOverloads constructor(windowAncestor: Window,

override fun windowGainedFocus(e: WindowEvent?) {}

override fun windowLostFocus(e: WindowEvent?) {
if(closeOnFocusLost) {
hide()
}
}
override fun windowLostFocus(e: WindowEvent?) {}

fun setLocation(width: Int, height: Int) = window.setLocation(width, height)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ public CreateImageSource(JFrame parent, EOCVSim eocvSim, File initialFile) {

public void initCreateImageSource() {

createImageSource.setModal(true);
createImageSource.setModal(false);
createImageSource.setFocusableWindowState(false);
createImageSource.setAlwaysOnTop(true);

createImageSource.setTitle("Create image source");
createImageSource.setSize(370, 200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public CreateSource(JFrame parent, EOCVSim eocvSim) {

chooseSource = new JDialog(parent);


this.parent = parent;
this.eocvSim = eocvSim;

Expand All @@ -55,7 +56,9 @@ private void initChooseSource() {

alreadyOpened = true;

chooseSource.setModal(true);
chooseSource.setModal(false);
chooseSource.setFocusableWindowState(false);
chooseSource.setAlwaysOnTop(true);

chooseSource.setTitle("Select source type");
chooseSource.setSize(300, 150);
Expand Down