View Javadoc
1   /*
2    * Tethys: GUI Utilities
3    * Copyright 2012-2026. Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6    * use this file except in compliance with the License.  You may obtain a copy
7    * of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
14   * License for the specific language governing permissions and limitations under
15   * the License.
16   */
17  package io.github.tonywasher.joceanus.tethys.swing.dialog;
18  
19  import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogManager;
20  import io.github.tonywasher.joceanus.oceanus.logger.OceanusLogger;
21  import io.github.tonywasher.joceanus.tethys.core.dialog.TethysUICoreDirectorySelector;
22  
23  import javax.swing.JFileChooser;
24  import java.awt.Component;
25  import java.io.File;
26  
27  /**
28   * Swing Directory Selector.
29   */
30  public class TethysUISwingDirectorySelector
31          extends TethysUICoreDirectorySelector {
32      /**
33       * Logger.
34       */
35      private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(TethysUISwingDirectorySelector.class);
36  
37      /**
38       * Parent stage.
39       */
40      private final Component theParent;
41  
42      /**
43       * Directory Chooser.
44       */
45      private final JFileChooser theChooser;
46  
47      /**
48       * The selected directory.
49       */
50      private File theSelectedDir;
51  
52      /**
53       * Constructor.
54       *
55       * @param pParent the parent
56       */
57      TethysUISwingDirectorySelector(final Component pParent) {
58          if (pParent == null) {
59              throw new IllegalArgumentException("Cannot create Dialog during initialisation");
60          }
61          theParent = pParent;
62          theChooser = new JFileChooser();
63          theChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
64      }
65  
66      /**
67       * select the file.
68       */
69      public void showDialog() {
70          /* Initialise selection */
71          theSelectedDir = null;
72  
73          /* Set values */
74          theChooser.setDialogTitle(getTitle());
75          theChooser.setCurrentDirectory(getInitialDirectory());
76  
77          /* Show the dialog */
78          final int myResult = theChooser.showOpenDialog(theParent);
79  
80          /* If we selected a directory */
81          if (myResult == JFileChooser.APPROVE_OPTION) {
82              theSelectedDir = theChooser.getSelectedFile();
83          }
84      }
85  
86      @Override
87      public File selectDirectory() {
88          TethysUISwingDialog.runInSwingThread(this::showDialog);
89          return theSelectedDir;
90      }
91  }