|
| 1 | +package org.fxmisc.richtext.demo.richtext; |
| 2 | + |
| 3 | +import java.io.DataInputStream; |
| 4 | +import java.io.DataOutputStream; |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +import javafx.scene.Node; |
| 9 | +import javafx.scene.image.Image; |
| 10 | +import javafx.scene.image.ImageView; |
| 11 | + |
| 12 | +import org.fxmisc.richtext.model.Codec; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * A custom object which contains a file path to an image file. |
| 17 | + * When rendered in the rich text editor, the image is loaded from the |
| 18 | + * specified file. |
| 19 | + */ |
| 20 | +public class LinkedImage<S> { |
| 21 | + |
| 22 | + public static <S> Codec<LinkedImage<S>> codec(Codec<S> styleCodec) { |
| 23 | + return new Codec<LinkedImage<S>>() { |
| 24 | + |
| 25 | + @Override |
| 26 | + public String getName() { |
| 27 | + return "LinkedImage<" + styleCodec.getName() + ">"; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public void encode(DataOutputStream os, LinkedImage<S> i) throws IOException { |
| 32 | + // external path rep should use forward slashes only |
| 33 | + String externalPath = i.imagePath.replace("\\", "/"); |
| 34 | + Codec.STRING_CODEC.encode(os, externalPath); |
| 35 | + styleCodec.encode(os, i.style); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public LinkedImage<S> decode(DataInputStream is) throws IOException { |
| 40 | + // Sanitize path - make sure that forward slashes only are used |
| 41 | + String imagePath = Codec.STRING_CODEC.decode(is); |
| 42 | + imagePath = imagePath.replace("\\", "/"); |
| 43 | + S style = styleCodec.decode(is); |
| 44 | + return new LinkedImage<>(imagePath, style); |
| 45 | + } |
| 46 | + |
| 47 | + }; |
| 48 | + } |
| 49 | + |
| 50 | + private final String imagePath; |
| 51 | + private final S style; |
| 52 | + |
| 53 | + /** |
| 54 | + * Creates a new linked image object. |
| 55 | + * |
| 56 | + * @param imagePath The path to the image file. |
| 57 | + * @param style The text style to apply to the corresponding segment. |
| 58 | + */ |
| 59 | + public LinkedImage(String imagePath, S style) { |
| 60 | + |
| 61 | + // if the image is below the current working directory, |
| 62 | + // then store as relative path name. |
| 63 | + String currentDir = System.getProperty("user.dir") + File.separatorChar; |
| 64 | + if (imagePath.startsWith(currentDir)) { |
| 65 | + imagePath = imagePath.substring(currentDir.length()); |
| 66 | + } |
| 67 | + |
| 68 | + this.imagePath = imagePath; |
| 69 | + this.style = style; |
| 70 | + } |
| 71 | + |
| 72 | + public LinkedImage<S> setStyle(S style) { |
| 73 | + return new LinkedImage<>(imagePath, style); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * @return The path of the image to render. |
| 79 | + */ |
| 80 | + public String getImagePath() { |
| 81 | + return imagePath; |
| 82 | + } |
| 83 | + |
| 84 | + public S getStyle() { |
| 85 | + return style; |
| 86 | + } |
| 87 | + |
| 88 | + |
| 89 | + @Override |
| 90 | + public String toString() { |
| 91 | + return String.format("LinkedImage[path=%s]", imagePath); |
| 92 | + } |
| 93 | + |
| 94 | + public Node createNode() { |
| 95 | + Image image = new Image("file:" + imagePath); // XXX: No need to create new Image objects each time - |
| 96 | + // could be cached in the model layer |
| 97 | + ImageView result = new ImageView(image); |
| 98 | + return result; |
| 99 | + } |
| 100 | +} |
0 commit comments