Skip to content

Commit

Permalink
Allow for specifying custom dialog titles
Browse files Browse the repository at this point in the history
This adds a configuration option to allow for specifying a custom
dialog title for choosers.

Fix: #8
  • Loading branch information
io7m committed Apr 10, 2021
1 parent 6a41a77 commit ef3303a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
9 changes: 7 additions & 2 deletions README-CHANGES.xml
Expand Up @@ -39,18 +39,23 @@
<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-10T21:12:49+00:00" is-open="true" ticket-system="com.github.io7m.jwheatsheaf" version="3.0.0">
<c:release date="2021-04-10T21:29:16+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>
<c:ticket id="14"/>
</c:tickets>
</c:change>
<c:change date="2021-04-10T21:12:49+00:00" summary="Allow for specifying an initial filename in choosers">
<c:change date="2021-04-10T00:00:00+00:00" summary="Allow for specifying an initial filename in choosers">
<c:tickets>
<c:ticket id="15"/>
</c:tickets>
</c:change>
<c:change date="2021-04-10T21:29:16+00:00" summary="Allow for specifying custom dialog titles">
<c:tickets>
<c:ticket id="8"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
</c:releases>
Expand Down
Expand Up @@ -63,6 +63,12 @@ public interface JWFileChooserConfigurationType

Optional<String> initialFileName();

/**
* @return A string that will override the generic dialog title
*/

Optional<String> title();

/**
* The image set used to select images for the file chooser UI. If no
* set is specified here, a default set of images and icons will be used.
Expand Down
Expand Up @@ -28,6 +28,7 @@
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;

import java.io.IOException;
Expand Down Expand Up @@ -64,6 +65,8 @@ public final class ExampleViewController implements Initializable
private CheckBox slowIO;
@FXML
private ChoiceBox<JWFileChooserAction> action;
@FXML
private TextField title;

public ExampleViewController()
{
Expand Down Expand Up @@ -150,7 +153,7 @@ private void onOpenSelected()
fileSystem.getPath("G", "H", "I")
);

final var configuration =
final var configurationBuilder =
JWFileChooserConfiguration.builder()
.setAllowDirectoryCreation(this.allowDirectoryCreation.isSelected())
.setFileSystem(fileSystem)
Expand All @@ -159,8 +162,14 @@ private void onOpenSelected()
.setAction(this.action.getValue())
.addFileFilters(new ExampleFilterRejectAll())
.addFileFilters(new ExampleFilterXML())
.addAllRecentFiles(recents)
.build();
.addAllRecentFiles(recents);

if (!this.title.getText().isEmpty()) {
configurationBuilder.setTitle(this.title.getText());
}

final var configuration =
configurationBuilder.build();

final var testingBuilder = JWFileChoosersTesting.builder();
if (this.slowIO.isSelected()) {
Expand Down
Expand Up @@ -6,6 +6,7 @@
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>
Expand Down Expand Up @@ -54,6 +55,13 @@
</children>
</HBox>
<Region layoutX="18.0" layoutY="170.0" maxHeight="-Infinity" minHeight="-Infinity" prefHeight="8.0" />
<HBox>
<children>
<Label maxHeight="-Infinity" minHeight="-Infinity" prefHeight="32.0" prefWidth="160.0" text="Title" />
<TextField fx:id="title" minHeight="-Infinity" prefHeight="32.0" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<Region maxHeight="-Infinity" minHeight="-Infinity" prefHeight="8.0" />
<HBox>
<children>
<Region HBox.hgrow="ALWAYS" />
Expand Down
Expand Up @@ -178,15 +178,25 @@ public JWFileChooserType create(
dialog.initStyle(StageStyle.DECORATED);
dialog.setScene(new Scene(pane));

switch (configuration.action()) {
case CREATE:
case OPEN_EXISTING_SINGLE:
dialog.setTitle(this.strings.fileSelect());
break;
case OPEN_EXISTING_MULTIPLE:
dialog.setTitle(this.strings.filesSelect());
break;
}
/*
* Configure the title for the dialog.
*/

configuration.title()
.ifPresentOrElse(
dialog::setTitle,
() -> {
switch (configuration.action()) {
case CREATE:
case OPEN_EXISTING_SINGLE:
dialog.setTitle(this.strings.fileSelect());
break;
case OPEN_EXISTING_MULTIPLE:
dialog.setTitle(this.strings.filesSelect());
break;
}
}
);

/*
* Close the dialog when escape is pressed.
Expand Down

0 comments on commit ef3303a

Please sign in to comment.