package com.scrivenvar.editor;
import com.scrivenvar.ui.AbstractPane;
import java.nio.file.Path;
import java.util.function.Consumer;
import javafx.application.Platform;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyDoubleProperty;
import javafx.beans.property.ReadOnlyDoubleWrapper;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ChangeListener;
import javafx.event.Event;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.InputEvent;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.StyleClassedTextArea;
import org.fxmisc.undo.UndoManager;
import org.fxmisc.wellbehaved.event.EventPattern;
import org.fxmisc.wellbehaved.event.InputMap;
import static org.fxmisc.wellbehaved.event.InputMap.consume;
import org.fxmisc.wellbehaved.event.Nodes;
public class EditorPane extends AbstractPane {
private StyleClassedTextArea editor;
private VirtualizedScrollPane<StyleClassedTextArea> scrollPane;
private final ReadOnlyDoubleWrapper scrollY = new ReadOnlyDoubleWrapper();
private final ObjectProperty<Path> path = new SimpleObjectProperty<>();
private String lineSeparator = getLineSeparator();
private InputMap<InputEvent> nodeMap;
@Override
public void requestFocus() {
Platform.runLater( () -> getEditor().requestFocus() );
}
public void undo() {
getUndoManager().undo();
}
public void redo() {
getUndoManager().redo();
}
public UndoManager getUndoManager() {
return getEditor().getUndoManager();
}
public String getText() {
String text = getEditor().getText();
if( !this.lineSeparator.equals( "\n" ) ) {
text = text.replace( "\n", this.lineSeparator );
}
return text;
}
public void setText( final String text ) {
this.lineSeparator = determineLineSeparator( text );
getEditor().deselect();
getEditor().replaceText( text );
getUndoManager().mark();
}
public void addChangeListener( final ChangeListener<? super String> listener ) {
getEditor().textProperty().addListener( listener );
}
public void addCaretParagraphListener( final ChangeListener<? super Integer> listener ) {
getEditor().currentParagraphProperty().addListener( listener );
}
public <T extends Event, U extends T> void addEventListener(
final EventPattern<? super T, ? extends U> event,
final Consumer<? super U> consumer ) {
Nodes.addInputMap( getEditor(), consume( event, consumer ) );
}
@SuppressWarnings( "unchecked" )
public void addEventListener( final InputMap<InputEvent> map ) {
this.nodeMap = (InputMap<InputEvent>)getInputMap();
Nodes.addInputMap( getEditor(), map );
}
public void removeEventListener( final InputMap<InputEvent> map ) {
Nodes.removeInputMap( getEditor(), map );
Nodes.addInputMap( getEditor(), this.nodeMap );
}
private Object getInputMap() {
return getEditor().getProperties().get( getInputMapKey() );
}
private String getInputMapKey() {
return "org.fxmisc.wellbehaved.event.inputmap";
}
public void scrollToTop() {
getEditor().moveTo( 0 );
}
private void setEditor( StyleClassedTextArea textArea ) {
this.editor = textArea;
}
public synchronized StyleClassedTextArea getEditor() {
if( this.editor == null ) {
setEditor( createTextArea() );
}
return this.editor;
}
public synchronized VirtualizedScrollPane<StyleClassedTextArea> getScrollPane() {
if( this.scrollPane == null ) {
this.scrollPane = createScrollPane();
}
return this.scrollPane;
}
protected VirtualizedScrollPane<StyleClassedTextArea> createScrollPane() {
final VirtualizedScrollPane<StyleClassedTextArea> pane = new VirtualizedScrollPane<>( getEditor() );
pane.setVbarPolicy( ScrollPane.ScrollBarPolicy.ALWAYS );
return pane;
}
protected StyleClassedTextArea createTextArea() {
return new StyleClassedTextArea( false );
}
public double getScrollY() {
return this.scrollY.get();
}
protected void setScrollY( double scrolled ) {
this.scrollY.set( scrolled );
}
public ReadOnlyDoubleProperty scrollYProperty() {
return this.scrollY.getReadOnlyProperty();
}
public Path getPath() {
return this.path.get();
}
public void setPath( final Path path ) {
this.path.set( path );
}
public ObjectProperty<Path> pathProperty() {
return this.path;
}
private String getLineSeparator() {
final String separator = getOptions().getLineSeparator();
return (separator != null)
? separator
: System.lineSeparator();
}
private String determineLineSeparator( final String s ) {
final int length = s.length();
for( int i = 0; i < length; i++ ) {
char ch = s.charAt( i );
if( ch == '\n' ) {
return (i > 0 && s.charAt( i - 1 ) == '\r') ? "\r\n" : "\n";
}
}
return getLineSeparator();
}
}