Skip to content

Commit

Permalink
added leading/trailing graphic to JFXTextField/JFXPassword
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadi Shaheen committed Sep 28, 2020
1 parent 9cb737a commit bbb653a
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 208 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ protected void layoutChildren(final double x, final double y, final double w, fi

final double height = getSkinnable().getHeight();
linesWrapper.layoutLines(x, y, w, h, height, promptText == null ? 0 : promptText.getLayoutBounds().getHeight() + 3);
linesWrapper.layoutPrompt(x, y, w, h);
errorContainer.layoutPane(x, height + linesWrapper.focusedLine.getHeight(), w, h);
linesWrapper.updateLabelFloatLayout();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@

package com.jfoenix.android.skins;

import com.jfoenix.controls.JFXTextField;
import com.jfoenix.controls.base.IFXLabelFloatControl;
import com.jfoenix.skins.JFXTextFieldSkin;
import com.jfoenix.skins.PromptLinesWrapper;
import com.jfoenix.skins.ValidationPane;
import com.jfoenix.utils.JFXNodeUtils;
import com.sun.javafx.scene.control.skin.TextFieldSkin;
import com.sun.javafx.scene.control.skin.TextFieldSkinAndroid;
import javafx.beans.property.DoubleProperty;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.control.Control;
import javafx.scene.input.KeyEvent;
import javafx.scene.layout.Pane;
import javafx.scene.text.Text;

import java.lang.reflect.Field;
import java.util.Set;

/**
* <h1>Material Design TextField Skin for android</h1>
Expand All @@ -45,7 +49,7 @@
* @version 2.0
* @since 2017-01-25
*/
public class JFXTextFieldSkinAndroid<T extends TextField & IFXLabelFloatControl> extends TextFieldSkinAndroid {
public class JFXTextFieldSkinAndroid<T extends JFXTextField & IFXLabelFloatControl> extends TextFieldSkinAndroid {

private boolean invalid = true;

Expand All @@ -60,7 +64,6 @@ public class JFXTextFieldSkinAndroid<T extends TextField & IFXLabelFloatControl>
public JFXTextFieldSkinAndroid(T textField) {
super(textField);
textPane = (Pane) this.getChildren().get(0);

// get parent fields
reflectionFieldConsumer("textNode", field -> textNode = (Node) field.get(this));
reflectionFieldConsumer("textTranslateX", field -> textTranslateX = (DoubleProperty) field.get(this));
Expand All @@ -80,15 +83,24 @@ public JFXTextFieldSkinAndroid(T textField) {

getChildren().addAll(linesWrapper.line, linesWrapper.focusedLine, linesWrapper.promptContainer, errorContainer);

updateGraphic(textField.getLeadingGraphic(), "leading");
updateGraphic(textField.getTrailingGraphic(), "trailing");

registerChangeListener(textField.disableProperty(), "DISABLE_NODE");
registerChangeListener(textField.focusColorProperty(), "FOCUS_COLOR");
registerChangeListener(textField.unFocusColorProperty(), "UNFOCUS_COLOR");
registerChangeListener(textField.disableAnimationProperty(), "DISABLE_ANIMATION");
registerChangeListener(textField.leadingGraphicProperty(), "LEADING_GRAPHIC");
registerChangeListener(textField.trailingGraphicProperty(), "TRAILING_GRAPHIC");
}

@Override
protected void handleControlPropertyChanged(String propertyReference) {
if ("DISABLE_NODE".equals(propertyReference)) {
if ("LEADING_GRAPHIC".equals(propertyReference)) {
updateGraphic(((JFXTextField) getSkinnable()).getLeadingGraphic(), "leading");
} else if ("TRAILING_GRAPHIC".equals(propertyReference)) {
updateGraphic(((JFXTextField) getSkinnable()).getTrailingGraphic(), "trailing");
} else if ("DISABLE_NODE".equals(propertyReference)) {
linesWrapper.updateDisabled();
} else if ("FOCUS_COLOR".equals(propertyReference)) {
linesWrapper.updateFocusColor();
Expand All @@ -104,12 +116,29 @@ protected void handleControlPropertyChanged(String propertyReference) {

@Override
protected void layoutChildren(final double x, final double y, final double w, final double h) {
super.layoutChildren(x, y, w, h);

final double height = getSkinnable().getHeight();
final Node leadingGraphic = ((JFXTextField) getSkinnable()).getLeadingGraphic();
final Node trailingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
final double leadingW = leadingGraphic == null ? 0.0 : snapSize(leadingGraphic.prefWidth(height));
final double trailingW = trailingGraphic == null ? 0.0 : snapSize(trailingGraphic.prefWidth(height));

final double textX = snapPosition(x) + leadingW;
final double textW = w - snapSize(leadingW) - snapSize(trailingW);

super.layoutChildren(textX, y, textW, h);
linesWrapper.layoutLines(x, y, w, h, height, Math.floor(h));
linesWrapper.layoutPrompt(textX, y, textW, h);
errorContainer.layoutPane(x, height + linesWrapper.focusedLine.getHeight(), w, h);

// draw leading/trailing graphics
if (leadingGraphic != null) {
leadingGraphic.resizeRelocate(snappedLeftInset(), 0, leadingW, height);
}

if (trailingGraphic != null) {
trailingGraphic.resizeRelocate(w - trailingW + snappedLeftInset(), 0, trailingW, height);
}

if (getSkinnable().getWidth() > 0) {
updateTextPos();
}
Expand All @@ -125,6 +154,18 @@ protected void layoutChildren(final double x, final double y, final double w, fi
}
}

private final void updateGraphic(Node graphic, String id) {
Node old = getSkinnable().lookup("#" + id);
getChildren().remove(old);
if (graphic != null) {
graphic.setId(id);
graphic.setManaged(false);
// add tab events handler as there is a bug in javafx traversing engine
Set<Control> controls = JFXNodeUtils.getAllChildren(graphic, Control.class);
controls.forEach(control -> control.addEventHandler(KeyEvent.KEY_PRESSED, JFXNodeUtils.TRAVERSE_HANDLER));
getChildren().add(graphic);
}
}

private void updateTextPos() {
double textWidth = textNode.getLayoutBounds().getWidth();
Expand Down Expand Up @@ -196,4 +237,44 @@ private <T> void reflectionFieldConsumer(String fieldName, CheckedConsumer<Field
private interface CheckedConsumer<T> {
void accept(T t) throws Exception;
}

@Override
protected double computePrefWidth(double h, double topInset, double rightInset, double bottomInset, double leftInset) {
final double w = super.computePrefWidth(h, topInset, rightInset, bottomInset, leftInset);
Node leadingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
Node trailingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
final double leadingW = leadingGraphic == null ? 0.0 : snapSize(leadingGraphic.prefWidth(h));
final double trailingW = trailingGraphic == null ? 0.0 : snapSize(trailingGraphic.prefWidth(h));
return w + trailingW + leadingW;
}

@Override
protected double computePrefHeight(double w, double topInset, double rightInset, double bottomInset, double leftInset) {
final double h = super.computePrefHeight(w, topInset, rightInset, bottomInset, leftInset);
Node leadingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
Node trailingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
final double leadingH = leadingGraphic == null ? 0.0 : snapSize(leadingGraphic.prefHeight(w));
final double trailingH = trailingGraphic == null ? 0.0 : snapSize(trailingGraphic.prefHeight(w));
return Math.max(Math.max(h, leadingH), trailingH);
}

@Override
protected double computeMinWidth(double h, double topInset, double rightInset, double bottomInset, double leftInset) {
final double w = super.computeMinWidth(h, topInset, rightInset, bottomInset, leftInset);
Node leadingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
Node trailingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
final double leadingW = leadingGraphic == null ? 0.0 : snapSize(leadingGraphic.minWidth(h));
final double trailingW = trailingGraphic == null ? 0.0 : snapSize(trailingGraphic.minWidth(h));
return w + trailingW + leadingW;
}

@Override
protected double computeMinHeight(double w, double topInset, double rightInset, double bottomInset, double leftInset) {
final double h = super.computeMinHeight(w, topInset, rightInset, bottomInset, leftInset);
Node leadingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
Node trailingGraphic = ((JFXTextField) getSkinnable()).getTrailingGraphic();
final double leadingH = leadingGraphic == null ? 0.0 : snapSize(leadingGraphic.minHeight(w));
final double trailingH = trailingGraphic == null ? 0.0 : snapSize(trailingGraphic.minHeight(w));
return Math.max(Math.max(h, leadingH), trailingH);
}
}
Loading

0 comments on commit bbb653a

Please sign in to comment.