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

com.jfoenix.android.skins.JFXTextAreaSkinAndroid 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.JFXTextArea;
import com.jfoenix.skins.PromptLinesWrapper;
import com.jfoenix.skins.ValidationPane;
import com.sun.javafx.scene.control.skin.TextAreaSkin;
import com.sun.javafx.scene.control.skin.TextAreaSkinAndroid;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;

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

/**
 * 

Material Design TextArea Skin for android

* The JFXTextAreaSkinAndroid implements material design text area for android * when porting JFoenix to android using JavaFXPorts * * Note: the implementation is a copy of the original {@link com.jfoenix.skins.JFXTextAreaSkin JFXTextAreaSkin} * however it extends the JavaFXPorts text area android skin. * * @author Shadi Shaheen * @version 2.0 * @since 2017-01-25 */ public class JFXTextAreaSkinAndroid extends TextAreaSkinAndroid { private boolean invalid = true; private ScrollPane scrollPane; private Text promptText; private ValidationPane errorContainer; private PromptLinesWrapper linesWrapper; public JFXTextAreaSkinAndroid(JFXTextArea textArea) { super(textArea); // init text area properties scrollPane = (ScrollPane) getChildren().get(0); textArea.setWrapText(true); linesWrapper = new PromptLinesWrapper<>( textArea, super.promptTextFill, textArea.textProperty(), textArea.promptTextProperty(), () -> promptText); linesWrapper.init(() -> createPromptNode(), scrollPane); errorContainer = new ValidationPane<>(textArea); getChildren().addAll(linesWrapper.line, linesWrapper.focusedLine, linesWrapper.promptContainer, errorContainer); registerChangeListener(textArea.disableProperty(), "DISABLE_NODE"); registerChangeListener(textArea.focusColorProperty(), "FOCUS_COLOR"); registerChangeListener(textArea.unFocusColorProperty(), "UNFOCUS_COLOR"); registerChangeListener(textArea.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, promptText == null ? 0 : promptText.getLayoutBounds().getHeight() + 3); errorContainer.layoutPane(x, height + linesWrapper.focusedLine.getHeight(), w, h); linesWrapper.updateLabelFloatLayout(); if (invalid) { invalid = false; // set the default background of text area viewport to white Region viewPort = (Region) scrollPane.getChildrenUnmodifiable().get(0); viewPort.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY))); // reapply css of scroll pane in case set by the user viewPort.applyCss(); errorContainer.invalid(w); // focus linesWrapper.invalid(); } } 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.setTranslateX(1); promptText.getTransforms().add(linesWrapper.promptTextScale); linesWrapper.promptContainer.getChildren().add(promptText); if (getSkinnable().isFocused() && ((JFXTextArea) getSkinnable()).isLabelFloat()) { promptText.setTranslateY(-Math.floor(scrollPane.getHeight())); linesWrapper.promptTextScale.setX(0.85); linesWrapper.promptTextScale.setY(0.85); } try { reflectionFieldConsumer("promptNode", field -> { Object oldValue = field.get(this); if (oldValue != null) { removeHighlight(Arrays.asList(((Node) oldValue))); } field.set(this, promptText); }); } catch (Exception e) { e.printStackTrace(); } } private void reflectionFieldConsumer(String fieldName, CheckedConsumer consumer) { Field field = null; try { field = TextAreaSkin.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