|
2.17.5 InterruptDialog
The InterruptDialog class gives you an "Interrupt Evaluation" dialog box similar to the one you see in the notebook front end when you choose Interrupt Evaluation from the Kernel menu. The dialog box that appears has choices for aborting, quitting the kernel, and so on, depending on what the kernel is doing at the time. Because InterruptDialog uses some Swing user interface classes, it can only be used on a Java 1.2 or later runtime or, in some cases, a Java 1.1.x runtime that has had the Swing class library installed separately.
The InterruptDialog constructor takes a Dialog or Frame instance that will be the parent window of the dialog box. What you supply for this argument will typically be the main top-level window in your application. InterruptDialog implements the PacketListener interface, and you use it like any other PacketListener:
// Don't forget to import it (a different package than the rest of J/Link):
// import com.wolfram.jlink.ui.InterruptDialog;
ml.addPacketListener(new InterruptDialog(myParentFrame));
After the above line of code is executed, whenever you interrupt a computation (by sending an MLINTERRUPTMESSAGE or, more commonly, by calling the KernelLink interruptEvaluation() method), a modal dialog box will apppear with choices for how to proceed.
The SimpleFrontEnd sample program discussed in Section 2.17.3 makes use of an InterruptDialog. To see it in action, launch that sample program and execute the following Mathematica statement:
While[True]
Then select Interrupt Evaluation from the Kernel menu. The InterruptDialog dialog box will appear and you can click the Abort Command Being Evaluated button to stop the computation. To use an InterruptDialog in your own program, your user interface must provide a means for users to send an interrupt request, such as an Interrupt button or special key combination. In response to this action, your program would call the KernelLink interruptEvaluation() method.
That a behavior as complex as a complete Interrupt Evaluation dialog box can be plugged into a Java program with only a single line of code is a testament to the versatility of the PacketListener interface, described in Section 2.8.6. The InterruptDialog class works by monitoring the incoming flow of packets from the kernel and detecting the special type of MenuPacket that the kernel sends after an interrupt request. Anytime you have some application logic that needs to know about packets that arrive from Mathematica, you should implement it as a PacketListener.
|