package ij.gui;
import ij.*;
import java.awt.event.*;
import java.awt.EventQueue;
import java.awt.GraphicsEnvironment;
import java.awt.Frame;
public class NonBlockingGenericDialog extends GenericDialog {
ImagePlus imp; WindowListener windowListener;
public NonBlockingGenericDialog(String title) {
super(title, null);
setModal(false);
IJ.protectStatusBar(false);
instance = this;
}
public synchronized void showDialog() {
super.showDialog();
if (isMacro())
return;
if (!IJ.macroRunning()) { final NonBlockingGenericDialog thisDialog = this;
EventQueue.invokeLater(new Runnable() {
public void run() {
WindowManager.addWindow(thisDialog);
}
});
}
if (imp != null) {
ImageWindow win = imp.getWindow();
if (win != null) { final NonBlockingGenericDialog gd = this;
windowListener = new WindowAdapter() {
public void windowClosed(WindowEvent e) {
cancelDialogAndClose();
}
};
win.addWindowListener(windowListener);
}
}
try {
wait();
} catch (InterruptedException e) { }
}
private void cancelDialogAndClose() {
super.windowClosing(null); }
public synchronized void actionPerformed(ActionEvent e) {
super.actionPerformed(e);
if (!isVisible())
notify();
}
public synchronized void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (wasOKed() || wasCanceled())
notify();
}
public synchronized void windowClosing(WindowEvent e) {
super.windowClosing(e);
if (wasOKed() || wasCanceled())
notify();
}
public void dispose() {
super.dispose();
WindowManager.removeWindow(this);
if (imp != null) {
ImageWindow win = imp.getWindow();
if (win != null && windowListener != null)
win.removeWindowListener(windowListener);
}
}
public static GenericDialog newDialog(String title, ImagePlus imp) {
return GUI.newNonBlockingDialog(title, imp);
}
public static GenericDialog newDialog(String title) {
return GUI.newNonBlockingDialog(title);
}
@Override
public void windowActivated(WindowEvent e) {
if ((e.getWindow() instanceof ImageWindow) && e.getOppositeWindow()!=this)
toFront();
WindowManager.setWindow(this);
}
}