Skip to content

Commit

Permalink
Starting implementation of the jpro-file module
Browse files Browse the repository at this point in the history
  • Loading branch information
besidev committed Oct 13, 2023
1 parent 6c9847a commit 5b94e92
Show file tree
Hide file tree
Showing 12 changed files with 900 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
JPRO_PLATFORM_VERSION = 0.2.7-SNAPSHOT
JAVAFX_VERSION = 17.0.8
JPRO_VERSION = 2023.3.0
JPRO_VERSION = 2023.3.1-SNAPSHOT
SIMPLEFX_VERSION = 3.2.25
JMEMORYBUDDY_VERSION = 0.5.1
JNODES_VERSION = 0.8.1
Expand Down
5 changes: 5 additions & 0 deletions jpro-file/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
dependencies {
implementation "com.sandec.jpro:jpro-webapi:$JPRO_VERSION"
implementation "org.jetbrains:annotations:$JETBRAINS_ANNOTATIONS_VERSION"
runtimeOnly "ch.qos.logback:logback-classic:$LOGBACK_VERSION"
}
13 changes: 13 additions & 0 deletions jpro-file/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Module descriptor for JPro File Manager module.
*
* @author Besmir Beqiri
*/
module one.jpro.platform.file {
requires javafx.controls;
requires jpro.webapi;
requires org.jetbrains.annotations;

exports one.jpro.platform.file;
exports one.jpro.platform.file.picker;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package one.jpro.platform.file;

import java.util.List;

/**
* Creates an {@code ExtensionFilter} with the specified description
* and the file name extensions.
* <p>
* File name extension should be specified in the {@code *.<extension>}
* format.
*
* @param description the textual description for the filter
* @param extensions a list of the accepted file name extensions
* @author Besmir Beqiri
*/
public record ExtensionFilter(String description, List<String> extensions) {

/**
* Compact constructor for {@code ExtensionFilter}.
*
* @throws NullPointerException if the description or the extensions are {@code null}
* @throws IllegalArgumentException if the description or the extensions are empty
*/
public ExtensionFilter {
validateArgs(description, extensions);
}

/**
* Creates an {@code ExtensionFilter} with the specified description
* and the file name extensions.
* <p>
* File name extension should be specified in the {@code *.<extension>}
* format.
*
* @param description the textual description for the filter
* @param extensions an array of the accepted file name extensions
* @throws NullPointerException if the description or the extensions are {@code null}
* @throws IllegalArgumentException if the description or the extensions are empty
* @return the created {@code ExtensionFilter}
*/
public static ExtensionFilter of(String description, String... extensions) {
return new ExtensionFilter(description, List.of(extensions));
}

/**
* Validates the arguments.
*
* @param description the textual description for the filter
* @param extensions the accepted file name extensions
* @throws NullPointerException if the description or the extensions are {@code null}
* @throws IllegalArgumentException if the description or the extensions are empty
*/
private static void validateArgs(final String description, final List<String> extensions) {
if (description == null) {
throw new NullPointerException("Description must not be null");
}

if (description.isEmpty()) {
throw new IllegalArgumentException("Description must not be empty");
}

if (extensions == null) {
throw new NullPointerException("Extensions must not be null");
}

if (extensions.isEmpty()) {
throw new IllegalArgumentException("At least one extension must be defined");
}

for (String extension : extensions) {
if (extension == null) {
throw new NullPointerException("Extension must not be null");
}

if (extension.isEmpty()) {
throw new IllegalArgumentException("Extension must not be empty");
}
}
}
}
72 changes: 72 additions & 0 deletions jpro-file/src/main/java/one/jpro/platform/file/FileSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package one.jpro.platform.file;

import javafx.beans.property.ReadOnlyLongProperty;
import javafx.beans.property.ReadOnlyLongWrapper;
import javafx.beans.property.ReadOnlyStringProperty;
import javafx.beans.property.ReadOnlyStringWrapper;

/**
* File source.
*
* @author Besmir Beqiri
*/
public sealed abstract class FileSource<T> permits NativeFileSource, WebFileSource {

private final T platformBlob;

public FileSource(T platformBlob) {
this.platformBlob = platformBlob;
}

public final T getPlatformFile() {
return platformBlob;
}

abstract String _getName();

// name property
private ReadOnlyStringWrapper nameProperty;

public final String getName() {
return nameProperty == null ? _getName() : nameProperty.get();
}

public final ReadOnlyStringProperty nameProperty() {
if (nameProperty == null) {
nameProperty = new ReadOnlyStringWrapper(this, "name", _getName());
}
return nameProperty.getReadOnlyProperty();
}

abstract long _getSize();

// size property
private ReadOnlyLongWrapper sizeProperty;

public final long getSize() {
return sizeProperty == null ? _getSize() : sizeProperty.get();
}

public final ReadOnlyLongProperty sizeProperty() {
if (sizeProperty == null) {
sizeProperty = new ReadOnlyLongWrapper(this, "size", _getSize());
}
return sizeProperty.getReadOnlyProperty();
}

abstract String _getObjectURL();

// objectURL property
private ReadOnlyStringWrapper objectURLProperty;

public final String getObjectURL() {
return objectURLProperty == null ? _getObjectURL() : objectURLProperty.get();
}

public final ReadOnlyStringProperty objectURLProperty() {
if (objectURLProperty == null) {
objectURLProperty = new ReadOnlyStringWrapper(this, "objectURL", _getObjectURL());
}
return objectURLProperty.getReadOnlyProperty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package one.jpro.platform.file;

import java.io.File;

/**
* Java file source.
*
* @author Besmir Beqiri
*/
public final class NativeFileSource extends FileSource<File> {

public NativeFileSource(final File file) {
super(file);
}

@Override
String _getName() {
return getPlatformFile().getName();
}

@Override
long _getSize() {
return getPlatformFile().length();
}

@Override
String _getObjectURL() {
return getPlatformFile().toURI().toString();
}
}
32 changes: 32 additions & 0 deletions jpro-file/src/main/java/one/jpro/platform/file/WebFileSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package one.jpro.platform.file;

import com.jpro.webapi.WebAPI;

/**
* Web file source.
*
* @author Besmir Beqiri
*/
public final class WebFileSource extends FileSource<WebAPI.JSFile> {

public WebFileSource(WebAPI.JSFile jsFile) {
super(jsFile);
}

@Override
String _getName() {
return getPlatformFile().getFilename();
}

@Override
long _getSize() {
return getPlatformFile().getFileSize();
}

@Override
String _getObjectURL() {
return getPlatformFile().getObjectURL().getName();
}


}
Loading

0 comments on commit 5b94e92

Please sign in to comment.