1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
29
30 public class TethysUISwingDirectorySelector
31 extends TethysUICoreDirectorySelector {
32
33
34
35 private static final OceanusLogger LOGGER = OceanusLogManager.getLogger(TethysUISwingDirectorySelector.class);
36
37
38
39
40 private final Component theParent;
41
42
43
44
45 private final JFileChooser theChooser;
46
47
48
49
50 private File theSelectedDir;
51
52
53
54
55
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
68
69 public void showDialog() {
70
71 theSelectedDir = null;
72
73
74 theChooser.setDialogTitle(getTitle());
75 theChooser.setCurrentDirectory(getInitialDirectory());
76
77
78 final int myResult = theChooser.showOpenDialog(theParent);
79
80
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 }