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

com.jfoenix.skins.JFXTextAreaSkin Maven / Gradle / Ivy

There is a newer version: 17-0.1.1
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.skins;

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

import com.jfoenix.adapters.ReflectionHelper;
import com.jfoenix.controls.JFXTextArea;

import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.skin.TextAreaSkin;
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;

/**
 * 

Material Design TextArea Skin

* * @author Shadi Shaheen * @version 2.0 * @since 2017-01-25 */ public class JFXTextAreaSkin extends TextAreaSkin { private boolean invalid = true; private ScrollPane scrollPane; private Text promptText; private ValidationPane errorContainer; private PromptLinesWrapper linesWrapper; public JFXTextAreaSkin(JFXTextArea textArea) { super(textArea); // init text area properties scrollPane = (ScrollPane) getChildren().get(0); textArea.setWrapText(true); linesWrapper = new PromptLinesWrapper<>(textArea, promptTextFillProperty(), 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(), obs -> linesWrapper.updateDisabled()); registerChangeListener(textArea.focusColorProperty(), obs -> linesWrapper.updateFocusColor()); registerChangeListener(textArea.unFocusColorProperty(), obs -> linesWrapper.updateUnfocusColor()); registerChangeListener(textArea.disableAnimationProperty(), obs -> errorContainer.updateClip()); } @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 { Field field = ReflectionHelper.getField(TextAreaSkin.class, "promptNode"); Object oldValue = field.get(this); if (oldValue != null) { removeHighlight(Arrays.asList(((Node) oldValue))); } field.set(this, promptText); } catch (Exception e) { e.printStackTrace(); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy