All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jfoenix.android.skins.JFXTextFieldSkinAndroid Maven / Gradle / Ivy

There is a newer version: 9.0.10
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.jfoenix.android.skins;

import com.jfoenix.controls.base.IFXLabelFloatControl;
import com.jfoenix.skins.JFXTextFieldSkin;
import com.jfoenix.skins.PromptLinesWrapper;
import com.jfoenix.skins.ValidationPane;
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.layout.Pane;
import javafx.scene.text.Text;

import java.lang.reflect.Field;

/**
 * 

Material Design TextField Skin for android

* The JFXTextFieldSkinAndroid implements material design text field for android * when porting JFoenix to android using JavaFXPorts *

* Note: the implementation is a copy of the original {@link JFXTextFieldSkin} * however it extends the JavaFXPorts text field android skin. * * @author Shadi Shaheen * @version 2.0 * @since 2017-01-25 */ public class JFXTextFieldSkinAndroid extends TextFieldSkinAndroid { private boolean invalid = true; private Text promptText; private Pane textPane; private Node textNode; private DoubleProperty textTranslateX; private ValidationPane errorContainer; private PromptLinesWrapper linesWrapper; 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)); linesWrapper = new PromptLinesWrapper( textField, super.promptTextFill, textField.textProperty(), textField.promptTextProperty(), () -> promptText); linesWrapper.init(() -> createPromptNode(), textPane); reflectionFieldConsumer("usePromptText", field -> field.set(this, linesWrapper.usePromptText)); errorContainer = new ValidationPane<>(textField); getChildren().addAll(linesWrapper.line, linesWrapper.focusedLine, linesWrapper.promptContainer, errorContainer); registerChangeListener(textField.disableProperty(), "DISABLE_NODE"); registerChangeListener(textField.focusColorProperty(), "FOCUS_COLOR"); registerChangeListener(textField.unFocusColorProperty(), "UNFOCUS_COLOR"); registerChangeListener(textField.disableAnimationProperty(), "DISABLE_ANIMATION"); } @Override protected void handleControlPropertyChanged(String propertyReference) { if ("DISABLE_NODE".equals(propertyReference)) { linesWrapper.updateDisabled(); } else if ("FOCUS_COLOR".equals(propertyReference)) { linesWrapper.updateFocusColor(); } else if ("UNFOCUS_COLOR".equals(propertyReference)) { linesWrapper.updateUnfocusColor(); } else if ("DISABLE_ANIMATION".equals(propertyReference)) { // remove error clip if animation is disabled errorContainer.updateClip(); } else { super.handleControlPropertyChanged(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(); linesWrapper.layoutLines(x, y, w, h, height, Math.floor(h)); errorContainer.layoutPane(x, height + linesWrapper.focusedLine.getHeight(), w, h); if (getSkinnable().getWidth() > 0) { updateTextPos(); } linesWrapper.updateLabelFloatLayout(); if (invalid) { invalid = false; // update validation container errorContainer.invalid(w); // focus linesWrapper.invalid(); } } private void updateTextPos() { double textWidth = textNode.getLayoutBounds().getWidth(); final double promptWidth = promptText == null ? 0 : promptText.getLayoutBounds().getWidth(); switch (getHAlignment()) { case CENTER: linesWrapper.promptTextScale.setPivotX(promptWidth / 2); double midPoint = textRight.get() / 2; double newX = midPoint - textWidth / 2; if (newX + textWidth <= textRight.get()) { textTranslateX.set(newX); } break; case LEFT: linesWrapper.promptTextScale.setPivotX(0); break; case RIGHT: linesWrapper.promptTextScale.setPivotX(promptWidth); break; } } private void createPromptNode() { if (promptText != null || !linesWrapper.usePromptText.get()) { return; } promptText = new Text(); promptText.setManaged(false); promptText.getStyleClass().add("text"); promptText.visibleProperty().bind(linesWrapper.usePromptText); promptText.fontProperty().bind(getSkinnable().fontProperty()); promptText.textProperty().bind(getSkinnable().promptTextProperty()); promptText.fillProperty().bind(linesWrapper.animatedPromptTextFill); promptText.setLayoutX(1); promptText.getTransforms().add(linesWrapper.promptTextScale); linesWrapper.promptContainer.getChildren().add(promptText); if (getSkinnable().isFocused() && ((IFXLabelFloatControl) getSkinnable()).isLabelFloat()) { promptText.setTranslateY(-Math.floor(textPane.getHeight())); linesWrapper.promptTextScale.setX(0.85); linesWrapper.promptTextScale.setY(0.85); } try { reflectionFieldConsumer("promptNode", field -> { Object oldValue = field.get(this); if (oldValue != null) { textPane.getChildren().remove(oldValue); } field.set(this, promptText); }); } catch (Exception e) { e.printStackTrace(); } } private void reflectionFieldConsumer(String fieldName, CheckedConsumer consumer) { Field field = null; try { field = TextFieldSkin.class.getDeclaredField(fieldName); field.setAccessible(true); consumer.accept(field); } catch (Exception e) { e.printStackTrace(); } } private interface CheckedConsumer { void accept(T t) throws Exception; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy