1 /* 2 * ************************************************************************************************************************************************************* 3 * 4 * SteelBlue: DCI User Interfaces 5 * http://tidalwave.it/projects/steelblue 6 * 7 * Copyright (C) 2015 - 2024 by Tidalwave s.a.s. (http://tidalwave.it) 8 * 9 * ************************************************************************************************************************************************************* 10 * 11 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 12 * You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 17 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 18 * 19 * ************************************************************************************************************************************************************* 20 * 21 * git clone https://bitbucket.org/tidalwave/steelblue-src 22 * git clone https://github.com/tidalwave-it/steelblue-src 23 * 24 * ************************************************************************************************************************************************************* 25 */ 26 package it.tidalwave.role.ui.javafx.impl.filechooser; 27 28 import javax.annotation.Nonnull; 29 import javax.annotation.Nullable; 30 import java.util.concurrent.Executor; 31 import java.io.File; 32 import java.nio.file.Path; 33 import javafx.stage.DirectoryChooser; 34 import javafx.stage.FileChooser; 35 import javafx.application.Platform; 36 import it.tidalwave.util.ui.UserNotificationWithFeedback; 37 import it.tidalwave.role.ui.BoundProperty; 38 import it.tidalwave.role.ui.javafx.impl.common.DelegateSupport; 39 import lombok.extern.slf4j.Slf4j; 40 41 /*************************************************************************************************************************************************************** 42 * 43 * @author Fabrizio Giudici 44 * 45 **************************************************************************************************************************************************************/ 46 @Slf4j 47 public class FileChooserBindings extends DelegateSupport 48 { 49 /*********************************************************************************************************************************************************** 50 * 51 **********************************************************************************************************************************************************/ 52 public FileChooserBindings (@Nonnull final Executor executor) 53 { 54 super(executor); 55 } 56 57 /*********************************************************************************************************************************************************** 58 * {@inheritDoc} 59 **********************************************************************************************************************************************************/ 60 public void openFileChooserFor (@Nonnull final UserNotificationWithFeedback notification, 61 @Nonnull final BoundProperty<Path> selectedFile) 62 { 63 log.debug("openFileChooserFor({}, {})", notification, selectedFile); 64 assertIsFxApplicationThread(); 65 66 final var fileChooser = new FileChooser(); 67 fileChooser.setTitle(notification.getCaption()); 68 fileChooser.setInitialDirectory(selectedFile.get().toFile()); 69 70 // It seems we need to take care of modality: https://javafx-jira.kenai.com/browse/RT-13949 71 final var file = runWhileDisabling(mainWindow, () -> fileChooser.showOpenDialog(mainWindow)); 72 73 notifyFile(file, notification, selectedFile); 74 } 75 76 /*********************************************************************************************************************************************************** 77 * {@inheritDoc} 78 **********************************************************************************************************************************************************/ 79 public void openDirectoryChooserFor (@Nonnull final UserNotificationWithFeedback notification, 80 @Nonnull final BoundProperty<Path> selectedFolder) 81 { 82 log.debug("openDirectoryChooserFor({}, {})", notification, selectedFolder); 83 assertIsFxApplicationThread(); 84 85 final var directoryChooser = new DirectoryChooser(); 86 directoryChooser.setTitle(notification.getCaption()); 87 directoryChooser.setInitialDirectory(selectedFolder.get().toFile()); 88 89 // It seems we need to take care of modality: https://javafx-jira.kenai.com/browse/RT-13949 90 final var file = runWhileDisabling(mainWindow, () -> directoryChooser.showDialog(mainWindow)); 91 92 notifyFile(file, notification, selectedFolder); 93 } 94 95 /*********************************************************************************************************************************************************** 96 * 97 **********************************************************************************************************************************************************/ 98 private void notifyFile (@Nullable final File file, 99 @Nonnull final UserNotificationWithFeedback notification, 100 @Nonnull final BoundProperty<Path> selectedFile) 101 { 102 Platform.runLater(() -> 103 { 104 try 105 { 106 if (file == null) 107 { 108 notification.cancel(); 109 } 110 else 111 { 112 selectedFile.set(file.toPath()); 113 notification.confirm(); 114 } 115 } 116 catch (Exception e) 117 { 118 log.warn("", e); 119 } 120 }); 121 } 122 }