-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DirectoryOpenPicker to the jpro-file module. This allows the us…
…er to select a directory instead of a file.
- Loading branch information
1 parent
9cd4a27
commit c689ee4
Showing
5 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
jpro-file/src/main/java/one/jpro/platform/file/picker/BaseDirectoryOpenPicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package one.jpro.platform.file.picker; | ||
|
||
import javafx.scene.Node; | ||
|
||
/** | ||
* This is an abstract class that implements the {@link DirectoryOpenPicker} interface. | ||
* It provides a base implementation for common functionality used by native implementation. | ||
* | ||
* @author Florian Kirmaier | ||
*/ | ||
abstract class BaseDirectoryOpenPicker extends BaseFileOpenPicker implements DirectoryOpenPicker { | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
BaseDirectoryOpenPicker(Node node) { | ||
super(node); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
jpro-file/src/main/java/one/jpro/platform/file/picker/DirectoryOpenPicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package one.jpro.platform.file.picker; | ||
|
||
import com.jpro.webapi.WebAPI; | ||
import javafx.scene.Node; | ||
import one.jpro.platform.file.MultipleFileSelector; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* {@link FilePicker} interface extension for file open operations. | ||
* | ||
* @author Florian Kirmaier | ||
*/ | ||
public interface DirectoryOpenPicker extends FileOpenPicker { | ||
|
||
/** | ||
* Creates a file picker. | ||
* It only works in desktop applications. | ||
* | ||
* @param node the associated node for this file picker | ||
* @return a {@link DirectoryOpenPicker} object. | ||
* @throws NullPointerException if the node is null | ||
*/ | ||
static DirectoryOpenPicker create(Node node) { | ||
Objects.requireNonNull(node, "node must not be null"); | ||
if (WebAPI.isBrowser()) { | ||
throw new UnsupportedOperationException("DirectoryOpenPicker is not supported in the browser"); | ||
} | ||
return new NativeDirectoryOpenPicker(node); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
jpro-file/src/main/java/one/jpro/platform/file/picker/NativeDirectoryOpenPicker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package one.jpro.platform.file.picker; | ||
|
||
import javafx.beans.property.ObjectProperty; | ||
import javafx.beans.property.SimpleObjectProperty; | ||
import javafx.beans.property.SimpleStringProperty; | ||
import javafx.beans.property.StringProperty; | ||
import javafx.scene.Node; | ||
import javafx.scene.control.SelectionMode; | ||
import javafx.scene.input.MouseEvent; | ||
import javafx.stage.DirectoryChooser; | ||
import javafx.stage.Window; | ||
import one.jpro.platform.file.FileSource; | ||
import one.jpro.platform.file.NativeFileSource; | ||
import one.jpro.platform.file.util.NodeUtils; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Represents a {@link DirectoryOpenPicker} implementation for JavaFX desktop/mobile | ||
* applications. This class specializes for selecting and opening directories from | ||
* the native file system. | ||
* | ||
* @author Florian Kirmaier | ||
*/ | ||
public class NativeDirectoryOpenPicker extends BaseDirectoryOpenPicker { | ||
|
||
private final DirectoryChooser fileChooser = new DirectoryChooser(); | ||
private List<NativeFileSource> nativeFileSources = List.of(); | ||
|
||
|
||
/** | ||
* Initializes a new instance associated with the specified node. | ||
* | ||
* @param node The node associated with this file picker. | ||
*/ | ||
public NativeDirectoryOpenPicker(Node node) { | ||
super(node); | ||
|
||
// Define the action that should be performed when the user clicks on the node. | ||
NodeUtils.addEventHandler(node, MouseEvent.MOUSE_CLICKED, actionEvent -> { | ||
Window window = node.getScene().getWindow(); | ||
final File file = fileChooser.showDialog(window); | ||
if (file != null) { | ||
// Create a list of native file sources from the selected file. | ||
nativeFileSources = List.of(new NativeFileSource(file)); | ||
|
||
// Invoke the onFilesSelected consumer. | ||
Consumer<List<? extends FileSource>> onFilesSelectedConsumer = getOnFilesSelected(); | ||
if (onFilesSelectedConsumer != null) { | ||
onFilesSelectedConsumer.accept(nativeFileSources); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public final String getTitle() { | ||
return fileChooser.getTitle(); | ||
} | ||
|
||
@Override | ||
public final void setTitle(final String value) { | ||
fileChooser.setTitle(value); | ||
} | ||
|
||
@Override | ||
public final StringProperty titleProperty() { | ||
return fileChooser.titleProperty(); | ||
} | ||
|
||
@Override | ||
public final StringProperty initialFileNameProperty() { | ||
if (initialFileName == null) { | ||
initialFileName = new SimpleStringProperty(this, "initialFileName"); | ||
} | ||
return initialFileName; | ||
} | ||
|
||
@Override | ||
public final File getInitialDirectory() { | ||
return fileChooser.getInitialDirectory(); | ||
} | ||
|
||
@Override | ||
public final void setInitialDirectory(final File value) { | ||
fileChooser.setInitialDirectory(value); | ||
} | ||
|
||
@Override | ||
public final ObjectProperty<File> initialDirectoryProperty() { | ||
return fileChooser.initialDirectoryProperty(); | ||
} | ||
|
||
@Override | ||
public final ObjectProperty<SelectionMode> selectionModeProperty() { | ||
if (selectionMode == null) { | ||
selectionMode = new SimpleObjectProperty<>(this, "selectionMode", SelectionMode.SINGLE); | ||
} | ||
return selectionMode; | ||
} | ||
} |