Skip to content

Instantly share code, notes, and snippets.

@mehmetbebek
Created September 22, 2014 11:47
Show Gist options
  • Save mehmetbebek/bf969268e685813c1f12 to your computer and use it in GitHub Desktop.
Save mehmetbebek/bf969268e685813c1f12 to your computer and use it in GitHub Desktop.
Java Highlight
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javafxapplication4;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker.State;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
/**
*
* @author m00264122
*/
public class JavaFXApplication4 extends Application {
public static final int TEXT_WIDTH = 214;
private TextField regex;
private TextArea text;
private WebView result;
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
GridPane root = new GridPane();
root.setHgap(10);
root.setVgap(4);
Label regexLbl = new Label("Regex");
regex = new TextField();
regex.setMinHeight(25);
regex.setMinWidth(TEXT_WIDTH);
root.add(regexLbl, 0, 0);
root.add(regex, 1, 0);
Label textLbl = new Label("Text");
text = new TextArea();
text.setMinHeight(50);
text.setPrefRowCount(10);
text.setMinWidth(TEXT_WIDTH);
root.add(textLbl, 0, 1);
root.add(text, 1, 1);
result = new WebView();
root.add(result, 0, 2, 2, 1);
Button match = new Button("Match");
match.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
matchRegExp();
}
});
root.add(match, 0, 3);
Button dumpHtml = new Button("Dump html");
dumpHtml.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {
//System.err.println(result.getHtmlText());
}
});
root.add(dumpHtml, 1, 3);
Scene scene = new Scene(root, 800, 600);
stage.setScene(scene);
stage.show();
}
private void matchRegExp() {
Pattern pattern = null;
try {
pattern = Pattern.compile(regex.getText());
} catch (PatternSyntaxException psex) {
//result.setHtmlText(String.format(
//"<html><body><p>" +
//"<span style=\"color:Red;\">%s</span>" +
//"</p></body></html>",
//psex.getMessage()));
return;
}
Matcher matcher = pattern.matcher(text.getText());
boolean found = false;
StringBuilder sb = new StringBuilder();
class Tuple {
final int start, end;
Tuple(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public String toString() {
return "Tuple{start=" + start + ", end=" + end + '}';
}
}
List<Tuple> positions = new ArrayList<Tuple>();
while (matcher.find()) {
positions.add(new Tuple(matcher.start(), matcher.end()));
found = true;
}
if (!found) {
sb.append(String.format("No match found.%n"));
} else {
System.out.println(positions);
int pos = 0;
for (int i = 0; i < positions.size(); i++) {
sb.append(text.getText().substring(pos, positions.get(i).start))
.append("<span style=\"color:ForestGreen;font-size:20px;text-decoration:underline\">")
.append(text.getText().substring(positions.get(i).start, positions.get(i).end))
.append("</span>");
pos = positions.get(i).end;
}
if (pos > 0) {
sb.append(text.getText().substring(pos));
}
}
String html = String.format(""
+ "<html><body><p >%s</p></body></html>", text.getText());
// final WebView webView = new WebView();
final WebEngine engine = result.getEngine();
getChangedEngine(engine, regex.getText());
engine.loadContent(html);
// WebEngine engine = result.getEngine();
// engine.loadContent(html);
//result.set setHtmlText(html);
double rs = 0.0;
{
int a = 5;
}
}
private WebEngine getChangedEngine(final WebEngine engine,final String searchText)
{
engine.getLoadWorker().stateProperty().addListener(
new ChangeListener<State>() {
@Override
public void changed(ObservableValue ov, State oldState, State newState) {
if (newState == State.SUCCEEDED) {
engine.executeScript(
"function highlightWord(root,word){"
+ " textNodesUnder(root).forEach(highlightWords);"
+ ""
+ " function textNodesUnder(root){"
+ " var n,a=[],w=document.createTreeWalker(root,NodeFilter.SHOW_TEXT,null,false);"
+ " while(n=w.nextNode()) a.push(n);"
+ " return a;"
+ " }"
+ ""
+ " function highlightWords(n){"
+ " for (var i; (i=n.nodeValue.indexOf(word,i)) > -1; n=after){"
+ " var after = n.splitText(i+word.length);"
+ " var highlighted = n.splitText(i);"
+ " var span = document.createElement('span');"
+ " span.style.backgroundColor='#f00';"
+ " span.appendChild(highlighted);"
+ " after.parentNode.insertBefore(span,after);"
+ " }"
+ " }"
+ "}"
+ "\n"
+ "highlightWord(document.body,'"+searchText +"');");
}
}
});
return engine;
}
}
//style='white-space: pre-wrap;'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment