I have a program that when prompted, opens an external editor (currently hard coded to sublime). The user will then type whatever they're typing in the editor, save the temp file, and then close the editor. When the user closes the editor, I want the contents of the temp file to be displayed in the program. My main concern is creating a conditional that can tell when the editor has been closed. Can WindowListener be used in reference to an outside program being launched? Here is my code so far: (Note: I use the Runtime due to a compatibility issue with Desktop and my current version of Gnome. This will only be run on Linux.)
private CachedTextInfo cti;
private File temp = File.createTempFile("tempfile",".tmp");
try{
theText.setText(cti.initialText);
String currentText = theText.getText();
BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
bw.write(currentText);
bw.close();
Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
//When editor closes, display tmp contents
}catch(IOException e) {
e.printStackTrace();
}
Thank you and let me know if you require any additional information.
Runtime.exec()
returns a Process
instance, which has a waitFor()
method.
So you can do
Process p = Runtime.getRuntime().exec("subl "+ temp.getAbsolutePath());
try {
p.waitFor();
// display tmp contents...
} catch (InterruptedException exc) {
// thread was interrupted waiting for process to complete...
}