Skip to content
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

Closed
PavelTurk opened this issue May 18, 2024 · 8 comments
Closed

Question: when to use StyleSpans? #1229

PavelTurk opened this issue May 18, 2024 · 8 comments

Comments

@PavelTurk
Copy link
Contributor

I have a CodeArea. For piece of a text I can set style using StyleSpansor without them:

textArea.setStyleSpans(int from,  StyleSpans<? extends S> styleSpans)//with
textArea.setStyleClass(int from, int to, String styleClass)//without

Could anyone explain when I should use StyleSpans?

@Jugen
Copy link
Collaborator

Jugen commented May 20, 2024

There are two cases for when to use StyleSpans:

  1. setStyleClass will erase & replace the current styling of the specified range, while setStyleSpans will add the provided styling to any existing styling.
  2. Every setStyleClass and setStyleSpans 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 using StyleSpansBuilder and submit them in one go with setStyleSpans. 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.

@PavelTurk
Copy link
Contributor Author

@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:

Screenshot from 2024-05-21 19-42-11

Both layers where built using builder that's why there are empty spans in both of them. Now I have two types of task:

  1. I need to remove all spans from the first layer (for highlighting).
  2. 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?

@Jugen
Copy link
Collaborator

Jugen commented May 22, 2024

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();
    }
}

@PavelTurk
Copy link
Contributor Author

PavelTurk commented May 22, 2024

@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?

@Jugen
Copy link
Collaborator

Jugen commented May 23, 2024

Could something in StyleSpans (ApiDoc) (Code) maybe help you ?

@PavelTurk
Copy link
Contributor Author

PavelTurk commented May 23, 2024

@Jugen As I understand StyleSpans is just a collection. I would like to have three different things:

  1. 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.
  2. 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 own StyleSpan.
  3. I would like to get events on the StyleSpan I added to area.

By other words, I want more control for non empty StyleSpans I add. Now you will say that I want too much :)

@Jugen
Copy link
Collaborator

Jugen commented May 23, 2024

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;
        }
    }

@PavelTurk
Copy link
Contributor Author

@Jugen Yes, this is what I was looking for. Thank you very much for your time and help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants