-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from afester/dottedUnderline
Support underlined text with some customization of the underline.
- Loading branch information
Showing
7 changed files
with
429 additions
and
11 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
richtextfx-demos/src/main/java/org/fxmisc/richtext/demo/SpellChecking.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.fxmisc.richtext.demo; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.text.BreakIterator; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.fxmisc.flowless.VirtualizedScrollPane; | ||
import org.fxmisc.richtext.StyleClassedTextArea; | ||
import org.fxmisc.richtext.StyleSpans; | ||
import org.fxmisc.richtext.StyleSpansBuilder; | ||
|
||
import javafx.application.Application; | ||
import javafx.scene.Scene; | ||
import javafx.scene.layout.StackPane; | ||
import javafx.stage.Stage; | ||
|
||
public class SpellChecking extends Application { | ||
|
||
private static final Set<String> dictionary = new HashSet<String>(); | ||
|
||
public static void main(String[] args) { | ||
launch(args); | ||
} | ||
|
||
@Override | ||
public void start(Stage primaryStage) { | ||
StyleClassedTextArea textArea = new StyleClassedTextArea(); | ||
textArea.setWrapText(true); | ||
|
||
textArea.richChanges() | ||
.filter(ch -> !ch.getInserted().equals(ch.getRemoved())) // XXX | ||
.subscribe(change -> { | ||
textArea.setStyleSpans(0, computeHighlighting(textArea.getText())); | ||
}); | ||
|
||
// load the dictionary | ||
try (InputStream input = getClass().getResourceAsStream("spellchecking.dict"); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(input))) { | ||
String line; | ||
while ((line = br.readLine()) != null) { | ||
dictionary.add(line); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
// load the sample document | ||
InputStream input2 = getClass().getResourceAsStream("spellchecking.txt"); | ||
try(java.util.Scanner s = new java.util.Scanner(input2)) { | ||
String document = s.useDelimiter("\\A").hasNext() ? s.next() : ""; | ||
textArea.replaceText(0, 0, document); | ||
} | ||
|
||
Scene scene = new Scene(new StackPane(new VirtualizedScrollPane<>(textArea)), 600, 400); | ||
scene.getStylesheets().add(getClass().getResource("spellchecking.css").toExternalForm()); | ||
primaryStage.setScene(scene); | ||
primaryStage.setTitle("Spell Checking Demo"); | ||
primaryStage.show(); | ||
} | ||
|
||
|
||
private static StyleSpans<Collection<String>> computeHighlighting(String text) { | ||
|
||
StyleSpansBuilder<Collection<String>> spansBuilder = new StyleSpansBuilder<>(); | ||
|
||
BreakIterator wb = BreakIterator.getWordInstance(); | ||
wb.setText(text); | ||
|
||
int lastIndex = wb.first(); | ||
int lastKwEnd = 0; | ||
while(lastIndex != BreakIterator.DONE) { | ||
int firstIndex = lastIndex; | ||
lastIndex = wb.next(); | ||
|
||
if (lastIndex != BreakIterator.DONE | ||
&& Character.isLetterOrDigit(text.charAt(firstIndex))) { | ||
String word = text.substring(firstIndex, lastIndex).toLowerCase(); | ||
if (!dictionary.contains(word)) { | ||
spansBuilder.add(Collections.emptyList(), firstIndex - lastKwEnd); | ||
spansBuilder.add(Collections.singleton("underlined"), lastIndex - firstIndex); | ||
lastKwEnd = lastIndex; | ||
} | ||
System.err.println(); | ||
} | ||
} | ||
spansBuilder.add(Collections.emptyList(), text.length() - lastKwEnd); | ||
|
||
return spansBuilder.create(); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/spellchecking.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.underlined { | ||
-fx-underline-color: red; | ||
-fx-underline-dash-array: 2 2; | ||
-fx-underline-width: 1; | ||
-fx-underline-cap: butt; | ||
} |
27 changes: 27 additions & 0 deletions
27
richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/spellchecking.dict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
a | ||
applied | ||
basic | ||
brown | ||
but | ||
could | ||
document | ||
dog | ||
fox | ||
here | ||
if | ||
is | ||
its | ||
jumps | ||
lazy | ||
no | ||
over | ||
quick | ||
rendering | ||
sample | ||
see | ||
styling | ||
the | ||
there | ||
this | ||
were | ||
you |
3 changes: 3 additions & 0 deletions
3
richtextfx-demos/src/main/resources/org/fxmisc/richtext/demo/spellchecking.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The quik brown fox jumps over the lazy dog. | ||
Ths is a sample dokument. | ||
There is no styling aplied, but if there were, you could see its basic rndering here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.