Skip to content

Commit

Permalink
Add: FlagLoaderFlags for FileLoaders, to provide simple context infor…
Browse files Browse the repository at this point in the history
…mation
  • Loading branch information
SonarSonic committed Oct 21, 2024
1 parent 3e7ea16 commit 98d65ef
Show file tree
Hide file tree
Showing 16 changed files with 97 additions and 40 deletions.
29 changes: 25 additions & 4 deletions src/main/java/drawingbot/DrawingBotV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
package drawingbot;

import java.io.File;
import java.util.EnumSet;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
Expand All @@ -16,6 +18,7 @@
import drawingbot.files.json.projects.DBTaskContext;
import drawingbot.files.json.projects.ObservableProject;
import drawingbot.files.loaders.AbstractFileLoader;
import drawingbot.files.loaders.FileLoaderFlags;
import drawingbot.image.ImageFilterSettings;
import drawingbot.image.format.ImageData;
import drawingbot.javafx.FXController;
Expand Down Expand Up @@ -292,26 +295,44 @@ public void resetTaskService(){

///////////////////////////////////////////////////////////////////////////////////////////////////////

private Set<FileLoaderFlags> createFileLoaderFlags(boolean internal, boolean isSubTask){
EnumSet<FileLoaderFlags> flags = EnumSet.noneOf(FileLoaderFlags.class);
if(internal){
flags.add(FileLoaderFlags.INTERNAL_FILE);
}
if(isSubTask){
flags.add(FileLoaderFlags.INTERNAL_FILE);
}
return flags;
}

public void openFile(DBTaskContext context, File file, boolean internal, boolean isSubTask) {
AbstractFileLoader loadingTask = getImageLoaderTask(context, file, internal, isSubTask);
openFile(context, file, createFileLoaderFlags(internal, isSubTask));
}

public void openFile(DBTaskContext context, File file, Set<FileLoaderFlags> flags) {
AbstractFileLoader loadingTask = getImageLoaderTask(context, file, flags);
if(loadingTask != null){
taskMonitor.queueTask(loadingTask);
}
}

public AbstractFileLoader getImageLoaderTask(DBTaskContext context, File file, boolean internal, boolean isSubTask){
AbstractFileLoader loadingTask = MasterRegistry.INSTANCE.getFileLoader(context, file, internal, isSubTask);
return getImageLoaderTask(context, file, createFileLoaderFlags(internal, isSubTask));
}

public AbstractFileLoader getImageLoaderTask(DBTaskContext context, File file, Set<FileLoaderFlags> flags){
AbstractFileLoader loadingTask = MasterRegistry.INSTANCE.getFileLoader(context, file, flags);

//if the file loader could provide an image, wipe the current one
if(!isSubTask && loadingTask.hasImageData() && context.project.activeTask.get() != null){
if(!flags.contains(FileLoaderFlags.SUB_TASK) && loadingTask.hasImageData() && context.project.activeTask.get() != null){
context.project.activeTask.get().cancel();
context.taskManager().setActiveTask(null);
context.project.openImage.set(null);
}

loadingTask.setOnSucceeded(e -> {
if(!isSubTask && e.getSource().getValue() != null){
if(!flags.contains(FileLoaderFlags.SUB_TASK) && e.getSource().getValue() != null){
context.project.openImage.set((ImageData) e.getSource().getValue());
Platform.runLater(() -> context.project().setDisplayMode(Register.INSTANCE.DISPLAY_MODE_IMAGE));
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/drawingbot/FXApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import drawingbot.files.RecentProjectHandler;
import drawingbot.files.json.JsonLoaderManager;
import drawingbot.files.json.projects.ObservableProject;
import drawingbot.files.loaders.FileLoaderFlags;
import drawingbot.javafx.FXHelper;
import drawingbot.javafx.preferences.DBPreferences;
import drawingbot.javafx.util.MouseMonitor;
Expand Down Expand Up @@ -39,6 +40,7 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
Expand Down Expand Up @@ -274,7 +276,7 @@ protected Boolean call() throws Exception {
DrawingBotV3.logger.info("Attempting to load file at startup");
try {
File startupFile = new File(launchArgs[0]);
DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), startupFile, false, false);
DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), startupFile, EnumSet.noneOf(FileLoaderFlags.class));
} catch (Exception e) {
DrawingBotV3.logger.log(Level.SEVERE, "Failed to load file at startup", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import drawingbot.files.FileUtils;
import drawingbot.files.VersionControl;
import drawingbot.files.json.PresetData;
import drawingbot.files.loaders.FileLoaderFlags;
import drawingbot.geom.MaskingSettings;
import drawingbot.image.ImageFilterSettings;
import drawingbot.image.blend.EnumBlendMode;
Expand Down Expand Up @@ -50,6 +51,7 @@

import java.io.File;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.LinkedHashMap;
import java.util.List;

Expand Down Expand Up @@ -303,7 +305,7 @@ public void copy(ObservableProject project){
exportRange.set(project.exportRange.get());

if(openImage.get() != null){
DrawingBotV3.INSTANCE.openFile(context, openImage.get().getSourceFile(), false, true);
DrawingBotV3.INSTANCE.openFile(context, openImage.get().getSourceFile(), EnumSet.of(FileLoaderFlags.SUB_TASK, FileLoaderFlags.PROJECT_LOADING));
}
if(currentDrawing.get() != null) {
currentDrawing.set(project.currentDrawing.get().copy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import drawingbot.files.json.JsonLoaderManager;
import drawingbot.files.json.PresetDataLoader;
import drawingbot.files.loaders.AbstractFileLoader;
import drawingbot.files.loaders.FileLoaderFlags;
import drawingbot.image.format.ImageData;
import drawingbot.image.format.ImageCropping;
import drawingbot.javafx.FXHelper;
Expand All @@ -32,6 +33,7 @@
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -200,7 +202,7 @@ public void saveData(DBTaskContext context, ImageCropping data, GenericPreset<Pr
@Override
public void loadData(DBTaskContext context, ImageCropping data, GenericPreset<PresetProjectSettings> preset) {
if(!preset.data.imagePath.isEmpty()) {
AbstractFileLoader loadingTask = DrawingBotV3.INSTANCE.getImageLoaderTask(context, new File(preset.data.imagePath), false, false);
AbstractFileLoader loadingTask = DrawingBotV3.INSTANCE.getImageLoaderTask(context, new File(preset.data.imagePath), EnumSet.of(FileLoaderFlags.PROJECT_LOADING));
loadingTask.stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == Worker.State.FAILED) {
File initialDirectory = null;
Expand All @@ -224,7 +226,7 @@ public void loadData(DBTaskContext context, ImageCropping data, GenericPreset<Pr
initialDirectory = context.project().getImportDirectory();
}

FXHelper.importFile(context, (file, chooser) -> DrawingBotV3.INSTANCE.openFile(context, file, false, false), initialDirectory, new FileChooser.ExtensionFilter[]{FileUtils.IMPORT_ALL}, "Locate the input image");
FXHelper.importFile(context, (file, chooser) -> DrawingBotV3.INSTANCE.openFile(context, file, EnumSet.of(FileLoaderFlags.PROJECT_LOADING)), initialDirectory, new FileChooser.ExtensionFilter[]{FileUtils.IMPORT_ALL}, "Locate the input image");
}
if(newValue == Worker.State.SUCCEEDED){
ImageData imageData = loadingTask.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import drawingbot.api.Hooks;
import drawingbot.files.FileUtils;
import drawingbot.files.loaders.AbstractFileLoader;
import drawingbot.files.loaders.FileLoaderFlags;
import drawingbot.javafx.FXHelper;
import drawingbot.javafx.GenericPreset;
import drawingbot.javafx.observables.ObservableDrawingSet;
Expand All @@ -22,6 +23,7 @@
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.UUID;

/**
Expand Down Expand Up @@ -150,10 +152,10 @@ public static void applyPreset(ObservableProject project, GenericPreset<PresetPr
project.openImage.set(null);
project.context.taskManager.setCurrentDrawing(null);
if(!presetData.imagePath.isEmpty()) {
AbstractFileLoader loadingTask = DrawingBotV3.INSTANCE.getImageLoaderTask(DrawingBotV3.context(), new File(presetData.imagePath), false, true);
AbstractFileLoader loadingTask = DrawingBotV3.INSTANCE.getImageLoaderTask(DrawingBotV3.context(), new File(presetData.imagePath), EnumSet.of(FileLoaderFlags.PROJECT_LOADING, FileLoaderFlags.SUB_TASK));
loadingTask.stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == Worker.State.FAILED) {
FXHelper.importFile(project.context, (file, chooser) -> DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), file, false, true), new FileChooser.ExtensionFilter[]{FileUtils.IMPORT_IMAGES}, "Locate the input image");
FXHelper.importFile(project.context, (file, chooser) -> DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), file, EnumSet.of(FileLoaderFlags.PROJECT_LOADING, FileLoaderFlags.SUB_TASK)), new FileChooser.ExtensionFilter[]{FileUtils.IMPORT_IMAGES}, "Locate the input image");
}
});
DrawingBotV3.INSTANCE.taskMonitor.queueTask(loadingTask);
Expand All @@ -166,11 +168,11 @@ public static void applyPreset(ObservableProject project, GenericPreset<PresetPr
project.openImage.set(null);
project.context.taskManager.setCurrentDrawing(null);
Platform.runLater(() -> {
AbstractFileLoader loadingTask = DrawingBotV3.INSTANCE.getImageLoaderTask(DrawingBotV3.context(), new File(presetData.imagePath), false, true);
AbstractFileLoader loadingTask = DrawingBotV3.INSTANCE.getImageLoaderTask(DrawingBotV3.context(), new File(presetData.imagePath), EnumSet.of(FileLoaderFlags.PROJECT_LOADING, FileLoaderFlags.SUB_TASK));
loadingTask.stateProperty().addListener((observable, oldValue, newValue) -> {
if (newValue == Worker.State.FAILED) {
FXHelper.importFile(project.context, (file, chooser) -> {
DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), file, false, true);
DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), file, EnumSet.of(FileLoaderFlags.PROJECT_LOADING, FileLoaderFlags.SUB_TASK));
DrawingBotV3.INSTANCE.taskService.submit(() -> Hooks.runHook(Hooks.DESERIALIZE_DRAWING_STATE, project.context, presetData.drawingState));
}, new FileChooser.ExtensionFilter[]{FileUtils.IMPORT_IMAGES}, "Locate the input image");
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/drawingbot/files/loaders/AbstractFileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
import drawingbot.utils.DBTask;

import java.io.File;
import java.util.Set;

/**
* Some file loaders may return null FilteredImageData, e.g. Project Loaders
*/
public abstract class AbstractFileLoader extends DBTask<ImageData> {

public File file;
public boolean internal;
public boolean isSubTask;
public Set<FileLoaderFlags> flags;

public AbstractFileLoader(DBTaskContext context, File file, boolean internal, boolean isSubTask){
public AbstractFileLoader(DBTaskContext context, File file, Set<FileLoaderFlags> flags){
super(context);
this.file = file;
this.internal = internal;
this.isSubTask = isSubTask;
this.flags = flags;
}

public abstract boolean hasImageData();
Expand All @@ -31,7 +30,7 @@ public AbstractFileLoader(DBTaskContext context, File file, boolean internal, bo
* Called after the image data has been loaded into DrawingBotV3, allowing the loader to run additional steps
*/
public void onFileLoaded(){
if(!isSubTask){
if(!flags.contains(FileLoaderFlags.SUB_TASK)){
NotificationOverlays.INSTANCE.showWithSubtitle("Loaded " + getFileTypeDisplayName(), file.toString());
}
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/drawingbot/files/loaders/FileLoaderFlags.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package drawingbot.files.loaders;

public enum FileLoaderFlags {

/**
* The file loader is a sub task of another task, e.g. a project reloading task, or batch processing task
*/
SUB_TASK,
/**
* The file should be loaded from inside the applications resources directory
*/
INTERNAL_FILE,

/**
* The file is being loaded during a project reload, and should not alter the state of the project
*/
PROJECT_LOADING;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import drawingbot.files.json.projects.DBTaskContext;

import java.io.File;
import java.util.Set;

public interface IFileLoaderFactory {

Expand All @@ -12,10 +13,9 @@ public interface IFileLoaderFactory {
*
* @param context the task context
* @param file the file to load
* @param internal true if the file is located within the DrawingBotV3 jar.
* @param isSubTask true if the loader is being run as a Sub Task of another task e.g. Batch Processing
* @param flags flags to indicate the context of the file loader
* @return the file loader instance, or null if this file loader can't load the file
*/
AbstractFileLoader createLoader(DBTaskContext context, File file, boolean internal, boolean isSubTask);
AbstractFileLoader createLoader(DBTaskContext context, File file, Set<FileLoaderFlags> flags);

}
7 changes: 4 additions & 3 deletions src/main/java/drawingbot/files/loaders/ImageFileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import javax.imageio.ImageReader;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Set;

public class ImageFileLoader extends AbstractFileLoader {

public ImageFileLoader(DBTaskContext context, File file, boolean internal, boolean isSubTask) {
super(context, file, internal, isSubTask);
public ImageFileLoader(DBTaskContext context, File file, Set<FileLoaderFlags> flags) {
super(context, file, flags);
}

@Override
Expand All @@ -26,7 +27,7 @@ protected ImageData call() throws Exception {
updateTitle("Importing Image: " + file.toString());

updateMessage("Loading");
BufferedImage source = BufferedImageLoader.loadImage(file.toString(), internal);
BufferedImage source = BufferedImageLoader.loadImage(file.toString(), flags.contains(FileLoaderFlags.INTERNAL_FILE));


updateMessage("Finished");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import drawingbot.files.json.projects.DBTaskContext;

import java.io.File;
import java.util.Set;

public class ImageFileLoaderFactory implements IFileLoaderFactory{

Expand All @@ -11,8 +12,8 @@ public String getName() {
}

@Override
public AbstractFileLoader createLoader(DBTaskContext context, File file, boolean internal, boolean isSubTask) {
return new ImageFileLoader(context, file, internal, isSubTask);
public AbstractFileLoader createLoader(DBTaskContext context, File file, Set<FileLoaderFlags> flags) {
return new ImageFileLoader(context, file, flags);
}

}
5 changes: 3 additions & 2 deletions src/main/java/drawingbot/files/loaders/ProjectFileLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
import javafx.application.Platform;

import java.io.File;
import java.util.Set;

public class ProjectFileLoader extends AbstractFileLoader{

public ProjectFileLoader(DBTaskContext context, File file, boolean internal, boolean isSubTask) {
super(context, file, internal, isSubTask);
public ProjectFileLoader(DBTaskContext context, File file, Set<FileLoaderFlags> flags) {
super(context, file, flags);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import drawingbot.files.json.projects.DBTaskContext;

import java.io.File;
import java.util.Set;

public class ProjectFileLoaderFactory implements IFileLoaderFactory{

Expand All @@ -13,10 +14,10 @@ public String getName() {
}

@Override
public AbstractFileLoader createLoader(DBTaskContext context, File file, boolean internal, boolean isSubTask) {
public AbstractFileLoader createLoader(DBTaskContext context, File file, Set<FileLoaderFlags> flags) {
String extension = FileUtils.getExtension(file.toString());
if(extension.equalsIgnoreCase(".drawingbotv3")) {
return new ProjectFileLoader(context, file, internal, isSubTask);
return new ProjectFileLoader(context, file, flags);
}
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/drawingbot/javafx/FXController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import drawingbot.files.UpdateChecker;
import drawingbot.files.json.JsonData;
import drawingbot.files.json.projects.ObservableProject;
import drawingbot.files.loaders.FileLoaderFlags;
import drawingbot.image.blend.EnumBlendMode;
import drawingbot.javafx.controllers.*;
import drawingbot.javafx.controls.ContextMenuObservableProject;
Expand Down Expand Up @@ -740,7 +741,7 @@ public void initViewport(){
boolean success = false;
if(db.hasFiles()){
List<File> files = db.getFiles();
DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), files.get(0), false, false);
DrawingBotV3.INSTANCE.openFile(DrawingBotV3.context(), files.get(0), EnumSet.noneOf(FileLoaderFlags.class));
success = true;
}

Expand Down
Loading

0 comments on commit 98d65ef

Please sign in to comment.