Giter Club home page Giter Club logo

Comments (8)

PavelTurk avatar PavelTurk commented on September 26, 2024 2

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

from richtextfx.

Jugen avatar Jugen commented on September 26, 2024 1

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.

from richtextfx.

PavelTurk avatar PavelTurk commented on September 26, 2024

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

from richtextfx.

Jugen avatar Jugen commented on September 26, 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();
    }
}

from richtextfx.

PavelTurk avatar PavelTurk commented on September 26, 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?

from richtextfx.

Jugen avatar Jugen commented on September 26, 2024

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

from richtextfx.

PavelTurk avatar PavelTurk commented on September 26, 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 :)

from richtextfx.

Jugen avatar Jugen commented on September 26, 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;
        }
    }

from richtextfx.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.