Skip to content

Commit

Permalink
Improve "select directly" behaviour
Browse files Browse the repository at this point in the history
If the path entered into the "select directly" dialog is a
file, then set the current directory to the parent of the
file, and select the file.

If the path entered into the "select directly" dialog is a
directory, then set the current directory to the path,
and select ".".

Fix: #29
  • Loading branch information
io7m committed May 1, 2021
1 parent 19c8242 commit 1ea9115
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 25 deletions.
9 changes: 7 additions & 2 deletions README-CHANGES.xml
Expand Up @@ -39,7 +39,7 @@
<c:change date="2020-04-10T00:00:00+00:00" summary="Enable CheckStyle to enforce code style"/>
</c:changes>
</c:release>
<c:release date="2021-05-01T13:22:17+00:00" is-open="true" ticket-system="com.github.io7m.jwheatsheaf" version="3.0.0">
<c:release date="2021-05-01T14:39:36+00:00" is-open="true" ticket-system="com.github.io7m.jwheatsheaf" version="3.0.0">
<c:changes>
<c:change date="2021-04-10T00:00:00+00:00" summary="Allow the escape key to close file choosers">
<c:tickets>
Expand Down Expand Up @@ -86,11 +86,16 @@
<c:ticket id="28"/>
</c:tickets>
</c:change>
<c:change date="2021-05-01T13:22:17+00:00" summary="Allow for including &quot;..&quot; in directory listings">
<c:change date="2021-05-01T00:00:00+00:00" summary="Allow for including &quot;..&quot; in directory listings">
<c:tickets>
<c:ticket id="23"/>
</c:tickets>
</c:change>
<c:change date="2021-05-01T14:39:36+00:00" summary="Improve &quot;select directly&quot; dialog behaviour">
<c:tickets>
<c:ticket id="29"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
Expand Up @@ -90,7 +90,7 @@ public void stop()
*/

