-
Notifications
You must be signed in to change notification settings - Fork 236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: when to use StyleSpans? #1229
Comments
There are two cases for when to use StyleSpans:
|
@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:
How to do taking into consideration that text can be rather long? Or I understand something wrong? |
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();
}
} |
@Jugen I finally did it. Thank you for your detailed explanations. But I have an idea. What if we could use keys to distinguish
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 What do you think? Or this functionality is already supported? |
@Jugen As I understand
By other words, I want more control for non empty |
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;
}
} |
@Jugen Yes, this is what I was looking for. Thank you very much for your time and help! |
I have a
CodeArea
. For piece of a text I can set style usingStyleSpans
or without them:Could anyone explain when I should use
StyleSpans
?The text was updated successfully, but these errors were encountered: