Skip to content

Commit

Permalink
Added getLocale and setLocale for BreakIterator use. (#920)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jugen authored Mar 23, 2020
1 parent aaee089 commit c22bb10
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 9 deletions.
8 changes: 4 additions & 4 deletions richtextfx/src/main/java/org/fxmisc/richtext/Caret.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() ));
}

/**
Expand All @@ -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() ));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion richtextfx/src/main/java/org/fxmisc/richtext/CodeArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Duration> mouseOverTextDelay = new SimpleObjectProperty<>(null);
@Override public ObjectProperty<Duration> mouseOverTextDelayProperty() { return mouseOverTextDelay; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +16,7 @@
import org.reactfx.value.Var;

import java.util.List;
import java.util.Locale;
import java.util.Optional;

/**
Expand All @@ -41,6 +41,12 @@ public interface TextEditingArea<PS, SEG, S> {
default int getLength() { return lengthProperty().getValue(); }
ObservableValue<Integer> 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.
*/
Expand Down

0 comments on commit c22bb10

Please sign in to comment.