@Test
public void testActionSaveNamed(
public void test_NameField_ExplicitlyTypedName_CandidateSelected(
final FxRobot robot,
final TestInfo info)
{
Expand Down Expand Up @@ -126,13 +126,15 @@ public void testActionSaveNamed(
}

/**
* The enter directly dialog works.
* If the path entered into the "select direct" dialog is not a directory,
* then the parent of that path's file name is set as the current directory,
* and the file name is selected.
*
* @param robot The FX test robot
*/

@Test
public void testDirectorySelectDirect(
public void test_SelectDirectly_TargetIsNotFile_TargetSelected(
final FxRobot robot,
final TestInfo info)
{
Expand Down Expand Up @@ -167,4 +169,98 @@ public void testDirectorySelectDirect(
);
Assertions.assertEquals(0, this.events.size());
}

/**
* If the path entered into the "select direct" dialog is not a directory,
* then the parent of that path's file name is set as the current directory,
* and the file name is selected.
*
* @param robot The FX test robot
*/

@Test
public void test_SelectDirectly_TargetIsFile_TargetSelected(
final FxRobot robot,
final TestInfo info)
{
JWFileWindowTitles.setTitle(this.chooser, info);

final var okButton =
robot.lookup("#fileChooserOKButton")
.queryButton();

final var selectButton =
robot.lookup("#fileChooserSelectDirectButton")
.queryButton();

FxAssert.verifyThat(okButton, NodeMatchers.isDisabled());
robot.clickOn(selectButton);

FxAssert.verifyThat(okButton, NodeMatchers.isDisabled());
robot.write("Z:\\USERS\\GROUCH\\PHOTO.JPG");

FxAssert.verifyThat(okButton, NodeMatchers.isDisabled());
robot.type(KeyCode.ENTER);

FxAssert.verifyThat(okButton, NodeMatchers.isEnabled());
robot.clickOn(okButton);

Assertions.assertEquals(
List.of("Z:\\USERS\\GROUCH\\PHOTO.JPG"),
this.chooser.result()
.stream()
.map(Path::toString)
.collect(Collectors.toList())
);
Assertions.assertEquals(0, this.events.size());
}

/**
* If the path entered into the "select direct" dialog is a directory,
* then that path becomes the new current directory, and "." is selected.
*
* @param robot The FX test robot
*/

@Test
public void test_SelectDirectly_TargetIsDirectory_TargetSelected(
final FxRobot robot,
final TestInfo info)
throws Exception
{
JWFileWindowTitles.setTitle(this.chooser, info);

final var delegate = new JWRobotDelegate(robot);

final var okButton =
robot.lookup("#fileChooserOKButton")
.queryButton();

final var selectButton =
robot.lookup("#fileChooserSelectDirectButton")
.queryButton();

FxAssert.verifyThat(okButton, NodeMatchers.isDisabled());
robot.clickOn(selectButton);

FxAssert.verifyThat(okButton, NodeMatchers.isDisabled());
robot.write("Z:\\USERS\\");

FxAssert.verifyThat(okButton, NodeMatchers.isDisabled());
robot.type(KeyCode.ENTER);

delegate.waitUntil(() -> !okButton.isDisabled());

FxAssert.verifyThat(okButton, NodeMatchers.isEnabled());
robot.clickOn(okButton);

Assertions.assertEquals(
List.of("Z:\\USERS"),
this.chooser.result()
.stream()
.map(Path::toString)
.collect(Collectors.toList())
);
Assertions.assertEquals(0, this.events.size());
}
}
Expand Up @@ -180,7 +180,7 @@ public int getRegexFlags()
.build();

Files.createDirectories(filesystem.getPath("Z:\\HOME"));
Files.writeString(filesystem.getPath("Z:\\HOME","FILE.TXT"), "FILE!");
Files.writeString(filesystem.getPath("Z:\\HOME", "FILE.TXT"), "FILE!");
Files.createDirectories(filesystem.getPath("DOC"));
Files.writeString(filesystem.getPath("README.TXT"), "HELLO!");
Files.writeString(filesystem.getPath("DATA.XML"), "Some data.");
Expand Down
Expand Up @@ -78,6 +78,10 @@ public final class JWFileChooserViewController
private static final Logger LOG =
LoggerFactory.getLogger(JWFileChooserViewController.class);

private static final Runnable AND_THEN_DO_NOTHING = () -> {

};

private final AtomicReference<Consumer<JWFileChooserEventType>> eventReceiver;
private final BlockingDeque<String> initialFilename;
private final ChangeListener<Path> listener;
Expand Down Expand Up @@ -241,7 +245,7 @@ public void setConfiguration(
this.upDirectoryButton
);

this.setCurrentDirectory(startDirectory);
this.setCurrentDirectory(startDirectory, AND_THEN_DO_NOTHING);
}

private void configureSearch()
Expand Down Expand Up @@ -340,11 +344,12 @@ protected void updateItem(
*/

private void setCurrentDirectory(
final Path path)
final Path path,
final Runnable andThen)
{
this.currentDirectory = Objects.requireNonNull(path, "path");
this.rebuildPathMenu(path);
this.populateDirectoryTable(path);
this.populateDirectoryTable(path, andThen);
}

private void rebuildPathMenu(
Expand All @@ -359,17 +364,20 @@ private void rebuildPathMenu(
}

private void populateDirectoryTable(
final Path directory)
final Path directory,
final Runnable andThen)
{
this.populateDirectoryTableWith(
() -> JWFileItems.listDirectory(
directory,
this.configuration.showParentDirectory())
this.configuration.showParentDirectory()),
andThen
);
}

