Comments (8)
@Jugen Yes, this is what I was looking for. Thank you very much for your time and help!
from richtextfx.
There are two cases for when to use StyleSpans:
setStyleClass
will erase & replace the current styling of the specified range, whilesetStyleSpans
will add the provided styling to any existing styling.- Every
setStyleClass
andsetStyleSpans
invocation is a single change event to the text which triggers a redraw for each change. If however you want to bundle multiple style changes into one undo event or optimize the amount of redrawing then you can bundle a group of StyleSpans together usingStyleSpansBuilder
and submit them in one go withsetStyleSpans
. Note that a series of StyleSpans are consecutive, that is the next StyleSpan position starts where the last one ended. So if you need to change separate sections of text in one go, you can insert an empty StyleSpan with the appropriate length to "move the change cursor" to the next change position.
from richtextfx.
@Jugen Could you say, what is the best way to remove style spans? For example, I have two layers of spans - the first one for highlighting and the second one for spell check:
Both layers where built using builder that's why there are empty spans in both of them. Now I have two types of task:
- I need to remove all spans from the first layer (for highlighting).
- I need to remove only one style span for highlighting?
How to do taking into consideration that text can be rather long? Or I understand something wrong?
from richtextfx.
So you would have to get the style spans for the section of text that you are interested in, make changes to the style spans as appropriate, and then apply the changed style spans back. Something like:
private void changeStyle( IndexRange range )
{
if ( range.getLength() > 0 )
{
var newStyles = new StyleSpansBuilder<String>();
for ( var span : area.getStyleSpans( range ) )
{
var style = span.getStyle();
// Add or Remove styles as appropriate
newStyles.add( style, span.getLength() );
}
area.setStyleSpans( range.getStart(), newStyles.create() );
}
}
You can also try going from style span to style, something like:
private void changeParagraphStyle( int p )
{
var para = area.getParagraphs.get(p);
var spanStart = 0;
for ( var span : para.getStyleSpans() )
{
var style = span.getStyle();
// Add or Remove styles as appropriate
area.setStyle( p, spanStart, spanStart+span.getLength(), style );
spanStart += span.getLength();
}
}
from richtextfx.
@Jugen I finally did it. Thank you for your detailed explanations. But I have an idea. What if we could use keys to distinguish StyleSpan
? For example, something like this:
public class StyleSpanTest extends Application {
private static enum StyleSpanKey {
MISSPELLED, HIGHLIGHTED, BOTH
}
private static class KeyedStyleSpan<S> extends StyleSpan<S> {
private final StyleSpanKey key;
public KeyedStyleSpan(StyleSpanKey key, S style, int length) {
super(style, length);
this.key = key;
}
public StyleSpanKey getKey() {
return key;
}
@Override
public String toString() {
return super.toString() + ", key=" + key;
}
}
@Override
public void start(Stage stage) {
CodeArea codeArea = new CodeArea("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
codeArea.setWrapText(true);
var addButton = new Button("Add");
var removeButton = new Button("Remove");
var listButton = new Button("List");
Scene scene = new Scene(new VBox(codeArea, new HBox(addButton, removeButton, listButton)), 600, 200);
scene.getStylesheets().add(StyleSpanTest.class.getResource("test7.css").toExternalForm());
stage.setScene(scene);
stage.show();
var newStyles = new StyleSpansBuilder<Collection<String>>();
newStyles.add(new KeyedStyleSpan<>(StyleSpanKey.MISSPELLED, Collections.singleton("misspelled"), 5));
codeArea.setStyleSpans(6, newStyles.create());
addButton.setOnAction(e -> {
var onlyHighlightedList = List.of("highlighted");
var sbuilder = new StyleSpansBuilder<Collection<String>>();
for ( var span : codeArea.getStyleSpans(0, 20)){
var styles = span.getStyle();
if (styles.isEmpty()) {
sbuilder.add(new KeyedStyleSpan<Collection<String>>(StyleSpanKey.HIGHLIGHTED, onlyHighlightedList, span.getLength()));
} else {
var newList = new ArrayList<String>(styles);
newList.add("highlighted");
sbuilder.add(new KeyedStyleSpan<Collection<String>>(StyleSpanKey.BOTH, newList, span.getLength()));
}
}
codeArea.setStyleSpans(0, sbuilder.create());
});
listButton.setOnAction(e -> {
for (var span : codeArea.getStyleSpans(0, codeArea.getText().length())){
System.out.println(span);
}
});
}
}
It seems to me it could be useful to work with style spans as with model. To make it work it is necessary to control creating StyleSpan
instances. For example, to add to StyleSpan
method createChild(style, length)
(when RichTextFX splits one keyed span into two).
What do you think? Or this functionality is already supported?
from richtextfx.
Could something in StyleSpans
(ApiDoc) (Code) maybe help you ?
from richtextfx.
@Jugen As I understand StyleSpans
is just a collection. I would like to have three different things:
- I want to give a
StyleSpan
a marker (Object) not to check every time using loops and string equals what the span I am working at. I think it is the easiest thing. Because, it is necessary to add control of splitting existing StyleSpan into two. - I would like to use
MyStyleSpan
class in order to keep in them extra information I need. The problem is that RichTextFX replaces them with its ownStyleSpan
. - I would like to get events on the StyleSpan I added to area.
By other words, I want more control for non empty StyleSpan
s I add. Now you will say that I want too much :)
from richtextfx.
Maybe you should use a custom style class instead, where you can put the extra information etc. Try the the following framework to get started:
public class CustomStyleArea extends StyledTextArea<String,MyStyle>
{
public CustomStyleArea()
{
super( "", CustomStyleArea::styleParagraph, MyStyle.NONE, CustomStyleArea::styleText );
// Style codecs are used for (de)serialization during copy, paste, save, and load
setStyleCodecs( Codec.STRING_CODEC, Codec.styledTextCodec(new MyStyleCodec()) );
}
private static void styleParagraph( TextFlow paragraph, String style )
{
// paragraph.getStyleClass().addAll( styles );
paragraph.setStyle( style );
}
private static void styleText( TextExt text, MyStyle style )
{
// text.getStyleClass().addAll( style.getCssClasses() );
// or
// text.setStyle( style.getCss() );
}
}
class MyStyle
{
public static final MyStyle NONE = new MyStyle();
// TODO Styling info plus any extra data, flags, and methods eg:
// public List<String> getCssClasses()
// public String getCss()
}
/**
* Codec methods are used for (de)serialization during copy, paste, saving, and load.
* Note: you can leave this till last, once you've finished MyStyle.
*/
class MyStyleCodec implements Codec<MyStyle>
{
@Override public String getName()
{
return "richtextfx/mystyle";
}
@Override public void encode( DataOutputStream os, MyStyle t ) throws IOException
{
// TODO
}
@Override public MyStyle decode( DataInputStream is ) throws IOException
{
// TODO
return null;
}
}
from richtextfx.
Related Issues (20)
- Remove null character at end of pasted string
- JDK8 project HOT 1
- Question: is it possible to disable scrolling to bottom on appending text? HOT 2
- Question: How to set background color for current line/paragraph using CSS? HOT 4
- Question: Is possible to detect when INSERT mode is used? HOT 16
- Question: How to make wave underline? HOT 4
- Question: What is the strategy to implement Highlight All for found matches for modifiable text? HOT 5
- Question: Getting font size of the CodeArea HOT 3
- Bug: Observer is not removed from richChanges() HOT 3
- Question: how to get real size of one span at position X? HOT 15
- Code building fails on Ubuntu HOT 19
- How to remove all styles from undo text? HOT 4
- How to convert absolute position to paragraph/column position? HOT 1
- Question: is textArea.getLength() equal to the number of Unicode code points HOT 5
- Bug: text area removes selection on text append HOT 10
- Selected color with alpha add extra colors HOT 3
- How to select text and move caret to the beginning of the selection? HOT 2
- Behavior of setParagraphStyle bas been changed in 0.11 in comparison with 0.10.9 version HOT 1
- Question: How to show whole text when text area is added to scene HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from richtextfx.