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

Is there anyway to have an inline css setstyle() for a StyleClassedTextArea or CodeArea please? #272

Closed
05dfleming opened this issue Mar 27, 2016 · 2 comments

Comments

@05dfleming
Copy link

I am so sorry to bother you about this, but just wondering if there was any way to set an inline css style such as area.setStyle(0,2, "-fx-fill: #00ff00 ; ") for a StyleClassedTextArea or CodeArea please? As I was hoping to have a way to take a colour from the fx colour picker then in real time then change the styles of certain words in code using that. But hoping to find an alternative to writing a few thousand colours in CSS to match the fx color picker :) As at the moment if I try that above, the colour just defaults to black. Any help would be massively appreciated, thanks :)

@JordanMartinez
Copy link
Contributor

No. If you look at the code that applies the style to ParagraphBox and ParagraphText, you'll see there's no way around this (taken from StyleClassedTextArea's constructor:

super(Collections.<String>emptyList(),
                (paragraph, styleClasses) -> paragraph.getStyleClass().addAll(styleClasses),
                Collections.<String>emptyList(),
                (text, styleClasses) -> text.getStyleClass().addAll(styleClasses),
                document, preserveStyle
        );

To get the result you want, you'd need to subclass StyledTextArea and have some Style object that either sets the style class or sets the style itself using something like this BiConsumer:

(text, styleObj) -> styleObj.isStyleClass() 
                ? text.getStyleClass().addAll(styleObj.toStyleClass()) 
                : text.setStyle(styleObj.toCss());

@TomasMikula
Copy link
Member

@05dfleming StyledTextArea doesn't care what your representation of "style" is, as long as you provide a way (really a function) to apply it to text. StyledClassedTextArea (and CodeArea) is just a convenient subclass for cases when you decide that your representation of "style" is a set of style classes. It defines the function to apply the style (i.e. a set of style classes) for you, as can be seen in the code quoted by @JordanMartinez.

(If by any chance you actually don't need style classes and only need inline CSS, then InlineCssTextArea might work for you.)

If you want a richer definition of "style" than just a set of style classes or just inline CSS, then neither StyleClassedTextArea nor InlineCssTextArea will work for you.

As Jordan pointed out, you will need to use StyledTextArea directly (you don't even need to subclass it, but you can).

If you want to combine style classes and inline CSS, your "style" representation could be Tuple2<Collection<String>, String>, i.e. a pair of a collection of style classes, and a CSS string. If you don't worry about using paragraph-level styles, let's use Void for those. You then instantiate your area like this:

import org.reactfx.util.Tuple2;
import static org.reactfx.util.Tuples._;

StyledTextArea<Void, Tuple2<Collection<String>, String>> area = new StyledTextArea<>(
    null, // initial paragraph style; not going to use it
    (par, ps) -> { /* do nothing */ }, // function to apply paragraph style
    t(Collections.<String>emptyList(), ""), // initial text style: no style classes and empty CSS string
    (text, s) -> { // function to apply text style
        text.getStyleClass().addAll(s._1); // apply style classes (first element of the style tuple)
        text.setStyle(s._2); // apply inline CSS (second element of the style tuple)
    }
);

And then you can do

Collection<String> styleClasses = ...;
String css = "-fx-fill: #00ff00 ;";
area.setStyle(0, 2, t(styleClasses, css));

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

3 participants