Skip to content

Commit

Permalink
Add non-working merge functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Jun 5, 2024
1 parent 29478d3 commit 1afb0cb
Show file tree
Hide file tree
Showing 20 changed files with 1,767 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ public void start(
final var exporters = new LExporterDialogs(services, strings);
services.register(LExporterDialogs.class, exporters);

final var mergers = new LMergeDialogs(services, strings);
services.register(LMergeDialogs.class, mergers);

final var choosers = new LFileChoosers();
services.register(LFileChoosers.class, choosers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.io7m.anethum.api.ParseStatusType;
import com.io7m.anethum.api.ParsingException;
import com.io7m.jdeferthrow.core.ExceptionTracker;
import com.io7m.laurel.gui.internal.model.LMCaption;
import com.io7m.laurel.gui.internal.model.LMImage;
import com.io7m.laurel.gui.internal.model.LMImageCreate;
Expand All @@ -39,6 +40,7 @@
import com.io7m.laurel.gui.internal.model.LModelType;
import com.io7m.laurel.io.LExportRequest;
import com.io7m.laurel.io.LExporters;
import com.io7m.laurel.io.LImageSets;
import com.io7m.laurel.io.LImporters;
import com.io7m.laurel.io.LParsers;
import com.io7m.laurel.io.LSerializers;
Expand All @@ -61,6 +63,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.LinkedList;
Expand Down Expand Up @@ -748,6 +751,81 @@ public void captionsAssignedPaste()
);
}

@Override
public CompletableFuture<Object> merge(
final List<Path> files)
{
Objects.requireNonNull(files, "files");

this.attributes.clear();

for (int index = 0; index < files.size(); ++index) {
this.attributes.put(
"File [%d]".formatted(Integer.valueOf(index)),
files.get(index).toString()
);
}

this.busySet(true);

final var future = new CompletableFuture<>();
future.whenComplete((ignored0, ignored1) -> {
LOG.debug("Merge completed.");
Platform.runLater(() -> this.busySet(false));
});

Thread.ofVirtual()
.start(() -> {
final var exceptions =
new ExceptionTracker<Exception>();

final var imageSets =
new ArrayList<LImageSet>(files.size());

for (final var file : files) {
try {
LOG.info("Open: {}", file);
imageSets.add(PARSERS.parseFile(file));
} catch (final ParsingException e) {
LOG.error("", e);
this.publishParseErrors(e.statusValues());
this.publishError(e);
exceptions.addException(e);
} catch (final Throwable e) {
LOG.error("", e);
this.publishError(e);
exceptions.addException(new Exception(e));
}
}

try {
exceptions.throwIfNecessary();
} catch (final Exception e) {
future.completeExceptionally(e);
return;
}

final var merged =
LImageSets.mergeAll(imageSets);

Platform.runLater(() -> {
try {
this.model.replaceWith(
Paths.get(""),
merged
);
future.complete(null);
} catch (final Exception e) {
LOG.error("", e);
this.publishError(e);
future.completeExceptionally(e);
}
});
});

return future;
}

@Override
public ReadOnlyProperty<LModelFileStatusType> fileStatus()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,4 +335,15 @@ void imagesDelete(
*/

void captionsAssignedPaste();

/**
* Merge the contents of the given files.
*
* @param files The files
*
* @return The operation in progress
*/

CompletableFuture<Object> merge(
List<Path> files);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;

Expand All @@ -61,6 +63,7 @@ public final class LMainView implements LScreenViewType
private final Stage stage;
private final LExporterDialogs exporterDialogs;
private final LPreferencesType preferences;
private final LMergeDialogs mergeDialogs;

private @FXML Parent root;
private @FXML MenuItem menuItemNew;
Expand Down Expand Up @@ -96,6 +99,8 @@ public LMainView(
services.requireService(LErrorDialogs.class);
this.exporterDialogs =
services.requireService(LExporterDialogs.class);
this.mergeDialogs =
services.requireService(LMergeDialogs.class);
this.preferences =
services.requireService(LPreferencesType.class);

Expand Down Expand Up @@ -601,4 +606,40 @@ public void onAboutSelected()
{
LAbout.open(this.strings);
}

/**
* The user tried to merge.
*/

@FXML
public void onMergeSelected()
{
if (this.controller.isSaved()) {
this.controller.closeSet();
this.tryMerge();
return;
}

switch (this.onConfirmUnsaved()) {
case CANCEL -> {
return;
}
case DISCARD -> {
this.controller.closeSet();
this.tryMerge();
return;
}
case SAVE -> {
this.controller.save();
this.controller.closeSet();
this.tryMerge();
return;
}
}
}

private void tryMerge()
{
this.mergeDialogs.open();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright © 2023 Mark Raynsford <[email protected]> https://www.io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/


package com.io7m.laurel.gui.internal;

import com.io7m.repetoir.core.RPServiceDirectoryType;
import com.io7m.repetoir.core.RPServiceType;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Objects;

import static javafx.stage.Modality.APPLICATION_MODAL;

/**
* A service for creating merge dialogs.
*/

public final class LMergeDialogs implements RPServiceType
{
private final LStrings strings;
private final RPServiceDirectoryType services;

/**
* A service for creating merge dialogs.
*
* @param inServices The services
* @param inStrings The string resources
*/

public LMergeDialogs(
final RPServiceDirectoryType inServices,
final LStrings inStrings)
{
this.services =
Objects.requireNonNull(inServices, "services");
this.strings =
Objects.requireNonNull(inStrings, "inStrings");
}

/**
* Open a dialog.
*
* @return The dialog
*/

public LMergeView open()
{
try {
final var stage = new Stage();

final var layout =
LMergeDialogs.class.getResource(
"/com/io7m/laurel/gui/internal/merge.fxml");

Objects.requireNonNull(layout, "layout");

final var loader =
new FXMLLoader(layout, this.strings.resources());

final var merger = new LMergeView(this.services, stage);
loader.setControllerFactory(param -> {
return merger;
});

final Pane pane = loader.load();
LCSS.setCSS(pane);

stage.initModality(APPLICATION_MODAL);
stage.setTitle(this.strings.format("mergeTitle"));
stage.setWidth(640.0);
stage.setHeight(480.0);
stage.setMinWidth(640.0);
stage.setMinHeight(480.0);
stage.setScene(new Scene(pane));
stage.showAndWait();

return merger;
} catch (final IOException e) {
throw new UncheckedIOException(e);
}
}

@Override
public String toString()
{
return String.format(
"[LMergeDialogs 0x%08x]",
Integer.valueOf(this.hashCode())
);
}

@Override
public String description()
{
return "Merge dialog service";
}
}
Loading

0 comments on commit 1afb0cb

Please sign in to comment.