1818package org .jackhuang .hmcl .ui .construct ;
1919
2020import com .jfoenix .controls .JFXButton ;
21+ import com .jfoenix .controls .JFXPopup ;
2122import com .jfoenix .controls .JFXTextField ;
2223import javafx .beans .property .SimpleStringProperty ;
2324import javafx .beans .property .StringProperty ;
3031import org .jackhuang .hmcl .ui .Controllers ;
3132import org .jackhuang .hmcl .ui .FXUtils ;
3233import org .jackhuang .hmcl .ui .SVG ;
34+ import org .jackhuang .hmcl .util .StringUtils ;
3335import org .jackhuang .hmcl .util .io .FileUtils ;
3436
3537import java .nio .file .Path ;
3840
3941public class FileSelector extends HBox {
4042 private final StringProperty value = new SimpleStringProperty ();
41- private String chooserTitle = i18n ( "selector.choose_file" ) ;
42- private boolean directory = false ;
43+ private String chooserTitle = "" ;
44+ private SelectionMode selectionMode = SelectionMode . FILE ;
4345 private final ObservableList <FileChooser .ExtensionFilter > extensionFilters = FXCollections .observableArrayList ();
4446
47+ JFXButton selectButton = FXUtils .newToggleButton4 (SVG .FOLDER_OPEN , 15 );
48+
49+ public enum SelectionMode {
50+ FILE ,
51+ DIRECTORY ,
52+ FILE_OR_DIRECTORY
53+ }
54+
4555 public String getValue () {
4656 return value .get ();
4757 }
@@ -63,12 +73,12 @@ public FileSelector setChooserTitle(String chooserTitle) {
6373 return this ;
6474 }
6575
66- public boolean isDirectory () {
67- return directory ;
76+ public SelectionMode getSelectionMode () {
77+ return selectionMode ;
6878 }
6979
70- public FileSelector setDirectory ( boolean directory ) {
71- this .directory = directory ;
80+ public FileSelector setSelectionMode ( SelectionMode selectionMode ) {
81+ this .selectionMode = selectionMode ;
7282 return this ;
7383 }
7484
@@ -80,26 +90,20 @@ public FileSelector() {
8090 JFXTextField customField = new JFXTextField ();
8191 FXUtils .bindString (customField , valueProperty ());
8292
83- JFXButton selectButton = FXUtils .newToggleButton4 (SVG .FOLDER_OPEN , 15 );
8493 selectButton .setOnAction (e -> {
85- if (directory ) {
86- DirectoryChooser chooser = new DirectoryChooser ();
87- chooser .setTitle (chooserTitle );
88- Path dir = FileUtils .toPath (chooser .showDialog (Controllers .getStage ()));
89- if (dir != null ) {
90- String path = FileUtils .getAbsolutePath (dir );
91- customField .setText (path );
92- value .setValue (path );
93- }
94- } else {
95- FileChooser chooser = new FileChooser ();
96- chooser .getExtensionFilters ().addAll (getExtensionFilters ());
97- chooser .setTitle (chooserTitle );
98- Path file = FileUtils .toPath (chooser .showOpenDialog (Controllers .getStage ()));
99- if (file != null ) {
100- String path = FileUtils .getAbsolutePath (file );
101- customField .setText (path );
102- value .setValue (path );
94+ switch (selectionMode ) {
95+ case FILE -> openFileChooser (customField );
96+ case DIRECTORY -> openDirectoryChooser (customField );
97+ case FILE_OR_DIRECTORY -> {
98+ PopupMenu selectPopupMenu = new PopupMenu ();
99+ JFXPopup selectModePopup = new JFXPopup (selectPopupMenu );
100+
101+ selectPopupMenu .getContent ().addAll (
102+ new IconedMenuItem (SVG .FILE_OPEN , i18n ("selector.choose_file" ), () -> openFileChooser (customField ), selectModePopup ),
103+ new IconedMenuItem (SVG .FOLDER_OPEN , i18n ("selector.choose_directory" ), () -> openDirectoryChooser (customField ), selectModePopup )
104+ );
105+
106+ selectModePopup .show (selectButton , JFXPopup .PopupVPosition .TOP , JFXPopup .PopupHPosition .RIGHT , -selectButton .getWidth (), 0 );
103107 }
104108 }
105109 });
@@ -108,4 +112,27 @@ public FileSelector() {
108112 setSpacing (3 );
109113 getChildren ().addAll (customField , selectButton );
110114 }
115+
116+ private void openFileChooser (JFXTextField customField ) {
117+ FileChooser chooser = new FileChooser ();
118+ chooser .getExtensionFilters ().addAll (getExtensionFilters ());
119+ chooser .setTitle (StringUtils .isBlank (chooserTitle ) ? i18n ("selector.choose_file" ) : chooserTitle );
120+ Path file = FileUtils .toPath (chooser .showOpenDialog (Controllers .getStage ()));
121+ if (file != null ) {
122+ String path = FileUtils .getAbsolutePath (file );
123+ customField .setText (path );
124+ value .setValue (path );
125+ }
126+ }
127+
128+ private void openDirectoryChooser (JFXTextField customField ) {
129+ DirectoryChooser chooser = new DirectoryChooser ();
130+ chooser .setTitle (StringUtils .isBlank (chooserTitle ) ? i18n ("selector.choose_directory" ) : chooserTitle );
131+ Path dir = FileUtils .toPath (chooser .showDialog (Controllers .getStage ()));
132+ if (dir != null ) {
133+ String path = FileUtils .getAbsolutePath (dir );
134+ customField .setText (path );
135+ value .setValue (path );
136+ }
137+ }
111138}
0 commit comments