run jython script using Thread replacing SwingWorker for Thread stop

This commit is contained in:
wyq 2025-02-09 22:06:43 +08:00
parent fc22eea6eb
commit 8a101d3454
3 changed files with 130 additions and 19 deletions

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<MeteoInfo File="milconfig.xml" Type="configurefile">
<Path OpenPath="D:\Working\MIScript\Jython\mis\io\burf">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite"/>
<Path OpenPath="D:\Working\MIScript\Jython\mis\test">
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\satellite\FY"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\plot_types\3d"/>
@ -11,22 +10,23 @@
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\array"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\common_math\stats"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\dataframe"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\netcdf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\io\burf"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis"/>
<RecentFolder Folder="D:\Working\MIScript\Jython\mis\test"/>
</Path>
<File>
<OpenedFiles>
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\radar\radar_x_phase_2.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd_1.py"/>
<OpenedFile File="D:\Working\MIScript\Jython\mis\test\array_test_loop.py"/>
</OpenedFiles>
<RecentFiles>
<RecentFile File="D:\Working\MIScript\Jython\mis\io\radar\radar_x_phase_2.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\io\burf\bufr_cma_rsd_1.py"/>
<RecentFile File="D:\Working\MIScript\Jython\mis\test\array_test_loop.py"/>
</RecentFiles>
</File>
<Font>

View File

