Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow for setting a default file filter
The file chooser configuration has been extended with a property that allows for setting the default file filter used in the UI. The filter must be included in the list of file filters given in the configuration. Fix: #9
- Loading branch information
Showing
6 changed files
with
280 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...tsheaf.tests/src/test/java/com/io7m/jwheatsheaf/tests/JWFileChooserConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package com.io7m.jwheatsheaf.tests; | ||
|
||
import com.io7m.jaffirm.core.PreconditionViolationException; | ||
import com.io7m.jwheatsheaf.api.JWFileChooserConfiguration; | ||
import com.io7m.jwheatsheaf.api.JWFileChooserFilterType; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.file.FileSystems; | ||
import java.nio.file.Path; | ||
|
||
public final class JWFileChooserConfigurationTest | ||
{ | ||
/** | ||
* Trying to specify a default filter that doesn't appear in the list of | ||
* filters fails. | ||
*/ | ||
|
||
@Test | ||
public void testFilterNotInList() | ||
{ | ||
Assertions.assertThrows(PreconditionViolationException.class, () -> { | ||
JWFileChooserConfiguration.builder() | ||
.setFileSystem(FileSystems.getDefault()) | ||
.setFileFilterDefault(new EmptyFilter()) | ||
.build(); | ||
}); | ||
} | ||
|
||
private static final class EmptyFilter | ||
implements JWFileChooserFilterType | ||
{ | ||
EmptyFilter() | ||
{ | ||
|
||
} | ||
|
||
@Override | ||
public String description() | ||
{ | ||
return "failure"; | ||
} | ||
|
||
@Override | ||
public boolean isAllowed( | ||
final Path path) | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
132 changes: 132 additions & 0 deletions
132
...tsheaf.tests/src/test/java/com/io7m/jwheatsheaf/tests/JWFileChooserFilterDefaultTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
* Copyright © 2020 Mark Raynsford <code@io7m.com> http://io7m.com | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package com.io7m.jwheatsheaf.tests; | ||
|
||
import com.io7m.jwheatsheaf.api.JWFileChooserAction; | ||
import com.io7m.jwheatsheaf.api.JWFileChooserConfiguration; | ||
import com.io7m.jwheatsheaf.api.JWFileChooserEventType; | ||
import com.io7m.jwheatsheaf.api.JWFileChooserType; | ||
import com.io7m.jwheatsheaf.api.JWFileChoosersType; | ||
import com.io7m.jwheatsheaf.ui.JWFileChoosers; | ||
import javafx.scene.input.KeyCode; | ||
import javafx.stage.Stage; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.testfx.api.FxAssert; | ||
import org.testfx.api.FxRobot; | ||
import org.testfx.framework.junit5.ApplicationExtension; | ||
import org.testfx.framework.junit5.Start; | ||
import org.testfx.framework.junit5.Stop; | ||
import org.testfx.matcher.base.NodeMatchers; | ||
import org.testfx.matcher.control.TableViewMatchers; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.concurrent.TimeUnit.*; | ||
|
||
@ExtendWith(ApplicationExtension.class) | ||
public final class JWFileChooserFilterDefaultTest | ||
{ | ||
private JWTestFilesystems filesystems; | ||
private FileSystem dosFilesystem; | ||
private FileSystem brokenFilesystem; | ||
private FileSystem brokenFilesFilesystem; | ||
private JWFileChooserType chooser; | ||
private List<Path> selected; | ||
private List<JWFileChooserEventType> events; | ||
private JWFileChoosersType choosers; | ||
|
||
@Start | ||
public void start(final Stage stage) | ||
throws Exception | ||
{ | ||
this.events = Collections.synchronizedList(new ArrayList<>()); | ||
|
||
this.filesystems = JWTestFilesystems.create(); | ||
final var systems = this.filesystems.filesystems(); | ||
this.dosFilesystem = systems.get("ExampleDOS"); | ||
this.brokenFilesystem = systems.get("Broken"); | ||
this.brokenFilesFilesystem = systems.get("BrokenFiles"); | ||
|
||
final var filter = | ||
new JWFileFilterHideAll(); | ||
|
||
final var configuration = | ||
JWFileChooserConfiguration.builder() | ||
.setAllowDirectoryCreation(true) | ||
.setAction(JWFileChooserAction.CREATE) | ||
.setFileSystem(this.dosFilesystem) | ||
.addFileFilters(filter) | ||
.setFileFilterDefault(filter) | ||
.build(); | ||
|
||
this.choosers = JWFileChoosers.create(); | ||
this.chooser = this.choosers.create(stage, configuration); | ||
this.chooser.setEventListener(event -> this.events.add(event)); | ||
this.chooser.show(); | ||
} | ||
|
||
@Stop | ||
public void stop() | ||
throws IOException | ||
{ | ||
this.choosers.close(); | ||
} | ||
|
||
/** | ||
* A "hide everything" filter set as the default... Hides everything! | ||
* | ||
* @param robot The FX test robot | ||
*/ | ||
|
||
@Test | ||
public void testFilterDefaultHidesEverything( | ||
final FxRobot robot, | ||
final TestInfo info) | ||
{ | ||
JWFileWindowTitles.setTitle(this.chooser, info); | ||
|
||
final var okButton = | ||
robot.lookup("#fileChooserOKButton") | ||
.queryButton(); | ||
final var cancelButton = | ||
robot.lookup("#fileChooserCancelButton") | ||
.queryButton(); | ||
|
||
robot.sleep(1L, SECONDS); | ||
FxAssert.verifyThat(okButton, NodeMatchers.isDisabled()); | ||
|
||
final var tableView = | ||
robot.lookup("#fileChooserDirectoryTable") | ||
.queryTableView(); | ||
|
||
FxAssert.verifyThat(tableView, TableViewMatchers.hasNumRows(0)); | ||
robot.clickOn(cancelButton); | ||
|
||
Assertions.assertEquals(List.of(), this.chooser.result()); | ||
Assertions.assertEquals(0, this.events.size()); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
com.io7m.jwheatsheaf.tests/src/test/java/com/io7m/jwheatsheaf/tests/JWFileFilterHideAll.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright © 2021 Mark Raynsford <code@io7m.com> http://io7m.com | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY | ||
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR | ||
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
package com.io7m.jwheatsheaf.tests; | ||
|
||
import com.io7m.jwheatsheaf.api.JWFileChooserFilterType; | ||
|
||
import java.nio.file.Path; | ||
|
||
public final class JWFileFilterHideAll | ||
implements JWFileChooserFilterType | ||
{ | ||
public JWFileFilterHideAll() | ||
{ | ||
|
||
} | ||
|
||
@Override | ||
public String description() | ||
{ | ||
return "Nothing!"; | ||
} | ||
|
||
@Override | ||
public boolean isAllowed( | ||
final Path path) | ||
{ | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters