Skip to content

Commit

Permalink
Preserve the working directory when gui is run from cli (#3391)
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow authored Aug 6, 2024
1 parent d6b1a57 commit 5bf784e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ public static void runWithDefaults(Class<? extends MATSimApplication> clazz, Str

if (ApplicationUtils.isRunFromDesktop() && args.length == 0) {

System.setProperty("MATSIM_GUI_DESKTOP", "true");

if (defaultArgs.length > 0) {
String value = String.join(ARGS_DELIMITER, defaultArgs);
System.setProperty("MATSIM_GUI_ARGS", value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public Integer call() throws Exception {

Gui gui = f.get();

// Set the current working directory to be used in the gui, when run from the command line
// If the gui is run from desktop, the working directory is not overwritten

// Assumption is that starting something from command line, the user expects that the working directory remains the same
if (!System.getProperty("MATSIM_GUI_DESKTOP", "false").equals("true"))
gui.setWorkingDirectory(new File(""));

while (gui.isShowing())
Thread.sleep(250);

Expand Down
14 changes: 12 additions & 2 deletions matsim/src/main/java/org/matsim/run/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public class Gui extends JFrame {

private File configFile;
private File lastUsedDirectory;

/**
* This is the working directory for the simulation. If it is null, the working directory is the directory of the config file.
*/
private File workingDirectory = null;
private ConfigEditor editor = null;
private ScheduleValidatorWindow transitValidator = null;

Expand Down Expand Up @@ -439,6 +444,8 @@ private void startMATSim() {
textStdOut.setText("");
textErrOut.setText("");

String cwd = workingDirectory == null ? new File(txtConfigfilename.getText()).getParent() : workingDirectory.getAbsolutePath();

new Thread(() -> {
String classpath = System.getProperty("java.class.path");
String[] cpParts = classpath.split(File.pathSeparator);
Expand All @@ -457,8 +464,7 @@ private void startMATSim() {
"--add-exports", "java.desktop/sun.java2d=ALL-UNNAMED",
mainClass, txtConfigfilename.getText() };
// see https://jogamp.org/bugzilla/show_bug.cgi?id=1317#c21 and/or https://github.com/matsim-org/matsim-libs/pull/2940
exeRunner = ExeRunner.run(cmdArgs, textStdOut, textErrOut,
new File(txtConfigfilename.getText()).getParent());
exeRunner = ExeRunner.run(cmdArgs, textStdOut, textErrOut, cwd);
int exitcode = exeRunner.waitForFinish();
exeRunner = null;

Expand Down Expand Up @@ -575,6 +581,10 @@ public static void main(String[] args) {
Gui.show("MATSim", RunMatsim.class, args.length == 1 ? new File(args[0]) : null);
}

public void setWorkingDirectory(File cwd) {
this.workingDirectory = cwd;
}

// Is it a problem to make the following available to the outside? If so, why? Would it
// be better to rather copy/paste the above code and start from there? kai, jun/aug'18

Expand Down

0 comments on commit 5bf784e

Please sign in to comment.