Skip to content

Commit

Permalink
Improve the filename field semantics
Browse files Browse the repository at this point in the history
This attempts to improve the semantics of the filename field in file
choosers. The old behaviour was essentially to ignore the return key
when typing into the field. The new behaviour is to attempt to select
an item when pressing the return key and, if an item is selected and
the OK button is unlocked, focus is shifted to the OK button. The
user can then press return to confirm the selection.

Affects: #28
  • Loading branch information
io7m committed Apr 29, 2021
1 parent 90b911f commit 7533dcb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 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-04-29T10:58:54+00:00" is-open="true" ticket-system="com.github.io7m.jwheatsheaf" version="3.0.0">
<c:release date="2021-04-29T13:21:57+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 @@ -76,11 +76,16 @@
<c:ticket id="9"/>
</c:tickets>
</c:change>
<c:change date="2021-04-29T10:58:54+00:00" summary="Add support for glob-based filters">
<c:change date="2021-04-29T00:00:00+00:00" summary="Add support for glob-based filters">
<c:tickets>
<c:ticket id="19"/>
</c:tickets>
</c:change>
<c:change date="2021-04-29T13:21:57+00:00" summary="Improve filename field behaviour">
<c:tickets>
<c:ticket id="28"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
Expand Up @@ -142,6 +142,8 @@ public void testDirectorySelect(

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

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

Expand Down Expand Up @@ -198,6 +200,7 @@ public void testPathMenuSelect(

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

FxAssert.verifyThat(okButton, NodeMatchers.isEnabled());
robot.clickOn(okButton);
Expand Down Expand Up @@ -452,6 +455,7 @@ public void testDirectoryUp(

robot.clickOn(targetCell);
robot.sleep(1L, SECONDS);
robot.clickOn(targetCell);

FxAssert.verifyThat(okButton, NodeMatchers.isEnabled());
robot.clickOn(okButton);
Expand Down Expand Up @@ -729,4 +733,44 @@ public void testSortSize(
);
Assertions.assertEquals(0, this.events.size());
}

/**
* Typing a name into the name field and pressing return selects an item and
* requests focus for the OK button. Pressing return again presses the button.
*
* @param robot The FX test robot
*/

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

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

final var fileField =
robot.lookup("#fileChooserNameField")
.query();

robot.clickOn(fileField);
robot.write("DATA.XML");
robot.type(KeyCode.ENTER);
robot.sleep(1L, SECONDS);

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

Assertions.assertEquals(
List.of("Z:\\USERS\\GROUCH\\DATA.XML"),
this.chooser.result()
.stream()
.map(Path::toString)
.collect(Collectors.toList())
);
Assertions.assertEquals(0, this.events.size());
}
}
Expand Up @@ -59,6 +59,7 @@
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
Expand Down Expand Up @@ -397,11 +398,12 @@ private void populateDirectoryTableWith(
}

/**
* Select the item in the list of file items that has the given name. If
* none of them have the given name, do nothing.
* Select the item in the list of file items that has the given name and
* return it. If none of them have the given name, do nothing and return
* nothing.
*/

private void trySelectDirectoryItem(
private Optional<JWFileItem> trySelectDirectoryItem(
final List<JWFileItem> items,
final String name)
{
Expand All @@ -410,10 +412,30 @@ private void trySelectDirectoryItem(
if (itemFileName != null) {
if (Objects.equals(itemFileName.toString(), name)) {
this.directoryTable.getSelectionModel().select(item);
return;
return Optional.of(item);
}
}
}
return Optional.empty();
}

/**
* Select the item in the list of file items that has the given name and
* return it. If none of them have the given name, do nothing and clear
* the directory table selection.
*/

private Optional<JWFileItem> trySelectDirectoryItemOrDeselect(
final TableView<JWFileItem> tableView,
final String name)
{
final var selected =
this.trySelectDirectoryItem(tableView.getItems(), name);

if (selected.isEmpty()) {
tableView.getSelectionModel().clearSelection();
}
return selected;
}

private void ioUnlockUI()
Expand Down Expand Up @@ -582,9 +604,26 @@ private void onHomeSelected()
homeDirectoryOpt.ifPresent(this::setCurrentDirectory);
}

@FXML
private void onNameFieldAction()
{
this.trySelectDirectoryItemOrDeselect(
this.directoryTable,
this.fileName.getText()
);

this.reconfigureOKButton();
this.okButton.requestFocus();
}

@FXML
private void onNameFieldChanged()
{
this.trySelectDirectoryItemOrDeselect(
this.directoryTable,
this.fileName.getText()
);

this.reconfigureOKButton();
}

Expand Down
Expand Up @@ -87,7 +87,7 @@
<padding>
<Insets right="16.0" />
</padding></Label>
<TextField id="fileChooserNameField" fx:id="fileName" maxHeight="-Infinity" minHeight="-Infinity" onAction="#onNameFieldChanged" onKeyTyped="#onNameFieldChanged" prefHeight="32.0" HBox.hgrow="ALWAYS" />
<TextField id="fileChooserNameField" fx:id="fileName" maxHeight="-Infinity" minHeight="-Infinity" onAction="#onNameFieldAction" onKeyTyped="#onNameFieldChanged" prefHeight="32.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<Region layoutX="10.0" layoutY="150.0" maxHeight="-Infinity" minHeight="-Infinity" prefHeight="8.0" VBox.vgrow="NEVER" />
Expand Down

0 comments on commit 7533dcb

Please sign in to comment.