|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package com.jfoenix.controls; |
| 21 | + |
| 22 | +import com.jfoenix.utils.JFXNodeUtils; |
| 23 | +import javafx.beans.InvalidationListener; |
| 24 | +import javafx.beans.WeakInvalidationListener; |
| 25 | +import javafx.geometry.Insets; |
| 26 | +import javafx.scene.Node; |
| 27 | +import javafx.scene.control.TreeCell; |
| 28 | +import javafx.scene.control.TreeItem; |
| 29 | +import javafx.scene.layout.*; |
| 30 | +import javafx.scene.paint.Color; |
| 31 | + |
| 32 | +import java.lang.ref.WeakReference; |
| 33 | + |
| 34 | +/// JFXTreeCell is simple material design implementation of a tree cell. |
| 35 | +/// |
| 36 | +/// @author Shadi Shaheen |
| 37 | +/// @version 1.0 |
| 38 | +/// @since 2017-02-15 |
| 39 | +public class JFXTreeCell<T> extends TreeCell<T> { |
| 40 | + |
| 41 | + protected JFXRippler cellRippler = new JFXRippler(this) { |
| 42 | + @Override |
| 43 | + protected Node getMask() { |
| 44 | + Region clip = new Region(); |
| 45 | + JFXNodeUtils.updateBackground(JFXTreeCell.this.getBackground(), clip); |
| 46 | + double width = control.getLayoutBounds().getWidth(); |
| 47 | + double height = control.getLayoutBounds().getHeight(); |
| 48 | + clip.resize(width, height); |
| 49 | + return clip; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + protected void positionControl(Node control) { |
| 54 | + // do nothing |
| 55 | + } |
| 56 | + }; |
| 57 | + private HBox hbox; |
| 58 | + private final StackPane selectedPane = new StackPane(); |
| 59 | + |
| 60 | + private final InvalidationListener treeItemGraphicInvalidationListener = observable -> updateDisplay(getItem(), |
| 61 | + isEmpty()); |
| 62 | + private final WeakInvalidationListener weakTreeItemGraphicListener = new WeakInvalidationListener( |
| 63 | + treeItemGraphicInvalidationListener); |
| 64 | + |
| 65 | + private WeakReference<TreeItem<T>> treeItemRef; |
| 66 | + |
| 67 | + public JFXTreeCell() { |
| 68 | + selectedPane.getStyleClass().add("selection-bar"); |
| 69 | + selectedPane.setBackground(new Background(new BackgroundFill(Color.RED, CornerRadii.EMPTY, Insets.EMPTY))); |
| 70 | + selectedPane.setPrefWidth(3); |
| 71 | + selectedPane.setMouseTransparent(true); |
| 72 | + selectedProperty().addListener((o, oldVal, newVal) -> selectedPane.setVisible(newVal ? true : false)); |
| 73 | + |
| 74 | + final InvalidationListener treeItemInvalidationListener = observable -> { |
| 75 | + TreeItem<T> oldTreeItem = treeItemRef == null ? null : treeItemRef.get(); |
| 76 | + if (oldTreeItem != null) { |
| 77 | + oldTreeItem.graphicProperty().removeListener(weakTreeItemGraphicListener); |
| 78 | + } |
| 79 | + |
| 80 | + TreeItem<T> newTreeItem = getTreeItem(); |
| 81 | + if (newTreeItem != null) { |
| 82 | + newTreeItem.graphicProperty().addListener(weakTreeItemGraphicListener); |
| 83 | + treeItemRef = new WeakReference<>(newTreeItem); |
| 84 | + } |
| 85 | + }; |
| 86 | + final WeakInvalidationListener weakTreeItemListener = new WeakInvalidationListener(treeItemInvalidationListener); |
| 87 | + treeItemProperty().addListener(weakTreeItemListener); |
| 88 | + if (getTreeItem() != null) { |
| 89 | + getTreeItem().graphicProperty().addListener(weakTreeItemGraphicListener); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + protected void layoutChildren() { |
| 95 | + super.layoutChildren(); |
| 96 | + if (!getChildren().contains(selectedPane)) { |
| 97 | + getChildren().add(0, cellRippler); |
| 98 | + cellRippler.rippler.clear(); |
| 99 | + getChildren().add(0, selectedPane); |
| 100 | + } |
| 101 | + cellRippler.resizeRelocate(0, 0, getWidth(), getHeight()); |
| 102 | + cellRippler.releaseRipple(); |
| 103 | + selectedPane.resizeRelocate(0, 0, selectedPane.prefWidth(-1), getHeight()); |
| 104 | + selectedPane.setVisible(isSelected()); |
| 105 | + } |
| 106 | + |
| 107 | + private void updateDisplay(T item, boolean empty) { |
| 108 | + if (item == null || empty) { |
| 109 | + hbox = null; |
| 110 | + setText(null); |
| 111 | + setGraphic(null); |
| 112 | + } else { |
| 113 | + TreeItem<T> treeItem = getTreeItem(); |
| 114 | + if (treeItem != null && treeItem.getGraphic() != null) { |
| 115 | + if (item instanceof Node) { |
| 116 | + setText(null); |
| 117 | + if (hbox == null) { |
| 118 | + hbox = new HBox(3); |
| 119 | + } |
| 120 | + hbox.getChildren().setAll(treeItem.getGraphic(), (Node) item); |
| 121 | + setGraphic(hbox); |
| 122 | + } else { |
| 123 | + hbox = null; |
| 124 | + setText(item.toString()); |
| 125 | + setGraphic(treeItem.getGraphic()); |
| 126 | + } |
| 127 | + } else { |
| 128 | + hbox = null; |
| 129 | + if (item instanceof Node) { |
| 130 | + setText(null); |
| 131 | + setGraphic((Node) item); |
| 132 | + } else { |
| 133 | + setText(item.toString()); |
| 134 | + setGraphic(null); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + protected void updateItem(T item, boolean empty) { |
| 142 | + super.updateItem(item, empty); |
| 143 | + updateDisplay(item, empty); |
| 144 | + setMouseTransparent(item == null || empty); |
| 145 | + } |
| 146 | +} |
0 commit comments