View Javadoc
1   /*
2    * *************************************************************************************************************************************************************
3    *
4    * SteelBlue: DCI User Interfaces
5    * http://tidalwave.it/projects/steelblue
6    *
7    * Copyright (C) 2015 - 2025 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.ui.javafx.impl.filechooser;
27  
28  import jakarta.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.ui.core.UserNotificationWithFeedback;
37  import it.tidalwave.ui.core.BoundProperty;
38  import it.tidalwave.ui.javafx.impl.common.DelegateSupport;
39  import lombok.extern.slf4j.Slf4j;
40  import static it.tidalwave.ui.javafx.impl.DefaultJavaFXBinder.enforceFxApplicationThread;
41  
42  /***************************************************************************************************************************************************************
43   *
44   * @author  Fabrizio Giudici
45   *
46   **************************************************************************************************************************************************************/
47  @Slf4j
48  public class FileChooserBindings extends DelegateSupport
49    {
50      /***********************************************************************************************************************************************************
51       *
52       **********************************************************************************************************************************************************/
53      public FileChooserBindings (@Nonnull final Executor executor)
54        {
55          super(executor);
56        }
57  
58      /***********************************************************************************************************************************************************
59       * {@inheritDoc}
60       **********************************************************************************************************************************************************/
61      public void openFileChooserFor (@Nonnull final UserNotificationWithFeedback notification,
62                                      @Nonnull final BoundProperty<Path> selectedFile)
63        {
64          log.debug("openFileChooserFor({}, {})", notification, selectedFile);
65          enforceFxApplicationThread();
66  
67          final var fileChooser = new FileChooser();
68          fileChooser.setTitle(notification.getCaption());
69          fileChooser.setInitialDirectory(selectedFile.get().toFile());
70  
71          // It seems we need to take care of modality: https://javafx-jira.kenai.com/browse/RT-13949
72          final var file = runWhileDisabling(mainWindow, () -> fileChooser.showOpenDialog(mainWindow));
73  
74          notifyFile(file, notification, selectedFile);
75        }
76  
77      /***********************************************************************************************************************************************************
78       * {@inheritDoc}
79       **********************************************************************************************************************************************************/
80      public void openDirectoryChooserFor (@Nonnull final UserNotificationWithFeedback notification,
81                                           @Nonnull final BoundProperty<Path> selectedFolder)
82        {
83          log.debug("openDirectoryChooserFor({}, {})", notification, selectedFolder);
84          enforceFxApplicationThread();
85  
86          final var directoryChooser = new DirectoryChooser();
87          directoryChooser.setTitle(notification.getCaption());
88          directoryChooser.setInitialDirectory(selectedFolder.get().toFile());
89  
90          // It seems we need to take care of modality: https://javafx-jira.kenai.com/browse/RT-13949
91          final var file = runWhileDisabling(mainWindow, () -> directoryChooser.showDialog(mainWindow));
92  
93          notifyFile(file, notification, selectedFolder);
94        }
95  
96      /***********************************************************************************************************************************************************
97       *
98       **********************************************************************************************************************************************************/
99      private void notifyFile (@Nullable final File file,
100                              @Nonnull final UserNotificationWithFeedback notification,
101                              @Nonnull final BoundProperty<Path> selectedFile)
102       {
103         Platform.runLater(() ->
104           {
105             try
106               {
107                 if (file == null)
108                   {
109                     notification.cancel();
110                   }
111                 else
112                   {
113                     selectedFile.set(file.toPath());
114                     notification.confirm();
115                   }
116               }
117             catch (Exception e)
118               {
119                 log.warn("", e);
120               }
121           });
122       }
123   }