Skip to content

Commit 7487403

Browse files
authored
Merge pull request #356 from afester/customObject
Custom object support
2 parents 41b7919 + 578f86e commit 7487403

40 files changed

+2587
-1837
lines changed

richtextfx-demos/sample.png

1.06 KB
Loading

richtextfx-demos/sample.rtfx

1.66 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package org.fxmisc.richtext.demo.richtext;
2+
3+
import java.util.Optional;
4+
5+
import org.fxmisc.richtext.model.SegmentOps;
6+
7+
public class LinkedImageOps<S> implements SegmentOps<LinkedImage<S>, S> {
8+
9+
private final LinkedImage<S> emptySeg = new LinkedImage<>("", null);
10+
11+
@Override
12+
public int length(LinkedImage<S> seg) {
13+
return seg == emptySeg ? 0 : 1;
14+
}
15+
16+
@Override
17+
public char charAt(LinkedImage<S> seg, int index) {
18+
return seg == emptySeg ? '\0' : '\ufffc';
19+
}
20+
21+
@Override
22+
public String getText(LinkedImage<S> seg) {
23+
return seg == emptySeg ? "" : "\ufffc";
24+
}
25+
26+
@Override
27+
public LinkedImage<S> subSequence(LinkedImage<S> seg, int start, int end) {
28+
if (start < 0) {
29+
throw new IllegalArgumentException("Start cannot be negative. Start = " + start);
30+
}
31+
if (end > length(seg)) {
32+
throw new IllegalArgumentException("End cannot be greater than segment's length");
33+
}
34+
return start == 0 && end == 1
35+
? seg
36+
: emptySeg;
37+
}
38+
39+
@Override
40+
public LinkedImage<S> subSequence(LinkedImage<S> seg, int start) {
41+
if (start < 0) {
42+
throw new IllegalArgumentException("Start cannot be negative. Start = " + start);
43+
}
44+
return start == 0
45+
? seg
46+
: emptySeg;
47+
}
48+
49+
@Override
50+
public S getStyle(LinkedImage<S> seg) {
51+
return seg.getStyle();
52+
}
53+
54+
@Override
55+
public LinkedImage<S> setStyle(LinkedImage<S> seg, S style) {
56+
return seg == emptySeg ? emptySeg : seg.setStyle(style);
57+
}
58+
59+
@Override
60+
public Optional<LinkedImage<S>> join(LinkedImage<S> currentSeg, LinkedImage<S> nextSeg) {
61+
return Optional.empty();
62+
}
63+
64+
@Override
65+
public LinkedImage<S> createEmpty() {
66+
return emptySeg;
67+
}
68+
}

0 commit comments

Comments
 (0)