Skip to content

Commit

Permalink
Implement a "home" button
Browse files Browse the repository at this point in the history
If a home directory path is provided to the chooser configuration,
a home button will be shown that navigates directly to the home
directory.

Fix: #12
  • Loading branch information
io7m committed Apr 11, 2021
1 parent b65b73c commit af9be6b
Show file tree
Hide file tree
Showing 18 changed files with 166 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.in
@@ -0,0 +1 @@
An alternative to JavaFX's FileChooser that aims to be feature-compatible, if not fully API-compatible.
Expand Up @@ -57,6 +57,15 @@ public interface JWFileChooserConfigurationType

Optional<Path> initialDirectory();

/**
* The directory to which the chooser will navigate when the user clicks
* the "home" button. If no path is specified, the button is not shown.
*
* @return The user's home directory
*/

Optional<Path> homeDirectory();

/**
* @return The file name initially entered into the selection field
*/
Expand Down
Expand Up @@ -43,6 +43,12 @@ public interface JWFileImageSetType

URL forDirectoryUp();

/**
* @return An icon for the "home" button
*/

URL forHome();

/**
* @return An icon for the "recent items" entry
*/
Expand Down
Expand Up @@ -63,6 +63,12 @@ public URL forDirectoryUp()
return forName("goUp.png").orElseThrow();
}

@Override
public URL forHome()
{
return forName("home.png").orElseThrow();
}

@Override
public URL forRecentItems()
{
Expand Down
Expand Up @@ -64,6 +64,8 @@ public final class ExampleViewController implements Initializable
@FXML
private CheckBox slowIO;
@FXML
private CheckBox homeDirectory;
@FXML
private ChoiceBox<JWFileChooserAction> action;
@FXML
private TextField title;
Expand Down Expand Up @@ -135,6 +137,12 @@ private void onSlowIOChanged()

}

@FXML
private void onHomeDirectoryChanged()
{

}

@FXML
private void onOpenSelected()
throws IOException
Expand Down Expand Up @@ -164,6 +172,12 @@ private void onOpenSelected()
.addFileFilters(new ExampleFilterXML())
.addAllRecentFiles(recents);

if (this.homeDirectory.isSelected()) {
configurationBuilder.setHomeDirectory(
fileSystem.getPath(System.getProperty("user.home"))
);
}

if (!this.title.getText().isEmpty()) {
configurationBuilder.setTitle(this.title.getText());
}
Expand Down
Expand Up @@ -41,6 +41,13 @@
</children>
</HBox>
<Region layoutX="18.0" layoutY="130.0" maxHeight="-Infinity" minHeight="-Infinity" prefHeight="8.0" />
<HBox alignment="CENTER_LEFT">
<children>
<Region maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="32.0" prefWidth="160.0" />
<CheckBox fx:id="homeDirectory" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" mnemonicParsing="false" onAction="#onHomeDirectoryChanged" prefHeight="32.0" text="Home directory" HBox.hgrow="ALWAYS" />
</children>
</HBox>
<Region maxHeight="-Infinity" minHeight="-Infinity" prefHeight="8.0" />
<HBox layoutX="18.0" layoutY="58.0">
<children>
<Label maxHeight="-Infinity" minHeight="-Infinity" prefHeight="32.0" prefWidth="160.0" text="CSS" />
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -62,6 +62,12 @@ public URL forDirectoryUp()
return forName("go-up.png").orElseThrow();
}

@Override
public URL forHome()
{
return forName("go-home.png").orElseThrow();
}

@Override
public URL forRecentItems()
{
Expand Down
Expand Up @@ -19,7 +19,7 @@
*/

@Export
@Version("1.0.0")
@Version("1.1.0")
package com.io7m.jwheatsheaf.oxygen;

import org.osgi.annotation.bundle.Export;
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -83,6 +83,7 @@ public void start(final Stage stage)
.setAllowDirectoryCreation(true)
.setAction(JWFileChooserAction.OPEN_EXISTING_MULTIPLE)
.setFileSystem(this.dosFilesystem)
.setHomeDirectory(this.dosFilesystem.getPath("Z:\\HOME"))
.build();

this.choosers = JWFileChoosers.create();
Expand Down Expand Up @@ -470,6 +471,55 @@ public void testDirectoryUp(
Assertions.assertEquals(0, this.events.size());
}

/**
* The home button works.
*
* @param robot The FX test robot
*/

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

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

final var homeButton =
robot.lookup("#fileChooserHomeButton")
.queryButton();

robot.clickOn(homeButton);

final var targetCell =
robot.lookup(n -> n instanceof TableCell)
.queryAllAs(TableCell.class)
.stream()
.filter(cell -> Objects.equals(cell.getText(), "FILE.TXT"))
.findFirst()
.orElseThrow(() -> new IllegalStateException(
"Unable to locate a 'FILE.TXT' directory entry")
);

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

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

Assertions.assertEquals(
List.of("Z:\\HOME\\FILE.TXT"),
this.chooser.result()
.stream()
.map(Path::toString)
.collect(Collectors.toList())
);
Assertions.assertEquals(0, this.events.size());
}

