From c22bb1025bf9ef3cfdd9323ea59ab667d34f4749 Mon Sep 17 00:00:00 2001 From: Jurgen Date: Mon, 23 Mar 2020 14:28:47 +0200 Subject: [PATCH] Added getLocale and setLocale for BreakIterator use. (#920) --- .../src/main/java/org/fxmisc/richtext/Caret.java | 8 ++++---- .../org/fxmisc/richtext/CaretSelectionBindImpl.java | 2 +- .../src/main/java/org/fxmisc/richtext/CodeArea.java | 2 +- .../java/org/fxmisc/richtext/GenericStyledArea.java | 13 +++++++++++++ .../java/org/fxmisc/richtext/NavigationActions.java | 2 +- .../java/org/fxmisc/richtext/SelectionImpl.java | 2 +- .../java/org/fxmisc/richtext/TextEditingArea.java | 8 +++++++- 7 files changed, 28 insertions(+), 9 deletions(-) diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/Caret.java b/richtextfx/src/main/java/org/fxmisc/richtext/Caret.java index 470566a1d..392eeba5d 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/Caret.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/Caret.java @@ -184,11 +184,11 @@ default void moveToAreaStart() { void moveBreaksForwards(int numOfBreaks, BreakIterator breakIterator); default void moveWordBreaksForwards(int numOfWordBreaks) { - moveBreaksForwards(numOfWordBreaks, BreakIterator.getWordInstance()); + moveBreaksForwards(numOfWordBreaks, BreakIterator.getWordInstance( getArea().getLocale() )); } default void moveSentenceBreaksForwards(int numOfSentenceBreaks) { - moveBreaksForwards(numOfSentenceBreaks, BreakIterator.getSentenceInstance()); + moveBreaksForwards(numOfSentenceBreaks, BreakIterator.getSentenceInstance( getArea().getLocale() )); } /** @@ -197,11 +197,11 @@ default void moveSentenceBreaksForwards(int numOfSentenceBreaks) { void moveBreaksBackwards(int numOfBreaks, BreakIterator breakIterator); default void moveWordBreaksBackwards(int numOfWordBreaks) { - moveBreaksBackwards(numOfWordBreaks, BreakIterator.getWordInstance()); + moveBreaksBackwards(numOfWordBreaks, BreakIterator.getWordInstance( getArea().getLocale() )); } default void moveSentenceBreaksBackwards(int numOfSentenceBreaks) { - moveBreaksBackwards(numOfSentenceBreaks, BreakIterator.getSentenceInstance()); + moveBreaksBackwards(numOfSentenceBreaks, BreakIterator.getSentenceInstance( getArea().getLocale() )); } /** diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java b/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java index 0be828b92..6c4320993 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/CaretSelectionBindImpl.java @@ -335,7 +335,7 @@ public void selectWord(int wordPositionInArea) { return; } - BreakIterator breakIterator = BreakIterator.getWordInstance(); + BreakIterator breakIterator = BreakIterator.getWordInstance( getArea().getLocale() ); breakIterator.setText(getArea().getText()); int start = calculatePositionViaBreakingBackwards(1, breakIterator, wordPositionInArea); diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java index a6e022ecf..1fd99f236 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java @@ -63,7 +63,7 @@ public void selectWord() int position = csb.getColumnPosition(); String paragraphText = getText( paragraph ); - BreakIterator breakIterator = BreakIterator.getWordInstance(); + BreakIterator breakIterator = BreakIterator.getWordInstance( getLocale() ); breakIterator.setText( paragraphText ); breakIterator.preceding( position ); diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java index aa0cb6116..1edac58d8 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/GenericStyledArea.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.Locale; import java.util.Optional; import java.util.function.BiConsumer; import java.util.function.BiFunction; @@ -354,6 +355,18 @@ protected void invalidated() { this.undoManager = undoManager != null ? undoManager : UndoUtils.noOpUndoManager(); } + private Locale textLocale = Locale.getDefault(); + /** + * This is used to determine word and sentence breaks while navigating or selecting. + * Override this method if your paragraph or text style accommodates Locales as well. + * @return Locale.getDefault() by default + */ + @Override + public Locale getLocale() { return textLocale; } + public void setLocale( Locale editorLocale ) { + textLocale = editorLocale; + } + private final ObjectProperty mouseOverTextDelay = new SimpleObjectProperty<>(null); @Override public ObjectProperty mouseOverTextDelayProperty() { return mouseOverTextDelay; } diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/NavigationActions.java b/richtextfx/src/main/java/org/fxmisc/richtext/NavigationActions.java index 3c5170252..0ef5618de 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/NavigationActions.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/NavigationActions.java @@ -102,7 +102,7 @@ default void wordBreaksBackwards(int n, SelectionPolicy selectionPolicy) { return; } - BreakIterator wordBreakIterator = BreakIterator.getWordInstance(); + BreakIterator wordBreakIterator = BreakIterator.getWordInstance( getLocale() ); wordBreakIterator.setText(getText()); wordBreakIterator.preceding(getCaretPosition()); for (int i = 1; i < n; i++) { diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java b/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java index 54168e24c..85a16534d 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/SelectionImpl.java @@ -382,7 +382,7 @@ public void selectWord(int wordPositionInArea) { return; } - BreakIterator breakIterator = BreakIterator.getWordInstance(); + BreakIterator breakIterator = BreakIterator.getWordInstance( getArea().getLocale() ); breakIterator.setText(area.getText()); breakIterator.preceding(wordPositionInArea); breakIterator.next(); diff --git a/richtextfx/src/main/java/org/fxmisc/richtext/TextEditingArea.java b/richtextfx/src/main/java/org/fxmisc/richtext/TextEditingArea.java index 02c4e4530..62b1a74d7 100644 --- a/richtextfx/src/main/java/org/fxmisc/richtext/TextEditingArea.java +++ b/richtextfx/src/main/java/org/fxmisc/richtext/TextEditingArea.java @@ -8,7 +8,6 @@ import org.fxmisc.richtext.model.EditableStyledDocument; import org.fxmisc.richtext.model.Paragraph; import org.fxmisc.richtext.model.PlainTextChange; -import org.fxmisc.richtext.model.Replacement; import org.fxmisc.richtext.model.RichTextChange; import org.fxmisc.richtext.model.SegmentOps; import org.fxmisc.richtext.model.StyledDocument; @@ -17,6 +16,7 @@ import org.reactfx.value.Var; import java.util.List; +import java.util.Locale; import java.util.Optional; /** @@ -41,6 +41,12 @@ public interface TextEditingArea { default int getLength() { return lengthProperty().getValue(); } ObservableValue lengthProperty(); + /** + * This is used to determine word and sentence breaks while navigating or selecting. + * Override this method if your paragraph or text style accommodates Locales as well. + */ + default Locale getLocale() { return Locale.getDefault(); } + /** * Text content of this text-editing area. */