org.fxmisc.richtext.demo.TextAreaBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of richtextfx Show documentation
Show all versions of richtextfx Show documentation
FX-Text-Area for formatted text and other special effects.
package org.fxmisc.richtext.demo;
import java.time.Duration;
import javafx.beans.value.ChangeListener;
import javafx.css.PseudoClass;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Point2D;
import javafx.scene.control.Label;
import javafx.scene.text.Font;
import javafx.stage.Popup;
import org.fxmisc.flowless.VirtualizedScrollPane;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.event.MouseOverTextEvent;
/**
* Name: TextAreaBase
* Description: Basisklasse f?r spezialisierte TextAreas.
*
* Comment: ...
*
* Copyright: Copyright (c) 2018, 2019
* Company: >StA-Soft<
* @author StA
* @version 1.0
*/
public class TextAreaBase extends CodeArea implements EventHandler
{
public static VirtualizedScrollPane addScrollPane(CodeArea codeArea)
{
VirtualizedScrollPane vpane = new VirtualizedScrollPane<>(codeArea);
final PseudoClass FOCUSED = PseudoClass.getPseudoClass("focused");
codeArea.focusedProperty().addListener((obs, oldVal, newVal) ->
{
vpane.pseudoClassStateChanged(FOCUSED, newVal);
});
vpane.getStylesheets().add(TextAreaBase.class.getResource("editors.css").toExternalForm());
return vpane;
}
//===========================================================================
/**
* Standard-Constructor.
*/
public TextAreaBase()
{
}
/**
* Constructor mit Text.
* @param text Text
*/
public TextAreaBase(String text)
{
super(text);
}
/**
* Constructor mit Change-Listener.
* @param cl Change-Listener
*/
public TextAreaBase(ChangeListener cl)
{
if (cl != null)
{
textProperty().addListener(cl);
}
}
//===========================================================================
/**
* Cursor-Position setzen.
* @param pos Cursor-Position
*/
public void setCaretPosition(int pos)
{
// positionCaret(pos);
selectRange(pos, pos);
}
/**
* ChangeListener f?r Text?nderungen.
*/
protected ChangeListener myTextChangeListener = null;
/**
* ChangeListener f?r Text?nderungen setzen.
* @param cl ChangeListener f?r Text?nderungen
*/
public void setTextChangeListener(ChangeListener cl)
{
myTextChangeListener = cl;
textProperty().addListener(cl);
}
/**
* ChangeListener f?r Text?nderungen ermitteln.
* @return ChangeListener f?r Text?nderungen
*/
public ChangeListener getTextChangeListener()
{
return myTextChangeListener;
}
/**
* Text setzen, dabei wird der ChangeListener f?r Text?nderungen aufgerufen,
* falls gesetzt.
* @param text der zu setzende Text
*/
public void setText2(String text)
{
String oldtext = getText();
// super.setText(text);
replaceText(text);
// MLogger.deb("|" + text + "|");
if ((myTextChangeListener != null) && (((oldtext == null) && (text == null)) || ((oldtext != null) && oldtext.equals(text))))
{
myTextChangeListener.changed(textProperty(), oldtext, text);
}
}
Popup popup;
Label popupMsg;
@Override
public void handle(MouseOverTextEvent e)
{
EventType extends Event> eventtype = e.getEventType();
if (eventtype == MouseOverTextEvent.MOUSE_OVER_TEXT_BEGIN)
{
// int chIdx = e.getCharacterIndex();
Point2D pos = e.getScreenPosition();
// popupMsg.setText("Character '" + TextAreaBase.this.getText(chIdx, chIdx + 1) + "' at " + pos);
popup.show(TextAreaBase.this, pos.getX(), pos.getY() + 10);
}
else if (eventtype == MouseOverTextEvent.MOUSE_OVER_TEXT_END)
{
popup.hide();
}
}
/**
* Tool-Tip setzen.
* @param s Tool-Tip-Text
*/
public void setToolTipText(String s)
{
// setTooltip(s != null ? new Tooltip(s) : null);
if (s != null)
{
if (popup == null)
{
popup = new Popup();
popupMsg = new Label();
popupMsg.setStyle("-fx-background-color: black;" + "-fx-text-fill: white;" + "-fx-padding: 5;");
popup.getContent().add(popupMsg);
setMouseOverTextDelay(Duration.ofSeconds(1));
addEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_BEGIN, this);
addEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_END, this);
}
popupMsg.setText(s);
}
else
{
if (popup != null)
{
removeEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_BEGIN, this);
removeEventHandler(MouseOverTextEvent.MOUSE_OVER_TEXT_END, this);
popup = null;
popupMsg = null;
}
}
}
public void setFont(Font f)
{
// setStyle(
}
}