New File Finder Tool that is dynamic in what it uses

This commit is contained in:
Speiger 2021-09-30 07:49:50 +02:00
parent bb5ddf3142
commit d7164fb152
5 changed files with 287 additions and 0 deletions

View File

@ -53,12 +53,14 @@ dependencies {
compile "org.lwjgl:lwjgl-openal"
compile "org.lwjgl:lwjgl-opengl"
compile "org.lwjgl:lwjgl-stb"
compile "org.lwjgl:lwjgl-nfd"
compile "org.lwjgl:lwjgl::$lwjglNatives"
compile "org.lwjgl:lwjgl-glfw::$lwjglNatives"
compile "org.lwjgl:lwjgl-jemalloc::$lwjglNatives"
compile "org.lwjgl:lwjgl-openal::$lwjglNatives"
compile "org.lwjgl:lwjgl-opengl::$lwjglNatives"
compile "org.lwjgl:lwjgl-stb::$lwjglNatives"
compile "org.lwjgl:lwjgl-nfd::$lwjglNatives"
//Gson
compile 'com.google.code.gson:gson:2.8.6'

View File

@ -0,0 +1,118 @@
package speiger.src.coreengine.utils.io;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.collections.objects.sets.ObjectLinkedOpenHashSet;
import speiger.src.collections.objects.sets.ObjectSortedSet;
import speiger.src.collections.objects.utils.ObjectIterators;
import speiger.src.coreengine.utils.io.finders.IFileFinder;
import speiger.src.coreengine.utils.io.finders.JavaFileFinder;
import speiger.src.coreengine.utils.io.finders.NativeFileFinder;
public class FileFinder
{
public static IFileFinder DEFAULT_FINDER = NativeFileFinder.INSTANCE;
protected int flags;
protected String customPath;
protected ObjectSortedSet<String> fileFormats;
protected IFileFinder finder;
public FileFinder(int flags, String...fileFormats)
{
this.flags = flags;
this.fileFormats = new ObjectLinkedOpenHashSet<>(fileFormats);
finder = DEFAULT_FINDER;
}
public static FileFinder file(String...formats)
{
return new FileFinder(IFileFinder.FILE, formats);
}
public static FileFinder files(String...formats)
{
return new FileFinder(IFileFinder.MULTI_FILE, formats);
}
public static FileFinder folder()
{
return new FileFinder(IFileFinder.FOLDER);
}
public static FileFinder any(String...formats)
{
return new FileFinder(IFileFinder.ANY, formats).shell(JavaFileFinder.INSTANCE);
}
public static FileFinder save(String fileFormat)
{
return new FileFinder(IFileFinder.SAVE, fileFormat);
}
public static FileFinder save(String... fileFormat)
{
return new FileFinder(IFileFinder.SAVE, fileFormat);
}
public FileFinder path(File file)
{
return path(file.toString());
}
public FileFinder path(Path path)
{
return path(path.toString());
}
public FileFinder path(String s)
{
customPath = s;
return this;
}
public FileFinder shell(IFileFinder finder)
{
this.finder = finder;
return this;
}
public File singleFile(String description)
{
List<File> files = buildInternal(description);
return files.isEmpty() ? null : files.get(0);
}
public List<File> files(String description)
{
return buildInternal(description);
}
public Path singlePath(String description)
{
List<File> files = buildInternal(description);
return files.isEmpty() ? null : files.get(0).toPath();
}
public List<Path> paths(String description)
{
return ObjectIterators.pour(ObjectIterators.map(buildInternal(description).iterator(), File::toPath));
}
private ObjectList<File> buildInternal(String description)
{
ObjectList<File> files = finder.build(flags, customPath, fileFormats, description);
files.removeIf(this::isFileInvalid);
return files;
}
private boolean isFileInvalid(File file)
{
if(file.isDirectory()) return false;
String fileName = file.getName();
return !fileFormats.contains(fileName.substring(fileName.lastIndexOf(".")+1));
}
}

View File

@ -0,0 +1,25 @@
package speiger.src.coreengine.utils.io.finders;
import java.io.File;
import java.util.StringJoiner;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.collections.objects.sets.ObjectSortedSet;
public interface IFileFinder
{
public static final int FILE = 0;
public static final int MULTI_FILE = 1;
public static final int FOLDER = 2;
public static final int ANY = 4;
public static final int SAVE = 8;
public ObjectList<File> build(int flags, String startPath, ObjectSortedSet<String> validFormats, String description);
public default String toFileFormat(ObjectSortedSet<String> validFormats)
{
StringJoiner joiner = new StringJoiner(",");
for(String s : validFormats) joiner.add(s);
return joiner.toString();
}
}

View File

@ -0,0 +1,68 @@
package speiger.src.coreengine.utils.io.finders;
import java.io.File;
import java.util.StringJoiner;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.collections.objects.sets.ObjectSortedSet;
public class JavaFileFinder implements IFileFinder
{
public static final IFileFinder INSTANCE = new JavaFileFinder();
@Override
public ObjectList<File> build(int flags, String startPath, ObjectSortedSet<String> validFormats, String description)
{
JFileChooser file = new JFileChooser(startPath);
if((flags & FOLDER) != 0) file.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if((flags & ANY) != 0) file.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
if((flags & MULTI_FILE) != 0) file.setMultiSelectionEnabled(true);
file.setFileHidingEnabled(false);
file.setFileFilter(new Filter(description, validFormats));
if((flags & SAVE) != 0 && file.showSaveDialog(null) == 0) return ObjectArrayList.wrap(sanitize(file.getSelectedFiles(), validFormats.first()));
else if((flags & SAVE) == 0 && file.showOpenDialog(null) == 0) return ObjectArrayList.wrap(file.getSelectedFiles());
return new ObjectArrayList<>();
}
protected File[] sanitize(File[] files, String s)
{
for(int i = 0,m=files.length;i<m;i++)
{
File file = files[i];
if(file.getName().lastIndexOf(".") == -1) files[i] = new File(file.getParent(), file.getName()+"."+s);
}
return files;
}
private static class Filter extends FileFilter
{
String description;
ObjectSortedSet<String> validFormats;
public Filter(String description, ObjectSortedSet<String> validFormats)
{
StringJoiner joiner = new StringJoiner(", ", " (", ")");
for(String s : validFormats) joiner.add("."+s);
this.description = description + joiner.toString();
this.validFormats = validFormats;
}
@Override
public boolean accept(File f)
{
if(f.isDirectory()) return true;
String fileName = f.getName();
return validFormats.contains(fileName.substring(fileName.lastIndexOf(".")+1));
}
@Override
public String getDescription()
{
return description;
}
}
}

View File

@ -0,0 +1,74 @@
package speiger.src.coreengine.utils.io.finders;
import java.io.File;
import org.lwjgl.PointerBuffer;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.util.nfd.NFDPathSet;
import org.lwjgl.util.nfd.NativeFileDialog;
import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.objects.lists.ObjectList;
import speiger.src.collections.objects.sets.ObjectSortedSet;
public class NativeFileFinder implements IFileFinder
{
public static final IFileFinder INSTANCE = new NativeFileFinder();
@Override
public ObjectList<File> build(int flags, String startPath, ObjectSortedSet<String> validFormats, String description)
{
ObjectList<File> files = new ObjectArrayList<>();
if((flags & SAVE) != 0)
{
try(MemoryStack stack = MemoryStack.stackPush())
{
PointerBuffer output = stack.mallocPointer(1);
if(NativeFileDialog.NFD_SaveDialog(toFileFormat(validFormats), startPath, output) == NativeFileDialog.NFD_OKAY)
{
File file = new File(output.getStringUTF8());
if(file.getName().lastIndexOf(".") == -1) file = new File(file.getParent(), file.getName()+"."+validFormats.first());
files.add(file);
}
}
}
else if((flags & FOLDER) != 0)
{
try(MemoryStack stack = MemoryStack.stackPush())
{
PointerBuffer output = stack.mallocPointer(1);
if(NativeFileDialog.NFD_PickFolder(startPath, output) == NativeFileDialog.NFD_OKAY)
{
files.add(new File(output.getStringUTF8()));
}
}
}
else if((flags & MULTI_FILE) != 0)
{
try(MemoryStack stack = MemoryStack.stackPush())
{
NFDPathSet paths = NFDPathSet.mallocStack(stack);
if(NativeFileDialog.NFD_OpenDialogMultiple(toFileFormat(validFormats), startPath, paths) == NativeFileDialog.NFD_OKAY)
{
int size = (int)NativeFileDialog.NFD_PathSet_GetCount(paths);
for(int i = 0;i<size;i++)
{
files.add(new File(NativeFileDialog.NFD_PathSet_GetPath(paths, i)));
}
}
}
}
else
{
try(MemoryStack stack = MemoryStack.stackPush())
{
PointerBuffer output = stack.mallocPointer(1);
if(NativeFileDialog.NFD_OpenDialog(toFileFormat(validFormats), startPath, output) == NativeFileDialog.NFD_OKAY)
{
files.add(new File(output.getStringUTF8()));
}
}
}
return files;
}
}