Skip to content

Commit

Permalink
Implement image source modification
Browse files Browse the repository at this point in the history
Fix: #68
  • Loading branch information
io7m committed Sep 28, 2024
1 parent 9e9e89f commit 7489c10
Show file tree
Hide file tree
Showing 11 changed files with 377 additions and 3 deletions.
3 changes: 2 additions & 1 deletion checkstyle-filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
<!-- Seems unavoidable. -->
<suppress files="LFileModel.java"
checks="ClassDataAbstractionCoupling"/>
<!-- Seems unavoidable. -->
<suppress files="LFileModel.java"
checks="MethodCount"/>
<suppress files="LFileModel.java"
checks="FileLength"/>

</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,18 @@ CompletableFuture<?> captionsPaste(
*/

void exportClear();

/**
* Change the source of an image.
*
* @param image The image
* @param source The source
*
* @return The operation in progress
*/

CompletableFuture<?> imageSourceSet(
LImageID image,
URI source
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* Copyright © 2024 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.filemodel.internal;

import com.io7m.laurel.model.LException;
import org.jooq.DSLContext;

import java.util.Properties;

import static com.io7m.laurel.filemodel.internal.Tables.IMAGES;

/**
* Set an image source.
*/

public final class LCommandImageSourceSet
extends LCommandAbstract<LImageSourceSet>
{
private SavedData savedData;

private record SavedData(
long savedImageId,
String savedOldURI,
String savedNewURI)
{

}

/**
* Set an image source.
*/

public LCommandImageSourceSet()
{

}

/**
* @return A command factory
*/

public static LCommandFactoryType<LImageSourceSet> provider()
{
return new LCommandFactory<>(
LCommandImageSourceSet.class.getCanonicalName(),
LCommandImageSourceSet::fromProperties
);
}

private static LCommandImageSourceSet fromProperties(
final Properties p)
{
final var c = new LCommandImageSourceSet();

c.savedData = new SavedData(
Long.parseUnsignedLong(p.getProperty("image")),
p.getProperty("oldURI"),
p.getProperty("newURI")
);

c.setExecuted(true);
return c;
}

@Override
protected LCommandUndoable onExecute(
final LFileModel model,
final LDatabaseTransactionType transaction,
final LImageSourceSet request)
throws LException
{
final var context =
transaction.get(DSLContext.class);

final var old =
context.select(IMAGES.IMAGE_SOURCE)
.from(IMAGES)
.where(IMAGES.IMAGE_ID.eq(request.image().value()))
.fetchOne(IMAGES.IMAGE_SOURCE);

final var updated =
context.update(IMAGES)
.set(IMAGES.IMAGE_SOURCE, request.source().toString())
.where(IMAGES.IMAGE_ID.eq(request.image().value()))
.execute();

if (updated == 0) {
return LCommandUndoable.COMMAND_NOT_UNDOABLE;
}

this.savedData = new SavedData(
request.image().value(),
old,
request.source().toString()
);

model.setImagesAll(LCommandModelUpdates.listImages(context));
return LCommandUndoable.COMMAND_UNDOABLE;
}


@Override
protected void onUndo(
final LFileModel model,
final LDatabaseTransactionType transaction)
{
final var context =
transaction.get(DSLContext.class);

context.update(IMAGES)
.set(IMAGES.IMAGE_SOURCE, this.savedData.savedOldURI)
.where(IMAGES.IMAGE_ID.eq(this.savedData.savedImageId))
.execute();

model.setImagesAll(LCommandModelUpdates.listImages(context));
}

@Override
protected void onRedo(
final LFileModel model,
final LDatabaseTransactionType transaction)
{
final var context =
transaction.get(DSLContext.class);

context.update(IMAGES)
.set(IMAGES.IMAGE_SOURCE, this.savedData.savedNewURI)
.where(IMAGES.IMAGE_ID.eq(this.savedData.savedImageId))
.execute();

model.setImagesAll(LCommandModelUpdates.listImages(context));
}

@Override
public Properties toProperties()
{
final var p = new Properties();
p.setProperty("image", Long.toUnsignedString(this.savedData.savedImageId));
p.setProperty("oldURI", this.savedData.savedOldURI);
p.setProperty("newURI", this.savedData.savedNewURI);
return p;
}

@Override
public String describe()
{
return "Set image source";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,20 @@ public void exportClear()
this.exportEvents.set(List.of());
}

@Override
public CompletableFuture<?> imageSourceSet(
final LImageID image,
final URI source)
{
Objects.requireNonNull(image, "image");
Objects.requireNonNull(source, "source");

return this.runCommand(
new LCommandImageSourceSet(),
new LImageSourceSet(image, source)
);
}

private Optional<InputStream> executeImageStream(
final LImageID id)
throws LException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright © 2024 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.filemodel.internal;

import com.io7m.laurel.model.LImageID;

import java.net.URI;
import java.util.Objects;

/**
* A request to set a source for an image.
*
* @param image The image
* @param source The source
*/

public record LImageSourceSet(
LImageID image,
URI source)
{
/**
* A request to set a source for an image.
*
* @param image The image
* @param source The source
*/

public LImageSourceSet
{
Objects.requireNonNull(image, "image");
Objects.requireNonNull(source, "source");
}
}
2 changes: 2 additions & 0 deletions com.io7m.laurel.filemodel/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.io7m.laurel.filemodel.internal.LCommandImageCaptionsAssign;
import com.io7m.laurel.filemodel.internal.LCommandImageCaptionsUnassign;
import com.io7m.laurel.filemodel.internal.LCommandImageSelect;
import com.io7m.laurel.filemodel.internal.LCommandImageSourceSet;
import com.io7m.laurel.filemodel.internal.LCommandImagesAdd;
import com.io7m.laurel.filemodel.internal.LCommandImagesDelete;
import com.io7m.laurel.filemodel.internal.LCommandMetadataPut;
Expand Down Expand Up @@ -85,6 +86,7 @@
LCommandImageCaptionsAssign,
LCommandImageCaptionsUnassign,
LCommandImageSelect,
LCommandImageSourceSet,
LCommandImagesAdd,
LCommandImagesDelete,
LCommandMetadataPut,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputDialog;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
Expand All @@ -52,13 +53,15 @@

import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import static com.io7m.laurel.gui.internal.LStringConstants.IMAGES_SOURCE_SET;
import static javafx.stage.Modality.APPLICATION_MODAL;

/**
Expand Down Expand Up @@ -96,6 +99,8 @@ public final class LCaptionsView extends LAbstractViewWithModel
@FXML private MenuItem assignedCaptionsContextMenuCopy;
@FXML private MenuItem assignedCaptionsContextMenuPaste;
@FXML private MenuItem imagesCompareCaptions;
@FXML private TextField imageSource;
@FXML private Button imageSourceButton;

private Stage imageDisplayWindow;
private LImageView imageDisplay;
Expand Down Expand Up @@ -145,6 +150,7 @@ protected void onInitialize()
this.imageDelete.setDisable(true);
this.imageCaptionAssign.setDisable(true);
this.imageCaptionUnassign.setDisable(true);
this.imageSourceButton.setDisable(true);
this.captionNew.setDisable(false);
this.captionDelete.setDisable(true);
this.captionModify.setDisable(true);
Expand Down Expand Up @@ -413,10 +419,19 @@ private void onImageSelected(
fileModel.imageSelect(Optional.empty());
this.imageView.setImage(null);
this.imageDelete.setDisable(true);
this.imageSourceButton.setDisable(true);
this.imageSource.setText("");
return;
}

this.imageDelete.setDisable(false);
this.imageSourceButton.setDisable(false);
this.imageSource.setText(
image.image()
.source()
.map(URI::toString)
.orElse("")
);

fileModel.imageSelect(Optional.of(image.id()));
fileModel.imageStream(image.id())
Expand Down Expand Up @@ -723,4 +738,24 @@ private void onCaptionsCompareSelected()

this.comparisons.open(this.services, this.fileModelScope());
}

@FXML
private void onImageSetSourceSelected()
{
final var dialog = new TextInputDialog();
LCSS.setCSS(dialog.getDialogPane());

dialog.getEditor().setText(this.imageSource.getText());
dialog.setHeaderText(this.strings.format(IMAGES_SOURCE_SET));

final var r = dialog.showAndWait();
if (r.isPresent()) {
final var text = URI.create(r.get());
final var fileModel = this.fileModelNow();
fileModel.imageSourceSet(
fileModel.imageSelected().get().orElseThrow().id(),
text
);
}
}
}
Loading

0 comments on commit 7489c10

Please sign in to comment.