private void populateDirectoryTableWith(
final JWFileListingRetrieverType itemRetriever)
final JWFileListingRetrieverType itemRetriever,
final Runnable andThen)
{
this.directoryTable.setItems(this.fileListing.items());
this.ioLockUI();
Expand All @@ -389,6 +397,8 @@ private void populateDirectoryTableWith(
} catch (final NoSuchElementException e) {
// Most of the time, there's no initial filename.
}

andThen.run();
});
} catch (final Exception e) {
LOG.error("exception during directory listing: ", e);
Expand Down Expand Up @@ -513,7 +523,7 @@ private void onPathMenuItemSelected(
final Path oldValue,
final Path newValue)
{
this.setCurrentDirectory(newValue);
this.setCurrentDirectory(newValue, AND_THEN_DO_NOTHING);
}

@FXML
Expand Down Expand Up @@ -545,12 +555,40 @@ private void onSelectDirectButton()
final var nameOpt = dialog.showAndWait();
if (nameOpt.isPresent()) {
final var name = nameOpt.get();
final var path = this.configuration.fileSystem().getPath(name);
final var parent = path.getParent();
final var fileSystem = this.configuration.fileSystem();
final var targetPath = fileSystem.getPath(name);

/*
* If the specified path is a directory, then simply set the current
* directory to that path.
*/

if (Files.isDirectory(targetPath)) {
this.setCurrentDirectory(targetPath, () -> {
this.trySelectDirectoryItem(this.directoryTable.getItems(), ".");
});
return;
}

/*
* Otherwise, set the current directory to the parent of the target
* path, and attempt to select the file with the path's file name.
*/

final var targetFileNameOpt =
Optional.ofNullable(targetPath.getFileName())
.map(Path::toString);

final var parent = targetPath.getParent();
if (parent != null) {
this.setCurrentDirectory(parent);
this.setCurrentDirectory(parent, () -> {
targetFileNameOpt.ifPresent(
targetName -> this.fileName.setText(targetName));
});
} else {
targetFileNameOpt.ifPresent(
targetName -> this.fileName.setText(targetName));
}
this.fileName.setText(path.getFileName().toString());
}
}

Expand All @@ -559,7 +597,7 @@ private void onUpDirectoryButton()
{
final var parent = this.currentDirectory.getParent();
if (parent != null) {
this.setCurrentDirectory(parent);
this.setCurrentDirectory(parent, AND_THEN_DO_NOTHING);
}
}

Expand Down Expand Up @@ -592,7 +630,7 @@ private void onCreateDirectoryButton()
LOG.error("exception raised by event receiver: ", ex);
}
}
this.setCurrentDirectory(this.currentDirectory);
this.setCurrentDirectory(this.currentDirectory, AND_THEN_DO_NOTHING);
}
}

Expand Down Expand Up @@ -640,8 +678,10 @@ private void onCancelSelected()
@FXML
private void onHomeSelected()
{
final var homeDirectoryOpt = this.configuration.homeDirectory();
homeDirectoryOpt.ifPresent(this::setCurrentDirectory);
final var homeDirectoryOpt =
this.configuration.homeDirectory();
homeDirectoryOpt
.ifPresent(path -> this.setCurrentDirectory(path, AND_THEN_DO_NOTHING));
}

@FXML
Expand Down Expand Up @@ -849,7 +889,7 @@ private void onTableRowClicked(
if (item != null) {
final var directory = item.path();
if (Files.isDirectory(directory)) {
this.setCurrentDirectory(directory);
this.setCurrentDirectory(directory, AND_THEN_DO_NOTHING);
}
}
}
Expand All @@ -859,9 +899,11 @@ private void onSourceItemDoubleClicked(
final MouseEvent event)
{
if (event.getClickCount() == 2) {
final var item = this.sourcesList.getSelectionModel().getSelectedItem();
item.path().ifPresent(this::setCurrentDirectory);
this.populateDirectoryTableWith(item);
final var item =
this.sourcesList.getSelectionModel().getSelectedItem();
item.path()
.ifPresent(path -> this.setCurrentDirectory(path, AND_THEN_DO_NOTHING));
this.populateDirectoryTableWith(item, AND_THEN_DO_NOTHING);
}
}

Expand Down

0 comments on commit 1ea9115

Please sign in to comment.