Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added richtextfx-demos/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added richtextfx-demos/sample.rtfx
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
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;


/**
* 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<S> {

public static <S> Codec<LinkedImage<S>> codec(Codec<S> styleCodec) {
return new Codec<LinkedImage<S>>() {

@Override
public String getName() {
return "LinkedImage<" + styleCodec.getName() + ">";
}

@Override
public void encode(DataOutputStream os, LinkedImage<S> 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);
}

@Override
public LinkedImage<S> 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);
}

};
}

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<S> setStyle(S style) {
return new LinkedImage<>(imagePath, style);
}


/**
* @return The path of the image to render.
*/
public String getImagePath() {
return imagePath;
}

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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.fxmisc.richtext.demo.richtext;

import java.util.Optional;

import org.fxmisc.richtext.model.SegmentOps;

public class LinkedImageOps<S> implements SegmentOps<LinkedImage<S>, S> {

private final LinkedImage<S> emptySeg = new LinkedImage<>("", null);

@Override
public int length(LinkedImage<S> seg) {
return seg == emptySeg ? 0 : 1;
}

@Override
public char charAt(LinkedImage<S> seg, int index) {
return seg == emptySeg ? '\0' : '\ufffc';
}

@Override
public String getText(LinkedImage<S> seg) {
return seg == emptySeg ? "" : "\ufffc";
}

@Override
public LinkedImage<S> subSequence(LinkedImage<S> seg, int start, int end) {
if (start < 0) {
throw new IllegalArgumentException("Start cannot be negative. Start = " + start);
}
if (end > length(seg)) {
throw new IllegalArgumentException("End cannot be greater than segment's length");
}
return start == 0 && end == 1
? seg
: emptySeg;
}

@Override
public LinkedImage<S> subSequence(LinkedImage<S> seg, int start) {
if (start < 0) {
throw new IllegalArgumentException("Start cannot be negative. Start = " + start);
}
return start == 0
? seg
: emptySeg;
}

@Override
public S getStyle(LinkedImage<S> seg) {
return seg.getStyle();
}

@Override
public LinkedImage<S> setStyle(LinkedImage<S> seg, S style) {
return seg == emptySeg ? emptySeg : seg.setStyle(style);
}

@Override
public Optional<LinkedImage<S>> join(LinkedImage<S> currentSeg, LinkedImage<S> nextSeg) {
return Optional.empty();
}

@Override
public LinkedImage<S> createEmpty() {
return emptySeg;
}
}
Loading