|
| 1 | +package org.fxmisc.richtext; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.function.BiConsumer; |
| 5 | +import java.util.regex.Pattern; |
| 6 | + |
| 7 | +import org.fxmisc.richtext.model.EditableStyledDocument; |
| 8 | + |
| 9 | +import javafx.application.Application; |
| 10 | +import javafx.beans.NamedArg; |
| 11 | +import javafx.beans.property.ObjectProperty; |
| 12 | +import javafx.beans.property.ObjectPropertyBase; |
| 13 | +import javafx.event.ActionEvent; |
| 14 | +import javafx.event.EventHandler; |
| 15 | +import javafx.scene.AccessibleRole; |
| 16 | +import javafx.scene.Group; |
| 17 | +import javafx.scene.Node; |
| 18 | +import javafx.scene.Parent; |
| 19 | +import javafx.scene.Scene; |
| 20 | +import javafx.scene.control.TextField; |
| 21 | +import javafx.scene.input.KeyCode; |
| 22 | +import javafx.scene.input.KeyEvent; |
| 23 | +import javafx.scene.input.MouseEvent; |
| 24 | +import javafx.scene.layout.Pane; |
| 25 | +import javafx.scene.text.TextFlow; |
| 26 | + |
| 27 | +public class StyledTextField<PS, S> extends StyledTextArea |
| 28 | +{ |
| 29 | + private final Pattern VERTICAL_WHITESPACE = Pattern.compile( "\\v" ); |
| 30 | + private final static String STYLE_SHEET; |
| 31 | + private final static double HEIGHT; |
| 32 | + static { |
| 33 | + String globalCSS = System.getProperty( "javafx.userAgentStylesheetUrl" ); // JavaFX preference! |
| 34 | + if ( globalCSS == null ) globalCSS = Application.getUserAgentStylesheet(); |
| 35 | + if ( globalCSS == null ) globalCSS = Application.STYLESHEET_MODENA; |
| 36 | + globalCSS = "styled-text-field-"+ globalCSS.toLowerCase() +".css"; |
| 37 | + STYLE_SHEET = StyledTextField.class.getResource( globalCSS ).toExternalForm(); |
| 38 | + |
| 39 | + // Ugly hack to get a TextFields default height :( |
| 40 | + // as it differs between Caspian, Modena, etc. |
| 41 | + TextField tf = new TextField( "GetHeight" ); |
| 42 | + new Scene(tf); tf.applyCss(); tf.layout(); |
| 43 | + HEIGHT = tf.getHeight(); |
| 44 | + } |
| 45 | + |
| 46 | + private boolean selectAll = true; |
| 47 | + |
| 48 | + |
| 49 | + public StyledTextField(@NamedArg("initialParagraphStyle") PS initialParagraphStyle, |
| 50 | + @NamedArg("applyParagraphStyle") BiConsumer<TextFlow, PS> applyParagraphStyle, |
| 51 | + @NamedArg("initialTextStyle") S initialTextStyle, |
| 52 | + @NamedArg("applyStyle") BiConsumer<? super TextExt, S> applyStyle, |
| 53 | + @NamedArg("document") EditableStyledDocument<PS, String, S> document) |
| 54 | + { |
| 55 | + super( initialParagraphStyle, applyParagraphStyle, initialTextStyle, applyStyle, document, true ); |
| 56 | + |
| 57 | + getStylesheets().add( STYLE_SHEET ); |
| 58 | + getStyleClass().setAll( "styled-text-field" ); |
| 59 | + |
| 60 | + setAccessibleRole( AccessibleRole.TEXT_FIELD ); |
| 61 | + setPrefSize( 135, HEIGHT ); |
| 62 | + |
| 63 | + addEventFilter( KeyEvent.KEY_PRESSED, KE -> { |
| 64 | + if ( KE.getCode() == KeyCode.ENTER ) { |
| 65 | + fireEvent( new ActionEvent( this, null ) ); |
| 66 | + KE.consume(); |
| 67 | + } |
| 68 | + else if ( KE.getCode() == KeyCode.TAB ) { |
| 69 | + traverse( this.getParent(), this, KE.isShiftDown() ? -1 : +1 ); |
| 70 | + KE.consume(); |
| 71 | + } |
| 72 | + }); |
| 73 | + |
| 74 | + addEventFilter( MouseEvent.MOUSE_PRESSED, ME -> selectAll = isFocused() ); |
| 75 | + |
| 76 | + focusedProperty().addListener( (ob,was,focused) -> { |
| 77 | + if ( ! was && focused && selectAll ) { |
| 78 | + selectRange( getLength(), 0 ); |
| 79 | + } |
| 80 | + else if ( ! focused && was ) { |
| 81 | + moveTo( 0 ); requestFollowCaret(); |
| 82 | + } |
| 83 | + selectAll = true; |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + /* |
| 88 | + * There's no public API to move the focus forward or backward |
| 89 | + * without explicitly knowing the node. So here's a basic local |
| 90 | + * implementation to accomplish that. |
| 91 | + */ |
| 92 | + private Node traverse( Parent p, Node from, int dir ) |
| 93 | + { |
| 94 | + if ( p == null ) return null; |
| 95 | + |
| 96 | + List<Node> nodeList = p.getChildrenUnmodifiable(); |
| 97 | + int len = nodeList.size(); |
| 98 | + int neighbor = -1; |
| 99 | + |
| 100 | + if ( from != null ) while ( ++neighbor < len && nodeList.get(neighbor) != from ); |
| 101 | + else if ( dir == 1 ) neighbor = -1; |
| 102 | + else neighbor = len; |
| 103 | + |
| 104 | + for ( neighbor += dir; neighbor > -1 && neighbor < len; neighbor += dir ) { |
| 105 | + |
| 106 | + Node target = nodeList.get( neighbor ); |
| 107 | + |
| 108 | + if ( target instanceof Pane || target instanceof Group ) { |
| 109 | + target = traverse( (Parent) target, null, dir ); // down |
| 110 | + if ( target != null ) return target; |
| 111 | + } |
| 112 | + else if ( target.isVisible() && ! target.isDisabled() && target.isFocusTraversable() ) { |
| 113 | + target.requestFocus(); |
| 114 | + return target; |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return traverse( p.getParent(), p, dir ); // up |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + public void setText( String text ) |
| 123 | + { |
| 124 | + replaceText( text ); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * The action handler associated with this text field, or |
| 129 | + * {@code null} if no action handler is assigned. |
| 130 | + * |
| 131 | + * The action handler is normally called when the user types the ENTER key. |
| 132 | + */ |
| 133 | + private ObjectProperty<EventHandler<ActionEvent>> onAction = new ObjectPropertyBase<EventHandler<ActionEvent>>() { |
| 134 | + @Override |
| 135 | + protected void invalidated() { |
| 136 | + setEventHandler(ActionEvent.ACTION, get()); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public Object getBean() { |
| 141 | + return StyledTextField.this; |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String getName() { |
| 146 | + return "onAction"; |
| 147 | + } |
| 148 | + }; |
| 149 | + public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty() { return onAction; } |
| 150 | + public final EventHandler<ActionEvent> getOnAction() { return onActionProperty().get(); } |
| 151 | + public final void setOnAction(EventHandler<ActionEvent> value) { onActionProperty().set(value); } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void replaceText( int start, int end, String text ) |
| 155 | + { |
| 156 | + super.replaceText( start, end, VERTICAL_WHITESPACE.matcher( text ).replaceAll( " " ) ); |
| 157 | + } |
| 158 | +} |
0 commit comments