@ -40,6 +40,7 @@ public class ConsoleDockable extends DefaultSingleCDockable {
private PythonInteractiveInterpreter interp;
private JConsole console;
private SwingWorker myWorker;
private Thread myThread;
private ConsoleColors consoleColors;
public ConsoleDockable(FrmMain parent, String startupPath, String id, String title, CAction... actions) {
@ -74,12 +75,17 @@ public class ConsoleDockable extends DefaultSingleCDockable {
// Control-C
case (KeyEvent.VK_C):
if (ke.isControlDown()) {
if (myWorker != null && !myWorker.isCancelled() && !myWorker.isDone()) {
/*if (myWorker != null && !myWorker.isCancelled() && !myWorker.isDone()) {
myWorker.cancel(true);
myWorker = null;
//myWorker = new SmallWorker();
//myWorker.execute();
//enter();
}*/
if (myThread != null) {
myThread.stop();
myThread = null;
parent.getProgressBar().setVisible(false);
console.print("Running is stopped!");
//ConsoleDockable.this.enter();
}
}
break;
@ -220,6 +226,10 @@ public class ConsoleDockable extends DefaultSingleCDockable {
return this.myWorker;
}
public Thread getMyThread() {
return this.myThread;
}
/**
* Run a command line
*
@ -296,10 +306,10 @@ public class ConsoleDockable extends DefaultSingleCDockable {
protected String doInBackground() throws Exception {
parent.getProgressBar().setVisible(true);
interp.console.setStyle(consoleColors.getCommandColor());
interp.console.println("run script...");
interp.console.setFocusable(true);
interp.console.requestFocusInWindow();
interp.console.setStyle(consoleColors.getCommandColor());
interp.console.println("run script...");
interp.console.setFocusable(true);
interp.console.requestFocusInWindow();
try {
interp.exec("mipylib.plotlib.miplot.set_interactive(False)");
@ -340,6 +350,57 @@ public class ConsoleDockable extends DefaultSingleCDockable {
myWorker.execute();
}
/**
* Run a Jython file
*
* @param fn Jython file name
*/
public void execJythonFile(final String fn) {
myThread = new Thread(new Runnable() {
@Override
public void run() {
parent.getProgressBar().setVisible(true);
interp.console.setStyle(consoleColors.getCommandColor());
interp.console.println("run script...");
interp.console.setFocusable(true);
interp.console.requestFocusInWindow();
try {
interp.exec("mipylib.plotlib.miplot.set_interactive(False)");
interp.exec("mipylib.plotlib.miplot.clf()");
interp.execfile(fn);
interp.exec("mipylib.plotlib.miplot.set_interactive(True)");
} catch (Exception e) {
e.printStackTrace();
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
Logger.getLogger(PythonInteractiveInterpreter.class.getName()).log(Level.SEVERE, null, ex);
}
interp.console.print(">>> ", consoleColors.getPromptColor());
interp.console.setStyle(consoleColors.getCommandColor());
interp.exec("mipylib.plotlib.miplot.set_interactive(True)");
}
if (Thread.currentThread().isInterrupted()) {
parent.getProgressBar().setVisible(false);
} else {
IChartPanel cp = parent.getFigureDock().getCurrentFigure();
if (cp != null) {
cp.paintGraphics();
/*if (cp instanceof GLChartPanel) {
((GLChartPanel) cp).display();
}*/
}
parent.getProgressBar().setVisible(false);
}
}
});
myThread.start();
}
/**
* Run a Jython file text
*
@ -417,12 +478,52 @@ public class ConsoleDockable extends DefaultSingleCDockable {
};
myWorker.execute();
}
/**
* Run Jython script
*
* @param code
* @throws java.lang.InterruptedException
*/
public void runJythonScript(final String code) throws InterruptedException {
myThread = new Thread(new Runnable() {
@Override
public void run() {
parent.getProgressBar().setVisible(true);
interp.console.setStyle(consoleColors.getCommandColor());
interp.console.println("run script...");
interp.console.setFocusable(true);
interp.console.requestFocusInWindow();
String encoding = "utf-8";
try {
interp.exec("mipylib.plotlib.miplot.set_interactive(False)");
interp.exec("mipylib.plotlib.miplot.clf()");
interp.execfile(new ByteArrayInputStream(code.getBytes(encoding)));
interp.exec("mipylib.plotlib.miplot.set_interactive(True)");
} catch (Exception e) {
e.printStackTrace();
interp.exec("mipylib.plotlib.miplot.set_interactive(True)");
interp.fireConsoleExecEvent();
}
IChartPanel cp = parent.getFigureDock().getCurrentFigure();
if (cp != null) {
cp.paintGraphics();
}
parent.getProgressBar().setVisible(false);
}
});
myThread.start();
}
class SmallWorker extends SwingWorker<String, String> {
@Override
protected String doInBackground() throws Exception {
interp.exec("print('Thread cancled!')");
interp.exec("print('Thread canceled!')");
return "";
}

View File

@ -323,8 +323,10 @@ public class FrmMain extends javax.swing.JFrame implements IApplication {
jMenuItemRun.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (finalFile != null)
FrmMain.this.consoleDock.execfile(finalFile.getAbsolutePath());
if (finalFile != null) {
//FrmMain.this.consoleDock.execfile(finalFile.getAbsolutePath());
FrmMain.this.consoleDock.execJythonFile(finalFile.getAbsolutePath());
}
}
});
jPopupMenu.add(jMenuItemRun);
@ -361,10 +363,16 @@ public class FrmMain extends javax.swing.JFrame implements IApplication {
// Control-C
case (KeyEvent.VK_C):
if (ke.isControlDown()) {
SwingWorker myWorker = consoleDock.getSwingWorker();
/*SwingWorker myWorker = consoleDock.getSwingWorker();
if (myWorker != null && !myWorker.isCancelled() && !myWorker.isDone()) {
myWorker.cancel(true);
myWorker = null;
}*/
Thread myThread = consoleDock.getMyThread();
if (myThread != null) {
myThread.stop();
myThread = null;
}
}
break;
@ -976,12 +984,14 @@ public class FrmMain extends javax.swing.JFrame implements IApplication {
if (te.getFileName().isEmpty()) {
String code = te.getTextArea().getText();
try {
this.consoleDock.runPythonScript(code);
//this.consoleDock.runPythonScript(code);
this.consoleDock.runJythonScript(code);
} catch (InterruptedException ex) {
Logger.getLogger(FrmMain.class.getName()).log(Level.SEVERE, null, ex);
}
} else {
this.consoleDock.execfile(te.getFileName());
//this.consoleDock.execfile(te.getFileName());
this.consoleDock.execJythonFile(te.getFileName());
}
}//GEN-LAST:event_jButton_RunScriptActionPerformed