Skip to content

Commit

Permalink
Use a sane initial dir for the file chooser
Browse files Browse the repository at this point in the history
While having a look at 63945, I found a few places, where we look for the existence
of the initial directory on which the file chooser should start.

I extended and simplified those occurrences and think I found a bug in the logic that
chooses the initial location in FileDialoger#promptToOpenFile from line 177.

First we look, if existingFileName is pointing to something valid and use that as the
initial location or the global initial location, if none location has been used before.

After that a bit further down, we test if a location has been used before and may
initialize such a location.

Than -- and this is the potential bug -- we always use that location for the initial
location of the file chooser.

Another dodgy logic is that we test for the default initial location in two different ways.
First -- when no existingFileName is given -- we test for an empty default location and refuse
to use it, if it is empty. The second time, we accept the default location, even if it is
empty AND use it as the default location for the next runs.

Bugzilla Id: 63945
  • Loading branch information
FSchumacher committed Nov 27, 2019
1 parent 33dcfd8 commit bfabdb8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/core/src/main/java/org/apache/jmeter/gui/util/FileDialoger.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@

import java.awt.Component;
import java.io.File;
import java.util.Arrays;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

import org.apache.commons.lang3.StringUtils;
import org.apache.jmeter.gui.GuiPackage;
import org.apache.jmeter.gui.JMeterFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Class implementing a file open dialogue
Expand All @@ -39,6 +42,8 @@ public final class FileDialoger {

private static JFileChooser jfc = new JFileChooser();

private static final Logger LOG = LoggerFactory.getLogger(FileDialoger.class);

/**
* Prevent instantiation of utility class.
*/
Expand Down Expand Up @@ -180,30 +185,14 @@ public static JFileChooser promptToOpenFile(Component parentComponent, String[]
} else {
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
}
if(!StringUtils.isEmpty(existingFileName)) {
File existingFileStart = new File(existingFileName);
if(existingFileStart.exists() && existingFileStart.canRead()) {
jfc.setCurrentDirectory(new File(existingFileName));
}
}
else if (lastJFCDirectory == null) {
String start = System.getProperty("user.dir", ""); //$NON-NLS-1$//$NON-NLS-2$

if (start.length() > 0) {
jfc.setCurrentDirectory(new File(start));
}
}
setCurrentDirOnJFC(existingFileName, lastJFCDirectory, System.getProperty("user.dir"));
clearFileFilters();
if(exts != null && exts.length > 0) {
JMeterFileFilter currentFilter = new JMeterFileFilter(exts);
jfc.addChoosableFileFilter(currentFilter);
jfc.setAcceptAllFileFilterUsed(true);
jfc.setFileFilter(currentFilter);
}
if(lastJFCDirectory==null) {
lastJFCDirectory = System.getProperty("user.dir", ""); //$NON-NLS-1$//$NON-NLS-2$
}
jfc.setCurrentDirectory(new File(lastJFCDirectory));
int retVal = jfc.showOpenDialog(parentComponent);
lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();

Expand All @@ -213,6 +202,22 @@ else if (lastJFCDirectory == null) {
return null;
}

private static void setCurrentDirOnJFC(String... dirNames) {
for (String dirName : dirNames) {
if (StringUtils.isBlank(dirName)) {
continue;
}
File possibleDir = new File(dirName);
if (possibleDir.exists() && possibleDir.canRead()) {
jfc.setCurrentDirectory(possibleDir);
return;
}
}
LOG.info("No valid initial directory found for: {}",
Arrays.asList(dirNames));
jfc.setCurrentDirectory(null);
}

private static void clearFileFilters() {
FileFilter[] filters = jfc.getChoosableFileFilters();
for (FileFilter filter : filters) {
Expand Down
2 changes: 2 additions & 0 deletions xdocs/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ to view the last release notes of version 5.2.1.

<h3>General</h3>
<ul>
<li><bug>63945</bug>NPE when opening a file after file system change</li>
</ul>

<!-- =================== Thanks =================== -->
Expand All @@ -173,6 +174,7 @@ to view the last release notes of version 5.2.1.
</ul>
<p>We also thank bug reporters who helped us improve JMeter.</p>
<ul>
<li>Michael McDermott (mcdermott.michaelj at gmail.com)</li>
</ul>
<p>
Apologies if we have omitted anyone else.
Expand Down

0 comments on commit bfabdb8

Please sign in to comment.