Skip to content

Commit

Permalink
Fix a bug where file confirmation dialogs were always shown
Browse files Browse the repository at this point in the history
Fix: #38
  • Loading branch information
io7m committed May 15, 2024
1 parent ffa815a commit aa9ae01
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> https://www.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.JWFileChoosersType;
import com.io7m.jwheatsheaf.ui.JWFileChoosers;
import com.io7m.percentpass.extension.MinimumPassing;
import com.io7m.xoanon.commander.api.XCCommanderType;
import com.io7m.xoanon.commander.api.XCKey;
import com.io7m.xoanon.commander.api.XCRobotType;
import com.io7m.xoanon.extension.XoExtension;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static com.io7m.jwheatsheaf.tests.JWTestUtilities.TIMEOUT;
import static com.io7m.jwheatsheaf.tests.JWTestUtilities.assertIsSelected;
import static com.io7m.jwheatsheaf.tests.JWTestUtilities.createChooser;
import static com.io7m.jwheatsheaf.tests.JWTestUtilities.findNameField;
import static com.io7m.jwheatsheaf.tests.JWTestUtilities.findOKButton;
import static javafx.scene.input.KeyCode.ENTER;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(XoExtension.class)
public final class JWFileChooserBug38
{
private JWTestFilesystems filesystems;
private FileSystem dosFilesystem;
private List<JWFileChooserEventType> events;
private JWFileChoosersType choosers;
private JWFileChooserConfiguration configuration;

@BeforeAll
public static void beforeAll()
{
JWTestUtilities.publishApplicationInfo();
}

@BeforeEach
public void setup()
throws IOException
{
this.events = Collections.synchronizedList(new ArrayList<>());

this.filesystems = JWTestFilesystems.create();
final var systems = this.filesystems.filesystems();
this.dosFilesystem = systems.get("ExampleDOS");

this.configuration =
JWFileChooserConfiguration.builder()
.setAction(JWFileChooserAction.CREATE)
.setConfirmFileSelection(true)
.setFileSystem(this.dosFilesystem)
.setFileSelectionMode(path -> {
return Boolean.valueOf(Files.isRegularFile(path));
})
.build();

this.choosers = JWFileChoosers.create();
}

@AfterEach
public void tearDown()
throws IOException
{
this.choosers.close();
}

/**
* If CREATE and confirmFileSelection are used, the confirmation dialog is
* always shown.
*/

@Test
public void test_CREATEConfirmFileSelection_OnlyShownIfFileExists(
final XCCommanderType commander,
final XCRobotType robot)
throws Exception
{
final var chooser =
createChooser(this.choosers, this.configuration, commander);
final var window =
chooser.stage();

final var okButton =
findOKButton(robot, window);
final var nameField =
findNameField(robot, window);

robot.click(nameField);
robot.typeText(nameField, "THIS_DOES_NOT_EXIST.TXT");
robot.type(nameField, List.of(new XCKey(ENTER, false, false, false)));
robot.click(okButton);
robot.waitForStageToClose(window, TIMEOUT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.io7m.jwheatsheaf.ui.internal;

import com.io7m.jaffirm.core.Preconditions;
import com.io7m.junreachable.UnreachableCodeException;
import com.io7m.jwheatsheaf.api.JWDirectoryCreationFailed;
import com.io7m.jwheatsheaf.api.JWFileChooserConfiguration;
import com.io7m.jwheatsheaf.api.JWFileChooserEventType;
Expand Down Expand Up @@ -656,25 +655,22 @@ private void onOKSelected()

this.result = List.of();

var resultTarget = List.<Path>of();
switch (this.configuration.action()) {
case OPEN_EXISTING_MULTIPLE:
case OPEN_EXISTING_SINGLE:
resultTarget =
this.directoryTableSelectionModel
final var resultTarget =
switch (this.configuration.action()) {
case OPEN_EXISTING_MULTIPLE, OPEN_EXISTING_SINGLE -> {
yield this.directoryTableSelectionModel
.getSelectedItems()
.stream()
.map(JWFileItem::path)
.collect(Collectors.toList());
break;
case CREATE:
resultTarget =
List.of(this.currentDirectory.resolve(this.fileName.getText()));
break;
}
}
case CREATE -> {
yield List.of(this.currentDirectory.resolve(this.fileName.getText()));
}
};

final boolean confirmed;
if (this.isFileSelectionConfirmationRequired()) {
if (this.isFileSelectionConfirmationRequired(resultTarget)) {
confirmed = this.confirmFileSelection(resultTarget);
} else {
confirmed = true;
Expand Down Expand Up @@ -749,17 +745,29 @@ private boolean confirmFileSelection(
.anyMatch(b -> Objects.equals(b, confirm));
}

private boolean isFileSelectionConfirmationRequired()
/**
* File selection confirmation is only required if confirmation is enabled
* and any of the selected files exist.
*
* @param files The selected files
*
* @return {@code true} if a confirmation dialog must be displayed
*/

private boolean isFileSelectionConfirmationRequired(
final List<Path> files)
{
switch (this.configuration.action()) {
case OPEN_EXISTING_SINGLE:
case OPEN_EXISTING_MULTIPLE:
return false;
case CREATE:
return this.configuration.confirmFileSelection();
}
return switch (this.configuration.action()) {
case OPEN_EXISTING_SINGLE, OPEN_EXISTING_MULTIPLE -> false;
case CREATE -> {
if (this.configuration.confirmFileSelection()) {
yield files.stream().anyMatch(Files::exists);
} else {
yield false;
}
}
};

throw new UnreachableCodeException();
}

@FXML
Expand Down

0 comments on commit aa9ae01

Please sign in to comment.