-
Notifications
You must be signed in to change notification settings - Fork 236
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #356 from afester/customObject
Custom object support
- Loading branch information
Showing
40 changed files
with
2,587 additions
and
1,837 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
100 changes: 100 additions & 0 deletions
100
richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/richtext/LinkedImageOps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.