Skip to content

Commit

Permalink
Proposed paragraph style setting (Paragraph style vanishs after text/…
Browse files Browse the repository at this point in the history
…style update. Otherwise complete)
  • Loading branch information
MewesK authored and Jürgen Obernolte committed Oct 14, 2015
1 parent cb3b5f9 commit 0ae84c2
Show file tree
Hide file tree
Showing 15 changed files with 240 additions and 176 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.BooleanBinding;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.collections.FXCollections;
import javafx.scene.Scene;
import javafx.scene.control.Button;
Expand All @@ -33,6 +35,8 @@

import org.fxmisc.richtext.Codec;
import org.fxmisc.richtext.InlineStyleTextArea;
import org.fxmisc.richtext.Paragraph;
import org.fxmisc.richtext.StyleSpan;
import org.fxmisc.richtext.StyleSpans;
import org.reactfx.SuspendableNo;

Expand Down Expand Up @@ -180,14 +184,16 @@ private static String cssColor(Color color) {
final Optional<Color> backgroundColor;

public StyleInfo() {
bold = Optional.empty();
italic = Optional.empty();
underline = Optional.empty();
strikethrough = Optional.empty();
fontSize = Optional.empty();
fontFamily = Optional.empty();
textColor = Optional.empty();
backgroundColor = Optional.empty();
this(
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty(),
Optional.empty()
);
}

public StyleInfo(
Expand Down Expand Up @@ -350,6 +356,7 @@ public static void main(String[] args) {
}

private final SuspendableNo updatingToolbar = new SuspendableNo();
private final BooleanProperty applyToParagraph = new SimpleBooleanProperty(false);

@Override
public void start(Stage primaryStage) {
Expand Down Expand Up @@ -529,21 +536,32 @@ private void toggleStrikethrough() {
}

private void updateStyleInSelection(Function<StyleSpans<StyleInfo>, StyleInfo> mixinGetter) {
IndexRange selection = area.getSelection();
if(selection.getLength() != 0) {
StyleSpans<StyleInfo> styles = area.getStyleSpans(selection);
StyleInfo mixin = mixinGetter.apply(styles);
StyleSpans<StyleInfo> newStyles = styles.mapStyles(style -> style.updateWith(mixin));
area.setStyleSpans(selection.getStart(), newStyles);
if (applyToParagraph.get()) {
StyleInfo mixin = mixinGetter.apply(StyleSpans.singleton(new StyleSpan<>(StyleInfo.EMPTY, 0)));
Paragraph paragraph = area.getParagraph(area.getCurrentParagraph());
paragraph.setParagraphStyle(mixin);
} else {
IndexRange selection = area.getSelection();
if(selection.getLength() != 0) {
StyleSpans<StyleInfo> styles = area.getStyleSpans(selection);
StyleInfo mixin = mixinGetter.apply(styles);
StyleSpans<StyleInfo> newStyles = styles.mapStyles(style -> style.updateWith(mixin));
area.setStyleSpans(selection.getStart(), newStyles);
}
}
}

private void updateStyleInSelection(StyleInfo mixin) {
IndexRange selection = area.getSelection();
if(selection.getLength() != 0) {
StyleSpans<StyleInfo> styles = area.getStyleSpans(selection);
StyleSpans<StyleInfo> newStyles = styles.mapStyles(style -> style.updateWith(mixin));
area.setStyleSpans(selection.getStart(), newStyles);
if (applyToParagraph.get()) {
Paragraph paragraph = area.getParagraph(area.getCurrentParagraph());
paragraph.setParagraphStyle(mixin);
} else {
IndexRange selection = area.getSelection();
if (selection.getLength() != 0) {
StyleSpans<StyleInfo> styles = area.getStyleSpans(selection);
StyleSpans<StyleInfo> newStyles = styles.mapStyles(style -> style.updateWith(mixin));
area.setStyleSpans(selection.getStart(), newStyles);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* Clipboard actions for {@link TextEditingArea}.
*/
public interface ClipboardActions<S> extends EditActions<S> {
public interface ClipboardActions<S, PS> extends EditActions<S, PS> {

Optional<Codec<S>> getStyleCodec();

Expand Down
10 changes: 5 additions & 5 deletions richtextfx/src/main/java/org/fxmisc/richtext/EditActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Extended edit actions for {@link TextEditingArea}.
*/
public interface EditActions<S> extends TextEditingArea<S> {
public interface EditActions<S, PS> extends TextEditingArea<S, PS> {

/**
* Appends the given text to the end of the text content.
Expand All @@ -17,7 +17,7 @@ default void appendText(String text) {
/**
* Appends the given rich-text content to the end of this text-editing area.
*/
default void append(StyledDocument<S> document) {
default void append(StyledDocument<S, PS> document) {
insert(getLength(), document);
}

Expand All @@ -37,7 +37,7 @@ default void insertText(int index, String text) {
* @param index The location to insert the text.
* @param document The rich-text content to insert.
*/
default void insert(int index, StyledDocument<S> document) {
default void insert(int index, StyledDocument<S, PS> document) {
replace(index, index, document);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ default void replaceText(String replacement) {
/**
* Replaces the entire content with the given rich-text content.
*/
default void replace(StyledDocument<S> replacement) {
default void replace(StyledDocument<S, PS> replacement) {
replace(0, getLength(), replacement);
}

Expand All @@ -125,7 +125,7 @@ default void replaceSelection(String replacement) {
* caret position. If there was a selection, then the selection is cleared
* and the given replacement text is inserted.
*/
default void replaceSelection(StyledDocument<S> replacement) {
default void replaceSelection(StyledDocument<S, PS> replacement) {
replace(getSelection(), replacement);
}

Expand Down
Loading

0 comments on commit 0ae84c2

Please sign in to comment.