/**
* Pressing escape closes the dialog without selecting a file.
*
Expand Down
Expand Up @@ -179,6 +179,8 @@ public int getRegexFlags()
})
.build();

Files.createDirectories(filesystem.getPath("Z:\\HOME"));
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 @@ -93,6 +93,7 @@ public final class JWFileChooserViewController
@FXML private Button okButton;
@FXML private Button selectDirectButton;
@FXML private Button upDirectoryButton;
@FXML private Button homeDirectoryButton;
@FXML private ChoiceBox<Path> pathMenu;
@FXML private ComboBox<JWFileChooserFilterType> fileTypeMenu;
@FXML private ListView<JWFileSourceEntryType> sourcesList;
Expand Down Expand Up @@ -203,6 +204,9 @@ public void setConfiguration(
this.selectDirectButton.setGraphic(
JWImages.imageView16x16Of(this.imageSet.forSelectDirect())
);
this.homeDirectoryButton.setGraphic(
JWImages.imageView16x16Of(this.imageSet.forHome())
);

final var fileSystem =
this.configuration.fileSystem();
Expand All @@ -225,6 +229,7 @@ public void setConfiguration(

this.lockableViews = List.of(
this.directoryTable,
this.homeDirectoryButton,
this.newDirectoryButton,
this.okButton,
this.pathMenu,
Expand Down Expand Up @@ -557,6 +562,13 @@ private void onCancelSelected()
window.hide();
}

@FXML
private void onHomeSelected()
{
final var homeDirectoryOpt = this.configuration.homeDirectory();
homeDirectoryOpt.ifPresent(this::setCurrentDirectory);
}

@FXML
private void onNameFieldChanged()
{
Expand All @@ -565,6 +577,8 @@ private void onNameFieldChanged()

private void configureButtons()
{
this.configureButtonHome();

switch (this.configuration.action()) {
case OPEN_EXISTING_MULTIPLE:
case OPEN_EXISTING_SINGLE:
Expand All @@ -585,6 +599,19 @@ private void configureButtons()
.addListener(item -> this.reconfigureOKButton());
}

private void configureButtonHome()
{
final var homeParent =
this.homeDirectoryButton.getParent();
final var homeDirectoryOpt =
this.configuration.homeDirectory();

if (homeDirectoryOpt.isEmpty()) {
homeParent.setVisible(false);
homeParent.setManaged(false);
}
}

private void configureTableView()
{
final var selectionModel = this.directoryTable.getSelectionModel();
Expand Down
Expand Up @@ -66,6 +66,12 @@ public URL forDirectoryUp()
return forName("goUp.png").orElseThrow();
}

@Override
public URL forHome()
{
return forName("home.png").orElseThrow();
}

@Override
public URL forRecentItems()
{
Expand Down
Expand Up @@ -38,3 +38,4 @@ ui.tooltip.directoryCreate=Create a new directory…
ui.tooltip.enterDirectly=Enter a full file path directly…
ui.tooltip.goParentDirectory=Go to parent directory
ui.tooltip.search=Search for files in the current directory.
ui.tooltip.home=Navigate to your home directory.
Expand Up @@ -21,18 +21,36 @@
<children>
<ChoiceBox id="fileChooserPathMenu" fx:id="pathMenu" maxHeight="-Infinity" maxWidth="1.7976931348623157E308" minHeight="-Infinity" prefHeight="32.0" styleClass="fileChooserPathMenu" HBox.hgrow="ALWAYS" />
<Region maxWidth="-Infinity" minWidth="-Infinity" prefWidth="8.0" />
<Button id="fileChooserSelectDirectButton" fx:id="selectDirectButton" layoutX="722.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#onSelectDirectButton" prefHeight="32.0" prefWidth="32.0">
<tooltip>
<Tooltip text="%ui.tooltip.enterDirectly" />
</tooltip>
</Button>
<Region layoutX="754.0" layoutY="10.0" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="8.0" />
<Button id="fileChooserUpButton" fx:id="upDirectoryButton" layoutX="762.0" layoutY="10.0" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#onUpDirectoryButton" prefHeight="32.0" prefWidth="32.0">
<tooltip>
<Tooltip text="%ui.tooltip.goParentDirectory" />
</tooltip>
</Button>
<Region layoutX="722.0" layoutY="10.0" maxWidth="-Infinity" minWidth="-Infinity" prefWidth="8.0" />
<HBox>
<children>
<Button id="fileChooserHomeButton" fx:id="homeDirectoryButton" maxHeight="-Infinity" maxWidth="-Infinity"
minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#onHomeSelected" prefHeight="32.0" prefWidth="32.0">
<tooltip>
<Tooltip text="%ui.tooltip.home" />
</tooltip></Button>
<Region maxWidth="-Infinity" minWidth="-Infinity" prefWidth="8.0" />
</children>
</HBox>
<HBox>
<children>
<Button id="fileChooserSelectDirectButton" fx:id="selectDirectButton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#onSelectDirectButton" prefHeight="32.0" prefWidth="32.0">
<tooltip>
<Tooltip text="%ui.tooltip.enterDirectly" />
</tooltip>
</Button>
<Region maxWidth="-Infinity" minWidth="-Infinity" prefWidth="8.0" />
</children>
</HBox>
<HBox>
<children>
<Button id="fileChooserUpButton" fx:id="upDirectoryButton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#onUpDirectoryButton" prefHeight="32.0" prefWidth="32.0">
<tooltip>
<Tooltip text="%ui.tooltip.goParentDirectory" />
</tooltip>
</Button>
<Region maxWidth="-Infinity" minWidth="-Infinity" prefWidth="8.0" />
</children>
</HBox>
<Button id="fileChooserCreateDirectoryButton" fx:id="newDirectoryButton" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" mnemonicParsing="false" onAction="#onCreateDirectoryButton" prefHeight="32.0" prefWidth="32.0">
<tooltip>
<Tooltip text="%ui.tooltip.directoryCreate" />
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.

0 comments on commit af9be6b

Please sign in to comment.