|
| 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.animation.Interpolator; |
| 24 | +import javafx.animation.KeyFrame; |
| 25 | +import javafx.animation.KeyValue; |
| 26 | +import javafx.animation.Timeline; |
| 27 | +import javafx.application.Platform; |
| 28 | +import javafx.geometry.Insets; |
| 29 | +import javafx.scene.Node; |
| 30 | +import javafx.scene.control.Label; |
| 31 | +import javafx.scene.control.ListCell; |
| 32 | +import javafx.scene.control.Tooltip; |
| 33 | +import javafx.scene.layout.Region; |
| 34 | +import javafx.scene.shape.Rectangle; |
| 35 | +import javafx.scene.shape.Shape; |
| 36 | +import javafx.util.Duration; |
| 37 | + |
| 38 | +import java.util.Set; |
| 39 | + |
| 40 | +/// material design implementation of ListCell |
| 41 | +/// |
| 42 | +/// By default, JFXListCell will try to create a graphic node for the cell, |
| 43 | +/// to override it you need to set graphic to null in [#updateItem(Object, boolean)] method. |
| 44 | +/// |
| 45 | +/// NOTE: passive nodes (Labels and Shapes) will be set to mouse transparent in order to |
| 46 | +/// show the ripple effect upon clicking , to change this behavior you can override the |
| 47 | +/// method {[#makeChildrenTransparent()] |
| 48 | +/// |
| 49 | +/// @author Shadi Shaheen |
| 50 | +/// @version 1.0 |
| 51 | +/// @since 2016-03-09 |
| 52 | +public class JFXListCell<T> extends ListCell<T> { |
| 53 | + |
| 54 | + protected JFXRippler cellRippler = new JFXRippler(this) { |
| 55 | + @Override |
| 56 | + protected Node getMask() { |
| 57 | + Region clip = new Region(); |
| 58 | + JFXNodeUtils.updateBackground(JFXListCell.this.getBackground(), clip); |
| 59 | + double width = control.getLayoutBounds().getWidth(); |
| 60 | + double height = control.getLayoutBounds().getHeight(); |
| 61 | + clip.resize(width, height); |
| 62 | + return clip; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void positionControl(Node control) { |
| 67 | + // do nothing |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + protected Node cellContent; |
| 72 | + private Rectangle clip; |
| 73 | + |
| 74 | + private Timeline gapAnimation; |
| 75 | + private boolean playExpandAnimation = false; |
| 76 | + private boolean selectionChanged = false; |
| 77 | + |
| 78 | + /** |
| 79 | + * {@inheritDoc} |
| 80 | + */ |
| 81 | + public JFXListCell() { |
| 82 | + initialize(); |
| 83 | + initListeners(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * init listeners to update the vertical gap / selection animation |
| 88 | + */ |
| 89 | + private void initListeners() { |
| 90 | + listViewProperty().addListener((listObj, oldList, newList) -> { |
| 91 | + if (newList instanceof JFXListView<?> listView) { |
| 92 | + listView.currentVerticalGapProperty().addListener((o, oldVal, newVal) -> { |
| 93 | + cellRippler.rippler.setClip(null); |
| 94 | + if (newVal.doubleValue() != 0) { |
| 95 | + playExpandAnimation = true; |
| 96 | + getListView().requestLayout(); |
| 97 | + } else { |
| 98 | + // fake expand state |
| 99 | + double gap = clip.getY() * 2; |
| 100 | + gapAnimation = new Timeline( |
| 101 | + new KeyFrame(Duration.millis(240), |
| 102 | + new KeyValue(this.translateYProperty(), |
| 103 | + -gap / 2 - (gap * (getIndex())), |
| 104 | + Interpolator.EASE_BOTH) |
| 105 | + )); |
| 106 | + gapAnimation.play(); |
| 107 | + gapAnimation.setOnFinished((finish) -> { |
| 108 | + requestLayout(); |
| 109 | + Platform.runLater(() -> getListView().requestLayout()); |
| 110 | + }); |
| 111 | + } |
| 112 | + }); |
| 113 | + |
| 114 | + selectedProperty().addListener((o, oldVal, newVal) -> { |
| 115 | + if (newVal) { |
| 116 | + selectionChanged = true; |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected void layoutChildren() { |
| 125 | + super.layoutChildren(); |
| 126 | + cellRippler.resizeRelocate(0, 0, getWidth(), getHeight()); |
| 127 | + double gap = getGap(); |
| 128 | + |
| 129 | + if (clip == null) { |
| 130 | + clip = new Rectangle(0, gap / 2, getWidth(), getHeight() - gap); |
| 131 | + setClip(clip); |
| 132 | + } else { |
| 133 | + if (gap != 0) { |
| 134 | + if (playExpandAnimation || selectionChanged) { |
| 135 | + // fake list collapse state |
| 136 | + if (playExpandAnimation) { |
| 137 | + this.setTranslateY(-gap / 2 + (-gap * (getIndex()))); |
| 138 | + clip.setY(gap / 2); |
| 139 | + clip.setHeight(getHeight() - gap); |
| 140 | + gapAnimation = new Timeline(new KeyFrame(Duration.millis(240), |
| 141 | + new KeyValue(this.translateYProperty(), |
| 142 | + 0, |
| 143 | + Interpolator.EASE_BOTH))); |
| 144 | + playExpandAnimation = false; |
| 145 | + } else if (selectionChanged) { |
| 146 | + clip.setY(0); |
| 147 | + clip.setHeight(getHeight()); |
| 148 | + gapAnimation = new Timeline( |
| 149 | + new KeyFrame(Duration.millis(240), |
| 150 | + new KeyValue(clip.yProperty(), gap / 2, Interpolator.EASE_BOTH), |
| 151 | + new KeyValue(clip.heightProperty(), getHeight() - gap, Interpolator.EASE_BOTH) |
| 152 | + )); |
| 153 | + } |
| 154 | + playExpandAnimation = false; |
| 155 | + selectionChanged = false; |
| 156 | + gapAnimation.play(); |
| 157 | + } else { |
| 158 | + if (gapAnimation != null) { |
| 159 | + gapAnimation.stop(); |
| 160 | + } |
| 161 | + this.setTranslateY(0); |
| 162 | + clip.setY(gap / 2); |
| 163 | + clip.setHeight(getHeight() - gap); |
| 164 | + } |
| 165 | + } else { |
| 166 | + this.setTranslateY(0); |
| 167 | + clip.setY(0); |
| 168 | + clip.setHeight(getHeight()); |
| 169 | + } |
| 170 | + clip.setX(0); |
| 171 | + clip.setWidth(getWidth()); |
| 172 | + } |
| 173 | + if (!getChildren().contains(cellRippler)) { |
| 174 | + makeChildrenTransparent(); |
| 175 | + getChildren().add(0, cellRippler); |
| 176 | + cellRippler.rippler.clear(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * this method is used to set some nodes in cell content as mouse transparent nodes |
| 182 | + * so clicking on them will trigger the ripple effect. |
| 183 | + */ |
| 184 | + protected void makeChildrenTransparent() { |
| 185 | + for (Node child : getChildren()) { |
| 186 | + if (child instanceof Label) { |
| 187 | + Set<Node> texts = child.lookupAll("Text"); |
| 188 | + for (Node text : texts) { |
| 189 | + text.setMouseTransparent(true); |
| 190 | + } |
| 191 | + } else if (child instanceof Shape) { |
| 192 | + child.setMouseTransparent(true); |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * {@inheritDoc} |
| 199 | + */ |
| 200 | + @Override |
| 201 | + protected void updateItem(T item, boolean empty) { |
| 202 | + super.updateItem(item, empty); |
| 203 | + if (empty) { |
| 204 | + setText(null); |
| 205 | + setGraphic(null); |
| 206 | + // remove empty (Trailing cells) |
| 207 | + setMouseTransparent(true); |
| 208 | + setStyle("-fx-background-color:TRANSPARENT;"); |
| 209 | + } else { |
| 210 | + setMouseTransparent(false); |
| 211 | + setStyle(null); |
| 212 | + if (item instanceof Node newNode) { |
| 213 | + setText(null); |
| 214 | + Node currentNode = getGraphic(); |
| 215 | + if (currentNode == null || !currentNode.equals(newNode)) { |
| 216 | + cellContent = newNode; |
| 217 | + cellRippler.rippler.cacheRippleClip(false); |
| 218 | + // build the Cell node |
| 219 | + // RIPPLER ITEM : in case if the list item has its own rippler bind the list rippler and item rippler properties |
| 220 | + if (newNode instanceof JFXRippler newRippler) { |
| 221 | + // build cell container from exisiting rippler |
| 222 | + cellRippler.ripplerFillProperty().bind(newRippler.ripplerFillProperty()); |
| 223 | + cellRippler.maskTypeProperty().bind(newRippler.maskTypeProperty()); |
| 224 | + cellRippler.positionProperty().bind(newRippler.positionProperty()); |
| 225 | + cellContent = newRippler.getControl(); |
| 226 | + } |
| 227 | + ((Region) cellContent).setMaxHeight(cellContent.prefHeight(-1)); |
| 228 | + setGraphic(cellContent); |
| 229 | + } |
| 230 | + } else { |
| 231 | + setText(item == null ? "null" : item.toString()); |
| 232 | + setGraphic(null); |
| 233 | + } |
| 234 | + // show cell tooltip if it's toggled in JFXListView |
| 235 | + if (getListView() instanceof JFXListView<?> listView && listView.isShowTooltip()) { |
| 236 | + if (item instanceof Label label) { |
| 237 | + setTooltip(new Tooltip(label.getText())); |
| 238 | + } else if (getText() != null) { |
| 239 | + setTooltip(new Tooltip(getText())); |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + // Stylesheet Handling * |
| 246 | + |
| 247 | + /** |
| 248 | + * Initialize the style class to 'jfx-list-cell'. |
| 249 | + * <p> |
| 250 | + * This is the selector class from which CSS can be used to style |
| 251 | + * this control. |
| 252 | + */ |
| 253 | + private static final String DEFAULT_STYLE_CLASS = "jfx-list-cell"; |
| 254 | + |
| 255 | + private void initialize() { |
| 256 | + this.getStyleClass().add(DEFAULT_STYLE_CLASS); |
| 257 | + this.setPadding(new Insets(8, 12, 8, 12)); |
| 258 | + } |
| 259 | + |
| 260 | + @Override |
| 261 | + protected double computePrefHeight(double width) { |
| 262 | + double gap = getGap(); |
| 263 | + return super.computePrefHeight(width) + gap; |
| 264 | + } |
| 265 | + |
| 266 | + private double getGap() { |
| 267 | + return (getListView() instanceof JFXListView<?> listView) |
| 268 | + ? (listView.isExpanded() ? listView.currentVerticalGapProperty().get() : 0) |
| 269 | + : 0; |
| 270 | + } |
| 271 | +} |
0 commit comments