From e43e10cef835f69c06d710e93922b2082c512c7b Mon Sep 17 00:00:00 2001 From: Jordan Martinez Date: Tue, 24 Jan 2017 22:39:19 -0800 Subject: [PATCH] Update custom object example to use interface --- .../demo/richtext/EmptyLinkedImage.java | 26 ++++++ .../richtext/demo/richtext/LinkedImage.java | 84 ++++--------------- .../demo/richtext/LinkedImageOps.java | 2 +- .../demo/richtext/RealLinkedImage.java | 68 +++++++++++++++ .../richtext/demo/richtext/RichText.java | 2 +- 5 files changed, 114 insertions(+), 68 deletions(-) create mode 100644 richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/EmptyLinkedImage.java create mode 100644 richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RealLinkedImage.java diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/EmptyLinkedImage.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/EmptyLinkedImage.java new file mode 100644 index 000000000..3403cda0a --- /dev/null +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/EmptyLinkedImage.java @@ -0,0 +1,26 @@ +package org.fxmisc.richtext.demo.richtext; + +import javafx.scene.Node; + +public class EmptyLinkedImage implements LinkedImage { + + @Override + public LinkedImage setStyle(S style) { + return this; + } + + @Override + public S getStyle() { + return null; + } + + @Override + public String getImagePath() { + return ""; + } + + @Override + public Node createNode() { + throw new AssertionError("Unreachable code"); + } +} diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImage.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImage.java index 4a3155a98..f5857cbc7 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImage.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImage.java @@ -1,25 +1,14 @@ package org.fxmisc.richtext.demo.richtext; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.File; -import java.io.IOException; - import javafx.scene.Node; -import javafx.scene.image.Image; -import javafx.scene.image.ImageView; - import org.fxmisc.richtext.model.Codec; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; -/** - * A custom object which contains a file path to an image file. - * When rendered in the rich text editor, the image is loaded from the - * specified file. - */ -public class LinkedImage { - - public static Codec> codec(Codec styleCodec) { +public interface LinkedImage { + static Codec> codec(Codec styleCodec) { return new Codec>() { @Override @@ -29,72 +18,35 @@ public String getName() { @Override public void encode(DataOutputStream os, LinkedImage i) throws IOException { - // external path rep should use forward slashes only - String externalPath = i.imagePath.replace("\\", "/"); - Codec.STRING_CODEC.encode(os, externalPath); - styleCodec.encode(os, i.style); + // don't encode EmptyLinkedImage objects + if (i.getStyle() != null) { + // external path rep should use forward slashes only + String externalPath = i.getImagePath().replace("\\", "/"); + Codec.STRING_CODEC.encode(os, externalPath); + styleCodec.encode(os, i.getStyle()); + } } @Override - public LinkedImage decode(DataInputStream is) throws IOException { + public RealLinkedImage decode(DataInputStream is) throws IOException { // Sanitize path - make sure that forward slashes only are used String imagePath = Codec.STRING_CODEC.decode(is); imagePath = imagePath.replace("\\", "/"); S style = styleCodec.decode(is); - return new LinkedImage<>(imagePath, style); + return new RealLinkedImage<>(imagePath, style); } }; } - private final String imagePath; - private final S style; - - /** - * Creates a new linked image object. - * - * @param imagePath The path to the image file. - * @param style The text style to apply to the corresponding segment. - */ - public LinkedImage(String imagePath, S style) { - - // if the image is below the current working directory, - // then store as relative path name. - String currentDir = System.getProperty("user.dir") + File.separatorChar; - if (imagePath.startsWith(currentDir)) { - imagePath = imagePath.substring(currentDir.length()); - } - - this.imagePath = imagePath; - this.style = style; - } - - public LinkedImage setStyle(S style) { - return new LinkedImage<>(imagePath, style); - } + LinkedImage setStyle(S style); + S getStyle(); /** * @return The path of the image to render. */ - public String getImagePath() { - return imagePath; - } + String getImagePath(); - public S getStyle() { - return style; - } - - - @Override - public String toString() { - return String.format("LinkedImage[path=%s]", imagePath); - } - - public Node createNode() { - Image image = new Image("file:" + imagePath); // XXX: No need to create new Image objects each time - - // could be cached in the model layer - ImageView result = new ImageView(image); - return result; - } + Node createNode(); } diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImageOps.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImageOps.java index c286c14ef..65798b4fc 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImageOps.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImageOps.java @@ -6,7 +6,7 @@ public class LinkedImageOps implements SegmentOps, S> { - private final LinkedImage emptySeg = new LinkedImage<>("", null); + private final EmptyLinkedImage emptySeg = new EmptyLinkedImage<>(); @Override public int length(LinkedImage seg) { diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RealLinkedImage.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RealLinkedImage.java new file mode 100644 index 000000000..5b1d6fc75 --- /dev/null +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RealLinkedImage.java @@ -0,0 +1,68 @@ +package org.fxmisc.richtext.demo.richtext; + +import java.io.File; + +import javafx.scene.Node; +import javafx.scene.image.Image; +import javafx.scene.image.ImageView; + + +/** + * A custom object which contains a file path to an image file. + * When rendered in the rich text editor, the image is loaded from the + * specified file. + */ +public class RealLinkedImage implements LinkedImage { + + private final String imagePath; + private final S style; + + /** + * Creates a new linked image object. + * + * @param imagePath The path to the image file. + * @param style The text style to apply to the corresponding segment. + */ + public RealLinkedImage(String imagePath, S style) { + + // if the image is below the current working directory, + // then store as relative path name. + String currentDir = System.getProperty("user.dir") + File.separatorChar; + if (imagePath.startsWith(currentDir)) { + imagePath = imagePath.substring(currentDir.length()); + } + + this.imagePath = imagePath; + this.style = style; + } + + @Override + public RealLinkedImage setStyle(S style) { + return new RealLinkedImage<>(imagePath, style); + } + + + @Override + public String getImagePath() { + return imagePath; + } + + @Override + public S getStyle() { + return style; + } + + + @Override + public String toString() { + return String.format("RealLinkedImage[path=%s]", imagePath); + } + + @Override + public Node createNode() { + Image image = new Image("file:" + imagePath); // XXX: No need to create new Image objects each time - + // could be cached in the model layer + ImageView result = new ImageView(image); + return result; + } +} diff --git a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichText.java b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichText.java index 7341ec050..31ed2646c 100644 --- a/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichText.java +++ b/richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/RichText.java @@ -439,7 +439,7 @@ private void insertImage() { String imagePath = selectedFile.getAbsolutePath(); imagePath = imagePath.replace('\\', '/'); ReadOnlyStyledDocument, LinkedImage>, TextStyle> ros = - ReadOnlyStyledDocument.fromSegment(Either.right(new LinkedImage<>(imagePath, TextStyle.EMPTY)), + ReadOnlyStyledDocument.fromSegment(Either.right(new RealLinkedImage<>(imagePath, TextStyle.EMPTY)), ParStyle.EMPTY, TextStyle.EMPTY, area.getSegOps()); area.replaceSelection(ros); }